Gazebo Transport

API Reference

13.7.0
Node.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_TRANSPORT_NODE_HH_
18 #define GZ_TRANSPORT_NODE_HH_
19 
20 #include <algorithm>
21 #include <functional>
22 #include <memory>
23 #include <mutex>
24 #include <optional>
25 #include <string>
26 #include <unordered_set>
27 #include <vector>
28 
30 #include "gz/transport/config.hh"
31 #include "gz/transport/Export.hh"
43 
44 namespace gz::transport
45 {
46  // Inline bracket to help doxygen filtering.
47  inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
48  //
49  // Forward declarations.
50  class NodePrivate;
51 
62  int GZ_TRANSPORT_VISIBLE rcvHwm();
63 
74  int GZ_TRANSPORT_VISIBLE sndHwm();
75 
80 
83  struct GZ_TRANSPORT_VISIBLE TopicInfo
84  {
87 
90 
93  };
94 
95  class GZ_TRANSPORT_VISIBLE Node
96  {
97  class PublisherPrivate;
98  class SubscriberPrivate;
99 
116  public: class GZ_TRANSPORT_VISIBLE Publisher
117  {
119  public: Publisher();
120 
123  public: explicit Publisher(const MessagePublisher &_publisher);
124 
126  public: virtual ~Publisher();
127 
131  public: operator bool();
132 
136  public: operator bool() const;
137 
141  public: bool Valid() const;
142 
148  public: bool Publish(const ProtoMsg &_msg);
149 
171  public: bool PublishRaw(
172  const std::string &_msgData,
173  const std::string &_msgType);
174 
183  public: bool ThrottledUpdateReady() const;
184 
188  private: bool UpdateThrottling();
189 
192  public: bool HasConnections() const;
193 
199 #ifdef _WIN32
200 // Disable warning C4251 which is triggered by
201 // std::shared_ptr
202 #pragma warning(push)
203 #pragma warning(disable: 4251)
204 #endif
205  private: std::shared_ptr<PublisherPrivate> dataPtr;
206 #ifdef _WIN32
207 #pragma warning(pop)
208 #endif
209  };
210 
224  public: class GZ_TRANSPORT_VISIBLE Subscriber
225  {
227  public: Subscriber();
228 
234  public: Subscriber(const std::string &_topic,
235  const std::string &_nUuid,
236  const NodeOptions &_nOpts,
237  const std::string &_hUuid);
238 
241  public: virtual ~Subscriber();
242 
245  public: bool Unsubscribe();
246 
250  public: operator bool();
251 
255  public: operator bool() const;
256 
259  public: Subscriber(Subscriber &&_other);
260 
265 
269  public: bool Valid() const;
270 
273 #ifdef _WIN32
274 // Disable warning C4251 which is triggered by
275 // std::shared_ptr
276 #pragma warning(push)
277 #pragma warning(disable: 4251)
278 #endif
279  private: std::shared_ptr<SubscriberPrivate> dataPtr;
280 #ifdef _WIN32
281 #pragma warning(pop)
282 #endif
283  };
284 
285  public: Node();
286 
289  public: explicit Node(const NodeOptions &_options);
290 
292  public: virtual ~Node();
293 
302  public: template<typename MessageT>
304  const std::string &_topic,
306 
319  const std::string &_topic,
320  const std::string &_msgTypeName,
322 
326 
336  public: template<typename MessageT>
337  bool Subscribe(
338  const std::string &_topic,
339  void(*_callback)(const MessageT &_msg),
340  const SubscribeOptions &_opts = SubscribeOptions());
341 
350  public: template<typename MessageT>
351  bool Subscribe(
352  const std::string &_topic,
353  std::function<void(const MessageT &_msg)> _callback,
354  const SubscribeOptions &_opts = SubscribeOptions());
355 
366  public: template<typename ClassT, typename MessageT>
367  bool Subscribe(
368  const std::string &_topic,
369  void(ClassT::*_callback)(const MessageT &_msg),
370  ClassT *_obj,
371  const SubscribeOptions &_opts = SubscribeOptions());
372 
383  public: template<typename MessageT>
384  bool Subscribe(
385  const std::string &_topic,
386  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
387  const SubscribeOptions &_opts = SubscribeOptions());
388 
398  public: template<typename MessageT>
399  bool Subscribe(
400  const std::string &_topic,
401  std::function<void(const MessageT &_msg,
402  const MessageInfo &_info)> _callback,
403  const SubscribeOptions &_opts = SubscribeOptions());
404 
416  public: template<typename ClassT, typename MessageT>
417  bool Subscribe(
418  const std::string &_topic,
419  void(ClassT::*_callback)(const MessageT &_msg,
420  const MessageInfo &_info),
421  ClassT *_obj,
422  const SubscribeOptions &_opts = SubscribeOptions());
423 
443  public: template <typename ...Args>
445  Args && ...args);
446 
453 
457  public: bool Unsubscribe(const std::string &_topic);
458 
477  public: template<typename RequestT, typename ReplyT>
478  bool Advertise(
479  const std::string &_topic,
480  bool(*_callback)(const RequestT &_request, ReplyT &_reply),
482 
500  public: template<typename ReplyT>
501  bool Advertise(
502  const std::string &_topic,
503  bool(*_callback)(ReplyT &_reply),
505 
522  public: template<typename RequestT>
523  bool Advertise(
524  const std::string &_topic,
525  void(*_callback)(const RequestT &_request),
527 
546  public: template<typename RequestT, typename ReplyT>
547  bool Advertise(
548  const std::string &_topic,
549  std::function<bool(const RequestT &_request,
550  ReplyT &_reply)> _callback,
552 
570  public: template<typename ReplyT>
571  bool Advertise(
572  const std::string &_topic,
573  std::function<bool(ReplyT &_reply)> &_callback,
575 
592  public: template<typename RequestT>
593  bool Advertise(
594  const std::string &_topic,
595  std::function<void(const RequestT &_request)> &_callback,
597 
617  public: template<typename ClassT, typename RequestT, typename ReplyT>
618  bool Advertise(
619  const std::string &_topic,
620  bool(ClassT::*_callback)(const RequestT &_request, ReplyT &_reply),
621  ClassT *_obj,
623 
642  public: template<typename ClassT, typename ReplyT>
643  bool Advertise(
644  const std::string &_topic,
645  bool(ClassT::*_callback)(ReplyT &_reply),
646  ClassT *_obj,
648 
666  public: template<typename ClassT, typename RequestT>
667  bool Advertise(
668  const std::string &_topic,
669  void(ClassT::*_callback)(const RequestT &_request),
670  ClassT *_obj,
672 
676 
688  public: template<typename RequestT, typename ReplyT>
689  bool Request(
690  const std::string &_topic,
691  const RequestT &_request,
692  void(*_callback)(const ReplyT &_reply, const bool _result));
693 
704  public: template<typename ReplyT>
705  bool Request(
706  const std::string &_topic,
707  void(*_callback)(const ReplyT &_reply, const bool _result));
708 
720  public: template<typename RequestT, typename ReplyT>
721  bool Request(
722  const std::string &_topic,
723  const RequestT &_request,
724  std::function<void(const ReplyT &_reply,
725  const bool _result)> &_callback);
726 
737  public: template<typename ReplyT>
738  bool Request(
739  const std::string &_topic,
740  std::function<void(const ReplyT &_reply,
741  const bool _result)> &_callback);
742 
755  public: template<typename ClassT, typename RequestT, typename ReplyT>
756  bool Request(
757  const std::string &_topic,
758  const RequestT &_request,
759  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
760  ClassT *_obj);
761 
773  public: template<typename ClassT, typename ReplyT>
774  bool Request(
775  const std::string &_topic,
776  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
777  ClassT *_obj);
778 
788  public: template<typename RequestT, typename ReplyT>
789  bool Request(
790  const std::string &_topic,
791  const RequestT &_request,
792  const unsigned int &_timeout,
793  ReplyT &_reply,
794  bool &_result);
795 
804  public: template<typename ReplyT>
805  bool Request(
806  const std::string &_topic,
807  const unsigned int &_timeout,
808  ReplyT &_reply,
809  bool &_result);
810 
816  public: template<typename RequestT>
817  bool Request(const std::string &_topic, const RequestT &_request);
818 
833  public: bool RequestRaw(const std::string &_topic,
834  const std::string &_request, const std::string &_requestType,
835  const std::string &_responseType, unsigned int _timeout,
836  std::string &_response,
837  bool &_result);
838 
842  public: bool UnadvertiseSrv(const std::string &_topic);
843 
850  public: void TopicList(std::vector<std::string> &_topics) const;
851 
856  public: bool GZ_DEPRECATED(13) TopicInfo(const std::string &_topic,
857  std::vector<MessagePublisher> &_publishers) const;
858 
864  public: bool TopicInfo(const std::string &_topic,
865  std::vector<MessagePublisher> &_publishers,
866  std::vector<MessagePublisher> &_subscribers) const;
867 
871  public: bool AllTopicInfo(
872  std::vector<gz::transport::TopicInfo> &_topics) const;
873 
880  public: void ServiceList(std::vector<std::string> &_services) const;
881 
886  public: bool ServiceInfo(
887  const std::string &_service,
888  std::vector<ServicePublisher> &_publishers) const;
889 
891  public: enum class ServiceTypeResolution
892  {
894  kResolved = 0,
895 
897  kInvalidService,
898 
901  kNoProviders,
902 
905  kAmbiguousTypes,
906 
909  kIncompatibleTypes,
910  };
911 
935  const std::string &_service,
936  std::string &_reqType,
937  std::string &_repType,
938  unsigned int _timeoutMs = 0) const;
939 
953  public: bool SubscribeRaw(
954  const std::string &_topic,
955  const RawCallback &_callback,
956  const std::string &_msgType = kGenericMessageType,
957  const SubscribeOptions &_opts = SubscribeOptions());
958 
961  public: const NodeOptions &Options() const;
962 
969  public: bool EnableStats(const std::string &_topic, bool _enable,
970  const std::string &_publicationTopic = "/statistics",
971  uint64_t _publicationRate = 1);
972 
979  public: std::optional<TopicStatistics> TopicStats(
980  const std::string &_topic) const;
981 
988  public: void AddGlobalRelay(const std::string& _relayAddress);
989 
994 
998  private: NodeShared *Shared() const;
999 
1002  private: const std::string &NodeUuid() const;
1003 
1006  private: std::unordered_set<std::string> &TopicsSubscribed() const;
1007 
1010  private: std::unordered_set<std::string> &SrvsAdvertised() const;
1011 
1015  private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
1016 
1022  private: bool TopicInfoHelper(const std::string &_topic,
1023  std::vector<MessagePublisher> &_publishers,
1024  std::vector<MessagePublisher> &_subscribers) const;
1025 
1035  private: template<typename MessageT>
1037  const std::string &_topic,
1038  void(*_callback)(const MessageT &_msg),
1039  const SubscribeOptions &_opts = SubscribeOptions());
1040 
1049  private: template<typename MessageT>
1051  const std::string &_topic,
1052  std::function<void(const MessageT &_msg)> _callback,
1053  const SubscribeOptions &_opts = SubscribeOptions());
1054 
1065  private: template<typename ClassT, typename MessageT>
1067  const std::string &_topic,
1068  void(ClassT::*_callback)(const MessageT &_msg),
1069  ClassT *_obj,
1070  const SubscribeOptions &_opts = SubscribeOptions());
1071 
1082  private: template<typename MessageT>
1084  const std::string &_topic,
1085  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
1086  const SubscribeOptions &_opts = SubscribeOptions());
1087 
1097  private: template<typename MessageT>
1099  const std::string &_topic,
1100  std::function<void(const MessageT &_msg,
1101  const MessageInfo &_info)> _callback,
1102  const SubscribeOptions &_opts = SubscribeOptions());
1103 
1115  private: template<typename ClassT, typename MessageT>
1117  const std::string &_topic,
1118  void(ClassT::*_callback)(const MessageT &_msg,
1119  const MessageInfo &_info),
1120  ClassT *_obj,
1121  const SubscribeOptions &_opts = SubscribeOptions());
1122 
1123 #ifdef _WIN32
1124 // Disable warning C4251 which is triggered by
1125 // std::unique_ptr
1126 #pragma warning(push)
1127 #pragma warning(disable: 4251)
1128 #endif
1131  private: std::unique_ptr<transport::NodePrivate> dataPtr;
1132 #ifdef _WIN32
1133 #pragma warning(pop)
1134 #endif
1135  };
1136  }
1137 }
1138 
1139 #include "gz/transport/detail/Node.hh"
1140 
1141 #endif