Server.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/core/Clock.h>
13 #include <mrpt/system/COutputLogger.h>
14 #include <mvsim/Comms/ports.h>
15 #include <mvsim/Comms/zmq_fwrds.h>
16 
17 #include <atomic>
18 #include <set>
19 #include <shared_mutex> // read/write mutex
20 #include <thread>
21 
22 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
23 
24 #include "AdvertiseServiceRequest.pb.h"
25 #include "AdvertiseTopicRequest.pb.h"
26 #include "GetServiceInfoRequest.pb.h"
27 #include "ListNodesRequest.pb.h"
28 #include "ListTopicsRequest.pb.h"
29 #include "RegisterNodeRequest.pb.h"
30 #include "SubscribeRequest.pb.h"
31 #include "UnregisterNodeRequest.pb.h"
32 
33 #endif
34 
35 namespace mvsim
36 {
40 class World;
41 
60 class Server : public mrpt::system::COutputLogger
61 {
62  public:
63  Server();
64  ~Server();
65 
69  void start();
70 
73  void shutdown() noexcept;
76  unsigned int listenningPort() const { return serverPortNo_; }
77  void listenningPort(unsigned int port) { serverPortNo_ = port; }
78 
79  private:
80  std::thread mainThread_;
81  std::atomic<zmq::context_t*> mainThreadZMQcontext_ = nullptr;
83 
84  void internalServerThread();
85 
86  // ========= Message handlers ========
87 #if defined(MVSIM_HAS_ZMQ) && defined(MVSIM_HAS_PROTOBUF)
88  void handle(const mvsim_msgs::RegisterNodeRequest& m, zmq::socket_t& s);
89  void handle(const mvsim_msgs::UnregisterNodeRequest& m, zmq::socket_t& s);
90  void handle(const mvsim_msgs::SubscribeRequest& m, zmq::socket_t& s);
91  void handle(const mvsim_msgs::ListTopicsRequest& m, zmq::socket_t& s);
92  void handle(const mvsim_msgs::ListNodesRequest& m, zmq::socket_t& s);
93  void handle(const mvsim_msgs::AdvertiseTopicRequest& m, zmq::socket_t& s);
94  void handle(const mvsim_msgs::AdvertiseServiceRequest& m, zmq::socket_t& s);
95  void handle(const mvsim_msgs::GetServiceInfoRequest& m, zmq::socket_t& s);
96 #endif
97 
103  mutable std::shared_mutex dbMutex;
104 
106  void db_remove_node(const std::string& nodeName);
107 
109  void db_register_node(const std::string& nodeName);
110 
112  void db_advertise_topic(
113  const std::string& topicName, const std::string& topicTypeName,
114  const std::string& publisherEndpoint, const std::string& nodeName);
115 
118  const std::string& serviceName, const std::string& inputTypeName,
119  const std::string& outputTypeName, const std::string& publisherEndpoint,
120  const std::string& nodeName);
121 
123  bool db_get_service_info(
124  const std::string& serviceName, std::string& publisherEndpoint,
125  std::string& nodeName) const;
126 
128  const std::string& topicName, const std::string& updatesEndPoint);
129 
130  struct InfoPerNode
131  {
132  InfoPerNode(const std::string& name) : nodeName(name) {}
133 
134  const std::string nodeName;
135 
137  const mrpt::Clock::time_point timeConnected = mrpt::Clock::now();
138 
139  std::set<std::string> advertisedTopics;
140  std::set<std::string> subscribedTopics;
141  };
142  using node_name_t = std::string;
143  std::map<node_name_t, InfoPerNode> connectedNodes_;
144 
145  using endpoint_t = std::string;
146 
148  {
150  const std::string& topic_name,
151  const std::string& publisher_node_name,
152  const std::string& publisher_endpoint)
153  : topicName(topic_name),
154  publisherNodeName(publisher_node_name),
155  publisherEndpoint(publisher_endpoint)
156  {
157  }
158  const std::string topicName;
159  const std::string publisherNodeName;
161  };
162 
164  {
166  const std::string& topic_name,
167  const std::string& sub_updates_endpoint)
168  : topicName(topic_name),
169  subscriberUpdatesEndpoint(sub_updates_endpoint)
170  {
171  }
172  const std::string topicName;
174  };
175 
177  {
178  InfoPerTopic() = default;
180  const std::string& name, const std::string& topic_type_name)
181  : topicName(name), topicTypeName(topic_type_name)
182  {
183  }
184  std::string topicName, topicTypeName;
185 
186  std::map<node_name_t, InfoPerPublisher> publishers;
187  std::map<endpoint_t, InfoPerSubscriber> subscribers;
188  };
189  using topic_name_t = std::string;
190  std::map<topic_name_t, InfoPerTopic> knownTopics_;
191 
193  {
194  InfoPerService() = default;
196  const std::string& name, const std::string& in_type_name,
197  const std::string& out_type_name, const std::string& end_point,
198  const std::string& node_name)
199  : serviceName(name),
200  inputTypeName(in_type_name),
201  outputTypeName(out_type_name),
202  endpoint(end_point),
203  nodeName(node_name)
204  {
205  }
206  std::string serviceName, inputTypeName, outputTypeName;
207  std::string endpoint, nodeName;
208  };
209  using service_name_t = std::string;
210  std::map<service_name_t, InfoPerService> knownServices_;
211 
215 };
218 } // namespace mvsim
const std::string nodeName
Definition: Server.h:134
void db_advertise_topic(const std::string &topicName, const std::string &topicTypeName, const std::string &publisherEndpoint, const std::string &nodeName)
Definition: Server.cpp:201
std::set< std::string > subscribedTopics
Definition: Server.h:140
const std::string topicName
Definition: Server.h:158
std::map< node_name_t, InfoPerNode > connectedNodes_
Definition: Server.h:143
InfoPerNode(const std::string &name)
Definition: Server.h:132
InfoPerService(const std::string &name, const std::string &in_type_name, const std::string &out_type_name, const std::string &end_point, const std::string &node_name)
Definition: Server.h:195
void db_advertise_service(const std::string &serviceName, const std::string &inputTypeName, const std::string &outputTypeName, const std::string &publisherEndpoint, const std::string &nodeName)
Definition: Server.cpp:268
const endpoint_t subscriberUpdatesEndpoint
Definition: Server.h:173
const mrpt::Clock::time_point timeConnected
Definition: Server.h:137
std::string service_name_t
Definition: Server.h:209
mrpt::system::TTimeStamp now()
std::map< service_name_t, InfoPerService > knownServices_
Definition: Server.h:210
void internalServerThread()
Definition: Server.cpp:77
std::string node_name_t
Definition: Server.h:142
void db_remove_node(const std::string &nodeName)
Definition: Server.cpp:173
std::shared_mutex dbMutex
Definition: Server.h:103
InfoPerTopic(const std::string &name, const std::string &topic_type_name)
Definition: Server.h:179
std::string topic_name_t
Definition: Server.h:189
GLdouble s
void listenningPort(unsigned int port)
Definition: Server.h:77
InfoPerSubscriber(const std::string &topic_name, const std::string &sub_updates_endpoint)
Definition: Server.h:165
unsigned int listenningPort() const
Definition: Server.h:76
std::thread mainThread_
Definition: Server.h:80
std::map< node_name_t, InfoPerPublisher > publishers
Definition: Server.h:186
const std::string publisherNodeName
Definition: Server.h:159
std::atomic< zmq::context_t * > mainThreadZMQcontext_
Definition: Server.h:81
GLuint const GLchar * name
std::map< topic_name_t, InfoPerTopic > knownTopics_
Definition: Server.h:190
void requestMainThreadTermination()
Definition: Server.cpp:157
bool db_get_service_info(const std::string &serviceName, std::string &publisherEndpoint, std::string &nodeName) const
Definition: Server.cpp:296
void start()
Definition: Server.cpp:41
std::set< std::string > advertisedTopics
Definition: Server.h:139
void shutdown() noexcept
Definition: Server.cpp:59
unsigned int serverPortNo_
Definition: Server.h:214
void db_add_topic_subscriber(const std::string &topicName, const std::string &updatesEndPoint)
Definition: Server.cpp:232
constexpr unsigned int MVSIM_PORTNO_MAIN_REP
Definition: ports.h:17
const endpoint_t publisherEndpoint
Definition: Server.h:160
std::string endpoint_t
Definition: Server.h:145
const GLdouble * m
void db_register_node(const std::string &nodeName)
Definition: Server.cpp:192
std::string topicTypeName
Definition: Server.h:184
std::map< endpoint_t, InfoPerSubscriber > subscribers
Definition: Server.h:187
const std::string topicName
Definition: Server.h:172
InfoPerPublisher(const std::string &topic_name, const std::string &publisher_node_name, const std::string &publisher_endpoint)
Definition: Server.h:149


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51