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 <netcomm/fawkes/message_queue.h>
28 #include <netcomm/fawkes/message.h>
29 #include <netcomm/fawkes/component_ids.h>
30 
31 #include <core/exception.h>
32 #include <core/utils/lock_map.h>
33 #include <sys/socket.h>
34 
35 namespace fawkes {
36 
37 class StreamSocket;
38 class Mutex;
39 class WaitCondition;
40 class FawkesNetworkClientHandler;
41 class FawkesNetworkClientSendThread;
42 class FawkesNetworkClientRecvThread;
43 
45 {
46  public:
48 };
49 
50 #define FAWKES_TCP_PORT 1910
51 
53 {
54  friend class FawkesNetworkClientSendThread;
55  friend class FawkesNetworkClientRecvThread;
56  public:
58  FawkesNetworkClient(const char *host, unsigned short int port);
59  FawkesNetworkClient(unsigned int id, const char *host,
60  unsigned short int port);
62 
63  void connect();
64  void disconnect();
65  void connect(const char *host, unsigned short int port);
66  void connect(const char *hostname, const struct sockaddr *addr, socklen_t addrlen);
67  void connect(const char *hostname, const struct sockaddr_storage &addr);
68 
69  void enqueue(FawkesNetworkMessage *message);
70  void enqueue_and_wait(FawkesNetworkMessage *message, unsigned int timeout_sec = 15);
71 
72  void wait(unsigned int component_id, unsigned int timeout_sec = 15);
73  void wake(unsigned int component_id);
74 
75  void interrupt_connect();
76 
77  void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id);
78  void deregister_handler(unsigned int component_id);
79 
80  bool connected() const throw();
81 
82  bool has_id() const;
83  unsigned int id() const;
84 
85  const char *get_hostname() const;
86 
87  private:
88  void recv();
89  void notify_of_connection_established();
90  void notify_of_connection_dead();
91 
92  void wake_handlers(unsigned int cid);
93  void dispatch_message(FawkesNetworkMessage *m);
94  void connection_died();
95  void set_send_slave_alive();
96  void set_recv_slave_alive();
97 
98  char *__host;
99  unsigned short int __port;
100 
101  StreamSocket *s;
102 
104  HandlerMap handlers;
105 
106  WaitCondition *__connest_waitcond;
107  Mutex *__connest_mutex;
108  bool __connest;
109  bool __connest_interrupted;
110 
111  Mutex *__recv_mutex;
112  WaitCondition *__recv_waitcond;
113  std::map<unsigned int, bool> __recv_received;
114  FawkesNetworkClientRecvThread *__recv_slave;
115  FawkesNetworkClientSendThread *__send_slave;
116  bool __recv_slave_alive;
117  bool __send_slave_alive;
118 
119  bool connection_died_recently;
120  Mutex *slave_status_mutex;
121  bool _has_id;
122  unsigned int _id;
123 
124  struct sockaddr *addr_;
125  socklen_t addr_len_;
126 };
127 
128 } // end namespace fawkes
129 
130 #endif
Message handler for FawkesNetworkClient.
Wait until a given condition holds.
Simple Fawkes network client.
Definition: client.h:52
Fawkes library namespace.
Representation of a message that is sent over the network.
Definition: message.h:75
Fawkes network client send thread.
Definition: client.cpp:65
TCP stream socket over IP.
Definition: stream.h:31
Base class for exceptions in Fawkes.
Definition: exception.h:36
Client handler has already been registered.
Definition: client.h:44
Mutex mutual exclusion lock.
Definition: mutex.h:32
Fawkes network client receive thread.
Definition: client.cpp:181