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)