priority_mux_base.h
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 
12 #include <functional>
13 #include <map>
14 #include <string>
15 #include <unordered_map>
16 #include <utility>
17 
20 #include <ros/duration.h>
21 #include <ros/time.h>
22 
23 namespace cras
24 {
25 
26 namespace priority_mux
27 {
28 
33 {
34  ::std::string name;
35  ::std::string inTopic;
36  ::std::string outTopic;
37  int priority {0};
39  size_t queueSize;
40 };
41 
45 struct LockConfig
46 {
47  ::std::string name;
48  ::std::string topic;
49  int priority {0};
51 };
54 
59 {
60  ::std::string topic;
61  ::cras::optional<bool> forceLatch;
64  size_t numSubscribersToWait {0u};
66  size_t queueSize;
72 };
73 }
74 
84 {
85 public:
92  typedef ::std::function<void(const ::std::string& name, const ::ros::Duration& timeout)> SetTimerFn;
93 
105  PriorityMux(const ::std::unordered_map<::std::string, ::cras::priority_mux::TopicConfig>& topicConfigs,
106  const ::std::unordered_map<::std::string, ::cras::priority_mux::LockConfig>& lockConfigs,
107  const SetTimerFn& setTimerFn, const ::ros::Time& now, const ::cras::LogHelperPtr& log,
108  const ::std::string& noneTopic = "__none", int nonePriority = 0);
109 
110  virtual ~PriorityMux();
111 
120  virtual bool cb(const ::std::string& inTopic, const ::ros::Time& stamp, const ::ros::Time& now);
121 
130  virtual void disableCb(const ::std::string& inTopic, const ::ros::Time& stamp, bool disable, const ::ros::Time& now);
131 
140  virtual void lockCb(const ::std::string& topic, const ::ros::Time& stamp, bool locked, const ::ros::Time& now);
141 
147  virtual void update(const ::ros::Time& now);
148 
154  virtual void reset(const ::ros::Time& now);
155 
161  const ::std::unordered_map<::std::string, ::std::string>& getLastSelectedTopics() const;
162 
168  int getActivePriority() const;
169 
176  bool isDisabled(const ::std::string& inTopic, const ::ros::Time& now);
177 
178 protected:
185  int getHighestLockedPriority(const ::ros::Time& now);
186 
188  ::std::unordered_map<::std::string, ::cras::priority_mux::TopicConfig> topicConfigs;
189 
191  ::std::unordered_map<::std::string, ::cras::priority_mux::LockConfig> lockConfigs;
192 
195 
196  /*
197  * Implementation note: we use the composite key <priority, topic> instead of just <topic> in the maps below.
198  * The reasoning is that in the ordered maps, sorting first by priority and only then by string comparison should be
199  * much faster than sorting just by the strings.
200  */
201 
203  ::std::map<::std::pair<int, ::std::string>, ::ros::Time> lastReceiveStamps;
204 
206  ::std::map<::std::pair<int, ::std::string>, ::ros::Time> disabledStamps;
207 
209  ::std::map<::std::pair<int, ::std::string>, ::ros::Time> lastLockStamps;
210 
212  ::std::map<::std::pair<int, ::std::string>, bool> lockStates;
213 
215  ::std::string noneTopic;
216 
219 
222 
224  ::std::unordered_map<::std::string, ::std::string> lastSelectedTopics;
225 
226 private:
232  void resetImpl(const ::ros::Time& now);
233 };
234 
235 }
optional.hpp
cras::PriorityMux::isDisabled
bool isDisabled(const ::std::string &inTopic, const ::ros::Time &now)
Definition: priority_mux_base.cpp:252
cras::priority_mux::LockConfig::name
::std::string name
The human-readable name of the config.
Definition: priority_mux_base.h:47
cras::priority_mux::LockConfig
Configuration of a lock topic for the mux.
Definition: priority_mux_base.h:45
cras::priority_mux::TopicConfig::priority
int priority
The priority for the topic. Usually is a positive number.
Definition: priority_mux_base.h:37
cras::PriorityMux::cb
virtual bool cb(const ::std::string &inTopic, const ::ros::Time &stamp, const ::ros::Time &now)
Callback function run when input topic message is received.
Definition: priority_mux_base.cpp:45
cras
cras::PriorityMux::resetImpl
void resetImpl(const ::ros::Time &now)
Reset the mux to its original state.
Definition: priority_mux_base.cpp:232
cras::PriorityMux::lockConfigs
::std::unordered_map<::std::string, ::cras::priority_mux::LockConfig > lockConfigs
Configurations of lock topics.
Definition: priority_mux_base.h:191
cras::PriorityMux::noneTopic
::std::string noneTopic
Virtual name of a topic reported as selected when no priority is active.
Definition: priority_mux_base.h:215
cras::PriorityMux::topicConfigs
::std::unordered_map<::std::string, ::cras::priority_mux::TopicConfig > topicConfigs
Configurations of input topics.
Definition: priority_mux_base.h:188
time.h
cras::priority_mux::LockConfig::topic
::std::string topic
The topic associated with the lock.
Definition: priority_mux_base.h:48
cras::priority_mux::OutputTopicConfig::numSubscribersToWait
size_t numSubscribersToWait
Definition: priority_mux_base.h:65
cras::PriorityMux::disableCb
virtual void disableCb(const ::std::string &inTopic, const ::ros::Time &stamp, bool disable, const ::ros::Time &now)
Callback function to disable/enable an input topic.
Definition: priority_mux_base.cpp:80
cras::HasLogger
cras::priority_mux::OutputTopicConfig
Configuration of an output topic for the mux.
Definition: priority_mux_base.h:58
cras::priority_mux::TopicConfig::inTopic
::std::string inTopic
The input topic.
Definition: priority_mux_base.h:35
cras::PriorityMux::getHighestLockedPriority
int getHighestLockedPriority(const ::ros::Time &now)
Gets the highest locked priority.
Definition: priority_mux_base.cpp:197
cras::PriorityMux::lastLockStamps
::std::map<::std::pair< int, ::std::string >, ::ros::Time > lastLockStamps
The timestamps of the last received lock message for each lock topic.
Definition: priority_mux_base.h:209
cras::PriorityMux::lastReceiveStamps
::std::map<::std::pair< int, ::std::string >, ::ros::Time > lastReceiveStamps
The timestamps of the last received message for each input topic.
Definition: priority_mux_base.h:203
cras::priority_mux::OutputTopicConfig::subscriberConnectDelay
::ros::WallDuration subscriberConnectDelay
Definition: priority_mux_base.h:63
cras::PriorityMux
A class for priority-based muxing of topics.
Definition: priority_mux_base.h:83
log_utils.h
cras::PriorityMux::setTimer
SetTimerFn setTimer
The function to call when the mux needs to schedule a call to update() after some time.
Definition: priority_mux_base.h:194
cras::priority_mux::LockConfig::priority
int priority
The priority for the lock. Usually is a positive number.
Definition: priority_mux_base.h:49
duration.h
cras::PriorityMux::PriorityMux
PriorityMux(const ::std::unordered_map<::std::string, ::cras::priority_mux::TopicConfig > &topicConfigs, const ::std::unordered_map<::std::string, ::cras::priority_mux::LockConfig > &lockConfigs, const SetTimerFn &setTimerFn, const ::ros::Time &now, const ::cras::LogHelperPtr &log, const ::std::string &noneTopic="__none", int nonePriority=0)
Constructs a PriorityMux object.
Definition: priority_mux_base.cpp:21
cras::PriorityMux::lastActivePriority
int lastActivePriority
The currently active priority level.
Definition: priority_mux_base.h:221
cras::priority_mux::TopicConfig::name
::std::string name
The human-readable name of the config.
Definition: priority_mux_base.h:34
cras::priority_mux::TopicConfig
Configuration of an input to the mux.
Definition: priority_mux_base.h:32
cras::PriorityMux::getActivePriority
int getActivePriority() const
Return the active priority level.
Definition: priority_mux_base.cpp:222
cras::priority_mux::TopicConfig::timeout
::ros::Duration timeout
How long do messages on this topic keep the priority active.
Definition: priority_mux_base.h:38
cras::PriorityMux::SetTimerFn
::std::function< void(const ::std::string &name, const ::ros::Duration &timeout)> SetTimerFn
The function to call when the mux needs to schedule a call to update() after some time.
Definition: priority_mux_base.h:92
cras::PriorityMux::lockStates
::std::map<::std::pair< int, ::std::string >, bool > lockStates
Current lock status of each lock topic (whether the lock is explicitly locked or not).
Definition: priority_mux_base.h:212
cras::PriorityMux::lockCb
virtual void lockCb(const ::std::string &topic, const ::ros::Time &stamp, bool locked, const ::ros::Time &now)
Callback function run when a lock message is received.
Definition: priority_mux_base.cpp:112
cras::priority_mux::OutputTopicConfig::forceLatch
::cras::optional< bool > forceLatch
Definition: priority_mux_base.h:61
cras::PriorityMux::lastSelectedTopics
::std::unordered_map<::std::string, ::std::string > lastSelectedTopics
The currently selected topic for each output topic.
Definition: priority_mux_base.h:224
ros::Time
cras::PriorityMux::~PriorityMux
virtual ~PriorityMux()
cras::priority_mux::TopicConfig::queueSize
size_t queueSize
Queue size for the subscriber.
Definition: priority_mux_base.h:39
cras::HasLogger::log
::cras::LogHelperPtr log
cras::priority_mux::OutputTopicConfig::topic
::std::string topic
The output topic.
Definition: priority_mux_base.h:60
cras::PriorityMux::reset
virtual void reset(const ::ros::Time &now)
Resets the mux to its initial state.
Definition: priority_mux_base.cpp:227
cras::PriorityMux::disabledStamps
::std::map<::std::pair< int, ::std::string >, ::ros::Time > disabledStamps
The timestamps of the last received disable message for each input topic.
Definition: priority_mux_base.h:206
ros::WallDuration
cras::priority_mux::OutputTopicConfig::queueSize
size_t queueSize
Queue size for the publisher.
Definition: priority_mux_base.h:71
cras::PriorityMux::getLastSelectedTopics
const ::std::unordered_map<::std::string, ::std::string > & getLastSelectedTopics() const
Return the last selected topic for each output topic.
Definition: priority_mux_base.cpp:217
cras::PriorityMux::update
virtual void update(const ::ros::Time &now)
Updates the mux after changes have been made to some of its internal state.
Definition: priority_mux_base.cpp:138
ros::Duration
cras::priority_mux::LockConfig::timeout
::ros::Duration timeout
Definition: priority_mux_base.h:50
cras::priority_mux::TopicConfig::outTopic
::std::string outTopic
The output topic.
Definition: priority_mux_base.h:36
cras::PriorityMux::nonePriority
int nonePriority
Priority level signalling that no priority is active.
Definition: priority_mux_base.h:218


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