00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __NETCOMM_FAWKES_CLIENT_H_
00025 #define __NETCOMM_FAWKES_CLIENT_H_
00026
00027 #include <netcomm/fawkes/message_queue.h>
00028 #include <netcomm/fawkes/message.h>
00029 #include <netcomm/fawkes/component_ids.h>
00030
00031 #include <core/exception.h>
00032 #include <core/utils/lock_map.h>
00033
00034 namespace fawkes {
00035
00036 class StreamSocket;
00037 class Mutex;
00038 class WaitCondition;
00039 class FawkesNetworkClientHandler;
00040 class FawkesNetworkClientSendThread;
00041 class FawkesNetworkClientRecvThread;
00042
00043 class HandlerAlreadyRegisteredException : public Exception
00044 {
00045 public:
00046 HandlerAlreadyRegisteredException();
00047 };
00048
00049 #define FAWKES_TCP_PORT 1910
00050
00051 class FawkesNetworkClient
00052 {
00053 friend class FawkesNetworkClientSendThread;
00054 friend class FawkesNetworkClientRecvThread;
00055 public:
00056 FawkesNetworkClient();
00057 FawkesNetworkClient(const char *hostname, unsigned short int port, const char *ip = NULL);
00058 FawkesNetworkClient(unsigned int id, const char *hostname,
00059 unsigned short int port, const char *ip = NULL);
00060 ~FawkesNetworkClient();
00061
00062 void connect();
00063 void disconnect();
00064 void connect(const char *hostname, unsigned short int port);
00065 void connect(const char *hostname, const char *ip, unsigned short int port);
00066
00067 void enqueue(FawkesNetworkMessage *message);
00068 void enqueue_and_wait(FawkesNetworkMessage *message, unsigned int timeout_sec = 15);
00069
00070 void wait(unsigned int component_id, unsigned int timeout_sec = 15);
00071 void wake(unsigned int component_id);
00072
00073 void interrupt_connect();
00074
00075 void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id);
00076 void deregister_handler(unsigned int component_id);
00077
00078 bool connected() const throw();
00079
00080 bool has_id() const;
00081 unsigned int id() const;
00082
00083 const char *get_hostname() const;
00084 const char *get_ip() const;
00085
00086 private:
00087 void recv();
00088 void notify_of_connection_established();
00089 void notify_of_connection_dead();
00090
00091 void wake_handlers(unsigned int cid);
00092 void dispatch_message(FawkesNetworkMessage *m);
00093 void connection_died();
00094 void set_send_slave_alive();
00095 void set_recv_slave_alive();
00096
00097 char *__hostname;
00098 char *__ip;
00099 unsigned short int __port;
00100
00101 StreamSocket *s;
00102
00103 typedef LockMap<unsigned int, FawkesNetworkClientHandler *> HandlerMap;
00104 HandlerMap handlers;
00105
00106 WaitCondition *__connest_waitcond;
00107 Mutex *__connest_mutex;
00108 bool __connest;
00109 bool __connest_interrupted;
00110
00111 Mutex *__recv_mutex;
00112 WaitCondition *__recv_waitcond;
00113 std::map<unsigned int, bool> __recv_received;
00114 FawkesNetworkClientRecvThread *__recv_slave;
00115 FawkesNetworkClientSendThread *__send_slave;
00116 bool __recv_slave_alive;
00117 bool __send_slave_alive;
00118
00119 bool connection_died_recently;
00120 Mutex *slave_status_mutex;
00121 bool _has_id;
00122 unsigned int _id;
00123 };
00124
00125 }
00126
00127 #endif