Client.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 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/system/COutputLogger.h>
13 #include <mrpt/system/CTimeLogger.h>
14 #include <mvsim/Comms/common.h>
15 #include <mvsim/Comms/zmq_fwrds.h>
16 
17 #include <atomic>
18 #include <memory>
19 #include <thread>
20 
21 namespace mvsim::internal
22 {
23 struct InfoPerService;
24 struct InfoPerSubscribedTopic;
25 } // namespace mvsim::internal
26 
27 namespace mvsim
28 {
48 class Client : public mrpt::system::COutputLogger
49 {
50  public:
51  Client();
52  Client(const std::string& nodeName);
53 
54  ~Client();
55 
58  void setName(const std::string& nodeName);
59 
60  const std::string& serverHostAddress() const { return serverHostAddress_; }
61  void serverHostAddress(const std::string& serverIpOrAddressName)
62  {
63  ASSERT_(!connected());
64  serverHostAddress_ = serverIpOrAddressName;
65  }
66 
71  void connect();
72 
74  bool connected() const;
75 
79  void shutdown() noexcept;
80 
81  template <typename T>
82  void advertiseTopic(const std::string& topicName);
83 
84  void publishTopic(
85  const std::string& topicName, const google::protobuf::Message& msg);
86 
87  template <typename MSG_T>
88  void subscribeTopic(
89  const std::string& topicName,
90  const std::function<void(const MSG_T&)>& callback);
91 
92  template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
93  void advertiseService(
94  const std::string& serviceName,
95  const std::function<OUTPUT_MSG_T(const INPUT_MSG_T&)>& callback);
96 
97  template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
98  void callService(
99  const std::string& serviceName, const INPUT_MSG_T& input,
100  OUTPUT_MSG_T& output);
101 
103  std::string callService(
104  const std::string& serviceName, const std::string& inputSerializedMsg);
105 
107  void subscribeTopic(
108  const std::string& topicName,
109  const std::function<void(
110  const std::string& /*msgType*/,
111  const std::vector<uint8_t>& /*serializedMsg*/)>& callback);
112 
113  struct InfoPerNode
114  {
115  std::string name;
116  };
117  std::vector<InfoPerNode> requestListOfNodes();
118 
120  {
121  std::string name;
122  std::string type;
123  std::vector<std::string> endpoints, publishers;
124  };
125  std::vector<InfoPerTopic> requestListOfTopics();
126 
127  using topic_callback_t = std::function<void(const zmq::message_t& /*m*/)>;
128 
129  void subscribe_topic_raw(
130  const std::string& topicName, const topic_callback_t& callback);
131 
134  const mrpt::system::CTimeLogger& profiler() const { return profiler_; }
135  void enable_profiler(bool enable) { profiler_.enable(enable); }
136 
137  private:
138  struct ZMQImpl;
139  std::unique_ptr<ZMQImpl> zmq_;
140 
141  std::string serverHostAddress_ = "localhost";
142  std::string nodeName_ = "anonymous";
143 
145  std::thread topicUpdatesThread_;
146 
147  std::map<std::string, std::string> serviceToEndPointCache_;
149 
150  mrpt::system::CTimeLogger profiler_{false, "mvsim::Client"};
151 
152  void doRegisterClient();
153  void doUnregisterClient();
154 
155  void internalServiceServingThread();
156  void internalTopicUpdatesThread();
157  void internalTopicSubscribeThread(internal::InfoPerSubscribedTopic& ipt);
158 
159  using service_callback_t =
160  std::function<std::shared_ptr<google::protobuf::Message>(
161  const std::string& /*inAsString*/)>;
162 
163  void doAdvertiseTopic(
164  const std::string& topicName,
165  const google::protobuf::Descriptor* descriptor);
166  void doAdvertiseService(
167  const std::string& serviceName,
168  const google::protobuf::Descriptor* descIn,
169  const google::protobuf::Descriptor* descOut,
170  service_callback_t callback);
171 
172  void doSubscribeTopic(
173  const std::string& topicName,
174  const google::protobuf::Descriptor* descriptor,
175  const topic_callback_t& callback);
176  void doCallService(
177  const std::string& serviceName, const std::string& inputSerializedMsg,
178  mrpt::optional_ref<google::protobuf::Message> outputMsg,
179  mrpt::optional_ref<std::string> outputSerializedMsg = std::nullopt,
180  mrpt::optional_ref<std::string> outputMsgTypeName = std::nullopt);
181 
182  friend struct internal::InfoPerService;
183  friend struct internal::InfoPerSubscribedTopic;
184 };
185 
186 template <typename T>
187 void Client::advertiseTopic(const std::string& topicName)
188 {
189  doAdvertiseTopic(topicName, T::descriptor());
190 }
191 
192 template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
194  const std::string& serviceName,
195  const std::function<OUTPUT_MSG_T(const INPUT_MSG_T&)>& callback)
196 {
197  doAdvertiseService(
198  serviceName, INPUT_MSG_T::descriptor(), OUTPUT_MSG_T::descriptor(),
199  service_callback_t([callback](const std::string& inData) {
200  INPUT_MSG_T in;
201  in.ParseFromString(inData);
202  return std::make_shared<OUTPUT_MSG_T>(callback(in));
203  }));
204 }
205 
206 template <typename MSG_T>
208  const std::string& topicName,
209  const std::function<void(const MSG_T&)>& callback)
210 {
211  doSubscribeTopic(
212  topicName, MSG_T::descriptor(),
213  topic_callback_t([callback](const zmq::message_t& m) {
214  MSG_T in;
215  mvsim::parseMessage(m, in);
216  callback(in);
217  }));
218 }
219 
220 template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
222  const std::string& serviceName, const INPUT_MSG_T& input,
223  OUTPUT_MSG_T& output)
224 {
225  doCallService(serviceName, input.SerializeAsString(), output);
226 }
227 
228 } // namespace mvsim
void enable_profiler(bool enable)
Definition: Client.h:135
std::thread topicUpdatesThread_
Definition: Client.h:145
const mrpt::system::CTimeLogger & profiler() const
Definition: Client.h:134
std::unique_ptr< ZMQImpl > zmq_
Definition: Client.h:138
std::map< std::string, std::string > serviceToEndPointCache_
Definition: Client.h:147
void subscribeTopic(const std::string &topicName, const std::function< void(const MSG_T &)> &callback)
Definition: Client.h:207
ServiceServer advertiseService(ros::NodeHandle n, std::string name, boost::function< bool(const typename ActionSpec::_action_goal_type::_goal_type &, typename ActionSpec::_action_result_type::_result_type &result)> service_cb)
std::function< void(const zmq::message_t &)> topic_callback_t
Definition: Client.h:127
void advertiseService(const std::string &serviceName, const std::function< OUTPUT_MSG_T(const INPUT_MSG_T &)> &callback)
Definition: Client.h:193
void callService(const std::string &serviceName, const INPUT_MSG_T &input, OUTPUT_MSG_T &output)
Definition: Client.h:221
void serverHostAddress(const std::string &serverIpOrAddressName)
Definition: Client.h:61
typedef void(GLAD_API_PTR *GLDEBUGPROC)(GLenum source
std::thread serviceInvokerThread_
Definition: Client.h:144
void advertiseTopic(const std::string &topicName)
Definition: Client.h:187
ROSCONSOLE_DECL void shutdown()
const std::string & serverHostAddress() const
Definition: Client.h:60
std::vector< std::string > publishers
Definition: Client.h:123
std::function< std::shared_ptr< google::protobuf::Message >(const std::string &)> service_callback_t
Definition: Client.h:161
std::mutex serviceToEndPointCacheMtx_
Definition: Client.h:148


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19