ROS 2 rclcpp + rcl - rolling  rolling-4d14414d
ROS 2 C++ Client Library with ROS Client Library
graph_listener.hpp
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/guard_condition.hpp"
28 #include "rclcpp/macros.hpp"
29 #include "rclcpp/node_interfaces/node_graph_interface.hpp"
30 #include "rclcpp/visibility_control.hpp"
31 
32 namespace rclcpp
33 {
34 
35 namespace graph_listener
36 {
37 
39 class GraphListenerShutdownError : public std::runtime_error
40 {
41 public:
43  : std::runtime_error("GraphListener already shutdown") {}
44 };
45 
47 class NodeAlreadyAddedError : public std::runtime_error
48 {
49 public:
51  : std::runtime_error("node already added") {}
52 };
53 
55 class NodeNotFoundError : public std::runtime_error
56 {
57 public:
59  : std::runtime_error("node not found") {}
60 };
61 
63 class GraphListener : public std::enable_shared_from_this<GraphListener>
64 {
65 public:
66  RCLCPP_PUBLIC
67  explicit GraphListener(const rclcpp::Context::SharedPtr & parent_context);
68 
69  RCLCPP_PUBLIC
70  virtual ~GraphListener();
71 
73 
80  RCLCPP_PUBLIC
81  virtual
82  void
84 
86 
92  RCLCPP_PUBLIC
93  virtual
94  void
96 
98 
103  RCLCPP_PUBLIC
104  virtual
105  bool
107 
109 
115  RCLCPP_PUBLIC
116  virtual
117  void
119 
121 
140  RCLCPP_PUBLIC
141  virtual
142  void
143  shutdown();
144 
146  RCLCPP_PUBLIC
147  virtual
148  void
149  shutdown(const std::nothrow_t &) noexcept;
150 
152  RCLCPP_PUBLIC
153  virtual
154  bool
155  is_shutdown();
156 
158  RCLCPP_PUBLIC
159  virtual
160  bool
161  is_started();
162 
163 protected:
165  RCLCPP_PUBLIC
166  virtual
167  void
168  run();
169 
170  RCLCPP_PUBLIC
171  virtual
172  void
173  run_loop();
174 
175  RCLCPP_PUBLIC
176  void
177  init_wait_set();
178 
179  RCLCPP_PUBLIC
180  void
181  cleanup_wait_set();
182 
183 private:
184  RCLCPP_DISABLE_COPY(GraphListener)
185 
186 
187  void
188  __shutdown();
189 
190  std::shared_ptr<rcl_context_t> rcl_parent_context_;
191 
192  std::thread listener_thread_;
193  bool is_started_;
194  std::atomic_bool is_shutdown_;
195  mutable std::mutex shutdown_mutex_;
196 
197  mutable std::mutex node_graph_interfaces_barrier_mutex_;
198  mutable std::mutex node_graph_interfaces_mutex_;
199  std::vector<rclcpp::node_interfaces::NodeGraphInterface *> node_graph_interfaces_;
200 
201  rclcpp::GuardCondition interrupt_guard_condition_;
203 };
204 
205 } // namespace graph_listener
206 } // namespace rclcpp
207 
208 #endif // RCLCPP__GRAPH_LISTENER_HPP_
A condition that can be waited on in a single wait set and asynchronously triggered.
Thrown when a function is called on a GraphListener that is already shutdown.
Notifies many nodes of graph changes by listening in a thread.
virtual RCLCPP_PUBLIC void start_if_not_started()
Start the graph listener's listen thread if it hasn't been started.
virtual RCLCPP_PUBLIC void run()
Main function for the listening thread.
virtual RCLCPP_PUBLIC void shutdown()
Stop the listening thread.
virtual RCLCPP_PUBLIC bool is_shutdown()
Return true if shutdown() has been called, else false.
virtual RCLCPP_PUBLIC bool has_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Return true if the given node is in the graph listener's list of nodes.
virtual RCLCPP_PUBLIC void add_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Add a node to the graph listener's list of nodes.
virtual RCLCPP_PUBLIC bool is_started()
Return true if the graph listener was started.
virtual RCLCPP_PUBLIC void remove_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Remove a node from the graph listener's list of nodes.
Thrown when a node has already been added to the GraphListener.
Thrown when the given node is not in the GraphListener.
Pure virtual interface class for the NodeGraph part of the Node API.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Container for subscription's, guard condition's, etc to be waited on.
Definition: wait.h:42
RCL_PUBLIC RCL_WARN_UNUSED rcl_wait_set_t rcl_get_zero_initialized_wait_set(void)
Return a rcl_wait_set_t struct with members set to NULL.
Definition: wait.c:64