Client.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/system/COutputLogger.h>
13 #include <mvsim/Comms/common.h>
14 #include <mvsim/Comms/zmq_fwrds.h>
15 
16 #include <atomic>
17 #include <memory>
18 #include <thread>
19 
20 namespace mvsim::internal
21 {
22 struct InfoPerService;
23 struct InfoPerSubscribedTopic;
24 } // namespace mvsim::internal
25 
26 namespace mvsim
27 {
47 class Client : public mrpt::system::COutputLogger
48 {
49  public:
50  Client();
51  Client(const std::string& nodeName);
52 
53  ~Client();
54 
57  void setName(const std::string& nodeName);
58 
60  void connect();
61 
63  bool connected() const;
64 
68  void shutdown() noexcept;
69 
70  template <typename T>
71  void advertiseTopic(const std::string& topicName);
72 
73  void publishTopic(
74  const std::string& topicName, const google::protobuf::Message& msg);
75 
76  template <typename MSG_T>
77  void subscribeTopic(
78  const std::string& topicName,
79  const std::function<void(const MSG_T&)>& callback);
80 
81  template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
82  void advertiseService(
83  const std::string& serviceName,
84  const std::function<OUTPUT_MSG_T(const INPUT_MSG_T&)>& callback);
85 
86  template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
87  void callService(
88  const std::string& serviceName, const INPUT_MSG_T& input,
89  OUTPUT_MSG_T& output);
90 
91  // Overload for python wrapper
92  std::string callService(
93  const std::string& serviceName, const std::string& inputSerializedMsg);
94 
95  struct InfoPerNode
96  {
97  std::string name;
98  };
99  std::vector<InfoPerNode> requestListOfNodes();
100 
102  {
103  std::string name;
104  std::string type;
105  std::vector<std::string> endpoints, publishers;
106  };
107  std::vector<InfoPerTopic> requestListOfTopics();
108 
111  private:
112  struct ZMQImpl;
113  std::unique_ptr<ZMQImpl> zmq_;
114 
115  std::string serverHostAddress_ = "localhost";
116  std::string nodeName_ = "anonymous";
117 
119  std::thread topicUpdatesThread_;
120 
121  void doRegisterClient();
122  void doUnregisterClient();
123 
124  void internalServiceServingThread();
125  void internalTopicUpdatesThread();
126  void internalTopicSubscribeThread(internal::InfoPerSubscribedTopic& ipt);
127 
128  using service_callback_t =
129  std::function<std::shared_ptr<google::protobuf::Message>(
130  const std::string& /*inAsString*/)>;
131 
132  void doAdvertiseTopic(
133  const std::string& topicName,
134  const google::protobuf::Descriptor* descriptor);
135  void doAdvertiseService(
136  const std::string& serviceName,
137  const google::protobuf::Descriptor* descIn,
138  const google::protobuf::Descriptor* descOut,
139  service_callback_t callback);
140 
141  using topic_callback_t = std::function<void(const zmq::message_t& /*m*/)>;
142 
143  void doSubscribeTopic(
144  const std::string& topicName,
145  const google::protobuf::Descriptor* descriptor,
146  const topic_callback_t& callback);
147  void doCallService(
148  const std::string& serviceName, const std::string& inputSerializedMsg,
149  mrpt::optional_ref<google::protobuf::Message> outputMsg,
150  mrpt::optional_ref<std::string> outputSerializedMsg = std::nullopt,
151  mrpt::optional_ref<std::string> outputMsgTypeName = std::nullopt);
152 
153  friend struct internal::InfoPerService;
154  friend struct internal::InfoPerSubscribedTopic;
155 };
156 
157 template <typename T>
158 void Client::advertiseTopic(const std::string& topicName)
159 {
160  doAdvertiseTopic(topicName, T::descriptor());
161 }
162 
163 template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
165  const std::string& serviceName,
166  const std::function<OUTPUT_MSG_T(const INPUT_MSG_T&)>& callback)
167 {
168  doAdvertiseService(
169  serviceName, INPUT_MSG_T::descriptor(), OUTPUT_MSG_T::descriptor(),
170  service_callback_t([callback](const std::string& inData) {
171  INPUT_MSG_T in;
172  in.ParseFromString(inData);
173  return std::make_shared<OUTPUT_MSG_T>(callback(in));
174  }));
175 }
176 
177 template <typename MSG_T>
179  const std::string& topicName,
180  const std::function<void(const MSG_T&)>& callback)
181 {
182  doSubscribeTopic(
183  topicName, MSG_T::descriptor(),
184  topic_callback_t([callback](const zmq::message_t& m) {
185  MSG_T in;
186  mvsim::parseMessage(m, in);
187  callback(in);
188  }));
189 }
190 
191 template <typename INPUT_MSG_T, typename OUTPUT_MSG_T>
193  const std::string& serviceName, const INPUT_MSG_T& input,
194  OUTPUT_MSG_T& output)
195 {
196  doCallService(serviceName, input.SerializeAsString(), output);
197 }
198 
199 } // namespace mvsim
GLvoid *typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
std::thread topicUpdatesThread_
Definition: Client.h:119
std::unique_ptr< ZMQImpl > zmq_
Definition: Client.h:112
GLenum GLenum GLenum input
void subscribeTopic(const std::string &topicName, const std::function< void(const MSG_T &)> &callback)
Definition: Client.h:178
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:141
GLuint in
void advertiseService(const std::string &serviceName, const std::function< OUTPUT_MSG_T(const INPUT_MSG_T &)> &callback)
Definition: Client.h:164
void callService(const std::string &serviceName, const INPUT_MSG_T &input, OUTPUT_MSG_T &output)
Definition: Client.h:192
std::thread serviceInvokerThread_
Definition: Client.h:118
void advertiseTopic(const std::string &topicName)
Definition: Client.h:158
const GLdouble * m
std::vector< std::string > publishers
Definition: Client.h:105
std::function< std::shared_ptr< google::protobuf::Message >(const std::string &)> service_callback_t
Definition: Client.h:130


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