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;
146 private:
bool UpdateThrottling();
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>
293 void(*_cb)(
const MessageT &_msg,
const MessageInfo &_info),
297 [
_cb](
const MessageT & _internalMsg,
300 (*_cb)(_internalMsg, _internalInfo);
303 return this->Subscribe<MessageT>(
_topic,
f, _opts);
315 public:
template<
typename MessageT>
323 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
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>
408 const
std::
string &_topic,
409 void(*_cb)(const RequestT &_req, ReplyT &_rep,
bool &_result),
413 [=](
const RequestT &_internalReq, ReplyT &_internalRep) ->
bool
415 bool internalResult =
false;
416 (*_cb)(_internalReq, _internalRep, internalResult);
417 return internalResult;
420 return this->
Advertise(_topic, newCb, _options);
435 public:
template<
typename RequestT,
typename ReplyT>
438 bool(*_cb)(
const RequestT &_req, ReplyT &_rep),
439 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
451 [_cb](
const RequestT &_internalReq, ReplyT &_internalRep)
453 return (*_cb)(_internalReq, _internalRep);
456 return this->
Advertise(_topic, f, _options);
472 public:
template<
typename ReplyT>
474 const
std::
string &_topic,
475 void(*_cb)(ReplyT &_rep,
bool &_result),
476 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
479 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
481 bool internalResult =
false;
482 (*_cb)(_internalRep, internalResult);
483 return internalResult;
486 return this->
Advertise(_topic, f, _options);
500 public:
template<
typename ReplyT>
503 bool(*_cb)(ReplyT &_rep),
504 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
507 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
509 return (*_cb)(_internalRep);
511 return this->
Advertise(_topic, f, _options);
524 public:
template<
typename RequestT>
527 void(*_cb)(
const RequestT &_req),
531 [_cb](
const RequestT &_internalReq,
532 ignition::msgs::Empty &)
534 (*_cb)(_internalReq);
538 return this->
Advertise(_topic, f, _options);
556 public:
template<
typename RequestT,
typename ReplyT>
558 const
std::
string &_topic,
560 ReplyT &_rep,
bool &_result)> &_cb,
566 bool internalResult =
false;
568 return internalResult;
586 public:
template<
typename RequestT,
typename ReplyT>
589 std::function<
bool(
const RequestT &_req, ReplyT &_rep)> &_cb,
593 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
594 this->Options().NameSpace(),
_topic, fullyQualifiedTopic))
602 new RepHandler<RequestT, ReplyT>());
605 repHandlerPtr->SetCallback(_cb);
610 this->SrvsAdvertised().insert(fullyQualifiedTopic);
616 this->Shared()->repliers.AddHandler(
617 fullyQualifiedTopic, this->NodeUuid(), repHandlerPtr);
620 ServicePublisher publisher(fullyQualifiedTopic,
621 this->Shared()->myReplierAddress,
622 this->Shared()->replierId.ToString(),
623 this->Shared()->pUuid, this->NodeUuid(),
624 RequestT().GetTypeName(), ReplyT().GetTypeName(), _options);
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>
652 const
std::
string &_topic,
653 std::function<
void(ReplyT &_rep,
bool &_result)> &_cb,
654 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
657 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
659 bool internalResult =
false;
660 (
_cb)(_internalRep, internalResult);
661 return internalResult;
664 return this->
Advertise(_topic, f, _options);
678 public:
template<
typename ReplyT>
682 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
685 [
_cb](
const msgs::Empty &, ReplyT &_internalRep)
687 return (_cb)(_internalRep);
689 return this->
Advertise(_topic, f, _options);
702 public:
template<
typename RequestT>
706 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
708 std::function<bool(
const RequestT &, ignition::msgs::Empty &)>
f =
710 ignition::msgs::Empty &)
716 return this->
Advertise(_topic, f, _options);
735 public:
template<
typename ClassT,
typename RequestT,
typename ReplyT>
737 const
std::
string &_topic,
738 void(ClassT::*_cb)(const RequestT &_req, ReplyT &_rep,
bool &_result),
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>
800 const
std::
string &_topic,
801 void(ClassT::*_cb)(ReplyT &_rep,
bool &_result),
803 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
806 [_cb, _obj](
const msgs::Empty &, ReplyT &_internalRep)
809 (_obj->*_cb)(_internalRep, internalResult);
810 return internalResult;
813 return this->
Advertise(_topic, f, _options);
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);
841 return this->
Advertise(_topic, f, _options);
855 public:
template<
typename ClassT,
typename RequestT>
864 ignition::msgs::Empty &)
888 public:
template<
typename RequestT,
typename ReplyT>
891 const RequestT &_req,
892 void(*_cb)(
const ReplyT &_rep,
const bool _result))
895 [
_cb](
const ReplyT &_internalRep,
const bool _internalResult)
897 (*_cb)(_internalRep, _internalResult);
900 return this->Request<RequestT, ReplyT>(_topic, _req,
f);
913 public:
template<
typename ReplyT>
916 void(*_cb)(
const ReplyT &_rep,
const bool _result))
932 public:
template<
typename RequestT,
typename ReplyT>
935 const RequestT &_req,
936 std::function<
void(
const ReplyT &_rep,
const bool _result)> &_cb)
939 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
940 this->Options().NameSpace(), _topic, fullyQualifiedTopic))
953 ReplyT().GetTypeName(),
958 if (localResponserFound)
983 fullyQualifiedTopic, this->NodeUuid(), reqHandlerPtr);
987 if (this->
Shared()->TopicPublishers(fullyQualifiedTopic, addresses))
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>
1021 std::function<
void(
const ReplyT &_rep,
const bool _result)> &_cb)
1024 return this->
Request(_topic, req, _cb);
1038 public:
template<
typename ClassT,
typename RequestT,
typename ReplyT>
1041 const RequestT &_req,
1046 [
_cb,
_obj](
const ReplyT &_internalRep,
const bool _internalResult)
1048 auto cb =
std::bind(_cb, _obj, std::placeholders::_1,
1049 std::placeholders::_2);
1050 cb(_internalRep, _internalResult);
1053 return this->Request<RequestT, ReplyT>(_topic, _req, f);
1067 public:
template<
typename ClassT,
typename ReplyT>
1070 void(ClassT::*_cb)(const ReplyT &_rep, const bool _result),
1074 return this->Request(_topic, req, _cb, _obj);
1085 public:
template<
typename RequestT,
typename ReplyT>
1088 const RequestT &_req,
1089 const unsigned int &_timeout,
1094 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
1095 this->Options().NameSpace(), _topic, fullyQualifiedTopic))
1103 new ReqHandler<RequestT, ReplyT>(this->NodeUuid()));
1106 reqHandlerPtr->SetMessage(&_req);
1113 if (this->
Shared()->repliers.FirstHandler(fullyQualifiedTopic,
1114 _req.GetTypeName(),
_rep.GetTypeName(), repHandler))
1122 this->Shared()->requests.AddHandler(
1123 fullyQualifiedTopic, this->NodeUuid(), reqHandlerPtr);
1126 SrvAddresses_M addresses;
1127 if (this->Shared()->TopicPublishers(fullyQualifiedTopic, addresses))
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,
1187 return this->
Request(_topic, req, _timeout, _rep, _result);
1194 public:
template<
typename RequestT>
1197 const RequestT &_req)
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(
1260 const std::string &_msgType = kGenericMessageType,
1295 private:
bool SubscribeHelper(
const std::string &_fullyQualifiedTopic);
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:612
A class for customizing the publication options for a topic advertised. E.g.: Set the rate of message...
Definition AdvertiseOptions.hh:138
A class for customizing the publication options for a service advertised.
Definition AdvertiseOptions.hh:226
A class that provides information about the message received.
Definition MessageInfo.hh:34
This class stores all the information about a message publisher.
Definition Publisher.hh:199
A class for customizing the behavior of the Node. E.g.: Set a custom namespace or a partition name.
Definition NodeOptions.hh:36
Private data for the Node class. This class should not be directly used. You should use the Node clas...
Definition NodeShared.hh:60
A class that is used to store information about an advertised publisher. An instance of this class is...
Definition Node.hh:85
bool Valid() const
Return true if valid information, such as a non-empty topic name, is present.
Publisher(const MessagePublisher &_publisher)
Constructor.
virtual ~Publisher()
Destructor.
Publisher()
Default constructor.
bool PublishRaw(const std::string &_msgData, const std::string &_msgType)
Publish a raw pre-serialized message.
bool HasConnections() const
Return true if this publisher has subscribers.
bool Publish(const ProtoMsg &_msg)
Publish a message. This function will copy the message when publishing to interprocess subscribers....
A class that allows a client to communicate with other peers. There are two main communication modes:...
Definition Node.hh:65
Node::Publisher Advertise(const std::string &_topic, const std::string &_msgTypeName, const AdvertiseMessageOptions &_options=AdvertiseMessageOptions())
Advertise a new topic. If a topic is currently advertised, you cannot advertise it a second time (reg...
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
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
class ignition::transport::Node::Publisher Advertise[in]
std::vector< std::string > AdvertisedTopics() const
Get the list of topics advertised by this node.
virtual ~Node()
Destructor.
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
Node(const NodeOptions &_options=NodeOptions())
Constructor.
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
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 ...
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
bool Unsubscribe(const std::string &_topic)
Unsubscribe from a topic.
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 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
It creates a reply handler for the specific protobuf messages used. 'Req' is a protobuf message type ...
Definition ReqHandler.hh:177
A class to provide different options for a subscription.
Definition SubscribeOptions.hh:36
It creates a subscription handler for a specific protobuf message. 'T' is the Protobuf message type t...
Definition SubscriptionHandler.hh:146
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 localResponserFound
Definition Node.hh:946
SrvAddresses_M addresses
Definition Node.hh:986
IRepHandlerPtr repHandler
Definition Node.hh:947
_result
Definition Node.hh:1167
std::shared_ptr< ReqHandler< RequestT, ReplyT > > reqHandlerPtr(new ReqHandler< RequestT, ReplyT >(this->NodeUuid()))
std::unique_lock< std::recursive_mutex > lk(this->Shared() ->mutex)
*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
bool TopicInfo(const std::string &_topic, std::vector< MessagePublisher > &_publishers) const
*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:735
return this Request< RequestT, ReplyT >(_topic, _req, f)
void waitForShutdown()
Block the current thread until a SIGINT or SIGTERM is received. Note that this function registers a s...
void ServiceList(std::vector< std::string > &_services) const
bool UnadvertiseSrv(const std::string &_topic)
return this Request(_topic, req, _cb)
[_cb](const ReplyT &_internalRep, const bool _internalResult) {(*_cb)(_internalRep, _internalResult) f
Definition Node.hh:897
reqHandlerPtr SetMessage & _req
Definition Node.hh:973
this Shared() -> requests.AddHandler(fullyQualifiedTopic, this->NodeUuid(), reqHandlerPtr)
*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
*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 ReplyT const std::string void(ClassT::*) ClassT _obj)
Definition Node.hh:739
bool ServiceInfo(const std::string &_service, std::vector< ServicePublisher > &_publishers) const
*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 ReplyT const std::string _topic)
Definition Node.hh:558
const std::string kGenericMessageType
The string type used for generic messages.
Definition TransportTypes.hh:170
google::protobuf::Message ProtoMsg
Definition TransportTypes.hh:66
Addresses_M< ServicePublisher > SrvAddresses_M
Definition TransportTypes.hh:62
std::vector< std::string > AdvertisedServices() const
cb(_internalRep, _internalResult)
void TopicList(std::vector< std::string > &_topics) const
reqHandlerPtr SetResponse & _rep
Definition Node.hh:1107
Definition AdvertiseOptions.hh:28