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
34namespace fawkes {
35
36class StreamSocket;
37class Mutex;
38class WaitCondition;
39class FawkesNetworkClientHandler;
40class FawkesNetworkClientSendThread;
41class FawkesNetworkClientRecvThread;
42
44{
45public:
47};
48
49#define FAWKES_TCP_PORT 1910
50
52{
55
56public:
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 noexcept;
80
81 bool has_id() const;
82 unsigned int id() const;
83
84 const char *get_hostname() const;
85
86private:
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
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_;
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
Base class for exceptions in Fawkes.
Definition: exception.h:36
Message handler for FawkesNetworkClient.
Fawkes network client receive thread.
Definition: client.cpp:187
Fawkes network client send thread.
Definition: client.cpp:64
Simple Fawkes network client.
Definition: client.h:52
void wake(unsigned int component_id)
Wake a waiting thread.
Definition: client.cpp:814
void register_handler(FawkesNetworkClientHandler *handler, unsigned int component_id)
Register handler.
Definition: client.cpp:658
~FawkesNetworkClient()
Destructor.
Definition: client.cpp:402
const char * get_hostname() const
Get the client's hostname.
Definition: client.cpp:859
void wait(unsigned int component_id, unsigned int timeout_sec=15)
Wait for messages for component ID.
Definition: client.cpp:785
bool has_id() const
Check whether the client has an id.
Definition: client.cpp:837
void enqueue_and_wait(FawkesNetworkMessage *message, unsigned int timeout_sec=15)
Enqueue message to send and wait for answer.
Definition: client.cpp:614
void connect()
Connect to remote.
Definition: client.cpp:424
FawkesNetworkClient()
Constructor.
Definition: client.cpp:340
void disconnect()
Disconnect socket.
Definition: client.cpp:539
void deregister_handler(unsigned int component_id)
Deregister handler.
Definition: client.cpp:676
unsigned int id() const
Get the client's ID.
Definition: client.cpp:846
bool connected() const noexcept
Check if connection is alive.
Definition: client.cpp:828
void enqueue(FawkesNetworkMessage *message)
Enqueue message to send.
Definition: client.cpp:596
void interrupt_connect()
Interrupt connect().
Definition: client.cpp:576
Representation of a message that is sent over the network.
Definition: message.h:77
Client handler has already been registered.
Definition: client.h:44
Mutex mutual exclusion lock.
Definition: mutex.h:33
TCP stream socket over IP.
Definition: stream.h:32
Wait until a given condition holds.
Fawkes library namespace.