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 <vector>
33 #include <string>
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 Thread,
48  public FawkesNetworkHub,
50 {
51  public:
52  FawkesNetworkServerThread(bool enable_ipv4, bool enable_ipv6,
53  const std::string &listen_ipv4, const std::string &listen_ipv6,
54  unsigned int fawkes_port,
55  ThreadCollector *thread_collector = 0);
57 
58  virtual void loop();
59 
60  virtual void add_handler(FawkesNetworkHandler *handler);
61  virtual void remove_handler(FawkesNetworkHandler *handler);
62 
63  virtual void broadcast(FawkesNetworkMessage *msg);
64  virtual void broadcast(unsigned short int component_id, unsigned short int msg_id,
65  void *payload, unsigned int payload_size);
66  virtual void broadcast(unsigned short int component_id, unsigned short int msg_id);
67 
68  virtual void send(FawkesNetworkMessage *msg);
69  virtual void send(unsigned int to_clid,
70  unsigned short int component_id, unsigned short int msg_id);
71  virtual void send(unsigned int to_clid,
72  unsigned short int component_id, unsigned short int msg_id,
73  void *payload, unsigned int payload_size);
74  virtual void send(unsigned int to_clid,
75  unsigned short int component_id, unsigned short int msg_id,
77 
78  void add_connection(StreamSocket *s) throw();
79  void dispatch(FawkesNetworkMessage *msg);
80 
81  void force_send();
82 
83  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
84  protected: virtual void run() { Thread::run(); }
85 
86  private:
87  ThreadCollector *thread_collector;
88  unsigned int next_client_id;
89  std::vector<NetworkAcceptorThread *> acceptor_threads;
90 
91  // key: component id, value: handler
94 
95  // key: client id, value: client thread
98 
99  FawkesNetworkMessageQueue *inbound_messages;
100 };
101 
102 } // end namespace fawkes
103 
104 #endif
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: server_thread.h:84
A LockQueue of FawkesNetworkMessage to hold messages in inbound and outbound queues.
Definition: message_queue.h:33
virtual void broadcast(FawkesNetworkMessage *msg)
Broadcast a message.
void dispatch(FawkesNetworkMessage *msg)
Dispatch messages.
Fawkes library namespace.
virtual void run()
Code to execute in the thread.
Definition: thread.cpp:939
Representation of a message that is sent over the network.
Definition: message.h:75
Thread collector.
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void send(FawkesNetworkMessage *msg)
Send a message.
TCP stream socket over IP.
Definition: stream.h:31
Fawkes network message content.
Map with a lock.
Definition: lock_map.h:37
void add_connection(StreamSocket *s)
Add a new connection.
virtual ~FawkesNetworkServerThread()
Destructor.
Fawkes Network Hub.
Definition: hub.h:33
virtual void remove_handler(FawkesNetworkHandler *handler)
Remove handler.
virtual void add_handler(FawkesNetworkHandler *handler)
Add a handler.
Network handler abstract base class.
Definition: handler.h:31
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.
virtual void loop()
Fawkes network thread loop.
Interface for handling incoming connections.
void force_send()
Force sending of all pending messages.
Fawkes Network Thread.
Definition: server_thread.h:46