Fawkes API  Fawkes Development Version
client.h
1 
2 /***************************************************************************
3  * client.h - Fawkes network client
4  *
5  * Created: Tue Nov 21 18:42:10 2006
6  * Copyright 2006 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_CLIENT_H_
25 #define _NETCOMM_FAWKES_CLIENT_H_
26 
27 #include <core/exception.h>
28 #include <core/utils/lock_map.h>
29 #include <netcomm/fawkes/component_ids.h>
30 #include <netcomm/fawkes/message.h>
31 #include <netcomm/fawkes/message_queue.h>
32 #include <sys/socket.h>
33 
34 namespace fawkes {
35 
36 class StreamSocket;
37 class Mutex;
38 class WaitCondition;
39 class FawkesNetworkClientHandler;
40 class FawkesNetworkClientSendThread;
41 class FawkesNetworkClientRecvThread;
42 
43 class HandlerAlreadyRegisteredException : public Exception
44 {
45 public:
47 };
48 
49 #define FAWKES_TCP_PORT 1910
50 
52 {
55 
56 public:
58  FawkesNetworkClient(const char *host, unsigned short int port);
59  FawkesNetworkClient(unsigned int id, const char *host, unsigned short int port);
61 
62  void connect();
63  void disconnect();
64  void connect(const char *host, unsigned short int port);
65  void connect(const char *hostname, const struct sockaddr *addr, socklen_t addrlen);
66  void connect(const char *hostname, const struct sockaddr_storage &addr);
67 
68  void enqueue(FawkesNetworkMessage *message);
69  void enqueue_and_wait(FawkesNetworkMessage *message, unsigned int timeout_sec = 15);
70 
71  void wait(unsigned int component_id, unsigned int timeout_sec = 15);
72  void wake(unsigned int component_id);
73 
74  void interrupt_connect();
75 
76  void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id);
77  void deregister_handler(unsigned int component_id);
78 
79  bool connected() const throw();
80 
81  bool has_id() const;
82  unsigned int id() const;
83 
84  const char *get_hostname() const;
85 
86 private:
87  void recv();
88  void notify_of_connection_established();
89  void notify_of_connection_dead();
90 
91  void wake_handlers(unsigned int cid);
92  void dispatch_message(FawkesNetworkMessage *m);
93  void connection_died();
94  void set_send_slave_alive();
95  void set_recv_slave_alive();
96 
97  char * host_;
98  unsigned short int port_;
99 
100  StreamSocket *s;
101 
102  typedef LockMap<unsigned int, FawkesNetworkClientHandler *> HandlerMap;
103  HandlerMap handlers;
104 
105  WaitCondition *connest_waitcond_;
106  Mutex * connest_mutex_;
107  bool connest_;
108  bool connest_interrupted_;
109 
110  Mutex * recv_mutex_;
111  WaitCondition * recv_waitcond_;
112  std::map<unsigned int, bool> recv_received_;
113  FawkesNetworkClientRecvThread *recv_slave_;
114  FawkesNetworkClientSendThread *send_slave_;
115  bool recv_slave_alive_;
116  bool send_slave_alive_;
117 
118  bool connection_died_recently;
119  Mutex * slave_status_mutex;
120  bool _has_id;
121  unsigned int _id;
122 
123  struct sockaddr *addr_;
124  socklen_t addr_len_;
125 };
126 
127 } // end namespace fawkes
128 
129 #endif
fawkes::FawkesNetworkClient::deregister_handler
void deregister_handler(unsigned int component_id)
Deregister handler.
Definition: client.cpp:680
fawkes::FawkesNetworkClient::interrupt_connect
void interrupt_connect()
Interrupt connect().
Definition: client.cpp:580
fawkes::FawkesNetworkClient::enqueue
void enqueue(FawkesNetworkMessage *message)
Enqueue message to send.
Definition: client.cpp:600
fawkes::FawkesNetworkClientSendThread
Fawkes network client send thread.
Definition: client.cpp:67
fawkes::LockMap
Definition: lock_map.h:39
fawkes::FawkesNetworkClient::enqueue_and_wait
void enqueue_and_wait(FawkesNetworkMessage *message, unsigned int timeout_sec=15)
Enqueue message to send and wait for answer.
Definition: client.cpp:618
fawkes::FawkesNetworkClient::~FawkesNetworkClient
~FawkesNetworkClient()
Destructor.
Definition: client.cpp:406
fawkes::Mutex
Definition: mutex.h:36
fawkes::FawkesNetworkClient::connect
void connect()
Connect to remote.
Definition: client.cpp:428
fawkes::WaitCondition
Definition: wait_condition.h:40
fawkes::HandlerAlreadyRegisteredException::HandlerAlreadyRegisteredException
HandlerAlreadyRegisteredException()
Costructor.
Definition: client.cpp:56
fawkes::FawkesNetworkClient::wait
void wait(unsigned int component_id, unsigned int timeout_sec=15)
Wait for messages for component ID.
Definition: client.cpp:789
fawkes::FawkesNetworkClient::disconnect
void disconnect()
Disconnect socket.
Definition: client.cpp:543
fawkes::FawkesNetworkClient::get_hostname
const char * get_hostname() const
Get the client's hostname.
Definition: client.cpp:863
fawkes::FawkesNetworkClient::has_id
bool has_id() const
Check whether the client has an id.
Definition: client.cpp:841
fawkes::FawkesNetworkClientHandler
Definition: client_handler.h:35
fawkes
fawkes::FawkesNetworkClient::FawkesNetworkClient
FawkesNetworkClient()
Constructor.
Definition: client.cpp:344
fawkes::FawkesNetworkClient::register_handler
void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id)
Register handler.
Definition: client.cpp:662
fawkes::FawkesNetworkClientRecvThread
Fawkes network client receive thread.
Definition: client.cpp:190
fawkes::FawkesNetworkMessage
Definition: message.h:80
fawkes::FawkesNetworkClient::wake
void wake(unsigned int component_id)
Wake a waiting thread.
Definition: client.cpp:818
fawkes::StreamSocket
Definition: stream.h:35
fawkes::FawkesNetworkClient::connected
bool connected() const
Check if connection is alive.
Definition: client.cpp:832
fawkes::FawkesNetworkClient
Definition: client.h:55