37 #ifndef _OPENPRS_AGENT_OPRS_PROTOBUF_H_ 38 #define _OPENPRS_AGENT_OPRS_PROTOBUF_H_ 40 #include <core/threading/mutex.h> 41 #include <core/utils/lock_queue.h> 42 #include <protobuf_comm/server.h> 47 #include <oprs-type-pub.h> 48 #include <oprs-type_f-pub.h> 50 namespace protobuf_comm {
51 class ProtobufStreamClient;
52 class ProtobufBroadcastPeer;
55 namespace oprs_protobuf {
73 const std::map<long int, protobuf_comm::ProtobufBroadcastPeer *> &
84 return *message_register_;
91 std::shared_ptr<google::protobuf::Message>)> &
94 return sig_server_sent_;
100 boost::signals2::signal<
101 void(std::string,
unsigned short, std::shared_ptr<google::protobuf::Message>)> &
104 return sig_client_sent_;
110 boost::signals2::signal<void(
long int, std::shared_ptr<google::protobuf::Message>)> &
113 return sig_peer_sent_;
124 std::shared_ptr<google::protobuf::Message> *
oprs_create_msg(std::string full_name);
140 const std::string &crypto_key =
"",
141 const std::string &cipher =
"");
145 const std::string &crypto_key =
"",
146 const std::string &cipher =
"");
149 const std::string &crypto_key,
150 const std::string &cipher);
156 typedef enum { CT_SERVER, CT_CLIENT, CT_PEER } ClientType;
157 void clips_assert_message(std::pair<std::string, unsigned short> & endpoint,
160 std::shared_ptr<google::protobuf::Message> &msg,
162 unsigned int client_id = 0);
164 boost::asio::ip::tcp::endpoint & endpoint);
166 const boost::system::error_code & error);
169 uint16_t component_id,
171 std::shared_ptr<google::protobuf::Message> msg);
174 uint16_t component_id,
178 void handle_peer_msg(
long int peer_id,
179 boost::asio::ip::udp::endpoint & endpoint,
180 uint16_t component_id,
182 std::shared_ptr<google::protobuf::Message> msg);
183 void handle_peer_recv_error(
long int peer_id,
184 boost::asio::ip::udp::endpoint &endpoint,
186 void handle_peer_send_error(
long int peer_id,
const std::string &msg);
188 void handle_client_connected(
long int client_id);
189 void handle_client_disconnected(
long int client_id,
const boost::system::error_code &error);
190 void handle_client_msg(
long int client_id,
193 std::shared_ptr<google::protobuf::Message> msg);
194 void handle_client_receive_fail(
long int client_id,
197 const std::string &msg);
198 void oprs_assert_message(std::string & endpoint_host,
199 unsigned short endpoint_port,
202 std::shared_ptr<google::protobuf::Message> &msg,
203 OpenPRSProtobuf::ClientType ct,
204 unsigned int client_id);
205 void oprs_assert_server_client_event(
long int client_id,
209 void oprs_assert_client_event(
long int client_id,
bool connect);
212 std::shared_ptr<protobuf_comm::MessageRegister> message_register_;
216 std::shared_ptr<google::protobuf::Message>)>
218 boost::signals2::signal<
219 void(std::string,
unsigned short, std::shared_ptr<google::protobuf::Message>)>
221 boost::signals2::signal<void(
long int, std::shared_ptr<google::protobuf::Message>)>
225 long int next_client_id_;
227 std::map<long int, protobuf_comm::ProtobufStreamServer::ClientID> server_clients_;
228 typedef std::map<protobuf_comm::ProtobufStreamServer::ClientID, long int> RevServerClientMap;
229 RevServerClientMap rev_server_clients_;
230 std::map<long int, protobuf_comm::ProtobufStreamClient *> clients_;
231 std::map<long int, protobuf_comm::ProtobufBroadcastPeer *> peers_;
233 std::map<long int, std::pair<std::string, unsigned short>> client_endpoints_;
239 std::shared_ptr<google::protobuf::Message>,
std::shared_ptr< google::protobuf::Message > * oprs_create_msg(std::string full_name)
Create a new message of given type.
void oprs_pb_peer_destroy(long int peer_id)
Disable peer.
OpenPRS protobuf integration class.
bool oprs_pb_has_field(void *msgptr, std::string field_name)
Check if message has a specific field.
void oprs_pb_set_field(void *msgptr, std::string field_name, Term *value)
Set a field.
Term * oprs_pb_field_label(void *msgptr, std::string field_name)
Get a fields label.
Term * oprs_pb_field_value(void *msgptr, std::string field_name)
Get properly typed field value.
const std::map< long int, protobuf_comm::ProtobufBroadcastPeer * > & peers() const
Get protobuf_comm peers.
unsigned int ClientID
ID to identify connected clients.
OpenPRSProtobuf(std::vector< std::string > &proto_path)
Constructor.
Register to map msg type numbers to Protobuf messages.
void oprs_pb_broadcast(long int peer_id, void *msgptr)
Broadcast a message through a peer.
~OpenPRSProtobuf()
Destructor.
void oprs_pb_process()
Process all pending events.
Term * oprs_pb_client_connect(std::string host, int port)
Connect as a client to the given server.
protobuf_comm::MessageRegister & message_register()
Get the communicator's message register.
Term * oprs_pb_destroy(void *msgptr)
Destroy given message (reference).
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.
Term * oprs_pb_field_type(void *msgptr, std::string field_name)
Get type if a specific field.
void oprs_pb_enable_server(int port)
Enable protobuf stream server.
void oprs_pb_disconnect(long int client_id)
Disconnect a given client.
Term * oprs_pb_field_names(void *msgptr)
Get field names of message.
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.
protobuf_comm::ProtobufStreamServer * server() const
Get Protobuf server.
Term * oprs_pb_peer_create_local_crypto(const std::string &host, int send_port, int recv_port, const std::string &crypto_key="", const std::string &cipher="")
Enable protobuf peer.
boost::signals2::signal< void(long int, std::shared_ptr< google::protobuf::Message >)> & signal_peer_sent()
Signal invoked for a message that has been sent via broadcast.
bool oprs_pb_register_type(std::string full_name)
Register a new message type.
Term * oprs_pb_peer_create(const std::string &host, int port)
Enable protobuf peer.
Term * oprs_pb_ref(void *msgptr)
Create new reference to message.
bool oprs_pb_field_is_list(void *msgptr, std::string field_name)
Check if a given field is a list (repeated field).
Stream server for protobuf message transmission.
void oprs_pb_peer_setup_crypto(long int peer_id, const std::string &crypto_key, const std::string &cipher)
Setup crypto for peer.
void oprs_pb_disable_server()
Disable protobuf stream server.
void oprs_pb_add_list(void *msgptr, std::string field_name, Term *value)
Add value to a repeated field.
Mutex mutual exclusion lock.
Term * oprs_pb_peer_create_local(const std::string &host, int send_port, int recv_port)
Enable protobuf peer.
bool oprs_pb_events_pending()
Check if there are pending events.
Term * oprs_pb_peer_create_crypto(const std::string &host, int port, const std::string &crypto_key="", const std::string &cipher="")
Enable protobuf peer.
void oprs_pb_send(long int client_id, void *msgptr)
Send message to a specific client.
Term * oprs_pb_field_list(void *msgptr, std::string field_name)
Get list of values of a given message field.