Gazebo Transport

API Reference

11.4.2
gz/transport/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 
29 // TODO(anyone): Remove after fixing the warnings
30 #ifdef _MSC_VER
31 #pragma warning(push, 0)
32 #endif
33 #include <ignition/msgs.hh>
34 #ifdef _MSC_VER
35 #pragma warning(pop)
36 #endif
37 
39 #include "gz/transport/config.hh"
40 #include "gz/transport/Export.hh"
51 
52 namespace ignition::transport
53 {
54  // Inline bracket to help doxygen filtering.
55  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
56  //
57  // Forward declarations.
58  class NodePrivate;
59 
70  int IGNITION_TRANSPORT_VISIBLE rcvHwm();
71 
82  int IGNITION_TRANSPORT_VISIBLE sndHwm();
83 
87  void IGNITION_TRANSPORT_VISIBLE waitForShutdown();
88 
93  class IGNITION_TRANSPORT_VISIBLE Node
94  {
95  class PublisherPrivate;
96 
113  public: class IGNITION_TRANSPORT_VISIBLE Publisher
114  {
116  public: Publisher();
117 
120  public: explicit Publisher(const MessagePublisher &_publisher);
121 
123  public: virtual ~Publisher();
124 
128  public: operator bool();
129 
133  public: operator bool() const;
134 
138  public: bool Valid() const;
139 
145  public: bool Publish(const ProtoMsg &_msg);
146 
168  public: bool PublishRaw(
169  const std::string &_msgData,
170  const std::string &_msgType);
171 
180  public: bool ThrottledUpdateReady() const;
181 
185  private: bool UpdateThrottling();
186 
189  public: bool HasConnections() const;
190 
196 #ifdef _WIN32
197 // Disable warning C4251 which is triggered by
198 // std::shared_ptr
199 #pragma warning(push)
200 #pragma warning(disable: 4251)
201 #endif
202  private: std::shared_ptr<PublisherPrivate> dataPtr;
203 #ifdef _WIN32
204 #pragma warning(pop)
205 #endif
206  };
207 
210  public: explicit Node(const NodeOptions &_options = NodeOptions());
211 
213  public: virtual ~Node();
214 
223  public: template<typename MessageT>
224  Node::Publisher Advertise(
225  const std::string &_topic,
227 
239  public: Node::Publisher Advertise(
240  const std::string &_topic,
241  const std::string &_msgTypeName,
243 
246  public: std::vector<std::string> AdvertisedTopics() const;
247 
257  public: template<typename MessageT>
258  bool Subscribe(
259  const std::string &_topic,
260  void(*_callback)(const MessageT &_msg),
261  const SubscribeOptions &_opts = SubscribeOptions());
262 
271  public: template<typename MessageT>
272  bool Subscribe(
273  const std::string &_topic,
274  std::function<void(const MessageT &_msg)> _callback,
275  const SubscribeOptions &_opts = SubscribeOptions());
276 
287  public: template<typename ClassT, typename MessageT>
288  bool Subscribe(
289  const std::string &_topic,
290  void(ClassT::*_callback)(const MessageT &_msg),
291  ClassT *_obj,
292  const SubscribeOptions &_opts = SubscribeOptions());
293 
304  public: template<typename MessageT>
305  bool Subscribe(
306  const std::string &_topic,
307  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
308  const SubscribeOptions &_opts = SubscribeOptions());
309 
319  public: template<typename MessageT>
320  bool Subscribe(
321  const std::string &_topic,
322  std::function<void(const MessageT &_msg,
323  const MessageInfo &_info)> _callback,
324  const SubscribeOptions &_opts = SubscribeOptions());
325 
337  public: template<typename ClassT, typename MessageT>
338  bool Subscribe(
339  const std::string &_topic,
340  void(ClassT::*_callback)(const MessageT &_msg,
341  const MessageInfo &_info),
342  ClassT *_obj,
343  const SubscribeOptions &_opts = SubscribeOptions());
344 
350  public: std::vector<std::string> SubscribedTopics() const;
351 
355  public: bool Unsubscribe(const std::string &_topic);
356 
375  public: template<typename RequestT, typename ReplyT>
376  bool Advertise(
377  const std::string &_topic,
378  bool(*_callback)(const RequestT &_request, ReplyT &_reply),
380 
398  public: template<typename ReplyT>
399  bool Advertise(
400  const std::string &_topic,
401  bool(*_callback)(ReplyT &_reply),
403 
420  public: template<typename RequestT>
421  bool Advertise(
422  const std::string &_topic,
423  void(*_callback)(const RequestT &_request),
425 
444  public: template<typename RequestT, typename ReplyT>
445  bool Advertise(
446  const std::string &_topic,
447  std::function<bool(const RequestT &_request,
448  ReplyT &_reply)> _callback,
450 
468  public: template<typename ReplyT>
469  bool Advertise(
470  const std::string &_topic,
471  std::function<bool(ReplyT &_reply)> &_callback,
473 
490  public: template<typename RequestT>
491  bool Advertise(
492  const std::string &_topic,
493  std::function<void(const RequestT &_request)> &_callback,
495 
515  public: template<typename ClassT, typename RequestT, typename ReplyT>
516  bool Advertise(
517  const std::string &_topic,
518  bool(ClassT::*_callback)(const RequestT &_request, ReplyT &_reply),
519  ClassT *_obj,
521 
540  public: template<typename ClassT, typename ReplyT>
541  bool Advertise(
542  const std::string &_topic,
543  bool(ClassT::*_callback)(ReplyT &_reply),
544  ClassT *_obj,
546 
564  public: template<typename ClassT, typename RequestT>
565  bool Advertise(
566  const std::string &_topic,
567  void(ClassT::*_callback)(const RequestT &_request),
568  ClassT *_obj,
570 
573  public: std::vector<std::string> AdvertisedServices() const;
574 
586  public: template<typename RequestT, typename ReplyT>
587  bool Request(
588  const std::string &_topic,
589  const RequestT &_request,
590  void(*_callback)(const ReplyT &_reply, const bool _result));
591 
602  public: template<typename ReplyT>
603  bool Request(
604  const std::string &_topic,
605  void(*_callback)(const ReplyT &_reply, const bool _result));
606 
618  public: template<typename RequestT, typename ReplyT>
619  bool Request(
620  const std::string &_topic,
621  const RequestT &_request,
622  std::function<void(const ReplyT &_reply,
623  const bool _result)> &_callback);
624 
635  public: template<typename ReplyT>
636  bool Request(
637  const std::string &_topic,
638  std::function<void(const ReplyT &_reply,
639  const bool _result)> &_callback);
640 
653  public: template<typename ClassT, typename RequestT, typename ReplyT>
654  bool Request(
655  const std::string &_topic,
656  const RequestT &_request,
657  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
658  ClassT *_obj);
659 
671  public: template<typename ClassT, typename ReplyT>
672  bool Request(
673  const std::string &_topic,
674  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
675  ClassT *_obj);
676 
686  public: template<typename RequestT, typename ReplyT>
687  bool Request(
688  const std::string &_topic,
689  const RequestT &_request,
690  const unsigned int &_timeout,
691  ReplyT &_reply,
692  bool &_result);
693 
702  public: template<typename ReplyT>
703  bool Request(
704  const std::string &_topic,
705  const unsigned int &_timeout,
706  ReplyT &_reply,
707  bool &_result);
708 
714  public: template<typename RequestT>
715  bool Request(const std::string &_topic, const RequestT &_request);
716 
731  public: bool RequestRaw(const std::string &_topic,
732  const std::string &_request, const std::string &_requestType,
733  const std::string &_responseType, unsigned int _timeout,
734  std::string &_response,
735  bool &_result);
736 
740  public: bool UnadvertiseSrv(const std::string &_topic);
741 
748  public: void TopicList(std::vector<std::string> &_topics) const;
749 
754  public: bool TopicInfo(const std::string &_topic,
755  std::vector<MessagePublisher> &_publishers) const;
756 
763  public: void ServiceList(std::vector<std::string> &_services) const;
764 
769  public: bool ServiceInfo(
770  const std::string &_service,
771  std::vector<ServicePublisher> &_publishers) const;
772 
786  public: bool SubscribeRaw(
787  const std::string &_topic,
788  const RawCallback &_callback,
789  const std::string &_msgType = kGenericMessageType,
790  const SubscribeOptions &_opts = SubscribeOptions());
791 
794  public: const NodeOptions &Options() const;
795 
802  public: bool EnableStats(const std::string &_topic, bool _enable,
803  const std::string &_publicationTopic = "/statistics",
804  uint64_t _publicationRate = 1);
805 
812  public: std::optional<TopicStatistics> TopicStats(
813  const std::string &_topic) const;
814 
818  private: NodeShared *Shared() const;
819 
822  private: const std::string &NodeUuid() const;
823 
826  private: std::unordered_set<std::string> &TopicsSubscribed() const;
827 
830  private: std::unordered_set<std::string> &SrvsAdvertised() const;
831 
835  private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
836 
837 #ifdef _WIN32
838 // Disable warning C4251 which is triggered by
839 // std::unique_ptr
840 #pragma warning(push)
841 #pragma warning(disable: 4251)
842 #endif
846 #ifdef _WIN32
847 #pragma warning(pop)
848 #endif
849  };
850  }
851 }
852 
853 #include "gz/transport/detail/Node.hh"
854 
855 #endif
std::optional< TopicStatistics > TopicStats(const std::string &_topic) const
Get the current statistics for a topic. Statistics must have been enabled using the EnableStatistics ...
bool Request(const std::string &_topic, const RequestT &_request, void(*_callback)(const ReplyT &_reply, const bool _result))
Request a new service using a non-blocking call. In this version the callback is a free function.
Node(const NodeOptions &_options=NodeOptions())
Constructor.
void TopicList(std::vector< std::string > &_topics) const
Get the list of topics currently advertised in the network. Note that this function can block for som...
int sndHwm()
Get the capacity of the buffer (High Water Mark) that stores outgoing Ignition Transport messages....
std::vector< std::string > SubscribedTopics() const
Get the list of topics subscribed by this node. Note that we might be interested in one topic but we ...
STL class.
google::protobuf::Message ProtoMsg
Definition: gz/transport/TransportTypes.hh:68
void waitForShutdown()
Block the current thread until a SIGINT or SIGTERM is received. Note that this function registers a s...
std::vector< std::string > AdvertisedServices() const
Get the list of services advertised by this node.
bool Valid() const
Return true if valid information, such as a non-empty topic name, is present.
bool ThrottledUpdateReady() const
Check if message publication is throttled. If so, verify whether the next message should be published...
bool UnadvertiseSrv(const std::string &_topic)
Unadvertise a service.
This class stores all the information about a message publisher.
Definition: gz/transport/Publisher.hh:176
bool PublishRaw(const std::string &_msgData, const std::string &_msgType)
Publish a raw pre-serialized message.
bool TopicInfo(const std::string &_topic, std::vector< MessagePublisher > &_publishers) const
Get the information about a topic.
A class to provide different options for a subscription.
Definition: gz/transport/SubscribeOptions.hh:37
bool RequestRaw(const std::string &_topic, const std::string &_request, const std::string &_requestType, const std::string &_responseType, unsigned int _timeout, std::string &_response, bool &_result)
Request a new service using a blocking call. This request function expects a serialized protobuf mess...
A class for customizing the publication options for a topic advertised. E.g.: Set the rate of message...
Definition: gz/transport/AdvertiseOptions.hh:132
bool Unsubscribe(const std::string &_topic)
Unsubscribe from a topic.
void ServiceList(std::vector< std::string > &_services) const
Get the list of topics currently advertised in the network. Note that this function can block for som...
A class that allows a client to communicate with other peers. There are two main communication modes:...
Definition: gz/transport/Node.hh:93
A class that provides information about the message received.
Definition: gz/transport/MessageInfo.hh:35
int rcvHwm()
Get the capacity of the buffer (High Water Mark) that stores incoming Ignition Transport messages....
A class for customizing the behavior of the Node. E.g.: Set a custom namespace or a partition name.
Definition: gz/transport/NodeOptions.hh:38
bool Subscribe(const std::string &_topic, void(*_callback)(const MessageT &_msg), const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. Note that this callback does not include any message inf...
const NodeOptions & Options() const
Get the reference to the current node options.
bool ServiceInfo(const std::string &_service, std::vector< ServicePublisher > &_publishers) const
Get the information about a service.
bool EnableStats(const std::string &_topic, bool _enable, const std::string &_publicationTopic="/statistics", uint64_t _publicationRate=1)
Turn topic statistics on or off.
bool SubscribeRaw(const std::string &_topic, const RawCallback &_callback, const std::string &_msgType=kGenericMessageType, const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. The callback must accept a std::string to represent the ...
virtual ~Node()
Destructor.
Definition: gz/transport/AdvertiseOptions.hh:28
A class that is used to store information about an advertised publisher. An instance of this class is...
Definition: gz/transport/Node.hh:113
bool HasConnections() const
Return true if this publisher has subscribers.
Node::Publisher Advertise(const std::string &_topic, const AdvertiseMessageOptions &_options=AdvertiseMessageOptions())
Advertise a new topic. If a topic is currently advertised, you cannot advertise it a second time (reg...
const std::string kGenericMessageType
The string type used for generic messages.
Definition: gz/transport/TransportTypes.hh:172
A class for customizing the publication options for a service advertised.
Definition: gz/transport/AdvertiseOptions.hh:214
bool Publish(const ProtoMsg &_msg)
Publish a message. This function will copy the message when publishing to interprocess subscribers....
Private data for the Node class. This class should not be directly used. You should use the Node clas...
Definition: gz/transport/NodeShared.hh:64
std::vector< std::string > AdvertisedTopics() const
Get the list of topics advertised by this node.