ROS 2 rclcpp + rcl - kilted  kilted
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 
157 protected:
159  RCLCPP_PUBLIC
160  virtual
161  void
162  run();
163 
164  RCLCPP_PUBLIC
165  virtual
166  void
167  run_loop();
168 
169  RCLCPP_PUBLIC
170  void
171  init_wait_set();
172 
173  RCLCPP_PUBLIC
174  void
175  cleanup_wait_set();
176 
177 private:
178  RCLCPP_DISABLE_COPY(GraphListener)
179 
180 
181  void
182  __shutdown();
183 
184  std::weak_ptr<rclcpp::Context> weak_parent_context_;
185  std::shared_ptr<rcl_context_t> rcl_parent_context_;
186 
187  std::thread listener_thread_;
188  bool is_started_;
189  std::atomic_bool is_shutdown_;
190  mutable std::mutex shutdown_mutex_;
191 
192  mutable std::mutex node_graph_interfaces_barrier_mutex_;
193  mutable std::mutex node_graph_interfaces_mutex_;
194  std::vector<rclcpp::node_interfaces::NodeGraphInterface *> node_graph_interfaces_;
195 
196  rclcpp::GuardCondition interrupt_guard_condition_;
198 };
199 
200 } // namespace graph_listener
201 } // namespace rclcpp
202 
203 #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