impl/lazy_subscriber.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // SPDX-License-Identifier: BSD-3-Clause
4 // SPDX-FileCopyrightText: Czech Technical University in Prague
5 
13 #include <mutex>
14 #include <string>
15 #include <utility>
16 
17 #include <boost/bind.hpp>
18 #include <boost/bind/placeholders.hpp>
19 #include <boost/functional.hpp>
20 
21 #include <ros/advertise_options.h>
22 #include <ros/node_handle.h>
24 #include <ros/subscribe_options.h>
25 #include <ros/subscriber.h>
26 
28 
30 
31 template<typename PublisherMsgType>
32 ::cras::LazySubscriberBase<PublisherMsgType>::LazySubscriberBase(
33  ::ros::NodeHandle publisherNh, const ::std::string& publisherTopic,
34  typename ::cras::ConditionalSubscriber::ConnectFn connectFn,
35  typename ::cras::ConditionalSubscriber::DisconnectFn disconnectFn,
36  const ::cras::LogHelperPtr& logHelper) :
37  ConditionalSubscriber(::std::move(connectFn), ::std::move(disconnectFn), logHelper)
38 {
40  auto cb = ::boost::bind(&LazySubscriberBase<PublisherMsgType>::connectCb, this, ::boost::placeholders::_1);
41  opts.template init<PublisherMsgType>(publisherTopic, 1, cb, cb);
42 
43  // Need to create the publisher with connection mutex - connectCB can be called before the publisher is created
44  // in nodelet, which means no topics will connect.
45  ::std::lock_guard<::std::mutex> lock(this->connectMutex);
46  this->pub = publisherNh.advertise(opts);
47 }
48 
49 template<typename PublisherMsgType>
50 ::cras::LazySubscriberBase<PublisherMsgType>::LazySubscriberBase(
51  ::ros::NodeHandle publisherNh, const ::std::string& publisherTopic,
52  typename ::cras::ConditionalSubscriber::ConnectFn connectFn, const ::cras::LogHelperPtr& logHelper) :
53  LazySubscriberBase(::std::move(publisherNh), publisherTopic, ::std::move(connectFn),
54  [](::ros::Subscriber& sub) { sub.shutdown(); }, logHelper)
55 {
56 }
57 
58 template<typename PublisherMsgType>
59 void ::cras::LazySubscriberBase<PublisherMsgType>::connectCb(const ::ros::SingleSubscriberPublisher&)
60 {
61  if (!this->lazy)
62  return;
63 
64  this->updateSubscription();
65 }
66 
67 template<typename PublisherMsgType>
68 bool ::cras::LazySubscriberBase<PublisherMsgType>::shouldBeSubscribed() const
69 {
70  return this->pub && this->pub.getNumSubscribers() > 0;
71 }
72 
73 template<typename PublisherMsgType, typename CallbackType>
74 ::cras::LazySubscriber<PublisherMsgType, CallbackType>::LazySubscriber(
75  ::ros::NodeHandle publisherNh, const ::std::string& publisherTopic,
76  ::ros::NodeHandle subscriberNh, const ::std::string& subscriberTopic, const size_t subscriberQueueSize,
77  ::boost::function<void(CallbackType)> subscriberCallback, ::ros::SubscribeOptions subscribeOptions,
78  const ::cras::LogHelperPtr& logHelper) :
79  LazySubscriberBase<PublisherMsgType>(::std::move(publisherNh), publisherTopic,
80  [=](::ros::Subscriber& sub) mutable
81  {
82  subscribeOptions.template initByFullCallbackType<CallbackType>(
83  subscriberTopic, subscriberQueueSize, ::std::move(subscriberCallback));
84  sub = subscriberNh.subscribe(subscribeOptions);
85  },
86  logHelper)
87 {
88 }
89 
90 template<typename PublisherMsgType, typename CallbackType>
91 ::cras::LazySubscriber<PublisherMsgType, CallbackType>::LazySubscriber(
92  ::ros::NodeHandle publisherNh, const ::std::string& publisherTopic,
93  ::ros::NodeHandle subscriberNh, const ::std::string& subscriberTopic, const size_t subscriberQueueSize,
94  ::boost::function<void(CallbackType)> subscriberCallback, const ::cras::LogHelperPtr& logHelper) :
95  LazySubscriber<PublisherMsgType, CallbackType>(::std::move(publisherNh), publisherTopic, ::std::move(subscriberNh),
96  subscriberTopic, subscriberQueueSize, subscriberCallback, {}, logHelper)
97 {
98 }
node_handle.h
ros
lazy_subscriber.hpp
Lazy subscriber that subscribes only when a paired publisher has subscribers.
ros::NodeHandle::advertise
Publisher advertise(AdvertiseOptions &ops)
ros::AdvertiseOptions
cras::ConditionalSubscriber::sub
::ros::Subscriber sub
The underlying subscriber (valid only when subscribed is true).
Definition: lazy_subscriber.hpp:126
log_utils.h
subscriber.h
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
cras::LazySubscriber
Lazy subscriber that subscribes only when a paired publisher has subscribers.
Definition: lazy_subscriber.hpp:197
single_subscriber_publisher.h
advertise_options.h
ros::SubscribeOptions
std
cras::LazySubscriberBase
Base for lazy subscribers that subscribes only when a paired publisher has subscribers.
Definition: lazy_subscriber.hpp:149
cras::ConditionalSubscriber
A lazy subscriber that subscribes only when a condition is met.
Definition: lazy_subscriber.hpp:33
subscribe_options.h
ros::NodeHandle


cras_topic_tools
Author(s): Martin Pecka
autogenerated on Sun Jan 14 2024 03:48:28