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/macros.hpp"
29 
30 namespace rclcpp
31 {
32 
33 namespace graph_listener
34 {
35 
37 class GraphListenerShutdownError : public std::runtime_error
38 {
39 public:
41  : std::runtime_error("GraphListener already shutdown") {}
42 };
43 
45 class NodeAlreadyAddedError : public std::runtime_error
46 {
47 public:
49  : std::runtime_error("node already added") {}
50 };
51 
53 class NodeNotFoundError : public std::runtime_error
54 {
55 public:
57  : std::runtime_error("node not found") {}
58 };
59 
61 class GraphListener : public std::enable_shared_from_this<GraphListener>
62 {
63 public:
65  GraphListener();
66 
68  virtual ~GraphListener();
69 
71 
77  virtual
78  void
79  start_if_not_started();
80 
82 
89  virtual
90  void
91  add_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);
92 
94 
100  virtual
101  bool
102  has_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);
103 
105 
112  virtual
113  void
114  remove_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph);
115 
117 
132  virtual
133  void
134  shutdown();
135 
138  virtual
139  bool
140  is_shutdown();
141 
142 protected:
145  virtual
146  void
147  run();
148 
150  virtual
151  void
152  run_loop();
153 
154 private:
155  RCLCPP_DISABLE_COPY(GraphListener)
156 
157  std::thread listener_thread_;
158  bool is_started_;
159  std::atomic_bool is_shutdown_;
160  mutable std::mutex shutdown_mutex_;
161 
162  mutable std::mutex node_graph_interfaces_barrier_mutex_;
163  mutable std::mutex node_graph_interfaces_mutex_;
164  std::vector<rclcpp::node_interfaces::NodeGraphInterface *> node_graph_interfaces_;
165 
167  rcl_guard_condition_t * shutdown_guard_condition_;
169 };
170 
171 } // namespace graph_listener
172 } // namespace rclcpp
173 
174 #endif // RCLCPP__GRAPH_LISTENER_HPP_
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
GraphListenerShutdownError()
Definition: graph_listener.hpp:40
Definition: allocator_common.hpp:24
Definition: parameter.hpp:235
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:61
NodeAlreadyAddedError()
Definition: graph_listener.hpp:48
Thrown when a node has already been added to the GraphListener.
Definition: graph_listener.hpp:45
#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:37
NodeNotFoundError()
Definition: graph_listener.hpp:56
void shutdown()
Notify the signal handler and rmw that rclcpp is shutting down.
Thrown when the given node is not in the GraphListener.
Definition: graph_listener.hpp:53
Pure virtual interface class for the NodeGraph part of the Node API.
Definition: node_graph_interface.hpp:35