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"
42 
43 namespace gz::transport
44 {
45  // Inline bracket to help doxygen filtering.
46  inline namespace GZ_TRANSPORT_VERSION_NAMESPACE {
47  //
48  // Forward declarations.
49  class NodePrivate;
50 
61  int GZ_TRANSPORT_VISIBLE rcvHwm();
62 
73  int GZ_TRANSPORT_VISIBLE sndHwm();
74 
78  void GZ_TRANSPORT_VISIBLE waitForShutdown();
79 
84  class GZ_TRANSPORT_VISIBLE Node
85  {
86  class PublisherPrivate;
87  class SubscriberPrivate;
88 
105  public: class GZ_TRANSPORT_VISIBLE Publisher
106  {
108  public: Publisher();
109 
112  public: explicit Publisher(const MessagePublisher &_publisher);
113 
115  public: virtual ~Publisher();
116 
120  public: operator bool();
121 
125  public: operator bool() const;
126 
130  public: bool Valid() const;
131 
137  public: bool Publish(const ProtoMsg &_msg);
138 
160  public: bool PublishRaw(
161  const std::string &_msgData,
162  const std::string &_msgType);
163 
172  public: bool ThrottledUpdateReady() const;
173 
177  private: bool UpdateThrottling();
178 
181  public: bool HasConnections() const;
182 
188 #ifdef _WIN32
189 // Disable warning C4251 which is triggered by
190 // std::shared_ptr
191 #pragma warning(push)
192 #pragma warning(disable: 4251)
193 #endif
194  private: std::shared_ptr<PublisherPrivate> dataPtr;
195 #ifdef _WIN32
196 #pragma warning(pop)
197 #endif
198  };
199 
213  public: class GZ_TRANSPORT_VISIBLE Subscriber
214  {
216  public: Subscriber();
217 
223  public: Subscriber(const std::string &_topic,
224  const std::string &_nUuid,
225  const NodeOptions &_nOpts,
226  const std::string &_hUuid);
227 
230  public: virtual ~Subscriber();
231 
234  public: bool Unsubscribe();
235 
239  public: operator bool();
240 
244  public: operator bool() const;
245 
248  public: Subscriber(Subscriber &&_other);
249 
254 
258  public: bool Valid() const;
259 
262 #ifdef _WIN32
263 // Disable warning C4251 which is triggered by
264 // std::shared_ptr
265 #pragma warning(push)
266 #pragma warning(disable: 4251)
267 #endif
268  private: std::shared_ptr<SubscriberPrivate> dataPtr;
269 #ifdef _WIN32
270 #pragma warning(pop)
271 #endif
272  };
273 
274  public: Node();
275 
278  public: explicit Node(const NodeOptions &_options);
279 
281  public: virtual ~Node();
282 
291  public: template<typename MessageT>
293  const std::string &_topic,
295 
308  const std::string &_topic,
309  const std::string &_msgTypeName,
311 
315 
325  public: template<typename MessageT>
326  bool Subscribe(
327  const std::string &_topic,
328  void(*_callback)(const MessageT &_msg),
329  const SubscribeOptions &_opts = SubscribeOptions());
330 
339  public: template<typename MessageT>
340  bool Subscribe(
341  const std::string &_topic,
342  std::function<void(const MessageT &_msg)> _callback,
343  const SubscribeOptions &_opts = SubscribeOptions());
344 
355  public: template<typename ClassT, typename MessageT>
356  bool Subscribe(
357  const std::string &_topic,
358  void(ClassT::*_callback)(const MessageT &_msg),
359  ClassT *_obj,
360  const SubscribeOptions &_opts = SubscribeOptions());
361 
372  public: template<typename MessageT>
373  bool Subscribe(
374  const std::string &_topic,
375  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
376  const SubscribeOptions &_opts = SubscribeOptions());
377 
387  public: template<typename MessageT>
388  bool Subscribe(
389  const std::string &_topic,
390  std::function<void(const MessageT &_msg,
391  const MessageInfo &_info)> _callback,
392  const SubscribeOptions &_opts = SubscribeOptions());
393 
405  public: template<typename ClassT, typename MessageT>
406  bool Subscribe(
407  const std::string &_topic,
408  void(ClassT::*_callback)(const MessageT &_msg,
409  const MessageInfo &_info),
410  ClassT *_obj,
411  const SubscribeOptions &_opts = SubscribeOptions());
412 
432  public: template <typename ...Args>
434  Args && ...args);
435 
442 
446  public: bool Unsubscribe(const std::string &_topic);
447 
466  public: template<typename RequestT, typename ReplyT>
467  bool Advertise(
468  const std::string &_topic,
469  bool(*_callback)(const RequestT &_request, ReplyT &_reply),
471 
489  public: template<typename ReplyT>
490  bool Advertise(
491  const std::string &_topic,
492  bool(*_callback)(ReplyT &_reply),
494 
511  public: template<typename RequestT>
512  bool Advertise(
513  const std::string &_topic,
514  void(*_callback)(const RequestT &_request),
516 
535  public: template<typename RequestT, typename ReplyT>
536  bool Advertise(
537  const std::string &_topic,
538  std::function<bool(const RequestT &_request,
539  ReplyT &_reply)> _callback,
541 
559  public: template<typename ReplyT>
560  bool Advertise(
561  const std::string &_topic,
562  std::function<bool(ReplyT &_reply)> &_callback,
564 
581  public: template<typename RequestT>
582  bool Advertise(
583  const std::string &_topic,
584  std::function<void(const RequestT &_request)> &_callback,
586 
606  public: template<typename ClassT, typename RequestT, typename ReplyT>
607  bool Advertise(
608  const std::string &_topic,
609  bool(ClassT::*_callback)(const RequestT &_request, ReplyT &_reply),
610  ClassT *_obj,
612 
631  public: template<typename ClassT, typename ReplyT>
632  bool Advertise(
633  const std::string &_topic,
634  bool(ClassT::*_callback)(ReplyT &_reply),
635  ClassT *_obj,
637 
655  public: template<typename ClassT, typename RequestT>
656  bool Advertise(
657  const std::string &_topic,
658  void(ClassT::*_callback)(const RequestT &_request),
659  ClassT *_obj,
661 
665 
677  public: template<typename RequestT, typename ReplyT>
678  bool Request(
679  const std::string &_topic,
680  const RequestT &_request,
681  void(*_callback)(const ReplyT &_reply, const bool _result));
682 
693  public: template<typename ReplyT>
694  bool Request(
695  const std::string &_topic,
696  void(*_callback)(const ReplyT &_reply, const bool _result));
697 
709  public: template<typename RequestT, typename ReplyT>
710  bool Request(
711  const std::string &_topic,
712  const RequestT &_request,
713  std::function<void(const ReplyT &_reply,
714  const bool _result)> &_callback);
715 
726  public: template<typename ReplyT>
727  bool Request(
728  const std::string &_topic,
729  std::function<void(const ReplyT &_reply,
730  const bool _result)> &_callback);
731 
744  public: template<typename ClassT, typename RequestT, typename ReplyT>
745  bool Request(
746  const std::string &_topic,
747  const RequestT &_request,
748  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
749  ClassT *_obj);
750 
762  public: template<typename ClassT, typename ReplyT>
763  bool Request(
764  const std::string &_topic,
765  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
766  ClassT *_obj);
767 
777  public: template<typename RequestT, typename ReplyT>
778  bool Request(
779  const std::string &_topic,
780  const RequestT &_request,
781  const unsigned int &_timeout,
782  ReplyT &_reply,
783  bool &_result);
784 
793  public: template<typename ReplyT>
794  bool Request(
795  const std::string &_topic,
796  const unsigned int &_timeout,
797  ReplyT &_reply,
798  bool &_result);
799 
805  public: template<typename RequestT>
806  bool Request(const std::string &_topic, const RequestT &_request);
807 
822  public: bool RequestRaw(const std::string &_topic,
823  const std::string &_request, const std::string &_requestType,
824  const std::string &_responseType, unsigned int _timeout,
825  std::string &_response,
826  bool &_result);
827 
831  public: bool UnadvertiseSrv(const std::string &_topic);
832 
839  public: void TopicList(std::vector<std::string> &_topics) const;
840 
845  public: bool GZ_DEPRECATED(13) TopicInfo(const std::string &_topic,
846  std::vector<MessagePublisher> &_publishers) const;
847 
853  public: bool TopicInfo(const std::string &_topic,
854  std::vector<MessagePublisher> &_publishers,
855  std::vector<MessagePublisher> &_subscribers) const;
856 
863  public: void ServiceList(std::vector<std::string> &_services) const;
864 
869  public: bool ServiceInfo(
870  const std::string &_service,
871  std::vector<ServicePublisher> &_publishers) const;
872 
886  public: bool SubscribeRaw(
887  const std::string &_topic,
888  const RawCallback &_callback,
889  const std::string &_msgType = kGenericMessageType,
890  const SubscribeOptions &_opts = SubscribeOptions());
891 
894  public: const NodeOptions &Options() const;
895 
902  public: bool EnableStats(const std::string &_topic, bool _enable,
903  const std::string &_publicationTopic = "/statistics",
904  uint64_t _publicationRate = 1);
905 
912  public: std::optional<TopicStatistics> TopicStats(
913  const std::string &_topic) const;
914 
921  public: void AddGlobalRelay(const std::string& _relayAddress);
922 
926  public: std::vector<std::string> GlobalRelays() const;
927 
931  private: NodeShared *Shared() const;
932 
935  private: const std::string &NodeUuid() const;
936 
939  private: std::unordered_set<std::string> &TopicsSubscribed() const;
940 
943  private: std::unordered_set<std::string> &SrvsAdvertised() const;
944 
948  private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
949 
959  private: template<typename MessageT>
960  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
961  const std::string &_topic,
962  void(*_callback)(const MessageT &_msg),
963  const SubscribeOptions &_opts = SubscribeOptions());
964 
973  private: template<typename MessageT>
974  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
975  const std::string &_topic,
976  std::function<void(const MessageT &_msg)> _callback,
977  const SubscribeOptions &_opts = SubscribeOptions());
978 
989  private: template<typename ClassT, typename MessageT>
990  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
991  const std::string &_topic,
992  void(ClassT::*_callback)(const MessageT &_msg),
993  ClassT *_obj,
994  const SubscribeOptions &_opts = SubscribeOptions());
995 
1006  private: template<typename MessageT>
1007  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
1008  const std::string &_topic,
1009  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
1010  const SubscribeOptions &_opts = SubscribeOptions());
1011 
1021  private: template<typename MessageT>
1022  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
1023  const std::string &_topic,
1024  std::function<void(const MessageT &_msg,
1025  const MessageInfo &_info)> _callback,
1026  const SubscribeOptions &_opts = SubscribeOptions());
1027 
1039  private: template<typename ClassT, typename MessageT>
1040  std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
1041  const std::string &_topic,
1042  void(ClassT::*_callback)(const MessageT &_msg,
1043  const MessageInfo &_info),
1044  ClassT *_obj,
1045  const SubscribeOptions &_opts = SubscribeOptions());
1046 
1047 #ifdef _WIN32
1048 // Disable warning C4251 which is triggered by
1049 // std::unique_ptr
1050 #pragma warning(push)
1051 #pragma warning(disable: 4251)
1052 #endif
1055  private: std::unique_ptr<transport::NodePrivate> dataPtr;
1056 #ifdef _WIN32
1057 #pragma warning(pop)
1058 #endif
1059  };
1060  }
1061 }
1062 
1063 #include "gz/transport/detail/Node.hh"
1064 
1065 #endif