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 }
ConditionalSubscriber(ConnectFn connectFn, DisconnectFn disconnectFn, const ::cras::LogHelperPtr &logHelper=::std::make_shared<::cras::NodeLogHelper >())
Create the conditional subscriber.
void connectNoLock()
Connect the subscriber to its input.
#define CRAS_DEBUG(...)
void updateSubscriptionNoLock()
The callback called when the condition might have changed.
bool isSubscribed() const
Whether the subscriber is currently subscribed to its topic or not.
std::unique_ptr< SubscriberType > sub
The lazy subscriber.
Definition: heartbeat.h:75
void disconnectNoLock()
Disconnect the subscriber from its input.
virtual ~ConditionalSubscriber()
Destroy this object and unsubscribe the subscriber if it was subscribed.
void setLazy(bool lazy)
Set whether the subscriber behaves in the lazy way.
Lazy subscriber that subscribes only when a paired publisher has subscribers.
bool isLazy() const
Tell whether this subscriber has the lazy behavior enabled.
::cras::LogHelper::Ptr LogHelperPtr
void updateSubscription()
The callback called when the condition might have changed.


cras_topic_tools
Author(s): Martin Pecka
autogenerated on Sat Jun 17 2023 02:33:13