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(std::shared_ptr<rclcpp::Context> parent_context);
67 
69  virtual ~GraphListener();
70 
72 
78  virtual
79  void
80  start_if_not_started();
81 
83 
90  virtual
91  void
92  add_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);
93 
95 
101  virtual
102  bool
103  has_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);
104 
106 
113  virtual
114  void
115  remove_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);
116 
118 
133  virtual
134  void
135  shutdown();
136 
139  virtual
140  void
141  shutdown(const std::nothrow_t &) noexcept;
142 
145  virtual
146  bool
147  is_shutdown();
148 
149 protected:
152  virtual
153  void
154  run();
155 
157  virtual
158  void
159  run_loop();
160 
161 private:
162  RCLCPP_DISABLE_COPY(GraphListener)
163 
164 
165  void
166  __shutdown(bool);
167 
168  rclcpp::Context::SharedPtr parent_context_;
169 
170  std::thread listener_thread_;
171  bool is_started_;
172  std::atomic_bool is_shutdown_;
173  mutable std::mutex shutdown_mutex_;
174 
175  mutable std::mutex node_graph_interfaces_barrier_mutex_;
176  mutable std::mutex node_graph_interfaces_mutex_;
178 
180  std::shared_ptr<rcl_context_t> interrupt_guard_condition_context_;
181  rcl_guard_condition_t * shutdown_guard_condition_;
183 };
184 
185 } // namespace graph_listener
186 } // namespace rclcpp
187 
188 #endif // RCLCPP__GRAPH_LISTENER_HPP_
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
GraphListenerShutdownError()
Definition: graph_listener.hpp:41
Definition: allocator_common.hpp:24
bool shutdown(rclcpp::Context::SharedPtr context=nullptr, const std::string &reason="user called rclcpp::shutdown()")
Shutdown rclcpp context, invalidating it for derived entities.
rcl_wait_set_t rcl_get_zero_initialized_wait_set(void)
rcl_guard_condition_t rcl_get_zero_initialized_guard_condition(void)
Notifies many nodes of graph changes by listening in a thread.
Definition: graph_listener.hpp:62
NodeAlreadyAddedError()
Definition: graph_listener.hpp:49
Thrown when a node has already been added to the GraphListener.
Definition: graph_listener.hpp:46
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Thrown when a function is called on a GraphListener that is already shutdown.
Definition: graph_listener.hpp:38
NodeNotFoundError()
Definition: graph_listener.hpp:57
Thrown when the given node is not in the GraphListener.
Definition: graph_listener.hpp:54
Pure virtual interface class for the NodeGraph part of the Node API.
Definition: node_graph_interface.hpp:36