Fawkes API  Fawkes Development Version
server_thread.h
1 
2 /***************************************************************************
3  * server_thread.h - Thread to manage Fawkes network clients
4  *
5  * Created: Sun Nov 19 14:27:31 2006
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _NETCOMM_FAWKES_SERVER_THREAD_H_
25 #define _NETCOMM_FAWKES_SERVER_THREAD_H_
26 
27 #include <core/threading/thread.h>
28 #include <core/utils/lock_map.h>
29 #include <netcomm/fawkes/hub.h>
30 #include <netcomm/utils/incoming_connection_handler.h>
31 
32 #include <string>
33 #include <vector>
34 
35 namespace fawkes {
36 
37 class ThreadCollector;
38 class Mutex;
39 class FawkesNetworkServerClientThread;
40 class NetworkAcceptorThread;
41 class FawkesNetworkHandler;
42 class FawkesNetworkMessage;
43 class FawkesNetworkMessageQueue;
44 class FawkesNetworkMessageContent;
45 
47  public FawkesNetworkHub,
49 {
50 public:
51  FawkesNetworkServerThread(bool enable_ipv4,
52  bool enable_ipv6,
53  const std::string &listen_ipv4,
54  const std::string &listen_ipv6,
55  unsigned int fawkes_port,
56  ThreadCollector * thread_collector = 0);
58 
59  virtual void loop();
60 
61  virtual void add_handler(FawkesNetworkHandler *handler);
62  virtual void remove_handler(FawkesNetworkHandler *handler);
63 
64  virtual void broadcast(FawkesNetworkMessage *msg);
65  virtual void broadcast(unsigned short int component_id,
66  unsigned short int msg_id,
67  void * payload,
68  unsigned int payload_size);
69  virtual void broadcast(unsigned short int component_id, unsigned short int msg_id);
70 
71  virtual void send(FawkesNetworkMessage *msg);
72  virtual void
73  send(unsigned int to_clid, unsigned short int component_id, unsigned short int msg_id);
74  virtual void send(unsigned int to_clid,
75  unsigned short int component_id,
76  unsigned short int msg_id,
77  void * payload,
78  unsigned int payload_size);
79  virtual void send(unsigned int to_clid,
80  unsigned short int component_id,
81  unsigned short int msg_id,
83 
84  void add_connection(StreamSocket *s) throw();
85  void dispatch(FawkesNetworkMessage *msg);
86 
87  void force_send();
88 
89  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
90 protected:
91  virtual void
92  run()
93  {
94  Thread::run();
95  }
96 
97 private:
98  ThreadCollector * thread_collector;
99  unsigned int next_client_id;
100  std::vector<NetworkAcceptorThread *> acceptor_threads;
101 
102  // key: component id, value: handler
105 
106  // key: client id, value: client thread
109 
110  FawkesNetworkMessageQueue *inbound_messages;
111 };
112 
113 } // end namespace fawkes
114 
115 #endif
fawkes::ThreadCollector
Thread collector.
Definition: thread_collector.h:34
fawkes::FawkesNetworkServerThread::send
virtual void send(FawkesNetworkMessage *msg)
Send a message.
Definition: server_thread.cpp:304
fawkes::LockMap
Map with a lock.
Definition: lock_map.h:36
fawkes::FawkesNetworkServerThread::force_send
void force_send()
Force sending of all pending messages.
Definition: server_thread.cpp:234
fawkes::FawkesNetworkServerThread::add_connection
void add_connection(StreamSocket *s)
Add a new connection.
Definition: server_thread.cpp:123
fawkes::FawkesNetworkMessageQueue
A LockQueue of FawkesNetworkMessage to hold messages in inbound and outbound queues.
Definition: message_queue.h:33
fawkes::FawkesNetworkServerThread::dispatch
void dispatch(FawkesNetworkMessage *msg)
Dispatch messages.
Definition: server_thread.cpp:378
fawkes::FawkesNetworkHandler
Network handler abstract base class.
Definition: handler.h:32
fawkes::FawkesNetworkServerThread::run
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: server_thread.h:92
fawkes::FawkesNetworkServerThread::broadcast
virtual void broadcast(FawkesNetworkMessage *msg)
Broadcast a message.
Definition: server_thread.cpp:250
fawkes::FawkesNetworkServerThread::FawkesNetworkServerThread
FawkesNetworkServerThread(bool enable_ipv4, bool enable_ipv6, const std::string &listen_ipv4, const std::string &listen_ipv6, unsigned int fawkes_port, ThreadCollector *thread_collector=0)
Constructor.
Definition: server_thread.cpp:59
fawkes::FawkesNetworkServerThread::add_handler
virtual void add_handler(FawkesNetworkHandler *handler)
Add a handler.
Definition: server_thread.cpp:151
fawkes::FawkesNetworkServerThread::loop
virtual void loop()
Fawkes network thread loop.
Definition: server_thread.cpp:179
fawkes
Fawkes library namespace.
fawkes::FawkesNetworkServerThread::~FawkesNetworkServerThread
virtual ~FawkesNetworkServerThread()
Destructor.
Definition: server_thread.cpp:93
fawkes::FawkesNetworkServerThread::remove_handler
virtual void remove_handler(FawkesNetworkHandler *handler)
Remove handler.
Definition: server_thread.cpp:164
fawkes::Thread::run
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:918
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::NetworkIncomingConnectionHandler
Interface for handling incoming connections.
Definition: incoming_connection_handler.h:32
fawkes::FawkesNetworkMessage
Representation of a message that is sent over the network.
Definition: message.h:77
fawkes::FawkesNetworkServerThread
Fawkes Network Thread.
Definition: server_thread.h:49
fawkes::StreamSocket
TCP stream socket over IP.
Definition: stream.h:32
fawkes::FawkesNetworkHub
Fawkes Network Hub.
Definition: hub.h:34
fawkes::FawkesNetworkMessageContent
Fawkes network message content.
Definition: message_content.h:34