server_thread.h

00001 
00002 /***************************************************************************
00003  *  server_thread.h - Thread to manage Fawkes network clients
00004  *
00005  *  Created: Sun Nov 19 14:27:31 2006
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __NETCOMM_FAWKES_SERVER_THREAD_H_
00025 #define __NETCOMM_FAWKES_SERVER_THREAD_H_
00026 
00027 #include <core/threading/thread.h>
00028 #include <core/utils/lock_map.h>
00029 #include <netcomm/fawkes/hub.h>
00030 #include <netcomm/utils/incoming_connection_handler.h>
00031 
00032 namespace fawkes {
00033 
00034 class ThreadCollector;
00035 class Mutex;
00036 class FawkesNetworkServerClientThread;
00037 class NetworkAcceptorThread;
00038 class FawkesNetworkHandler;
00039 class FawkesNetworkMessage;
00040 class FawkesNetworkMessageQueue;
00041 class FawkesNetworkMessageContent;
00042 
00043 class FawkesNetworkServerThread
00044 : public Thread,
00045   public FawkesNetworkHub,
00046   public NetworkIncomingConnectionHandler
00047 {
00048  public:
00049   FawkesNetworkServerThread(unsigned int fawkes_port,
00050                             ThreadCollector *thread_collector = 0);
00051   virtual ~FawkesNetworkServerThread();
00052 
00053   virtual void loop();
00054 
00055   virtual void add_handler(FawkesNetworkHandler *handler);
00056   virtual void remove_handler(FawkesNetworkHandler *handler);
00057 
00058   virtual void broadcast(FawkesNetworkMessage *msg);
00059   virtual void broadcast(unsigned short int component_id, unsigned short int msg_id,
00060                          void *payload, unsigned int payload_size);
00061   virtual void broadcast(unsigned short int component_id, unsigned short int msg_id);
00062 
00063   virtual void send(FawkesNetworkMessage *msg);
00064   virtual void send(unsigned int to_clid,
00065                     unsigned short int component_id, unsigned short int msg_id);
00066   virtual void send(unsigned int to_clid,
00067                     unsigned short int component_id, unsigned short int msg_id,
00068                     void *payload, unsigned int payload_size);
00069   virtual void send(unsigned int to_clid,
00070                     unsigned short int component_id, unsigned short int msg_id,
00071                     FawkesNetworkMessageContent *content);
00072 
00073   void add_connection(StreamSocket *s) throw();
00074   void dispatch(FawkesNetworkMessage *msg);
00075 
00076   void force_send();
00077 
00078  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
00079  protected: virtual void run() { Thread::run(); }
00080 
00081  private:
00082   ThreadCollector       *thread_collector;
00083   unsigned int           next_client_id;
00084   NetworkAcceptorThread *acceptor_thread;
00085 
00086   // key: component id,  value: handler
00087   LockMap<unsigned int, FawkesNetworkHandler *> handlers;
00088   LockMap<unsigned int, FawkesNetworkHandler *>::iterator hit;
00089 
00090   // key: client id,     value: client thread
00091   LockMap<unsigned int, FawkesNetworkServerClientThread *> clients;
00092   LockMap<unsigned int, FawkesNetworkServerClientThread *>::iterator cit;
00093 
00094   FawkesNetworkMessageQueue *inbound_messages;
00095 };
00096 
00097 } // end namespace fawkes
00098 
00099 #endif