Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
34 namespace fawkes {
35 
36 class StreamSocket;
37 class Mutex;
38 class WaitCondition;
39 class FawkesNetworkClientHandler;
40 class FawkesNetworkClientSendThread;
41 class FawkesNetworkClientRecvThread;
42 
44 {
45  public:
47 };
48 
49 #define FAWKES_TCP_PORT 1910
50 
52 {
53  friend class FawkesNetworkClientSendThread;
54  friend class FawkesNetworkClientRecvThread;
55  public:
57  FawkesNetworkClient(const char *hostname, unsigned short int port, const char *ip = NULL);
58  FawkesNetworkClient(unsigned int id, const char *hostname,
59  unsigned short int port, const char *ip = NULL);
61 
62  void connect();
63  void disconnect();
64  void connect(const char *hostname, unsigned short int port);
65  void connect(const char *hostname, const char *ip, unsigned short int port);
66 
67  void enqueue(FawkesNetworkMessage *message);
68  void enqueue_and_wait(FawkesNetworkMessage *message, unsigned int timeout_sec = 15);
69 
70  void wait(unsigned int component_id, unsigned int timeout_sec = 15);
71  void wake(unsigned int component_id);
72 
73  void interrupt_connect();
74 
75  void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id);
76  void deregister_handler(unsigned int component_id);
77 
78  bool connected() const throw();
79 
80  bool has_id() const;
81  unsigned int id() const;
82 
83  const char *get_hostname() const;
84  const char *get_ip() 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 *__hostname;
98  char *__ip;
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 
125 } // end namespace fawkes
126 
127 #endif