Go to the documentation of this file.
17 #ifndef IGN_TRANSPORT_NODE_HH_
18 #define IGN_TRANSPORT_NODE_HH_
25 #include <unordered_set>
30 #pragma warning(push, 0)
32 #include <ignition/msgs.hh>
38 #include "ignition/transport/Export.hh"
64 class IGNITION_TRANSPORT_VISIBLE
Node
66 class PublisherPrivate;
99 public:
operator bool();
104 public:
operator bool()
const;
109 public:
bool Valid()
const;
116 public:
bool Publish(
const ProtoMsg &_msg);
139 public:
bool PublishRaw(
146 private:
bool UpdateThrottling();
150 public:
bool HasConnections()
const;
165 public:
virtual ~Node();
175 public:
template<
typename MessageT>
212 public:
template<
typename MessageT>
215 void(*
_cb)(
const MessageT &_msg),
219 [
_cb](
const MessageT & _internalMsg,
222 (*_cb)(_internalMsg);
225 return this->Subscribe<MessageT>(
_topic,
f, _opts);
236 public:
template<
typename MessageT>
243 [
_cb](
const MessageT & _internalMsg,
249 return this->Subscribe<MessageT>(
_topic,
f, _opts);
262 public:
template<
typename ClassT,
typename MessageT>
265 void(ClassT::*
_cb)(
const MessageT &_msg),
270 [
_cb,
_obj](
const MessageT & _internalMsg,
277 return this->Subscribe<MessageT>(
_topic,
f, _opts);
290 public:
template<
typename MessageT>
297 [
_cb](
const MessageT & _internalMsg,
300 (*_cb)(_internalMsg, _internalInfo);
303 return this->Subscribe<MessageT>(
_topic,
f, _opts);
315 public:
template<
typename MessageT>
324 this->Options().NameSpace(),
_topic, fullyQualifiedTopic))
335 subscrHandlerPtr->SetCallback(
_cb);
343 this->
Shared()->localSubscribers.normal.AddHandler(
344 fullyQualifiedTopic, this->NodeUuid(), subscrHandlerPtr);
346 return this->SubscribeHelper(fullyQualifiedTopic);
360 public:
template<
typename ClassT,
typename MessageT>
363 void(ClassT::*
_cb)(
const MessageT &_msg,
const MessageInfo &_info),
368 [
_cb,
_obj](
const MessageT & _internalMsg,
372 std::placeholders::_2);
373 cb(_internalMsg, _internalInfo);
376 return this->Subscribe<MessageT>(
_topic,
f, _opts);
406 public:
template<
typename RequestT,
typename ReplyT>
413 [=](
const RequestT &_internalReq, ReplyT &_internalRep) ->
bool
415 bool internalResult =
false;
416 (*_cb)(_internalReq, _internalRep, internalResult);
417 return internalResult;
435 public:
template<
typename RequestT,
typename ReplyT>
439 const AdvertiseServiceOptions &
_options = AdvertiseServiceOptions())
451 [
_cb](
const RequestT &_internalReq, ReplyT &_internalRep)
453 return (*
_cb)(_internalReq, _internalRep);
472 public:
template<
typename ReplyT>
476 const AdvertiseServiceOptions &
_options = AdvertiseServiceOptions())
479 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
481 bool internalResult =
false;
482 (*_cb)(_internalRep, internalResult);
483 return internalResult;
500 public:
template<
typename ReplyT>
507 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
509 return (*
_cb)(_internalRep);
524 public:
template<
typename RequestT>
532 ignition::msgs::Empty &)
534 (*_cb)(_internalReq);
556 public:
template<
typename RequestT,
typename ReplyT>
561 const AdvertiseServiceOptions &
_options = AdvertiseServiceOptions())
566 bool internalResult =
false;
568 return internalResult;
586 public:
template<
typename RequestT,
typename ReplyT>
594 this->Options().NameSpace(),
_topic, fullyQualifiedTopic))
605 repHandlerPtr->SetCallback(
_cb);
610 this->SrvsAdvertised().insert(fullyQualifiedTopic);
616 this->
Shared()->repliers.AddHandler(
617 fullyQualifiedTopic, this->NodeUuid(), repHandlerPtr);
621 this->
Shared()->myReplierAddress,
622 this->
Shared()->replierId.ToString(),
623 this->
Shared()->pUuid, this->NodeUuid(),
626 if (!this->
Shared()->AdvertisePublisher(publisher))
628 std::cerr <<
"Node::Advertise(): Error advertising a service. "
629 <<
"Did you forget to start the discovery service?"
650 public:
template<
typename ReplyT>
657 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
659 bool internalResult =
false;
660 (
_cb)(_internalRep, internalResult);
661 return internalResult;
678 public:
template<
typename ReplyT>
682 const AdvertiseServiceOptions &
_options = AdvertiseServiceOptions())
685 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
687 return (
_cb)(_internalRep);
702 public:
template<
typename RequestT>
706 const AdvertiseServiceOptions &
_options = AdvertiseServiceOptions())
710 ignition::msgs::Empty &)
735 public:
template<
typename ClassT,
typename RequestT,
typename ReplyT>
744 ReplyT &_internalRep)
747 (
_obj->*
_cb)(_internalReq, _internalRep, internalResult);
748 return internalResult;
767 public:
template<
typename ClassT,
typename RequestT,
typename ReplyT>
776 ReplyT &_internalRep)
778 return (
_obj->*
_cb)(_internalReq, _internalRep);
798 public:
template<
typename ClassT,
typename ReplyT>
803 const AdvertiseServiceOptions &
_options = AdvertiseServiceOptions())
806 [
_cb,
_obj](
const msgs::Empty &, ReplyT &_internalRep)
809 (
_obj->*
_cb)(_internalRep, internalResult);
810 return internalResult;
828 public:
template<
typename ClassT,
typename ReplyT>
831 bool(ClassT::*
_cb)(ReplyT &
_rep),
833 const AdvertiseServiceOptions &
_options = AdvertiseServiceOptions())
836 [
_cb,
_obj](
const msgs::Empty &, ReplyT &_internalRep)
838 return (
_obj->*
_cb)(_internalRep);
855 public:
template<
typename ClassT,
typename RequestT>
864 ignition::msgs::Empty &)
888 public:
template<
typename RequestT,
typename ReplyT>
895 [
_cb](
const ReplyT &_internalRep,
const bool _internalResult)
897 (*_cb)(_internalRep, _internalResult);
913 public:
template<
typename ReplyT>
932 public:
template<
typename RequestT,
typename ReplyT>
940 this->Options().NameSpace(),
_topic, fullyQualifiedTopic))
953 ReplyT().GetTypeName(),
970 new ReqHandler<RequestT, ReplyT>(this->NodeUuid()));
982 this->
Shared()->requests.AddHandler(
989 this->
Shared()->SendPendingRemoteReqs(fullyQualifiedTopic,
990 RequestT().GetTypeName(), ReplyT().GetTypeName());
995 if (!this->
Shared()->DiscoverService(fullyQualifiedTopic))
997 std::cerr <<
"Node::Request(): Error discovering a service. "
998 <<
"Did you forget to start the discovery service?"
1018 public:
template<
typename ReplyT>
1038 public:
template<
typename ClassT,
typename RequestT,
typename ReplyT>
1046 [
_cb,
_obj](
const ReplyT &_internalRep,
const bool _internalResult)
1049 std::placeholders::_2);
1050 cb(_internalRep, _internalResult);
1067 public:
template<
typename ClassT,
typename ReplyT>
1085 public:
template<
typename RequestT,
typename ReplyT>
1089 const unsigned int &_timeout,
1095 this->Options().NameSpace(),
_topic, fullyQualifiedTopic))
1103 new ReqHandler<RequestT, ReplyT>(this->NodeUuid()));
1113 if (this->
Shared()->repliers.FirstHandler(fullyQualifiedTopic,
1122 this->
Shared()->requests.AddHandler(
1129 this->
Shared()->SendPendingRemoteReqs(fullyQualifiedTopic,
1130 _req.GetTypeName(),
_rep.GetTypeName());
1135 if (!this->
Shared()->DiscoverService(fullyQualifiedTopic))
1137 std::cerr <<
"Node::Request(): Error discovering a service. "
1138 <<
"Did you forget to start the discovery service?"
1161 std::cerr <<
"Node::Request(): Error Parsing the response"
1179 public:
template<
typename ReplyT>
1182 const unsigned int &_timeout,
1194 public:
template<
typename RequestT>
1201 std::function<void(
const ignition::msgs::Empty &,
const bool)>
f =
1202 [](
const ignition::msgs::Empty &,
const bool)
1206 return this->Request<RequestT, ignition::msgs::Empty>(
_topic,
_req,
f);
1257 public:
bool SubscribeRaw(
1261 const SubscribeOptions &_opts = SubscribeOptions());
1274 private: NodeShared *
Shared()
const;
1290 private: NodeOptions &Options()
const;
1295 private:
bool SubscribeHelper(
const std::string &_fullyQualifiedTopic);
*brief Advertise a new service without any output parameter *In this version the callback is a free function *param[in] _topic Topic name associated to the service *param[in] _cb Callback to handle the service request with the *following void(*) const AdvertiseServiceOptions _options)
Definition: Node.hh:528
Node(const NodeOptions &_options=NodeOptions())
Constructor.
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: AdvertiseOptions.hh:27
bool Unsubscribe(const std::string &_topic)
Unsubscribe from a topic.
std::vector< std::string > AdvertisedServices() const
reqHandlerPtr SetMessage & _req
Definition: Node.hh:973
google::protobuf::Message ProtoMsg
Definition: TransportTypes.hh:66
std::vector< std::string > AdvertisedTopics() const
Get the list of topics advertised by this node.
std::function< void(const char *_msgData, const size_t _size, const MessageInfo &_info)> RawCallback
Definition: TransportTypes.hh:147
virtual ~Publisher()
Destructor.
class ignition::transport::Node::Publisher Advertise[in]
virtual ~Node()
Destructor.
void waitForShutdown()
Block the current thread until a SIGINT or SIGTERM is received. Note that this function registers a s...
bool Subscribe(const std::string &_topic, void(*_cb)(const MessageT &_msg), const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. Note that this callback does not include any message inf...
Definition: Node.hh:213
bool Valid() const
Return true if valid information, such as a non-empty topic name, is present.
bool localResponserFound
Definition: Node.hh:946
This class stores all the information about a message publisher.
Definition: Publisher.hh:198
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
this Shared() -> requests.AddHandler(fullyQualifiedTopic, this->NodeUuid(), reqHandlerPtr)
A class to provide different options for a subscription.
Definition: SubscribeOptions.hh:35
*brief Old method for advertising a service This signature is *considered deprecated Please migrate to the callback signature std::function< void(ReplyT &_rep, bool &_result)> const AdvertiseServiceOptions RequestT
Definition: Node.hh:654
A class for customizing the publication options for a topic advertised. E.g.: Set the rate of message...
Definition: AdvertiseOptions.hh:136
bool Unsubscribe(const std::string &_topic)
Unsubscribe from a topic.
bool Subscribe(const std::string &_topic, void(ClassT::*_cb)(const MessageT &_msg), ClassT *_obj, const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. Note that this callback does not include any message inf...
Definition: Node.hh:263
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 ...
Publisher()
Default constructor.
void TopicList(std::vector< std::string > &_topics) const
A class that allows a client to communicate with other peers. There are two main communication modes:...
Definition: Node.hh:64
_result
Definition: Node.hh:1167
A class that provides information about the message received.
Definition: MessageInfo.hh:33
std::unique_lock< std::recursive_mutex > lk(this->Shared() ->mutex)
std::shared_ptr< ReqHandler< RequestT, ReplyT > > reqHandlerPtr(new ReqHandler< RequestT, ReplyT >(this->NodeUuid()))
It creates a subscription handler for a specific protobuf message. 'T' is the Protobuf message type t...
Definition: SubscriptionHandler.hh:144
void ServiceList(std::vector< std::string > &_services) const
A class for customizing the behavior of the Node. E.g.: Set a custom namespace or a partition name.
Definition: NodeOptions.hh:35
*brief Advertise a new service without any output parameter *In this version the callback is a free function *param[in] _topic Topic name associated to the service *param[in] _cb Callback to handle the service request with the *following void(*) const AdvertiseServiceOptions const ReplyT std::string std::function< void(const RequestT &_req, ReplyT &_rep, bool &_result)> const AdvertiseServiceOptions ReplyT boo Advertise)(const std::string &_topic, std::function< bool(const RequestT &_req, ReplyT &_rep)> &_cb, const AdvertiseServiceOptions &_options=AdvertiseServiceOptions())
Definition: Node.hh:587
static bool FullyQualifiedName(const std::string &_partition, const std::string &_ns, const std::string &_topic, std::string &_name)
Get the full topic path given a namespace and a topic name. A fully qualified topic name's length mus...
bool Subscribe(const std::string &_topic, std::function< void(const MessageT &_msg)> &_cb, const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. Note that this callback does not include any message inf...
Definition: Node.hh:237
cb(_internalRep, _internalResult)
*brief Advertise a new service without any output parameter *In this version the callback is a free function *param[in] _topic Topic name associated to the service *param[in] _cb Callback to handle the service request with the *following void(*) const AdvertiseServiceOptions const ReplyT std::string _topic)
Definition: Node.hh:528
bool Subscribe(const std::string &_topic, void(ClassT::*_cb)(const MessageT &_msg, const MessageInfo &_info), ClassT *_obj, const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. Note that this callback includes message information....
Definition: Node.hh:361
with the service response.
Definition: RepHandler.hh:100
virtual ~Node()
Destructor.
A class that is used to store information about an advertised publisher. An instance of this class is...
Definition: Node.hh:84
return this Request(_topic, req, _cb)
IRepHandlerPtr repHandler
Definition: Node.hh:947
bool HasConnections() const
Return true if this publisher has subscribers.
bool Subscribe(const std::string &_topic, void(*_cb)(const MessageT &_msg, const MessageInfo &_info), const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. Note that this callback includes message information....
Definition: Node.hh:291
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...
Definition: Node.hh:176
This class stores all the information about a service publisher.
Definition: Publisher.hh:314
const std::string kGenericMessageType
The string type used for generic messages.
Definition: TransportTypes.hh:170
*brief Old method for advertising a service This signature is *considered deprecated Please migrate to the callback signature std::function< void(ReplyT &_rep, bool &_result)> const AdvertiseServiceOptions const ReplyT std::string void(ClassT::*) ClassT _obj)
Definition: Node.hh:739
A class for customizing the publication options for a service advertised.
Definition: AdvertiseOptions.hh:224
SrvAddresses_M addresses
Definition: Node.hh:986
bool UnadvertiseSrv(const std::string &_topic)
Addresses_M< ServicePublisher > SrvAddresses_M
Definition: TransportTypes.hh:62
bool Subscribe(const std::string &_topic, std::function< void(const MessageT &_msg, const MessageInfo &_info)> &_cb, const SubscribeOptions &_opts=SubscribeOptions())
Subscribe to a topic registering a callback. Note that this callback includes message information....
Definition: Node.hh:316
bool ServiceInfo(const std::string &_service, std::vector< ServicePublisher > &_publishers) const
[_cb](const ReplyT &_internalRep, const bool _internalResult) {(*_cb)(_internalRep, _internalResult) f
Definition: Node.hh:897
bool Publish(const ProtoMsg &_msg)
Publish a message. This function will copy the message when publishing to interprocess subscribers....
Node(const NodeOptions &_options=NodeOptions())
Constructor.
*brief Advertise a new service without any output parameter *In this version the callback is a free function *param[in] _topic Topic name associated to the service *param[in] _cb Callback to handle the service request with the *following void(* _cb)(const RequestT &_req)
Definition: Node.hh:527
std::vector< std::string > AdvertisedTopics() const
Get the list of topics advertised by this node.
reqHandlerPtr SetResponse & _rep
Definition: Node.hh:1107
return this Request< RequestT, ReplyT >(_topic, _req, f)