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 
38 {
39 public:
41  : std::runtime_error("GraphListener already shutdown") {}
42 };
43 
46 {
47 public:
49  : std::runtime_error("node already added") {}
50 };
51 
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
80 
82 
89  virtual
90  void
92 
94 
100  virtual
101  bool
103 
105 
112  virtual
113  void
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:
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_;
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_
NodeAlreadyAddedError()
Definition: graph_listener.hpp:48
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
GraphListenerShutdownError()
Definition: graph_listener.hpp:40
virtual bool has_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Return true if the given node is in the graph listener&#39;s list of nodes.
Definition: allocator_common.hpp:24
rcl_wait_set_t rcl_get_zero_initialized_wait_set(void)
rcl_guard_condition_t rcl_get_zero_initialized_guard_condition(void)
virtual void run()
Main function for the listening thread.
Notifies many nodes of graph changes by listening in a thread.
Definition: graph_listener.hpp:61
virtual void shutdown()
Stop the listening thread.
Pure virtual interface class for the NodeGraph part of the Node API.
Definition: node_graph_interface.hpp:35
virtual void add_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Add a node to the graph listener&#39;s list of nodes.
Thrown when a function is called on a GraphListener that is already shutdown.
Definition: graph_listener.hpp:37
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
virtual void start_if_not_started()
Start the graph listener&#39;s listen thread if it hasn&#39;t been started.
Thrown when a node has already been added to the GraphListener.
Definition: graph_listener.hpp:45
NodeNotFoundError()
Definition: graph_listener.hpp:56
virtual void remove_node(rclcpp::node_interfaces::NodeGraphInterface *node_graph)
Remove a node from the graph listener&#39;s list of nodes.
virtual bool is_shutdown()
Return true if shutdown() has been called, else false.
Thrown when the given node is not in the GraphListener.
Definition: graph_listener.hpp:53