Ignition Transport

API Reference

4.0.0
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 IGN_TRANSPORT_NODE_HH_
18#define IGN_TRANSPORT_NODE_HH_
19
20#include <algorithm>
21#include <functional>
22#include <memory>
23#include <mutex>
24#include <string>
25#include <unordered_set>
26#include <vector>
27
28// ToDo: Remove after fixing the warnings
29#ifdef _MSC_VER
30#pragma warning(push, 0)
31#endif
32#include <ignition/msgs.hh>
33#ifdef _MSC_VER
34#pragma warning(pop)
35#endif
36
38#include "ignition/transport/Export.hh"
48
49namespace ignition
50{
51 namespace transport
52 {
53 class NodePrivate;
54
58 void IGNITION_TRANSPORT_VISIBLE waitForShutdown();
59
64 class IGNITION_TRANSPORT_VISIBLE Node
65 {
66 class PublisherPrivate;
67
84 public: class IGNITION_TRANSPORT_VISIBLE Publisher
85 {
87 public: Publisher();
88
91 public: explicit Publisher(const MessagePublisher &_publisher);
92
94 public: virtual ~Publisher();
95
99 public: operator bool();
100
104 public: operator bool() const;
105
109 public: bool Valid() const;
110
116 public: bool Publish(const ProtoMsg &_msg);
117
139 public: bool PublishRaw(
140 const std::string &_msgData,
141 const std::string &_msgType);
142
146 private: bool UpdateThrottling();
147
150 public: bool HasConnections() const;
151
157 private: std::shared_ptr<PublisherPrivate> dataPtr;
158 };
159
162 public: explicit Node(const NodeOptions &_options = NodeOptions());
163
165 public: virtual ~Node();
166
175 public: template<typename MessageT>
177 const std::string &_topic,
179 {
180 return this->Advertise(_topic, MessageT().GetTypeName(), _options);
181 }
182
195 const std::string &_topic,
196 const std::string &_msgTypeName,
198
202
212 public: template<typename MessageT>
214 const std::string &_topic,
215 void(*_cb)(const MessageT &_msg),
216 const SubscribeOptions &_opts = SubscribeOptions())
217 {
218 std::function<void(const MessageT &, const MessageInfo &)> f =
219 [_cb](const MessageT & _internalMsg,
220 const MessageInfo &/*_internalInfo*/)
221 {
222 (*_cb)(_internalMsg);
223 };
224
225 return this->Subscribe<MessageT>(_topic, f, _opts);
226 }
227
236 public: template<typename MessageT>
238 const std::string &_topic,
239 std::function<void(const MessageT &_msg)> &_cb,
240 const SubscribeOptions &_opts = SubscribeOptions())
241 {
242 std::function<void(const MessageT &, const MessageInfo &)> f =
243 [_cb](const MessageT & _internalMsg,
244 const MessageInfo &/*_internalInfo*/)
245 {
246 _cb(_internalMsg);
247 };
248
249 return this->Subscribe<MessageT>(_topic, f, _opts);
250 }
251
262 public: template<typename ClassT, typename MessageT>
264 const std::string &_topic,
265 void(ClassT::*_cb)(const MessageT &_msg),
266 ClassT *_obj,
267 const SubscribeOptions &_opts = SubscribeOptions())
268 {
269 std::function<void(const MessageT &, const MessageInfo &)> f =
270 [_cb, _obj](const MessageT & _internalMsg,
271 const MessageInfo &/*_internalInfo*/)
272 {
273 auto cb = std::bind(_cb, _obj, std::placeholders::_1);
274 cb(_internalMsg);
275 };
276
277 return this->Subscribe<MessageT>(_topic, f, _opts);
278 }
279
290 public: template<typename MessageT>
292 const std::string &_topic,
293 void(*_cb)(const MessageT &_msg, const MessageInfo &_info),
294 const SubscribeOptions &_opts = SubscribeOptions())
295 {
296 std::function<void(const MessageT &, const MessageInfo &)> f =
297 [_cb](const MessageT & _internalMsg,
298 const MessageInfo &_internalInfo)
299 {
300 (*_cb)(_internalMsg, _internalInfo);
301 };
302
303 return this->Subscribe<MessageT>(_topic, f, _opts);
304 }
305
315 public: template<typename MessageT>
317 const std::string &_topic,
318 std::function<void(const MessageT &_msg,
319 const MessageInfo &_info)> &_cb,
320 const SubscribeOptions &_opts = SubscribeOptions())
321 {
322 std::string fullyQualifiedTopic;
323 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
324 this->Options().NameSpace(), _topic, fullyQualifiedTopic))
325 {
326 std::cerr << "Topic [" << _topic << "] is not valid." << std::endl;
327 return false;
328 }
329
330 // Create a new subscription handler.
332 new SubscriptionHandler<MessageT>(this->NodeUuid(), _opts));
333
334 // Insert the callback into the handler.
335 subscrHandlerPtr->SetCallback(_cb);
336
338
339 // Store the subscription handler. Each subscription handler is
340 // associated with a topic. When the receiving thread gets new data,
341 // it will recover the subscription handler associated to the topic and
342 // will invoke the callback.
343 this->Shared()->localSubscribers.normal.AddHandler(
344 fullyQualifiedTopic, this->NodeUuid(), subscrHandlerPtr);
345
346 return this->SubscribeHelper(fullyQualifiedTopic);
347 }
348
360 public: template<typename ClassT, typename MessageT>
362 const std::string &_topic,
363 void(ClassT::*_cb)(const MessageT &_msg, const MessageInfo &_info),
364 ClassT *_obj,
365 const SubscribeOptions &_opts = SubscribeOptions())
366 {
367 std::function<void(const MessageT &, const MessageInfo &)> f =
368 [_cb, _obj](const MessageT & _internalMsg,
369 const MessageInfo &_internalInfo)
370 {
371 auto cb = std::bind(_cb, _obj, std::placeholders::_1,
372 std::placeholders::_2);
373 cb(_internalMsg, _internalInfo);
374 };
375
376 return this->Subscribe<MessageT>(_topic, f, _opts);
377 }
378
385
389 public: bool Unsubscribe(const std::string &_topic);
390
406 public: template<typename RequestT, typename ReplyT>
407 IGN_DEPRECATED(4.0) bool Advertise(
408 const std::string &_topic,
409 void(*_cb)(const RequestT &_req, ReplyT &_rep, bool &_result),
411 {
412 std::function<bool(const RequestT &, ReplyT&)> newCb =
413 [=](const RequestT &_internalReq, ReplyT &_internalRep) -> bool
414 {
415 bool internalResult = false;
416 (*_cb)(_internalReq, _internalRep, internalResult);
417 return internalResult;
418 };
419
420 return this->Advertise(_topic, newCb, _options);
421 }
422
435 public: template<typename RequestT, typename ReplyT>
436 bool Advertise(
437 const std::string &_topic,
438 bool(*_cb)(const RequestT &_req, ReplyT &_rep),
439 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
440 {
441 // Dev Note: This overload of Advertise(~) is necessary so that the
442 // compiler can correctly infer the template arguments. We cannot rely
443 // on the compiler to implicitly cast the function pointer to a
444 // std::function object, because the compiler cannot infer the template
445 // parameters T1 and T2 from the signature of the function pointer that
446 // gets passed to Advertise(~).
447
448 // We create a std::function object so that we can explicitly call the
449 // baseline overload of Advertise(~).
450 std::function<bool(const RequestT&, ReplyT&)> f =
451 [_cb](const RequestT &_internalReq, ReplyT &_internalRep)
452 {
453 return (*_cb)(_internalReq, _internalRep);
454 };
455
456 return this->Advertise(_topic, f, _options);
457 }
458
472 public: template<typename ReplyT>
473 IGN_DEPRECATED(4.0) bool Advertise(
474 const std::string &_topic,
475 void(*_cb)(ReplyT &_rep, bool &_result),
476 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
477 {
478 std::function<bool(const msgs::Empty &, ReplyT &)> f =
479 [_cb](const msgs::Empty &/*_internalReq*/, ReplyT &_internalRep)
480 {
481 bool internalResult = false;
482 (*_cb)(_internalRep, internalResult);
483 return internalResult;
484 };
485
486 return this->Advertise(_topic, f, _options);
487 }
488
500 public: template<typename ReplyT>
501 bool Advertise(
502 const std::string &_topic,
503 bool(*_cb)(ReplyT &_rep),
504 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
505 {
506 std::function<bool(const msgs::Empty &, ReplyT &)> f =
507 [_cb](const msgs::Empty &/*_internalReq*/, ReplyT &_internalRep)
508 {
509 return (*_cb)(_internalRep);
510 };
511 return this->Advertise(_topic, f, _options);
512 }
513
524 public: template<typename RequestT>
525 bool Advertise(
526 const std::string &_topic,
527 void(*_cb)(const RequestT &_req),
529 {
530 std::function<bool(const RequestT &, ignition::msgs::Empty &)> f =
531 [_cb](const RequestT &_internalReq,
532 ignition::msgs::Empty &/*_internalRep*/)
533 {
534 (*_cb)(_internalReq);
535 return true;
536 };
537
538 return this->Advertise(_topic, f, _options);
539 }
556 public: template<typename RequestT, typename ReplyT>
557 IGN_DEPRECATED(4.0) bool Advertise(
558 const std::string &_topic,
559 std::function<void(const RequestT &_req,
560 ReplyT &_rep, bool &_result)> &_cb,
562 {
563 std::function<bool(const RequestT&, ReplyT&)> f =
564 [_cb](const RequestT &_req, ReplyT &_rep)
565 {
566 bool internalResult = false;
567 (_cb)(_req, _rep, internalResult);
568 return internalResult;
569 };
570
571 return this->Advertise(_topic, f, _options);
572 }
573
586 public: template<typename RequestT, typename ReplyT>
588 const std::string &_topic,
589 std::function<bool(const RequestT &_req, ReplyT &_rep)> &_cb,
591 {
592 std::string fullyQualifiedTopic;
593 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
594 this->Options().NameSpace(), _topic, fullyQualifiedTopic))
595 {
596 std::cerr << "Service [" << _topic << "] is not valid." << std::endl;
597 return false;
599
600 // Create a new service reply handler.
602 new RepHandler<RequestT, ReplyT>());
603
604 // Insert the callback into the handler.
605 repHandlerPtr->SetCallback(_cb);
606
607 std::lock_guard<std::recursive_mutex> lk(this->Shared()->mutex);
608
609 // Add the topic to the list of advertised services.
610 this->SrvsAdvertised().insert(fullyQualifiedTopic);
611
612 // Store the replier handler. Each replier handler is
613 // associated with a topic. When the receiving thread gets new requests,
614 // it will recover the replier handler associated to the topic and
615 // will invoke the service call.
616 this->Shared()->repliers.AddHandler(
617 fullyQualifiedTopic, this->NodeUuid(), repHandlerPtr);
618
619 // Notify the discovery service to register and advertise my responser.
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);
625
626 if (!this->Shared()->AdvertisePublisher(publisher))
627 {
628 std::cerr << "Node::Advertise(): Error advertising a service. "
629 << "Did you forget to start the discovery service?"
631 return false;
632 }
633
634 return true;
635 }
636
650 public: template<typename ReplyT>
651 IGN_DEPRECATED(4.0) bool Advertise(
652 const std::string &_topic,
653 std::function<void(ReplyT &_rep, bool &_result)> &_cb,
654 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
655 {
656 std::function<bool(const msgs::Empty &, ReplyT &)> f =
657 [_cb](const msgs::Empty &/*_internalReq*/, ReplyT &_internalRep)
658 {
659 bool internalResult = false;
660 (_cb)(_internalRep, internalResult);
661 return internalResult;
662 };
663
664 return this->Advertise(_topic, f, _options);
665 }
666
678 public: template<typename ReplyT>
679 bool Advertise(
680 const std::string &_topic,
681 std::function<bool(ReplyT &_rep)> &_cb,
682 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
683 {
684 std::function<bool(const msgs::Empty &, ReplyT &)> f =
685 [_cb](const msgs::Empty &/*_internalReq*/, ReplyT &_internalRep)
686 {
687 return (_cb)(_internalRep);
688 };
689 return this->Advertise(_topic, f, _options);
690 }
691
702 public: template<typename RequestT>
703 bool Advertise(
704 const std::string &_topic,
705 std::function<void(const RequestT &_req)> &_cb,
706 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
707 {
708 std::function<bool(const RequestT &, ignition::msgs::Empty &)> f =
709 [_cb](const RequestT &_internalReq,
710 ignition::msgs::Empty &/*_internalRep*/)
711 {
712 (_cb)(_internalReq);
713 return true;
714 };
715
716 return this->Advertise(_topic, f, _options);
717 }
718
735 public: template<typename ClassT, typename RequestT, typename ReplyT>
736 IGN_DEPRECATED(4.0) bool Advertise(
737 const std::string &_topic,
738 void(ClassT::*_cb)(const RequestT &_req, ReplyT &_rep, bool &_result),
739 ClassT *_obj,
741 {
742 std::function<bool(const RequestT &, ReplyT &)> f =
743 [_cb, _obj](const RequestT &_internalReq,
744 ReplyT &_internalRep)
745 {
746 bool internalResult;
747 (_obj->*_cb)(_internalReq, _internalRep, internalResult);
748 return internalResult;
749 };
750
751 return this->Advertise(_topic, f, _options);
753
767 public: template<typename ClassT, typename RequestT, typename ReplyT>
769 const std::string &_topic,
770 bool(ClassT::*_cb)(const RequestT &_req, ReplyT &_rep),
771 ClassT *_obj,
773 {
774 std::function<bool(const RequestT &, ReplyT &)> f =
775 [_cb, _obj](const RequestT &_internalReq,
776 ReplyT &_internalRep)
777 {
778 return (_obj->*_cb)(_internalReq, _internalRep);
779 };
780
781 return this->Advertise(_topic, f, _options);
782 }
783
798 public: template<typename ClassT, typename ReplyT>
799 IGN_DEPRECATED(4.0) bool Advertise(
800 const std::string &_topic,
801 void(ClassT::*_cb)(ReplyT &_rep, bool &_result),
802 ClassT *_obj,
803 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
804 {
805 std::function<bool(const msgs::Empty &, ReplyT &)> f =
806 [_cb, _obj](const msgs::Empty &/*_internalReq*/, ReplyT &_internalRep)
807 {
808 bool internalResult;
809 (_obj->*_cb)(_internalRep, internalResult);
810 return internalResult;
811 };
812
813 return this->Advertise(_topic, f, _options);
814 }
815
828 public: template<typename ClassT, typename ReplyT>
829 bool Advertise(
830 const std::string &_topic,
831 bool(ClassT::*_cb)(ReplyT &_rep),
832 ClassT *_obj,
833 const AdvertiseServiceOptions &_options = AdvertiseServiceOptions())
834 {
835 std::function<bool(const msgs::Empty &, ReplyT &)> f =
836 [_cb, _obj](const msgs::Empty &/*_internalReq*/, ReplyT &_internalRep)
837 {
838 return (_obj->*_cb)(_internalRep);
839 };
840
841 return this->Advertise(_topic, f, _options);
842 }
843
855 public: template<typename ClassT, typename RequestT>
857 const std::string &_topic,
858 void(ClassT::*_cb)(const RequestT &_req),
859 ClassT *_obj,
861 {
862 std::function<bool(const RequestT &, ignition::msgs::Empty &)> f =
863 [_cb, _obj](const RequestT &_internalReq,
864 ignition::msgs::Empty &/*_internalRep*/)
865 {
866 auto cb = std::bind(_cb, _obj, std::placeholders::_1);
867 cb(_internalReq);
868 return true;
869 };
870
871 return this->Advertise(_topic, f, _options);
872 }
873
877
888 public: template<typename RequestT, typename ReplyT>
889 bool Request(
890 const std::string &_topic,
891 const RequestT &_req,
892 void(*_cb)(const ReplyT &_rep, const bool _result))
893 {
894 std::function<void(const ReplyT &, const bool)> f =
895 [_cb](const ReplyT &_internalRep, const bool _internalResult)
896 {
897 (*_cb)(_internalRep, _internalResult);
898 };
899
900 return this->Request<RequestT, ReplyT>(_topic, _req, f);
901 }
902
913 public: template<typename ReplyT>
914 bool Request(
915 const std::string &_topic,
916 void(*_cb)(const ReplyT &_rep, const bool _result))
917 {
918 msgs::Empty req;
919 return this->Request(_topic, req, _cb);
920 }
921
932 public: template<typename RequestT, typename ReplyT>
933 bool Request(
934 const std::string &_topic,
935 const RequestT &_req,
936 std::function<void(const ReplyT &_rep, const bool _result)> &_cb)
937 {
938 std::string fullyQualifiedTopic;
939 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
940 this->Options().NameSpace(), _topic, fullyQualifiedTopic))
941 {
942 std::cerr << "Service [" << _topic << "] is not valid." << std::endl;
943 return false;
944 }
945
948 {
950 localResponserFound = this->Shared()->repliers.FirstHandler(
951 fullyQualifiedTopic,
952 RequestT().GetTypeName(),
953 ReplyT().GetTypeName(),
954 repHandler);
955 }
956
957 // If the responser is within my process.
958 if (localResponserFound)
959 {
960 // There is a responser in my process, let's use it.
961 ReplyT rep;
962 bool result = repHandler->RunLocalCallback(_req, rep);
963
964 _cb(rep, result);
965 return true;
966 }
967
968 // Create a new request handler.
970 new ReqHandler<RequestT, ReplyT>(this->NodeUuid()));
971
972 // Insert the request's parameters.
973 reqHandlerPtr->SetMessage(&_req);
974
975 // Insert the callback into the handler.
976 reqHandlerPtr->SetCallback(_cb);
977
978 {
980
981 // Store the request handler.
982 this->Shared()->requests.AddHandler(
983 fullyQualifiedTopic, this->NodeUuid(), reqHandlerPtr);
984
985 // If the responser's address is known, make the request.
987 if (this->Shared()->TopicPublishers(fullyQualifiedTopic, addresses))
988 {
989 this->Shared()->SendPendingRemoteReqs(fullyQualifiedTopic,
990 RequestT().GetTypeName(), ReplyT().GetTypeName());
991 }
992 else
993 {
994 // Discover the service responser.
995 if (!this->Shared()->DiscoverService(fullyQualifiedTopic))
996 {
997 std::cerr << "Node::Request(): Error discovering a service. "
998 << "Did you forget to start the discovery service?"
999 << std::endl;
1000 return false;
1001 }
1002 }
1003 }
1004
1005 return true;
1006 }
1007
1018 public: template<typename ReplyT>
1019 bool Request(
1020 const std::string &_topic,
1021 std::function<void(const ReplyT &_rep, const bool _result)> &_cb)
1022 {
1023 msgs::Empty req;
1024 return this->Request(_topic, req, _cb);
1025 }
1026
1038 public: template<typename ClassT, typename RequestT, typename ReplyT>
1039 bool Request(
1040 const std::string &_topic,
1041 const RequestT &_req,
1042 void(ClassT::*_cb)(const ReplyT &_rep, const bool _result),
1043 ClassT *_obj)
1044 {
1045 std::function<void(const ReplyT &, const bool)> f =
1046 [_cb, _obj](const ReplyT &_internalRep, const bool _internalResult)
1047 {
1048 auto cb = std::bind(_cb, _obj, std::placeholders::_1,
1049 std::placeholders::_2);
1050 cb(_internalRep, _internalResult);
1051 };
1052
1053 return this->Request<RequestT, ReplyT>(_topic, _req, f);
1054 }
1055
1067 public: template<typename ClassT, typename ReplyT>
1068 bool Request(
1069 const std::string &_topic,
1070 void(ClassT::*_cb)(const ReplyT &_rep, const bool _result),
1071 ClassT *_obj)
1072 {
1073 msgs::Empty req;
1074 return this->Request(_topic, req, _cb, _obj);
1075 }
1076
1085 public: template<typename RequestT, typename ReplyT>
1086 bool Request(
1087 const std::string &_topic,
1088 const RequestT &_req,
1089 const unsigned int &_timeout,
1090 ReplyT &_rep,
1091 bool &_result)
1092 {
1093 std::string fullyQualifiedTopic;
1094 if (!TopicUtils::FullyQualifiedName(this->Options().Partition(),
1095 this->Options().NameSpace(), _topic, fullyQualifiedTopic))
1096 {
1097 std::cerr << "Service [" << _topic << "] is not valid." << std::endl;
1098 return false;
1099 }
1100
1101 // Create a new request handler.
1103 new ReqHandler<RequestT, ReplyT>(this->NodeUuid()));
1104
1105 // Insert the request's parameters.
1106 reqHandlerPtr->SetMessage(&_req);
1107 reqHandlerPtr->SetResponse(&_rep);
1108
1110
1111 // If the responser is within my process.
1113 if (this->Shared()->repliers.FirstHandler(fullyQualifiedTopic,
1114 _req.GetTypeName(), _rep.GetTypeName(), repHandler))
1115 {
1116 // There is a responser in my process, let's use it.
1117 _result = repHandler->RunLocalCallback(_req, _rep);
1118 return true;
1119 }
1120
1121 // Store the request handler.
1122 this->Shared()->requests.AddHandler(
1123 fullyQualifiedTopic, this->NodeUuid(), reqHandlerPtr);
1124
1125 // If the responser's address is known, make the request.
1126 SrvAddresses_M addresses;
1127 if (this->Shared()->TopicPublishers(fullyQualifiedTopic, addresses))
1128 {
1129 this->Shared()->SendPendingRemoteReqs(fullyQualifiedTopic,
1130 _req.GetTypeName(), _rep.GetTypeName());
1131 }
1132 else
1133 {
1134 // Discover the service responser.
1135 if (!this->Shared()->DiscoverService(fullyQualifiedTopic))
1136 {
1137 std::cerr << "Node::Request(): Error discovering a service. "
1138 << "Did you forget to start the discovery service?"
1139 << std::endl;
1140 return false;
1141 }
1142 }
1143
1144 // Wait until the REP is available.
1145 bool executed = reqHandlerPtr->WaitUntil(lk, _timeout);
1146
1147 // The request was not executed.
1148 if (!executed)
1149 return false;
1150
1151 // The request was executed but did not succeed.
1152 if (!reqHandlerPtr->Result())
1153 {
1154 _result = false;
1155 return true;
1156 }
1157
1158 // Parse the response.
1159 if (!_rep.ParseFromString(reqHandlerPtr->Response()))
1160 {
1161 std::cerr << "Node::Request(): Error Parsing the response"
1162 << std::endl;
1163 _result = false;
1164 return true;
1165 }
1166
1167 _result = true;
1168 return true;
1169 }
1170
1179 public: template<typename ReplyT>
1180 bool Request(
1181 const std::string &_topic,
1182 const unsigned int &_timeout,
1183 ReplyT &_rep,
1184 bool &_result)
1185 {
1186 msgs::Empty req;
1187 return this->Request(_topic, req, _timeout, _rep, _result);
1188 }
1189
1194 public: template<typename RequestT>
1196 const std::string &_topic,
1197 const RequestT &_req)
1198 {
1199 // This callback is here for reusing the regular Request() call with
1200 // input and output parameters.
1201 std::function<void(const ignition::msgs::Empty &, const bool)> f =
1202 [](const ignition::msgs::Empty &, const bool)
1203 {
1204 };
1205
1206 return this->Request<RequestT, ignition::msgs::Empty>(_topic, _req, f);
1207 }
1208
1212 public: bool UnadvertiseSrv(const std::string &_topic);
1213
1220 public: void TopicList(std::vector<std::string> &_topics) const;
1221
1226 public: bool TopicInfo(const std::string &_topic,
1227 std::vector<MessagePublisher> &_publishers) const;
1228
1235 public: void ServiceList(std::vector<std::string> &_services) const;
1236
1241 public: bool ServiceInfo(const std::string &_service,
1242 std::vector<ServicePublisher> &_publishers) const;
1243
1257 public: bool SubscribeRaw(
1258 const std::string &_topic,
1259 const RawCallback &_callback,
1260 const std::string &_msgType = kGenericMessageType,
1261 const SubscribeOptions &_opts = SubscribeOptions());
1262
1265 private: const std::string &Partition() const;
1266
1269 private: const std::string &NameSpace() const;
1270
1274 private: NodeShared *Shared() const;
1275
1278 private: const std::string &NodeUuid() const;
1279
1282 private: std::unordered_set<std::string> &TopicsSubscribed() const;
1283
1286 private: std::unordered_set<std::string> &SrvsAdvertised() const;
1287
1290 private: NodeOptions &Options() const;
1291
1295 private: bool SubscribeHelper(const std::string &_fullyQualifiedTopic);
1296
1300 };
1301 }
1302}
1303#endif
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
T bind(T... args)
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.
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...
T endl(T... args)
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
STL namespace.