Gazebo Transport

API Reference

14.3.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
44namespace 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>
323 const std::string &_topic,
324 void(*_callback)(const MessageT &_msg),
325 const SubscribeOptions &_opts = SubscribeOptions());
326
335 public: template<typename MessageT>
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>
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>
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>
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>
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>
464 const std::string &_topic,
465 bool(*_callback)(const RequestT &_request, ReplyT &_reply),
467
485 public: template<typename ReplyT>
487 const std::string &_topic,
488 bool(*_callback)(ReplyT &_reply),
490
507 public: template<typename RequestT>
509 const std::string &_topic,
510 void(*_callback)(const RequestT &_request),
512
531 public: template<typename RequestT, typename ReplyT>
533 const std::string &_topic,
534 std::function<bool(const RequestT &_request,
535 ReplyT &_reply)> _callback,
537
555 public: template<typename ReplyT>
557 const std::string &_topic,
558 std::function<bool(ReplyT &_reply)> &_callback,
560
577 public: template<typename RequestT>
579 const std::string &_topic,
580 std::function<void(const RequestT &_request)> &_callback,
582
602 public: template<typename ClassT, typename RequestT, typename ReplyT>
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>
629 const std::string &_topic,
630 bool(ClassT::*_callback)(ReplyT &_reply),
631 ClassT *_obj,
633
651 public: template<typename ClassT, typename RequestT>
653 const std::string &_topic,
654 void(ClassT::*_callback)(const RequestT &_request),
655 ClassT *_obj,
657
661
673 public: template<typename RequestT, typename ReplyT>
675 const std::string &_topic,
676 const RequestT &_request,
677 void(*_callback)(const ReplyT &_reply, const bool _result));
678
689 public: template<typename ReplyT>
691 const std::string &_topic,
692 void(*_callback)(const ReplyT &_reply, const bool _result));
693
705 public: template<typename RequestT, typename ReplyT>
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>
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>
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>
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>
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>
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
870 public: enum class ServiceTypeResolution
871 {
873 kResolved = 0,
874
876 kInvalidService,
877
880 kNoProviders,
881
884 kAmbiguousTypes,
885
888 kIncompatibleTypes,
889 };
890
914 const std::string &_service,
915 std::string &_reqType,
916 std::string &_repType,
917 unsigned int _timeoutMs = 0) const;
918
932 public: bool SubscribeRaw(
933 const std::string &_topic,
934 const RawCallback &_callback,
935 const std::string &_msgType = kGenericMessageType,
936 const SubscribeOptions &_opts = SubscribeOptions());
937
940 public: const NodeOptions &Options() const;
941
948 public: bool EnableStats(const std::string &_topic, bool _enable,
949 const std::string &_publicationTopic = "/statistics",
950 uint64_t _publicationRate = 1);
951
958 public: std::optional<TopicStatistics> TopicStats(
959 const std::string &_topic) const;
960
967 public: void AddGlobalRelay(const std::string& _relayAddress);
968
973
977 private: NodeShared *Shared() const;
978
981 private: const std::string &NodeUuid() const;
982
985 private: std::unordered_set<std::string> &TopicsSubscribed() const;
986
989 private: std::unordered_set<std::string> &SrvsAdvertised() const;
990
994 private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
995
1005 private: template<typename MessageT>
1007 const std::string &_topic,
1008 void(*_callback)(const MessageT &_msg),
1009 const SubscribeOptions &_opts = SubscribeOptions());
1010
1019 private: template<typename MessageT>
1021 const std::string &_topic,
1022 std::function<void(const MessageT &_msg)> _callback,
1023 const SubscribeOptions &_opts = SubscribeOptions());
1024
1035 private: template<typename ClassT, typename MessageT>
1037 const std::string &_topic,
1038 void(ClassT::*_callback)(const MessageT &_msg),
1039 ClassT *_obj,
1040 const SubscribeOptions &_opts = SubscribeOptions());
1041
1052 private: template<typename MessageT>
1054 const std::string &_topic,
1055 void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
1056 const SubscribeOptions &_opts = SubscribeOptions());
1057
1067 private: template<typename MessageT>
1069 const std::string &_topic,
1070 std::function<void(const MessageT &_msg,
1071 const MessageInfo &_info)> _callback,
1072 const SubscribeOptions &_opts = SubscribeOptions());
1073
1085 private: template<typename ClassT, typename MessageT>
1087 const std::string &_topic,
1088 void(ClassT::*_callback)(const MessageT &_msg,
1089 const MessageInfo &_info),
1090 ClassT *_obj,
1091 const SubscribeOptions &_opts = SubscribeOptions());
1092
1093#ifdef _WIN32
1094// Disable warning C4251 which is triggered by
1095// std::unique_ptr
1096#pragma warning(push)
1097#pragma warning(disable: 4251)
1098#endif
1102#ifdef _WIN32
1103#pragma warning(pop)
1104#endif
1105 };
1106 }
1107}
1108
1109#include "gz/transport/detail/Node.hh"
1110
1111#endif