Gazebo Transport

API Reference

13.5.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  class GZ_TRANSPORT_VISIBLE Node
81  {
82  class PublisherPrivate;
83  class SubscriberPrivate;
84 
101  public: class GZ_TRANSPORT_VISIBLE Publisher
102  {
104  public: Publisher();
105 
108  public: explicit Publisher(const MessagePublisher &_publisher);
109 
111  public: virtual ~Publisher();
112 
116  public: operator bool();
117 
121  public: operator bool() const;
122 
126  public: bool Valid() const;
127 
133  public: bool Publish(const ProtoMsg &_msg);
134 
156  public: bool PublishRaw(
157  const std::string &_msgData,
158  const std::string &_msgType);
159 
168  public: bool ThrottledUpdateReady() const;
169 
173  private: bool UpdateThrottling();
174 
177  public: bool HasConnections() const;
178 
184 #ifdef _WIN32
185 // Disable warning C4251 which is triggered by
186 // std::shared_ptr
187 #pragma warning(push)
188 #pragma warning(disable: 4251)
189 #endif
190  private: std::shared_ptr<PublisherPrivate> dataPtr;
191 #ifdef _WIN32
192 #pragma warning(pop)
193 #endif
194  };
195 
209  public: class GZ_TRANSPORT_VISIBLE Subscriber
210  {
212  public: Subscriber();
213 
219  public: Subscriber(const std::string &_topic,
220  const std::string &_nUuid,
221  const NodeOptions &_nOpts,
222  const std::string &_hUuid);
223 
226  public: virtual ~Subscriber();
227 
230  public: bool Unsubscribe();
231 
235  public: operator bool();
236 
240  public: operator bool() const;
241 
244  public: Subscriber(Subscriber &&_other);
245 
250 
254  public: bool Valid() const;
255 
258 #ifdef _WIN32
259 // Disable warning C4251 which is triggered by
260 // std::shared_ptr
261 #pragma warning(push)
262 #pragma warning(disable: 4251)
263 #endif
264  private: std::shared_ptr<SubscriberPrivate> dataPtr;
265 #ifdef _WIN32
266 #pragma warning(pop)
267 #endif
268  };
269 
270  public: Node();
271 
274  public: explicit Node(const NodeOptions &_options);
275 
277  public: virtual ~Node();
278 
287  public: template<typename MessageT>
289  const std::string &_topic,
291 
304  const std::string &_topic,
305  const std::string &_msgTypeName,
307 
311 
321  public: template<typename MessageT>
322  bool Subscribe(
323  const std::string &_topic,
324  void(*_callback)(const MessageT &_msg),
325  const SubscribeOptions &_opts = SubscribeOptions());
326 
335  public: template<typename MessageT>
336  bool Subscribe(
337  const std::string &_topic,
338  std::function<void(const MessageT &_msg)> _callback,
339  const SubscribeOptions &_opts = SubscribeOptions());
340 
351  public: template<typename ClassT, typename MessageT>
352  bool Subscribe(
353  const std::string &_topic,
354  void(ClassT::*_callback)(const MessageT &_msg),
355  ClassT *_obj,
356  const SubscribeOptions &_opts = SubscribeOptions());
357 
368  public: template<typename MessageT>
369  bool Subscribe(
370  const std::string &_topic,
371  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
372  const SubscribeOptions &_opts = SubscribeOptions());
373 
383  public: template<typename MessageT>
384  bool Subscribe(
385  const std::string &_topic,
386  std::function<void(const MessageT &_msg,
387  const MessageInfo &_info)> _callback,
388  const SubscribeOptions &_opts = SubscribeOptions());
389 
401  public: template<typename ClassT, typename MessageT>
402  bool Subscribe(
403  const std::string &_topic,
404  void(ClassT::*_callback)(const MessageT &_msg,
405  const MessageInfo &_info),
406  ClassT *_obj,
407  const SubscribeOptions &_opts = SubscribeOptions());
408 
428  public: template <typename ...Args>
430  Args && ...args);
431 
438 
442  public: bool Unsubscribe(const std::string &_topic);
443 
462  public: template<typename RequestT, typename ReplyT>
463  bool Advertise(
464  const std::string &_topic,
465  bool(*_callback)(const RequestT &_request, ReplyT &_reply),
467 
485  public: template<typename ReplyT>
486  bool Advertise(
487  const std::string &_topic,
488  bool(*_callback)(ReplyT &_reply),
490 
507  public: template<typename RequestT>
508  bool Advertise(
509  const std::string &_topic,
510  void(*_callback)(const RequestT &_request),
512 
531  public: template<typename RequestT, typename ReplyT>
532  bool Advertise(
533  const std::string &_topic,
534  std::function<bool(const RequestT &_request,
535  ReplyT &_reply)> _callback,
537 
555  public: template<typename ReplyT>
556  bool Advertise(
557  const std::string &_topic,
558  std::function<bool(ReplyT &_reply)> &_callback,
560 
577  public: template<typename RequestT>
578  bool Advertise(
579  const std::string &_topic,
580  std::function<void(const RequestT &_request)> &_callback,
582 
602  public: template<typename ClassT, typename RequestT, typename ReplyT>
603  bool Advertise(
604  const std::string &_topic,
605  bool(ClassT::*_callback)(const RequestT &_request, ReplyT &_reply),
606  ClassT *_obj,
608 
627  public: template<typename ClassT, typename ReplyT>
628  bool Advertise(
629  const std::string &_topic,
630  bool(ClassT::*_callback)(ReplyT &_reply),
631  ClassT *_obj,
633 
651  public: template<typename ClassT, typename RequestT>
652  bool Advertise(
653  const std::string &_topic,
654  void(ClassT::*_callback)(const RequestT &_request),
655  ClassT *_obj,
657 
661 
673  public: template<typename RequestT, typename ReplyT>
674  bool Request(
675  const std::string &_topic,
676  const RequestT &_request,
677  void(*_callback)(const ReplyT &_reply, const bool _result));
678 
689  public: template<typename ReplyT>
690  bool Request(
691  const std::string &_topic,
692  void(*_callback)(const ReplyT &_reply, const bool _result));
693 
705  public: template<typename RequestT, typename ReplyT>
706  bool Request(
707  const std::string &_topic,
708  const RequestT &_request,
709  std::function<void(const ReplyT &_reply,
710  const bool _result)> &_callback);
711 
722  public: template<typename ReplyT>
723  bool Request(
724  const std::string &_topic,
725  std::function<void(const ReplyT &_reply,
726  const bool _result)> &_callback);
727 
740  public: template<typename ClassT, typename RequestT, typename ReplyT>
741  bool Request(
742  const std::string &_topic,
743  const RequestT &_request,
744  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
745  ClassT *_obj);
746 
758  public: template<typename ClassT, typename ReplyT>
759  bool Request(
760  const std::string &_topic,
761  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
762  ClassT *_obj);
763 
773  public: template<typename RequestT, typename ReplyT>
774  bool Request(
775  const std::string &_topic,
776  const RequestT &_request,
777  const unsigned int &_timeout,
778  ReplyT &_reply,
779  bool &_result);
780 
789  public: template<typename ReplyT>
790  bool Request(
791  const std::string &_topic,
792  const unsigned int &_timeout,
793  ReplyT &_reply,
794  bool &_result);
795 
801  public: template<typename RequestT>
802  bool Request(const std::string &_topic, const RequestT &_request);
803 
818  public: bool RequestRaw(const std::string &_topic,
819  const std::string &_request, const std::string &_requestType,
820  const std::string &_responseType, unsigned int _timeout,
821  std::string &_response,
822  bool &_result);
823 
827  public: bool UnadvertiseSrv(const std::string &_topic);
828 
835  public: void TopicList(std::vector<std::string> &_topics) const;
836 
841  public: bool GZ_DEPRECATED(13) TopicInfo(const std::string &_topic,
842  std::vector<MessagePublisher> &_publishers) const;
843 
849  public: bool TopicInfo(const std::string &_topic,
850  std::vector<MessagePublisher> &_publishers,
851  std::vector<MessagePublisher> &_subscribers) const;
852 
859  public: void ServiceList(std::vector<std::string> &_services) const;
860 
865  public: bool ServiceInfo(
866  const std::string &_service,
867  std::vector<ServicePublisher> &_publishers) const;
868 
882  public: bool SubscribeRaw(
883  const std::string &_topic,
884  const RawCallback &_callback,
885  const std::string &_msgType = kGenericMessageType,
886  const SubscribeOptions &_opts = SubscribeOptions());
887 
890  public: const NodeOptions &Options() const;
891 
898  public: bool EnableStats(const std::string &_topic, bool _enable,
899  const std::string &_publicationTopic = "/statistics",
900  uint64_t _publicationRate = 1);
901 
908  public: std::optional<TopicStatistics> TopicStats(
909  const std::string &_topic) const;
910 
917  public: void AddGlobalRelay(const std::string& _relayAddress);
918 
922  public: std::vector<std::string> GlobalRelays() const;
923 
927  private: NodeShared *Shared() const;
928 
931  private: const std::string &NodeUuid() const;
932 
935  private: std::unordered_set<std::string> &TopicsSubscribed() const;
936 
939  private: std::unordered_set<std::string> &SrvsAdvertised() const;
940 
944  private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
945 
955  private: template<typename MessageT>
956  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
957  const std::string &_topic,
958  void(*_callback)(const MessageT &_msg),
959  const SubscribeOptions &_opts = SubscribeOptions());
960 
969  private: template<typename MessageT>
970  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
971  const std::string &_topic,
972  std::function<void(const MessageT &_msg)> _callback,
973  const SubscribeOptions &_opts = SubscribeOptions());
974 
985  private: template<typename ClassT, typename MessageT>
986  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
987  const std::string &_topic,
988  void(ClassT::*_callback)(const MessageT &_msg),
989  ClassT *_obj,
990  const SubscribeOptions &_opts = SubscribeOptions());
991 
1002  private: template<typename MessageT>
1003  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
1004  const std::string &_topic,
1005  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
1006  const SubscribeOptions &_opts = SubscribeOptions());
1007 
1017  private: template<typename MessageT>
1018  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
1019  const std::string &_topic,
1020  std::function<void(const MessageT &_msg,
1021  const MessageInfo &_info)> _callback,
1022  const SubscribeOptions &_opts = SubscribeOptions());
1023 
1035  private: template<typename ClassT, typename MessageT>
1036  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
1037  const std::string &_topic,
1038  void(ClassT::*_callback)(const MessageT &_msg,
1039  const MessageInfo &_info),
1040  ClassT *_obj,
1041  const SubscribeOptions &_opts = SubscribeOptions());
1042 
1043 #ifdef _WIN32
1044 // Disable warning C4251 which is triggered by
1045 // std::unique_ptr
1046 #pragma warning(push)
1047 #pragma warning(disable: 4251)
1048 #endif
1051  private: std::unique_ptr<transport::NodePrivate> dataPtr;
1052 #ifdef _WIN32
1053 #pragma warning(pop)
1054 #endif
1055  };
1056  }
1057 }
1058 
1059 #include "gz/transport/detail/Node.hh"
1060 
1061 #endif