Fawkes API  Fawkes Development Version
communicator.h
1 
2 /***************************************************************************
3  * communicator.h - protobuf network communication for CLIPS
4  *
5  * Created: Tue Apr 16 13:41:13 2013
6  * Copyright 2013-2014 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_CLIPS_COMMUNICATOR_H_
38 #define __PROTOBUF_CLIPS_COMMUNICATOR_H_
39 
40 #include <list>
41 #include <map>
42 #include <clipsmm.h>
43 
44 #include <protobuf_comm/server.h>
45 #include <core/threading/mutex.h>
46 
47 namespace protobuf_comm {
48  class ProtobufStreamClient;
49  class ProtobufBroadcastPeer;
50 }
51 
52 namespace fawkes {
53  class Logger;
54 }
55 
56 namespace protobuf_clips {
57 #if 0 /* just to make Emacs auto-indent happy */
58 }
59 #endif
60 
62 {
63  public:
64  ClipsProtobufCommunicator(CLIPS::Environment *env, fawkes::Mutex &env_mutex,
65  fawkes::Logger *logger = NULL);
66  ClipsProtobufCommunicator(CLIPS::Environment *env, fawkes::Mutex &env_mutex,
67  std::vector<std::string> &proto_path,
68  fawkes::Logger *logger = NULL);
70 
71  void enable_server(int port);
72  void disable_server();
73 
74  /** Get Protobuf server.
75  * @return protobuf server */
77  { return server_; }
78 
79  /** Get protobuf_comm peers.
80  * @return protobuf_comm peer */
81  const std::map<long int, protobuf_comm::ProtobufBroadcastPeer *> &
82  peers() const
83  { return peers_; }
84 
85  /** Get the communicator's message register.
86  * @return message register */
88  { return *message_register_; }
89 
90  /** Signal invoked for a message that has been sent to a server client.
91  * @return signal
92  */
93  boost::signals2::signal<void (protobuf_comm::ProtobufStreamServer::ClientID,
94  std::shared_ptr<google::protobuf::Message>)> &
95  signal_server_sent() { return sig_server_sent_; }
96 
97  /** Signal invoked for a message that has been sent to a client.
98  * @return signal
99  */
100  boost::signals2::signal<void (std::string, unsigned short,
101  std::shared_ptr<google::protobuf::Message>)> &
102  signal_client_sent() { return sig_client_sent_; }
103 
104  /** Signal invoked for a message that has been sent via broadcast.
105  * @return signal
106  */
107  boost::signals2::signal<void (long,
108  std::shared_ptr<google::protobuf::Message>)> &
109  signal_peer_sent() { return sig_peer_sent_; }
110 
111  private:
112  void setup_clips();
113 
114  CLIPS::Value clips_pb_register_type(std::string full_name);
115  CLIPS::Values clips_pb_field_names(void *msgptr);
116  CLIPS::Value clips_pb_has_field(void *msgptr, std::string field_name);
117  CLIPS::Value clips_pb_field_value(void *msgptr, std::string field_name);
118  CLIPS::Value clips_pb_field_type(void *msgptr, std::string field_name);
119  CLIPS::Value clips_pb_field_label(void *msgptr, std::string field_name);
120  CLIPS::Values clips_pb_field_list(void *msgptr, std::string field_name);
121  CLIPS::Value clips_pb_field_is_list(void *msgptr, std::string field_name);
122  CLIPS::Value clips_pb_create(std::string full_name);
123  CLIPS::Value clips_pb_ref(void *msgptr);
124  void clips_pb_destroy(void *msgptr);
125  void clips_pb_set_field(void *msgptr, std::string field_name, CLIPS::Value value);
126  void clips_pb_add_list(void *msgptr, std::string field_name, CLIPS::Value value);
127  void clips_pb_send(long int client_id, void *msgptr);
128  std::string clips_pb_tostring(void *msgptr);
129  long int clips_pb_client_connect(std::string host, int port);
130  void clips_pb_disconnect(long int client_id);
131  void clips_pb_broadcast(long int peer_id, void *msgptr);
132  void clips_pb_enable_server(int port);
133 
134  long int clips_pb_peer_create(std::string host, int port);
135  long int clips_pb_peer_create_local(std::string host,
136  int send_port, int recv_port);
137  long int clips_pb_peer_create_crypto(std::string host, int port,
138  std::string crypto_key = "", std::string cipher = "");
139  long int clips_pb_peer_create_local_crypto(std::string host,
140  int send_port, int recv_port,
141  std::string crypto_key = "", std::string cipher = "");
142  void clips_pb_peer_destroy(long int peer_id);
143  void clips_pb_peer_setup_crypto(long int peer_id,
144  std::string crypto_key, std::string cipher);
145 
146 
147  typedef enum {
148  CT_SERVER, CT_CLIENT, CT_PEER
149  } ClientType;
150  void clips_assert_message(std::pair<std::string, unsigned short> &endpoint,
151  uint16_t comp_id, uint16_t msg_type,
152  std::shared_ptr<google::protobuf::Message> &msg,
153  ClientType ct, unsigned int client_id = 0);
154  void handle_server_client_connected(protobuf_comm::ProtobufStreamServer::ClientID client,
155  boost::asio::ip::tcp::endpoint &endpoint);
156  void handle_server_client_disconnected(protobuf_comm::ProtobufStreamServer::ClientID client,
157  const boost::system::error_code &error);
158 
159  void handle_server_client_msg(protobuf_comm::ProtobufStreamServer::ClientID client,
160  uint16_t component_id, uint16_t msg_type,
161  std::shared_ptr<google::protobuf::Message> msg);
162 
163  void handle_server_client_fail(protobuf_comm::ProtobufStreamServer::ClientID client,
164  uint16_t component_id, uint16_t msg_type,
165  std::string msg);
166 
167  void handle_peer_msg(long int peer_id,
168  boost::asio::ip::udp::endpoint &endpoint,
169  uint16_t component_id, uint16_t msg_type,
170  std::shared_ptr<google::protobuf::Message> msg);
171  void handle_peer_recv_error(long int peer_id, boost::asio::ip::udp::endpoint &endpoint, std::string msg);
172  void handle_peer_send_error(long int peer_id, std::string msg);
173 
174  void handle_client_connected(long int client_id);
175  void handle_client_disconnected(long int client_id,
176  const boost::system::error_code &error);
177  void handle_client_msg(long int client_id,
178  uint16_t comp_id, uint16_t msg_type,
179  std::shared_ptr<google::protobuf::Message> msg);
180  void handle_client_receive_fail(long int client_id,
181  uint16_t comp_id, uint16_t msg_type, std::string msg);
182 
183  static std::string to_string(const CLIPS::Value &v);
184 
185  private:
186  CLIPS::Environment *clips_;
187  fawkes::Mutex &clips_mutex_;
188 
189  fawkes::Logger *logger_;
190 
191  protobuf_comm::MessageRegister *message_register_;
193 
194  boost::signals2::signal<void (protobuf_comm::ProtobufStreamServer::ClientID,
195  std::shared_ptr<google::protobuf::Message>)> sig_server_sent_;
196  boost::signals2::signal<void (std::string, unsigned short,
197  std::shared_ptr<google::protobuf::Message>)> sig_client_sent_;
198  boost::signals2::signal<void (long int, std::shared_ptr<google::protobuf::Message>)> sig_peer_sent_;
199 
200  fawkes::Mutex map_mutex_;
201  long int next_client_id_;
202 
203 
204  std::map<long int, protobuf_comm::ProtobufStreamServer::ClientID> server_clients_;
205  typedef std::map<protobuf_comm::ProtobufStreamServer::ClientID, long int> RevServerClientMap;
206  RevServerClientMap rev_server_clients_;
207  std::map<long int, protobuf_comm::ProtobufStreamClient *> clients_;
208  std::map<long int, protobuf_comm::ProtobufBroadcastPeer *> peers_;
209 
210  std::map<long int, std::pair<std::string, unsigned short>> client_endpoints_;
211 
212  std::list<std::string> functions_;
213  CLIPS::Fact::pointer avail_fact_;
214 };
215 
216 } // end namespace protobuf_clips
217 
218 #endif
Fawkes library namespace.
unsigned int ClientID
ID to identify connected clients.
Definition: server.h:68
const std::map< long int, protobuf_comm::ProtobufBroadcastPeer * > & peers() const
Get protobuf_comm peers.
Definition: communicator.h:82
Register to map msg type numbers to Protobuf messages.
protobuf_comm::ProtobufStreamServer * server() const
Get Protobuf server.
Definition: communicator.h:76
Stream server for protobuf message transmission.
Definition: server.h:64
protobuf_comm::MessageRegister & message_register()
Get the communicator&#39;s message register.
Definition: communicator.h:87
boost::signals2::signal< void(long, std::shared_ptr< google::protobuf::Message >)> & signal_peer_sent()
Signal invoked for a message that has been sent via broadcast.
Definition: communicator.h:109
boost::signals2::signal< void(std::string, unsigned short, std::shared_ptr< google::protobuf::Message >)> & signal_client_sent()
Signal invoked for a message that has been sent to a client.
Definition: communicator.h:102
Mutex mutual exclusion lock.
Definition: mutex.h:32
CLIPS protobuf integration class.
Definition: communicator.h:61
Interface for logging.
Definition: logger.h:34
boost::signals2::signal< void(protobuf_comm::ProtobufStreamServer::ClientID, std::shared_ptr< google::protobuf::Message >)> & signal_server_sent()
Signal invoked for a message that has been sent to a server client.
Definition: communicator.h:95