Gazebo Transport

API Reference

14.1.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
43namespace 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>
327 const std::string &_topic,
328 void(*_callback)(const MessageT &_msg),
329 const SubscribeOptions &_opts = SubscribeOptions());
330
339 public: template<typename MessageT>
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>
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>
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>
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>
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
460 public: template<typename RequestT, typename ReplyT>
462 const std::string &_topic,
463 bool(*_callback)(const RequestT &_request, ReplyT &_reply),
465
477 public: template<typename ReplyT>
479 const std::string &_topic,
480 bool(*_callback)(ReplyT &_reply),
482
493 public: template<typename RequestT>
495 const std::string &_topic,
496 void(*_callback)(const RequestT &_request),
498
511 public: template<typename RequestT, typename ReplyT>
513 const std::string &_topic,
514 std::function<bool(const RequestT &_request,
515 ReplyT &_reply)> _callback,
517
529 public: template<typename ReplyT>
531 const std::string &_topic,
532 std::function<bool(ReplyT &_reply)> &_callback,
534
545 public: template<typename RequestT>
547 const std::string &_topic,
548 std::function<void(const RequestT &_request)> &_callback,
550
564 public: template<typename ClassT, typename RequestT, typename ReplyT>
566 const std::string &_topic,
567 bool(ClassT::*_callback)(const RequestT &_request, ReplyT &_reply),
568 ClassT *_obj,
570
583 public: template<typename ClassT, typename ReplyT>
585 const std::string &_topic,
586 bool(ClassT::*_callback)(ReplyT &_reply),
587 ClassT *_obj,
589
601 public: template<typename ClassT, typename RequestT>
603 const std::string &_topic,
604 void(ClassT::*_callback)(const RequestT &_request),
605 ClassT *_obj,
607
611
623 public: template<typename RequestT, typename ReplyT>
625 const std::string &_topic,
626 const RequestT &_request,
627 void(*_callback)(const ReplyT &_reply, const bool _result));
628
639 public: template<typename ReplyT>
641 const std::string &_topic,
642 void(*_callback)(const ReplyT &_reply, const bool _result));
643
655 public: template<typename RequestT, typename ReplyT>
657 const std::string &_topic,
658 const RequestT &_request,
659 std::function<void(const ReplyT &_reply,
660 const bool _result)> &_callback);
661
672 public: template<typename ReplyT>
674 const std::string &_topic,
675 std::function<void(const ReplyT &_reply,
676 const bool _result)> &_callback);
677
690 public: template<typename ClassT, typename RequestT, typename ReplyT>
692 const std::string &_topic,
693 const RequestT &_request,
694 void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
695 ClassT *_obj);
696
708 public: template<typename ClassT, typename ReplyT>
710 const std::string &_topic,
711 void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
712 ClassT *_obj);
713
723 public: template<typename RequestT, typename ReplyT>
725 const std::string &_topic,
726 const RequestT &_request,
727 const unsigned int &_timeout,
728 ReplyT &_reply,
729 bool &_result);
730
739 public: template<typename ReplyT>
741 const std::string &_topic,
742 const unsigned int &_timeout,
743 ReplyT &_reply,
744 bool &_result);
745
751 public: template<typename RequestT>
752 bool Request(const std::string &_topic, const RequestT &_request);
753
768 public: bool RequestRaw(const std::string &_topic,
769 const std::string &_request, const std::string &_requestType,
770 const std::string &_responseType, unsigned int _timeout,
771 std::string &_response,
772 bool &_result);
773
777 public: bool UnadvertiseSrv(const std::string &_topic);
778
785 public: void TopicList(std::vector<std::string> &_topics) const;
786
791 public: bool GZ_DEPRECATED(13) TopicInfo(const std::string &_topic,
792 std::vector<MessagePublisher> &_publishers) const;
793
799 public: bool TopicInfo(const std::string &_topic,
800 std::vector<MessagePublisher> &_publishers,
801 std::vector<MessagePublisher> &_subscribers) const;
802
809 public: void ServiceList(std::vector<std::string> &_services) const;
810
815 public: bool ServiceInfo(
816 const std::string &_service,
817 std::vector<ServicePublisher> &_publishers) const;
818
832 public: bool SubscribeRaw(
833 const std::string &_topic,
834 const RawCallback &_callback,
835 const std::string &_msgType = kGenericMessageType,
836 const SubscribeOptions &_opts = SubscribeOptions());
837
840 public: const NodeOptions &Options() const;
841
848 public: bool EnableStats(const std::string &_topic, bool _enable,
849 const std::string &_publicationTopic = "/statistics",
850 uint64_t _publicationRate = 1);
851
858 public: std::optional<TopicStatistics> TopicStats(
859 const std::string &_topic) const;
860
867 public: void AddGlobalRelay(const std::string& _relayAddress);
868
872 public: std::vector<std::string> GlobalRelays() const;
873
877 private: NodeShared *Shared() const;
878
881 private: const std::string &NodeUuid() const;
882
885 private: std::unordered_set<std::string> &TopicsSubscribed() const;
886
889 private: std::unordered_set<std::string> &SrvsAdvertised() const;
890
894 private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
895
905 private: template<typename MessageT>
906 std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
907 const std::string &_topic,
908 void(*_callback)(const MessageT &_msg),
909 const SubscribeOptions &_opts = SubscribeOptions());
910
919 private: template<typename MessageT>
920 std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
921 const std::string &_topic,
922 std::function<void(const MessageT &_msg)> _callback,
923 const SubscribeOptions &_opts = SubscribeOptions());
924
935 private: template<typename ClassT, typename MessageT>
936 std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
937 const std::string &_topic,
938 void(ClassT::*_callback)(const MessageT &_msg),
939 ClassT *_obj,
940 const SubscribeOptions &_opts = SubscribeOptions());
941
952 private: template<typename MessageT>
953 std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
954 const std::string &_topic,
955 void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
956 const SubscribeOptions &_opts = SubscribeOptions());
957
967 private: template<typename MessageT>
968 std::shared_ptr<SubscriptionHandler<MessageT>> SubscribeImpl(
969 const std::string &_topic,
970 std::function<void(const MessageT &_msg,
971 const MessageInfo &_info)> _callback,
972 const SubscribeOptions &_opts = SubscribeOptions());
973
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 const MessageInfo &_info),
990 ClassT *_obj,
991 const SubscribeOptions &_opts = SubscribeOptions());
992
993#ifdef _WIN32
994// Disable warning C4251 which is triggered by
995// std::unique_ptr
996#pragma warning(push)
997#pragma warning(disable: 4251)
998#endif
1002#ifdef _WIN32
1003#pragma warning(pop)
1004#endif
1005 };
1006 }
1007}
1008
1009#include "gz/transport/detail/Node.hh"
1010
1011#endif