24 #include <netcomm/fawkes/server_thread.h> 25 #include <netcomm/fawkes/server_client_thread.h> 26 #include <netcomm/utils/acceptor_thread.h> 27 #include <netcomm/fawkes/message.h> 28 #include <netcomm/fawkes/handler.h> 29 #include <netcomm/fawkes/message_queue.h> 30 #include <netcomm/fawkes/message_content.h> 31 #include <core/threading/thread_collector.h> 32 #include <core/threading/mutex.h> 33 #include <core/exception.h> 54 :
Thread(
"FawkesNetworkServerThread",
Thread::OPMODE_WAITFORWAKEUP)
56 this->thread_collector = thread_collector;
62 "FawkesNetworkAcceptorThread");
63 if ( thread_collector ) {
64 thread_collector->
add(acceptor_thread);
66 acceptor_thread->
start();
74 for (cit = clients.begin(); cit != clients.end(); ++cit) {
75 if ( thread_collector ) {
76 thread_collector->
remove((*cit).second);
78 (*cit).second->cancel();
79 (*cit).second->join();
83 if ( thread_collector ) {
84 thread_collector->
remove(acceptor_thread);
87 acceptor_thread->
join();
89 delete acceptor_thread;
91 delete inbound_messages;
106 if ( thread_collector ) {
107 thread_collector->
add(client);
111 clients[next_client_id] = client;
112 for (hit = handlers.begin(); hit != handlers.end(); ++hit) {
113 (*hit).second->client_connected(next_client_id);
129 if ( handlers.find(handler->
id()) != handlers.end()) {
131 throw Exception(
"Handler already registered");
133 handlers[handler->
id()] = handler;
145 if( handlers.find(handler->
id()) != handlers.end() ) {
146 handlers.erase(handler->
id());
164 cit = clients.begin();
165 while (cit != clients.end()) {
166 if ( ! cit->second->alive() ) {
167 if ( thread_collector ) {
168 thread_collector->
remove((*cit).second);
170 cit->second->cancel();
175 unsigned int clid = (*cit).first;
178 for (hit = handlers.begin(); hit != handlers.end(); ++hit) {
179 (*hit).second->client_disconnected(clid);
187 inbound_messages->
lock();
188 while ( ! inbound_messages->empty() ) {
190 if ( handlers.find(m->
cid()) != handlers.end()) {
191 handlers[m->
cid()]->handle_network_message(m);
194 inbound_messages->pop();
196 inbound_messages->
unlock();
207 for (cit = clients.begin(); cit != clients.end(); ++cit) {
208 (*cit).second->force_send();
223 for (cit = clients.begin(); cit != clients.end(); ++cit) {
224 if ( (*cit).second->alive() ) {
226 (*cit).second->enqueue(msg);
243 unsigned short int msg_id,
244 void *payload,
unsigned int payload_size)
247 payload, payload_size);
278 unsigned int clid = msg->
clid();
279 if ( clients.find(clid) != clients.end() ) {
280 if ( clients[clid]->alive() ) {
281 clients[clid]->enqueue(msg);
283 throw Exception(
"Client %u not alive", clid);
286 throw Exception(
"Client %u not found", clid);
302 unsigned short int component_id,
unsigned short int msg_id,
303 void *payload,
unsigned int payload_size)
306 payload, payload_size);
321 unsigned short int component_id,
unsigned short int msg_id,
340 unsigned short int component_id,
unsigned short int msg_id)
void unlock() const
Unlock list.
Fawkes Network Client Thread for server.
A LockQueue of FawkesNetworkMessage to hold messages in inbound and outbound queues.
void unref()
Decrement reference count and conditionally delete this instance.
virtual void broadcast(FawkesNetworkMessage *msg)
Broadcast a message.
void dispatch(FawkesNetworkMessage *msg)
Dispatch messages.
virtual void remove(ThreadList &tl)=0
Remove multiple threads.
Fawkes library namespace.
FawkesNetworkServerThread(unsigned int fawkes_port, ThreadCollector *thread_collector=0)
Constructor.
Representation of a message that is sent over the network.
Thread class encapsulation of pthreads.
virtual void send(FawkesNetworkMessage *msg)
Send a message.
TCP stream socket over IP.
Fawkes network message content.
void add_connection(StreamSocket *s)
Add a new connection.
virtual ~FawkesNetworkServerThread()
Destructor.
void set_clid(unsigned int client_id)
Set client ID.
void wakeup()
Wake up thread.
Base class for exceptions in Fawkes.
virtual void remove_handler(FawkesNetworkHandler *handler)
Remove handler.
virtual void add_handler(FawkesNetworkHandler *handler)
Add a handler.
void ref()
Increment reference count.
Network handler abstract base class.
virtual void add(ThreadList &tl)=0
Add multiple threads.
void cancel()
Cancel a thread.
virtual void loop()
Fawkes network thread loop.
void lock() const
Lock queue.
void push_locked(const Type &x)
Push element to queue with lock protection.
void join()
Join the thread.
unsigned short int id() const
Get the component ID for this handler.
unsigned short int cid() const
Get component ID.
void force_send()
Force sending of all pending messages.
unsigned int clid() const
Get client ID.
void start(bool wait=true)
Call this method to start the thread.