ROS 2 rclcpp + rcl - humble  humble
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 
135  RCLCPP_PUBLIC
136  virtual
137  void
138  shutdown();
139 
141  RCLCPP_PUBLIC
142  virtual
143  void
144  shutdown(const std::nothrow_t &) noexcept;
145 
147  RCLCPP_PUBLIC
148  virtual
149  bool
150  is_shutdown();
151 
152 protected:
154  RCLCPP_PUBLIC
155  virtual
156  void
157  run();
158 
159  RCLCPP_PUBLIC
160  virtual
161  void
162  run_loop();
163 
164  RCLCPP_PUBLIC
165  void
166  init_wait_set();
167 
168  RCLCPP_PUBLIC
169  void
170  cleanup_wait_set();
171 
172 private:
173  RCLCPP_DISABLE_COPY(GraphListener)
174 
175 
176  void
177  __shutdown();
178 
179  std::weak_ptr<rclcpp::Context> weak_parent_context_;
180  std::shared_ptr<rcl_context_t> rcl_parent_context_;
181 
182  std::thread listener_thread_;
183  bool is_started_;
184  std::atomic_bool is_shutdown_;
185  mutable std::mutex shutdown_mutex_;
186 
187  mutable std::mutex node_graph_interfaces_barrier_mutex_;
188  mutable std::mutex node_graph_interfaces_mutex_;
189  std::vector<rclcpp::node_interfaces::NodeGraphInterface *> node_graph_interfaces_;
190 
191  rclcpp::GuardCondition interrupt_guard_condition_;
193 };
194 
195 } // namespace graph_listener
196 } // namespace rclcpp
197 
198 #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 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