Gazebo Transport

API Reference

11.4.1
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
53 {
54  namespace transport
55  {
56  // Inline bracket to help doxygen filtering.
57  inline namespace IGNITION_TRANSPORT_VERSION_NAMESPACE {
58  //
59  // Forward declarations.
60  class NodePrivate;
61 
72  int IGNITION_TRANSPORT_VISIBLE rcvHwm();
73 
84  int IGNITION_TRANSPORT_VISIBLE sndHwm();
85 
89  void IGNITION_TRANSPORT_VISIBLE waitForShutdown();
90 
95  class IGNITION_TRANSPORT_VISIBLE Node
96  {
97  class PublisherPrivate;
98 
115  public: class IGNITION_TRANSPORT_VISIBLE Publisher
116  {
118  public: Publisher();
119 
122  public: explicit Publisher(const MessagePublisher &_publisher);
123 
125  public: virtual ~Publisher();
126 
130  public: operator bool();
131 
135  public: operator bool() const;
136 
140  public: bool Valid() const;
141 
147  public: bool Publish(const ProtoMsg &_msg);
148 
170  public: bool PublishRaw(
171  const std::string &_msgData,
172  const std::string &_msgType);
173 
182  public: bool ThrottledUpdateReady() const;
183 
187  private: bool UpdateThrottling();
188 
191  public: bool HasConnections() const;
192 
198 #ifdef _WIN32
199 // Disable warning C4251 which is triggered by
200 // std::shared_ptr
201 #pragma warning(push)
202 #pragma warning(disable: 4251)
203 #endif
204  private: std::shared_ptr<PublisherPrivate> dataPtr;
205 #ifdef _WIN32
206 #pragma warning(pop)
207 #endif
208  };
209 
212  public: explicit Node(const NodeOptions &_options = NodeOptions());
213 
215  public: virtual ~Node();
216 
225  public: template<typename MessageT>
226  Node::Publisher Advertise(
227  const std::string &_topic,
229 
241  public: Node::Publisher Advertise(
242  const std::string &_topic,
243  const std::string &_msgTypeName,
245 
248  public: std::vector<std::string> AdvertisedTopics() const;
249 
259  public: template<typename MessageT>
260  bool Subscribe(
261  const std::string &_topic,
262  void(*_callback)(const MessageT &_msg),
263  const SubscribeOptions &_opts = SubscribeOptions());
264 
273  public: template<typename MessageT>
274  bool Subscribe(
275  const std::string &_topic,
276  std::function<void(const MessageT &_msg)> _callback,
277  const SubscribeOptions &_opts = SubscribeOptions());
278 
289  public: template<typename ClassT, typename MessageT>
290  bool Subscribe(
291  const std::string &_topic,
292  void(ClassT::*_callback)(const MessageT &_msg),
293  ClassT *_obj,
294  const SubscribeOptions &_opts = SubscribeOptions());
295 
306  public: template<typename MessageT>
307  bool Subscribe(
308  const std::string &_topic,
309  void(*_callback)(const MessageT &_msg, const MessageInfo &_info),
310  const SubscribeOptions &_opts = SubscribeOptions());
311 
321  public: template<typename MessageT>
322  bool Subscribe(
323  const std::string &_topic,
324  std::function<void(const MessageT &_msg,
325  const MessageInfo &_info)> _callback,
326  const SubscribeOptions &_opts = SubscribeOptions());
327 
339  public: template<typename ClassT, typename MessageT>
340  bool Subscribe(
341  const std::string &_topic,
342  void(ClassT::*_callback)(const MessageT &_msg,
343  const MessageInfo &_info),
344  ClassT *_obj,
345  const SubscribeOptions &_opts = SubscribeOptions());
346 
352  public: std::vector<std::string> SubscribedTopics() const;
353 
357  public: bool Unsubscribe(const std::string &_topic);
358 
371  public: template<typename RequestT, typename ReplyT>
372  bool Advertise(
373  const std::string &_topic,
374  bool(*_callback)(const RequestT &_request, ReplyT &_reply),
376 
388  public: template<typename ReplyT>
389  bool Advertise(
390  const std::string &_topic,
391  bool(*_callback)(ReplyT &_reply),
393 
404  public: template<typename RequestT>
405  bool Advertise(
406  const std::string &_topic,
407  void(*_callback)(const RequestT &_request),
409 
422  public: template<typename RequestT, typename ReplyT>
423  bool Advertise(
424  const std::string &_topic,
425  std::function<bool(const RequestT &_request,
426  ReplyT &_reply)> _callback,
428 
440  public: template<typename ReplyT>
441  bool Advertise(
442  const std::string &_topic,
443  std::function<bool(ReplyT &_reply)> &_callback,
445 
456  public: template<typename RequestT>
457  bool Advertise(
458  const std::string &_topic,
459  std::function<void(const RequestT &_request)> &_callback,
461 
475  public: template<typename ClassT, typename RequestT, typename ReplyT>
476  bool Advertise(
477  const std::string &_topic,
478  bool(ClassT::*_callback)(const RequestT &_request, ReplyT &_reply),
479  ClassT *_obj,
481 
494  public: template<typename ClassT, typename ReplyT>
495  bool Advertise(
496  const std::string &_topic,
497  bool(ClassT::*_callback)(ReplyT &_reply),
498  ClassT *_obj,
500 
512  public: template<typename ClassT, typename RequestT>
513  bool Advertise(
514  const std::string &_topic,
515  void(ClassT::*_callback)(const RequestT &_request),
516  ClassT *_obj,
518 
521  public: std::vector<std::string> AdvertisedServices() const;
522 
534  public: template<typename RequestT, typename ReplyT>
535  bool Request(
536  const std::string &_topic,
537  const RequestT &_request,
538  void(*_callback)(const ReplyT &_reply, const bool _result));
539 
550  public: template<typename ReplyT>
551  bool Request(
552  const std::string &_topic,
553  void(*_callback)(const ReplyT &_reply, const bool _result));
554 
566  public: template<typename RequestT, typename ReplyT>
567  bool Request(
568  const std::string &_topic,
569  const RequestT &_request,
570  std::function<void(const ReplyT &_reply,
571  const bool _result)> &_callback);
572 
583  public: template<typename ReplyT>
584  bool Request(
585  const std::string &_topic,
586  std::function<void(const ReplyT &_reply,
587  const bool _result)> &_callback);
588 
601  public: template<typename ClassT, typename RequestT, typename ReplyT>
602  bool Request(
603  const std::string &_topic,
604  const RequestT &_request,
605  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
606  ClassT *_obj);
607 
619  public: template<typename ClassT, typename ReplyT>
620  bool Request(
621  const std::string &_topic,
622  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
623  ClassT *_obj);
624 
634  public: template<typename RequestT, typename ReplyT>
635  bool Request(
636  const std::string &_topic,
637  const RequestT &_request,
638  const unsigned int &_timeout,
639  ReplyT &_reply,
640  bool &_result);
641 
650  public: template<typename ReplyT>
651  bool Request(
652  const std::string &_topic,
653  const unsigned int &_timeout,
654  ReplyT &_reply,
655  bool &_result);
656 
662  public: template<typename RequestT>
663  bool Request(const std::string &_topic, const RequestT &_request);
664 
679  public: bool RequestRaw(const std::string &_topic,
680  const std::string &_request, const std::string &_requestType,
681  const std::string &_responseType, unsigned int _timeout,
682  std::string &_response,
683  bool &_result);
684 
688  public: bool UnadvertiseSrv(const std::string &_topic);
689 
696  public: void TopicList(std::vector<std::string> &_topics) const;
697 
702  public: bool TopicInfo(const std::string &_topic,
703  std::vector<MessagePublisher> &_publishers) const;
704 
711  public: void ServiceList(std::vector<std::string> &_services) const;
712 
717  public: bool ServiceInfo(
718  const std::string &_service,
719  std::vector<ServicePublisher> &_publishers) const;
720 
734  public: bool SubscribeRaw(
735  const std::string &_topic,
736  const RawCallback &_callback,
737  const std::string &_msgType = kGenericMessageType,
738  const SubscribeOptions &_opts = SubscribeOptions());
739 
742  public: const NodeOptions &Options() const;
743 
750  public: bool EnableStats(const std::string &_topic, bool _enable,
751  const std::string &_publicationTopic = "/statistics",
752  uint64_t _publicationRate = 1);
753 
760  public: std::optional<TopicStatistics> TopicStats(
761  const std::string &_topic) const;
762 
766  private: NodeShared *Shared() const;
767 
770  private: const std::string &NodeUuid() const;
771 
774  private: std::unordered_set<std::string> &TopicsSubscribed() const;
775 
778  private: std::unordered_set<std::string> &SrvsAdvertised() const;
779 
783  private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
784 
785 #ifdef _WIN32
786 // Disable warning C4251 which is triggered by
787 // std::unique_ptr
788 #pragma warning(push)
789 #pragma warning(disable: 4251)
790 #endif
794 #ifdef _WIN32
795 #pragma warning(pop)
796 #endif
797  };
798  }
799  }
800 }
801 
802 #include "gz/transport/detail/Node.hh"
803 
804 #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 ...
Definition: gz/transport/AdvertiseOptions.hh:28
STL class.
google::protobuf::Message ProtoMsg
Definition: gz/transport/TransportTypes.hh:70
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:181
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:39
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:134
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:95
A class that provides information about the message received.
Definition: gz/transport/MessageInfo.hh:37
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:40
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.
A class that is used to store information about an advertised publisher. An instance of this class is...
Definition: gz/transport/Node.hh:115
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:174
A class for customizing the publication options for a service advertised.
Definition: gz/transport/AdvertiseOptions.hh:216
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:66
std::vector< std::string > AdvertisedTopics() const
Get the list of topics advertised by this node.