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::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 
369  public: template<typename RequestT, typename ReplyT>
370  bool Advertise(
371  const std::string &_topic,
372  bool(*_callback)(const RequestT &_request, ReplyT &_reply),
374 
386  public: template<typename ReplyT>
387  bool Advertise(
388  const std::string &_topic,
389  bool(*_callback)(ReplyT &_reply),
391 
402  public: template<typename RequestT>
403  bool Advertise(
404  const std::string &_topic,
405  void(*_callback)(const RequestT &_request),
407 
420  public: template<typename RequestT, typename ReplyT>
421  bool Advertise(
422  const std::string &_topic,
423  std::function<bool(const RequestT &_request,
424  ReplyT &_reply)> _callback,
426 
438  public: template<typename ReplyT>
439  bool Advertise(
440  const std::string &_topic,
441  std::function<bool(ReplyT &_reply)> &_callback,
443 
454  public: template<typename RequestT>
455  bool Advertise(
456  const std::string &_topic,
457  std::function<void(const RequestT &_request)> &_callback,
459 
473  public: template<typename ClassT, typename RequestT, typename ReplyT>
474  bool Advertise(
475  const std::string &_topic,
476  bool(ClassT::*_callback)(const RequestT &_request, ReplyT &_reply),
477  ClassT *_obj,
479 
492  public: template<typename ClassT, typename ReplyT>
493  bool Advertise(
494  const std::string &_topic,
495  bool(ClassT::*_callback)(ReplyT &_reply),
496  ClassT *_obj,
498 
510  public: template<typename ClassT, typename RequestT>
511  bool Advertise(
512  const std::string &_topic,
513  void(ClassT::*_callback)(const RequestT &_request),
514  ClassT *_obj,
516 
519  public: std::vector<std::string> AdvertisedServices() const;
520 
532  public: template<typename RequestT, typename ReplyT>
533  bool Request(
534  const std::string &_topic,
535  const RequestT &_request,
536  void(*_callback)(const ReplyT &_reply, const bool _result));
537 
548  public: template<typename ReplyT>
549  bool Request(
550  const std::string &_topic,
551  void(*_callback)(const ReplyT &_reply, const bool _result));
552 
564  public: template<typename RequestT, typename ReplyT>
565  bool Request(
566  const std::string &_topic,
567  const RequestT &_request,
568  std::function<void(const ReplyT &_reply,
569  const bool _result)> &_callback);
570 
581  public: template<typename ReplyT>
582  bool Request(
583  const std::string &_topic,
584  std::function<void(const ReplyT &_reply,
585  const bool _result)> &_callback);
586 
599  public: template<typename ClassT, typename RequestT, typename ReplyT>
600  bool Request(
601  const std::string &_topic,
602  const RequestT &_request,
603  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
604  ClassT *_obj);
605 
617  public: template<typename ClassT, typename ReplyT>
618  bool Request(
619  const std::string &_topic,
620  void(ClassT::*_callback)(const ReplyT &_reply, const bool _result),
621  ClassT *_obj);
622 
632  public: template<typename RequestT, typename ReplyT>
633  bool Request(
634  const std::string &_topic,
635  const RequestT &_request,
636  const unsigned int &_timeout,
637  ReplyT &_reply,
638  bool &_result);
639 
648  public: template<typename ReplyT>
649  bool Request(
650  const std::string &_topic,
651  const unsigned int &_timeout,
652  ReplyT &_reply,
653  bool &_result);
654 
660  public: template<typename RequestT>
661  bool Request(const std::string &_topic, const RequestT &_request);
662 
677  public: bool RequestRaw(const std::string &_topic,
678  const std::string &_request, const std::string &_requestType,
679  const std::string &_responseType, unsigned int _timeout,
680  std::string &_response,
681  bool &_result);
682 
686  public: bool UnadvertiseSrv(const std::string &_topic);
687 
694  public: void TopicList(std::vector<std::string> &_topics) const;
695 
700  public: bool TopicInfo(const std::string &_topic,
701  std::vector<MessagePublisher> &_publishers) const;
702 
709  public: void ServiceList(std::vector<std::string> &_services) const;
710 
715  public: bool ServiceInfo(
716  const std::string &_service,
717  std::vector<ServicePublisher> &_publishers) const;
718 
732  public: bool SubscribeRaw(
733  const std::string &_topic,
734  const RawCallback &_callback,
735  const std::string &_msgType = kGenericMessageType,
736  const SubscribeOptions &_opts = SubscribeOptions());
737 
740  public: const NodeOptions &Options() const;
741 
748  public: bool EnableStats(const std::string &_topic, bool _enable,
749  const std::string &_publicationTopic = "/statistics",
750  uint64_t _publicationRate = 1);
751 
758  public: std::optional<TopicStatistics> TopicStats(
759  const std::string &_topic) const;
760 
764  private: NodeShared *Shared() const;
765 
768  private: const std::string &NodeUuid() const;
769 
772  private: std::unordered_set<std::string> &TopicsSubscribed() const;
773 
776  private: std::unordered_set<std::string> &SrvsAdvertised() const;
777 
781  private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
782 
783 #ifdef _WIN32
784 // Disable warning C4251 which is triggered by
785 // std::unique_ptr
786 #pragma warning(push)
787 #pragma warning(disable: 4251)
788 #endif
792 #ifdef _WIN32
793 #pragma warning(pop)
794 #endif
795  };
796  }
797 }
798 
799 #include "gz/transport/detail/Node.hh"
800 
801 #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.