rclcpp  master
C++ ROS Client Library API
graph_listener.hpp
Go to the documentation of this file.
1 // Copyright 2016 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__GRAPH_LISTENER_HPP_
16 #define RCLCPP__GRAPH_LISTENER_HPP_
17 
18 #include <atomic>
19 #include <memory>
20 #include <mutex>
21 #include <thread>
22 #include <vector>
23 
24 #include "rcl/guard_condition.h"
25 #include "rcl/wait.h"
26 #include "rclcpp/context.hpp"
27 #include "rclcpp/macros.hpp"
30 
31 namespace rclcpp
32 {
33 
34 namespace graph_listener
35 {
36 
39 {
40 public:
42  : std::runtime_error("GraphListener already shutdown") {}
43 };
44 
47 {
48 public:
50  : std::runtime_error("node already added") {}
51 };
52 
55 {
56 public:
58  : std::runtime_error("node not found") {}
59 };
60 
62 class GraphListener : public std::enable_shared_from_this<GraphListener>
63 {
64 public:
66  explicit GraphListener(const rclcpp::Context::SharedPtr & parent_context);
67 
69  virtual ~GraphListener();
70 
72 
80  virtual
81  void
83 
85 
92  virtual
93  void
95 
97 
103  virtual
104  bool
106 
108 
115  virtual
116  void
118 
120 
135  virtual
136  void
137  shutdown();
138 
141  virtual
142  void
143  shutdown(const std::nothrow_t &) noexcept;
144 
147  virtual
148  bool
149  is_shutdown();
150 
151 protected:
154  virtual
155  void
156  run();
157 
159  virtual
160  void
161  run_loop();
162 
164  void
165  init_wait_set();
166 
168  void
170 
171 private:
173 
174 
175  void
176  __shutdown();
177 
178  std::weak_ptr<rclcpp::Context> weak_parent_context_;
179  std::shared_ptr<rcl_context_t> rcl_parent_context_;
180 
181  std::thread listener_thread_;
182  bool is_started_;
183  std::atomic_bool is_shutdown_;
184  mutable std::mutex shutdown_mutex_;
185 
186  mutable std::mutex node_graph_interfaces_barrier_mutex_;
187  mutable std::mutex node_graph_interfaces_mutex_;
189 
192 };
193 
194 } // namespace graph_listener
195 } // namespace rclcpp
196 
197 #endif // RCLCPP__GRAPH_LISTENER_HPP_
node_graph_interface.hpp
std::shared_ptr< rcl_context_t >
rclcpp::graph_listener::GraphListener::start_if_not_started
virtual void start_if_not_started()
Start the graph listener's listen thread if it hasn't been started.
guard_condition.h
rclcpp::graph_listener::GraphListener::~GraphListener
virtual ~GraphListener()
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
rclcpp::graph_listener::GraphListener::GraphListener
GraphListener(const rclcpp::Context::SharedPtr &parent_context)
std::vector< rclcpp::node_interfaces::NodeGraphInterface * >
rclcpp::graph_listener::NodeAlreadyAddedError::NodeAlreadyAddedError
NodeAlreadyAddedError()
Definition: graph_listener.hpp:49
wait.h
rclcpp::graph_listener::GraphListener::shutdown
virtual void shutdown()
Stop the listening thread.
context.hpp
rclcpp::graph_listener::NodeNotFoundError::NodeNotFoundError
NodeNotFoundError()
Definition: graph_listener.hpp:57
rcl_guard_condition_t
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::graph_listener::GraphListener::add_node
virtual void add_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Add a node to the graph listener's list of nodes.
rclcpp::graph_listener::GraphListener::cleanup_wait_set
void cleanup_wait_set()
macros.hpp
std::thread
rclcpp::graph_listener::GraphListener::has_node
virtual bool has_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Return true if the given node is in the graph listener's list of nodes.
rcl_get_zero_initialized_guard_condition
rcl_guard_condition_t rcl_get_zero_initialized_guard_condition(void)
rclcpp::graph_listener::NodeAlreadyAddedError
Thrown when a node has already been added to the GraphListener.
Definition: graph_listener.hpp:46
std::enable_shared_from_this
rcl_wait_set_t
std::runtime_error
rclcpp::graph_listener::GraphListener
Notifies many nodes of graph changes by listening in a thread.
Definition: graph_listener.hpp:62
rcl_get_zero_initialized_wait_set
rcl_wait_set_t rcl_get_zero_initialized_wait_set(void)
std::weak_ptr< rclcpp::Context >
rclcpp::graph_listener::GraphListener::remove_node
virtual void remove_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Remove a node from the graph listener's list of nodes.
rclcpp::graph_listener::GraphListener::run
virtual void run()
Main function for the listening thread.
visibility_control.hpp
std
std::nothrow_t
rclcpp::graph_listener::GraphListenerShutdownError::GraphListenerShutdownError
GraphListenerShutdownError()
Definition: graph_listener.hpp:41
rclcpp::node_interfaces::NodeGraphInterface
Pure virtual interface class for the NodeGraph part of the Node API.
Definition: node_graph_interface.hpp:136
std::mutex
rclcpp::graph_listener::GraphListener::is_shutdown
virtual bool is_shutdown()
Return true if shutdown() has been called, else false.
rclcpp::graph_listener::NodeNotFoundError
Thrown when the given node is not in the GraphListener.
Definition: graph_listener.hpp:54
rclcpp::graph_listener::GraphListenerShutdownError
Thrown when a function is called on a GraphListener that is already shutdown.
Definition: graph_listener.hpp:38
rclcpp::graph_listener::GraphListener::run_loop
virtual void run_loop()
rclcpp::graph_listener::GraphListener::init_wait_set
void init_wait_set()