lazy_subscriber.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 // SPDX-FileCopyrightText: Czech Technical University in Prague
3 
11 #include <mutex>
12 #include <utility>
13 
14 #include <ros/subscriber.h>
15 
17 
19 
21  ConnectFn connectFn, DisconnectFn disconnectFn, const cras::LogHelperPtr& logHelper) :
22  cras::HasLogger(logHelper), connectFn(std::move(connectFn)), disconnectFn(std::move(disconnectFn))
23 {
24 }
25 
27  ConnectFn connectFn, const cras::LogHelperPtr& logHelper) :
28  ConditionalSubscriber(std::move(connectFn), [](ros::Subscriber& sub) { sub.shutdown(); }, logHelper)
29 {
30 }
31 
33 {
34  std::lock_guard<std::mutex> lock(this->connectMutex);
35  if (this->subscribed)
36  this->disconnectNoLock();
37 }
38 
40 {
41  return this->lazy;
42 }
43 
45 {
46  std::lock_guard<std::mutex> lock(this->connectMutex);
47 
48  if (lazy == this->lazy)
49  return;
50 
51  this->lazy = lazy;
52 
53  if (lazy)
54  CRAS_DEBUG("Switching to lazy subscription mode");
55  else
56  CRAS_DEBUG("Switching to non-lazy subscription mode");
57 
58  this->updateSubscriptionNoLock();
59 }
60 
62 {
63  std::lock_guard<std::mutex> lock(this->connectMutex);
64  return this->subscribed;
65 }
66 
68 {
69  std::lock_guard<std::mutex> lock(this->connectMutex);
70  this->updateSubscriptionNoLock();
71 }
72 
74 {
75  const auto shouldBeSubscribed = !this->lazy || this->shouldBeSubscribed();
76  if (this->subscribed && !shouldBeSubscribed)
77  {
78  this->disconnectNoLock();
79  }
80  else if (!this->subscribed && shouldBeSubscribed)
81  {
82  this->connectNoLock();
83  }
84 }
85 
87 {
88  this->connectFn(this->sub);
89  this->subscribed = true;
90  CRAS_DEBUG("Connected to topic " + this->sub.getTopic());
91 }
92 
94 {
95  CRAS_DEBUG("Disconnecting from topic " + this->sub.getTopic());
96  this->disconnectFn(this->sub);
97  this->subscribed = false;
98 }
cras::ConditionalSubscriber::updateSubscription
void updateSubscription()
The callback called when the condition might have changed.
Definition: lazy_subscriber.cpp:67
cras
cras::ConditionalSubscriber::setLazy
void setLazy(bool lazy)
Set whether the subscriber behaves in the lazy way.
Definition: lazy_subscriber.cpp:44
ros
lazy_subscriber.hpp
Lazy subscriber that subscribes only when a paired publisher has subscribers.
cras::ConditionalSubscriber::ConditionalSubscriber
ConditionalSubscriber(ConnectFn connectFn, DisconnectFn disconnectFn, const ::cras::LogHelperPtr &logHelper=::std::make_shared<::cras::NodeLogHelper >())
Create the conditional subscriber.
log_utils.h
cras::ConditionalSubscriber::isSubscribed
bool isSubscribed() const
Whether the subscriber is currently subscribed to its topic or not.
Definition: lazy_subscriber.cpp:61
cras::ConditionalSubscriber::isLazy
bool isLazy() const
Tell whether this subscriber has the lazy behavior enabled.
Definition: lazy_subscriber.cpp:39
cras::HeartbeatNodelet::sub
std::unique_ptr< SubscriberType > sub
The lazy subscriber.
Definition: heartbeat.h:75
cras::ConditionalSubscriber::~ConditionalSubscriber
virtual ~ConditionalSubscriber()
Destroy this object and unsubscribe the subscriber if it was subscribed.
Definition: lazy_subscriber.cpp:32
subscriber.h
cras::ConditionalSubscriber::updateSubscriptionNoLock
void updateSubscriptionNoLock()
The callback called when the condition might have changed.
Definition: lazy_subscriber.cpp:73
cras::LogHelperPtr
::cras::LogHelper::Ptr LogHelperPtr
cras::ConditionalSubscriber::connectNoLock
void connectNoLock()
Connect the subscriber to its input.
Definition: lazy_subscriber.cpp:86
std
CRAS_DEBUG
#define CRAS_DEBUG(...)
cras::ConditionalSubscriber::disconnectNoLock
void disconnectNoLock()
Disconnect the subscriber from its input.
Definition: lazy_subscriber.cpp:93


cras_topic_tools
Author(s): Martin Pecka
autogenerated on Tue Nov 26 2024 03:49:18