Fawkes API  Fawkes Development Version
client.h
1 
2 /***************************************************************************
3  * client.h - Protobuf stream protocol - client
4  *
5  * Created: Thu Jan 31 17:28:09 2013
6  * Copyright 2013 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * - Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * - Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  * - Neither the name of the authors nor the names of its contributors
20  * may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #ifndef _PROTOBUF_COMM_CLIENT_H_
38 #define _PROTOBUF_COMM_CLIENT_H_
39 
40 #include <google/protobuf/message.h>
41 #include <protobuf_comm/frame_header.h>
42 #include <protobuf_comm/message_register.h>
43 #include <protobuf_comm/queue_entry.h>
44 
45 #include <boost/asio.hpp>
46 #include <boost/signals2.hpp>
47 #include <cstdint>
48 #include <mutex>
49 #include <queue>
50 #include <string>
51 #include <thread>
52 
53 namespace protobuf_comm {
54 
56 {
57 public:
59  ProtobufStreamClient(std::vector<std::string> &proto_path);
60  ProtobufStreamClient(MessageRegister *mr, frame_header_version_t header_version = PB_FRAME_V2);
62 
63  /** Get the client's message register.
64  * @return message register
65  */
68  {
69  return *message_register_;
70  }
71 
72  void async_connect(const char *host, unsigned short port);
73  void disconnect();
74 
75  /** Check if client is connected.
76  * @return true if the client is connected, false otherwise
77  */
78  bool
79  connected() const
80  {
81  return connected_;
82  }
83 
84  void send(uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m);
85 
86  void send(std::shared_ptr<google::protobuf::Message> m);
87  void send(google::protobuf::Message &m);
88 
89  /** Signal that is invoked when a message has been received.
90  * @return signal
91  */
92  boost::signals2::signal<void(uint16_t, uint16_t, std::shared_ptr<google::protobuf::Message>)> &
94  {
95  return sig_rcvd_;
96  }
97 
98  /** Signal that is invoked when receiving a message failed.
99  * @return signal
100  */
101  boost::signals2::signal<void(uint16_t, uint16_t, std::string)> &
103  {
104  return sig_recv_failed_;
105  }
106 
107  /** Signal that is invoked when the connection has been established.
108  * @return signal
109  */
110  boost::signals2::signal<void()> &
112  {
113  return sig_connected_;
114  }
115 
116  /** Signal that is invoked when the connection is closed.
117  * @return signal
118  */
119  boost::signals2::signal<void(const boost::system::error_code &)> &
121  {
122  return sig_disconnected_;
123  }
124 
125 private: // types
126 private: // methods
127  void disconnect_nosig();
128  void run_asio();
129  void handle_resolve(const boost::system::error_code & err,
130  boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
131  void handle_connect(const boost::system::error_code &err);
132  void handle_write(const boost::system::error_code &error,
133  size_t /*bytes_transferred*/,
134  QueueEntry *entry);
135  void start_recv();
136  void handle_read_header(const boost::system::error_code &error);
137  void handle_read_message(const boost::system::error_code &error);
138 
139 private: // members
140  bool connected_;
141  std::mutex asio_mutex_;
142  boost::asio::io_service io_service_;
143  boost::asio::ip::tcp::resolver resolver_;
144  boost::asio::ip::tcp::socket socket_;
145  boost::asio::io_service::work io_service_work_;
146 
147  boost::signals2::signal<void(uint16_t, uint16_t, std::shared_ptr<google::protobuf::Message>)>
148  sig_rcvd_;
149  boost::signals2::signal<void(uint16_t, uint16_t, std::string)> sig_recv_failed_;
150  boost::signals2::signal<void()> sig_connected_;
151  boost::signals2::signal<void(const boost::system::error_code &)> sig_disconnected_;
152 
153  std::thread asio_thread_;
154 
155  std::queue<QueueEntry *> outbound_queue_;
156  std::mutex outbound_mutex_;
157  bool outbound_active_;
158 
159  void * in_frame_header_;
160  size_t in_frame_header_size_;
161  size_t in_data_size_;
162  void * in_data_;
163 
164  MessageRegister *message_register_;
165  bool own_message_register_;
166 
167  frame_header_version_t frame_header_version_;
168 };
169 
170 } // end namespace protobuf_comm
171 
172 #endif
Register to map msg type numbers to Protobuf messages.
Stream client for protobuf message transmission.
Definition: client.h:56
bool connected() const
Check if client is connected.
Definition: client.h:79
void async_connect(const char *host, unsigned short port)
Asynchronous connect.
Definition: client.cpp:154
boost::signals2::signal< void()> & signal_connected()
Signal that is invoked when the connection has been established.
Definition: client.h:111
void disconnect()
Disconnect from remote host.
Definition: client.cpp:225
boost::signals2::signal< void(uint16_t, uint16_t, std::shared_ptr< google::protobuf::Message >)> & signal_received()
Signal that is invoked when a message has been received.
Definition: client.h:93
boost::signals2::signal< void(uint16_t, uint16_t, std::string)> & signal_receive_failed()
Signal that is invoked when receiving a message failed.
Definition: client.h:102
MessageRegister & message_register()
Get the client's message register.
Definition: client.h:67
boost::signals2::signal< void(const boost::system::error_code &)> & signal_disconnected()
Signal that is invoked when the connection is closed.
Definition: client.h:120
void send(uint16_t component_id, uint16_t msg_type, google::protobuf::Message &m)
Send a message to the server.
Definition: client.cpp:354
Outgoing queue entry.
Definition: queue_entry.h:47