ROS 2 rclcpp + rcl - lyrical  lyrical
ROS 2 C++ Client Library with ROS Client Library
node_graph_interface.hpp
1 // Copyright 2016-2017 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__NODE_INTERFACES__NODE_GRAPH_INTERFACE_HPP_
16 #define RCLCPP__NODE_INTERFACES__NODE_GRAPH_INTERFACE_HPP_
17 
18 #include <algorithm>
19 #include <array>
20 #include <chrono>
21 #include <cstddef>
22 #include <cstdint>
23 #include <map>
24 #include <string>
25 #include <tuple>
26 #include <utility>
27 #include <vector>
28 
29 #include "rcl/graph.h"
30 #include "rcl/guard_condition.h"
31 
32 #include "rclcpp/event.hpp"
33 #include "rclcpp/macros.hpp"
34 #include "rclcpp/node_interfaces/detail/node_interfaces_helpers.hpp"
35 #include "rclcpp/qos.hpp"
36 #include "rclcpp/visibility_control.hpp"
37 
38 namespace rclcpp
39 {
40 
41 enum class EndpointType
42 {
43  Invalid = RMW_ENDPOINT_INVALID,
44  Publisher = RMW_ENDPOINT_PUBLISHER,
45  Subscription = RMW_ENDPOINT_SUBSCRIPTION,
46  Client = RMW_ENDPOINT_CLIENT,
47  Server = RMW_ENDPOINT_SERVER
48 };
49 
55 {
56 public:
58  RCLCPP_PUBLIC
60  : endpoint_type_(static_cast<rclcpp::EndpointType>(info.endpoint_type)),
61  qos_profile_({info.qos_profile.history, info.qos_profile.depth}, info.qos_profile),
62  topic_type_hash_(info.topic_type_hash)
63  {
64  if (!info.node_name || !info.node_namespace || !info.topic_type) {
65  throw std::invalid_argument("Constructor TopicEndpointInfo with invalid topic endpoint info");
66  }
67  node_name_ = info.node_name;
68  node_namespace_ = info.node_namespace;
69  topic_type_ = info.topic_type;
70 
71  std::copy(info.endpoint_gid, info.endpoint_gid + RMW_GID_STORAGE_SIZE, endpoint_gid_.begin());
72  }
73 
75  RCLCPP_PUBLIC
76  std::string &
77  node_name();
78 
80  RCLCPP_PUBLIC
81  const std::string &
82  node_name() const;
83 
85  RCLCPP_PUBLIC
86  std::string &
88 
90  RCLCPP_PUBLIC
91  const std::string &
92  node_namespace() const;
93 
95  RCLCPP_PUBLIC
96  std::string &
97  topic_type();
98 
100  RCLCPP_PUBLIC
101  const std::string &
102  topic_type() const;
103 
105  RCLCPP_PUBLIC
106  rclcpp::EndpointType &
107  endpoint_type();
108 
110  RCLCPP_PUBLIC
111  const rclcpp::EndpointType &
112  endpoint_type() const;
113 
115  RCLCPP_PUBLIC
116  std::array<uint8_t, RMW_GID_STORAGE_SIZE> &
117  endpoint_gid();
118 
120  RCLCPP_PUBLIC
121  const std::array<uint8_t, RMW_GID_STORAGE_SIZE> &
122  endpoint_gid() const;
123 
125  RCLCPP_PUBLIC
126  rclcpp::QoS &
127  qos_profile();
128 
130  RCLCPP_PUBLIC
131  const rclcpp::QoS &
132  qos_profile() const;
133 
135  RCLCPP_PUBLIC
136  rosidl_type_hash_t &
137  topic_type_hash();
138 
140  RCLCPP_PUBLIC
141  const rosidl_type_hash_t &
142  topic_type_hash() const;
143 
144 private:
145  std::string node_name_;
146  std::string node_namespace_;
147  std::string topic_type_;
148  rclcpp::EndpointType endpoint_type_;
149  std::array<uint8_t, RMW_GID_STORAGE_SIZE> endpoint_gid_;
150  rclcpp::QoS qos_profile_;
151  rosidl_type_hash_t topic_type_hash_;
152 };
153 
159 {
160 public:
162  RCLCPP_PUBLIC
164  : node_name_(info.node_name),
165  node_namespace_(info.node_namespace),
166  service_type_(info.service_type),
167  endpoint_type_(static_cast<rclcpp::EndpointType>(info.endpoint_type)),
168  service_type_hash_(info.service_type_hash),
169  endpoint_count_(info.endpoint_count)
170  {
171  for(size_t i = 0; i < endpoint_count_; i++) {
172  std::array<uint8_t, RMW_GID_STORAGE_SIZE> gid;
173  std::copy(info.endpoint_gids[i], info.endpoint_gids[i] + RMW_GID_STORAGE_SIZE, gid.begin());
174  endpoint_gids_.push_back(gid);
175 
176  rclcpp::QoS qos(
177  {info.qos_profiles[i].history, info.qos_profiles[i].depth}, info.qos_profiles[i]);
178  qos_profiles_.push_back(qos);
179  }
180  }
181 
183  RCLCPP_PUBLIC
184  std::string &
185  node_name();
186 
188  RCLCPP_PUBLIC
189  const std::string &
190  node_name() const;
191 
193  RCLCPP_PUBLIC
194  std::string &
195  node_namespace();
196 
198  RCLCPP_PUBLIC
199  const std::string &
200  node_namespace() const;
201 
203  RCLCPP_PUBLIC
204  std::string &
205  service_type();
206 
208  RCLCPP_PUBLIC
209  const std::string &
210  service_type() const;
211 
213  RCLCPP_PUBLIC
214  rclcpp::EndpointType &
215  endpoint_type();
216 
218  RCLCPP_PUBLIC
219  const rclcpp::EndpointType &
220  endpoint_type() const;
221 
223  RCLCPP_PUBLIC
224  size_t &
225  endpoint_count();
226 
228  RCLCPP_PUBLIC
229  const size_t &
230  endpoint_count() const;
231 
233  RCLCPP_PUBLIC
234  std::vector<std::array<uint8_t, RMW_GID_STORAGE_SIZE>> &
235  endpoint_gids();
236 
238  RCLCPP_PUBLIC
239  const std::vector<std::array<uint8_t, RMW_GID_STORAGE_SIZE>> &
240  endpoint_gids() const;
241 
243  RCLCPP_PUBLIC
244  std::vector<rclcpp::QoS> &
245  qos_profiles();
246 
248  RCLCPP_PUBLIC
249  const std::vector<rclcpp::QoS> &
250  qos_profiles() const;
251 
253  RCLCPP_PUBLIC
254  rosidl_type_hash_t &
256 
258  RCLCPP_PUBLIC
259  const rosidl_type_hash_t &
260  service_type_hash() const;
261 
262 private:
263  std::string node_name_;
264  std::string node_namespace_;
265  std::string service_type_;
266  rclcpp::EndpointType endpoint_type_;
267  std::vector<std::array<uint8_t, RMW_GID_STORAGE_SIZE>> endpoint_gids_;
268  std::vector<rclcpp::QoS> qos_profiles_;
269  rosidl_type_hash_t service_type_hash_;
270  size_t endpoint_count_;
271 };
272 
273 namespace node_interfaces
274 {
275 
278 {
279 public:
280  RCLCPP_SMART_PTR_ALIASES_ONLY(NodeGraphInterface)
281 
282  RCLCPP_PUBLIC
283  virtual
284  ~NodeGraphInterface() = default;
285 
287 
296  RCLCPP_PUBLIC
297  virtual
298  std::map<std::string, std::vector<std::string>>
299  get_topic_names_and_types(bool no_demangle = false) const = 0;
300 
302 
310  RCLCPP_PUBLIC
311  virtual
312  std::map<std::string, std::vector<std::string>>
314 
316 
325  RCLCPP_PUBLIC
326  virtual
327  std::map<std::string, std::vector<std::string>>
329  const std::string & node_name,
330  const std::string & namespace_) const = 0;
331 
333 
342  RCLCPP_PUBLIC
343  virtual
344  std::map<std::string, std::vector<std::string>>
346  const std::string & node_name,
347  const std::string & namespace_) const = 0;
348 
350 
360  RCLCPP_PUBLIC
361  virtual
362  std::map<std::string, std::vector<std::string>>
364  const std::string & node_name,
365  const std::string & namespace_,
366  bool no_demangle = false) const = 0;
367 
369 
379  RCLCPP_PUBLIC
380  virtual
381  std::map<std::string, std::vector<std::string>>
383  const std::string & node_name,
384  const std::string & namespace_,
385  bool no_demangle = false) const = 0;
386 
388  /*
389  * The returned names are the actual names after remap rules applied.
390  */
391  RCLCPP_PUBLIC
392  virtual
393  std::vector<std::string>
394  get_node_names() const = 0;
395 
397  /*
398  * The returned names are the actual names after remap rules applied.
399  * The enclaves contain the runtime security artifacts, those can be
400  * used to establish secured network.
401  * See https://design.ros2.org/articles/ros2_security_enclaves.html
402  */
403  RCLCPP_PUBLIC
404  virtual
405  std::vector<std::tuple<std::string, std::string, std::string>>
407 
409  /*
410  * The returned names are the actual names after remap rules applied.
411  */
412  RCLCPP_PUBLIC
413  virtual
414  std::vector<std::pair<std::string, std::string>>
416 
418  /*
419  * \param[in] topic_name the actual topic name used; it will not be automatically remapped.
420  */
421  RCLCPP_PUBLIC
422  virtual
423  size_t
424  count_publishers(const std::string & topic_name) const = 0;
425 
427  /*
428  * \param[in] topic_name the actual topic name used; it will not be automatically remapped.
429  */
430  RCLCPP_PUBLIC
431  virtual
432  size_t
433  count_subscribers(const std::string & topic_name) const = 0;
434 
436  /*
437  * \param[in] service_name the actual service name used; it will not be automatically remapped.
438  */
439  RCLCPP_PUBLIC
440  virtual
441  size_t
442  count_clients(const std::string & service_name) const = 0;
443 
445  /*
446  * \param[in] service_name the actual service name used; it will not be automatically remapped.
447  */
448  RCLCPP_PUBLIC
449  virtual
450  size_t
451  count_services(const std::string & service_name) const = 0;
452 
454  RCLCPP_PUBLIC
455  virtual
456  const rcl_guard_condition_t *
458 
460 
469  RCLCPP_PUBLIC
470  virtual
471  void
473 
475  RCLCPP_PUBLIC
476  virtual
477  void
479 
481 
486  RCLCPP_PUBLIC
487  virtual
488  rclcpp::Event::SharedPtr
490 
492 
499  RCLCPP_PUBLIC
500  virtual
501  void
503  rclcpp::Event::SharedPtr event,
504  std::chrono::nanoseconds timeout) = 0;
505 
507 
510  RCLCPP_PUBLIC
511  virtual
512  size_t
513  count_graph_users() const = 0;
514 
516 
522  RCLCPP_PUBLIC
523  virtual
524  std::vector<rclcpp::TopicEndpointInfo>
525  get_publishers_info_by_topic(const std::string & topic_name, bool no_mangle = false) const = 0;
526 
528 
534  RCLCPP_PUBLIC
535  virtual
536  std::vector<rclcpp::TopicEndpointInfo>
537  get_subscriptions_info_by_topic(const std::string & topic_name, bool no_mangle = false) const = 0;
538 
540 
546  RCLCPP_PUBLIC
547  virtual
548  std::vector<rclcpp::ServiceEndpointInfo>
549  get_clients_info_by_service(const std::string & service_name, bool no_mangle = false) const = 0;
550 
552 
558  RCLCPP_PUBLIC
559  virtual
560  std::vector<rclcpp::ServiceEndpointInfo>
561  get_servers_info_by_service(const std::string & service_name, bool no_mangle = false) const = 0;
562 };
563 
564 } // namespace node_interfaces
565 } // namespace rclcpp
566 
567 RCLCPP_NODE_INTERFACE_HELPERS_SUPPORT(rclcpp::node_interfaces::NodeGraphInterface, graph)
568 
569 #endif // RCLCPP__NODE_INTERFACES__NODE_GRAPH_INTERFACE_HPP_
Encapsulation of Quality of Service settings.
Definition: qos.hpp:116
RCLCPP_PUBLIC rclcpp::EndpointType & endpoint_type()
Get a mutable reference to the service endpoint type.
Definition: node_graph.cpp:996
RCLCPP_PUBLIC std::string & node_name()
Get a mutable reference to the node name.
Definition: node_graph.cpp:960
RCLCPP_PUBLIC size_t & endpoint_count()
Get a mutable reference to the endpoint count.
RCLCPP_PUBLIC rosidl_type_hash_t & service_type_hash()
Get a mutable reference to the type hash of the service endpoint.
RCLCPP_PUBLIC ServiceEndpointInfo(const rcl_service_endpoint_info_t &info)
Construct a ServiceEndpointInfo from a rcl_service_endpoint_info_t.
RCLCPP_PUBLIC std::vector< rclcpp::QoS > & qos_profiles()
Get a mutable reference to the QoS profile of the service endpoint.
RCLCPP_PUBLIC std::string & node_namespace()
Get a mutable reference to the node namespace.
Definition: node_graph.cpp:972
RCLCPP_PUBLIC std::vector< std::array< uint8_t, RMW_GID_STORAGE_SIZE > > & endpoint_gids()
Get a mutable reference to the GID of the service endpoint.
RCLCPP_PUBLIC std::string & service_type()
Get a mutable reference to the service type string.
Definition: node_graph.cpp:984
RCLCPP_PUBLIC TopicEndpointInfo(const rcl_topic_endpoint_info_t &info)
Construct a TopicEndpointInfo from a rcl_topic_endpoint_info_t.
RCLCPP_PUBLIC std::string & node_name()
Get a mutable reference to the node name.
Definition: node_graph.cpp:876
RCLCPP_PUBLIC std::string & node_namespace()
Get a mutable reference to the node namespace.
Definition: node_graph.cpp:888
RCLCPP_PUBLIC rosidl_type_hash_t & topic_type_hash()
Get a mutable reference to the type hash of the topic endpoint.
Definition: node_graph.cpp:948
RCLCPP_PUBLIC std::string & topic_type()
Get a mutable reference to the topic type string.
Definition: node_graph.cpp:900
RCLCPP_PUBLIC rclcpp::QoS & qos_profile()
Get a mutable reference to the QoS profile of the topic endpoint.
Definition: node_graph.cpp:936
RCLCPP_PUBLIC rclcpp::EndpointType & endpoint_type()
Get a mutable reference to the topic endpoint type.
Definition: node_graph.cpp:912
RCLCPP_PUBLIC std::array< uint8_t, RMW_GID_STORAGE_SIZE > & endpoint_gid()
Get a mutable reference to the GID of the topic endpoint.
Definition: node_graph.cpp:924
Pure virtual interface class for the NodeGraph part of the Node API.
virtual RCLCPP_PUBLIC rclcpp::Event::SharedPtr get_graph_event()=0
Return a graph event, which will be set anytime a graph change occurs.
virtual RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_client_names_and_types_by_node(const std::string &node_name, const std::string &namespace_) const =0
Return a map of existing service names and types with a specific node.
virtual RCLCPP_PUBLIC size_t count_clients(const std::string &service_name) const =0
Return the number of clients created for a given service.
virtual RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_subscriber_names_and_types_by_node(const std::string &node_name, const std::string &namespace_, bool no_demangle=false) const =0
Return a map of existing topic names to list of topic types for a specific node.
virtual RCLCPP_PUBLIC std::vector< std::pair< std::string, std::string > > get_node_names_and_namespaces() const =0
Return a vector of existing node names and namespaces (pair of string).
virtual RCLCPP_PUBLIC std::vector< rclcpp::TopicEndpointInfo > get_subscriptions_info_by_topic(const std::string &topic_name, bool no_mangle=false) const =0
Return the topic endpoint information about subscriptions on a given topic.
virtual RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_publisher_names_and_types_by_node(const std::string &node_name, const std::string &namespace_, bool no_demangle=false) const =0
Return a map of existing topic names to list of topic types for a specific node.
virtual RCLCPP_PUBLIC void notify_shutdown()=0
Notify any and all blocking node actions that shutdown has occurred.
virtual RCLCPP_PUBLIC std::vector< std::tuple< std::string, std::string, std::string > > get_node_names_with_enclaves() const =0
Return a vector of existing node names, namespaces and enclaves (tuple of string).
virtual RCLCPP_PUBLIC size_t count_subscribers(const std::string &topic_name) const =0
Return the number of subscribers who have created a subscription for a given topic.
virtual RCLCPP_PUBLIC size_t count_services(const std::string &service_name) const =0
Return the number of services created for a given service.
virtual RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_service_names_and_types_by_node(const std::string &node_name, const std::string &namespace_) const =0
Return a map of existing service names to list of service types for a specific node.
virtual RCLCPP_PUBLIC std::vector< rclcpp::TopicEndpointInfo > get_publishers_info_by_topic(const std::string &topic_name, bool no_mangle=false) const =0
Return the topic endpoint information about publishers on a given topic.
virtual RCLCPP_PUBLIC void notify_graph_change()=0
Notify threads waiting on graph changes.
virtual RCLCPP_PUBLIC std::vector< rclcpp::ServiceEndpointInfo > get_clients_info_by_service(const std::string &service_name, bool no_mangle=false) const =0
Return the service endpoint information about clients on a given service.
virtual RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_service_names_and_types() const =0
Return a map of existing service names to list of service types.
virtual RCLCPP_PUBLIC std::vector< std::string > get_node_names() const =0
Return a vector of existing node names (string).
virtual RCLCPP_PUBLIC size_t count_graph_users() const =0
Return the number of on loan graph events, see get_graph_event().
virtual RCLCPP_PUBLIC std::vector< rclcpp::ServiceEndpointInfo > get_servers_info_by_service(const std::string &service_name, bool no_mangle=false) const =0
Return the service endpoint information about servers on a given service.
virtual RCLCPP_PUBLIC void wait_for_graph_change(rclcpp::Event::SharedPtr event, std::chrono::nanoseconds timeout)=0
Wait for a graph event to occur by waiting on an Event to become set.
virtual RCLCPP_PUBLIC std::map< std::string, std::vector< std::string > > get_topic_names_and_types(bool no_demangle=false) const =0
Return a map of existing topic names to list of topic types.
virtual RCLCPP_PUBLIC const rcl_guard_condition_t * get_graph_guard_condition() const =0
Return the rcl guard condition which is triggered when the ROS graph changes.
virtual RCLCPP_PUBLIC size_t count_publishers(const std::string &topic_name) const =0
Return the number of publishers that are advertised on a given topic.
rmw_topic_endpoint_info_t rcl_topic_endpoint_info_t
Definition: graph.h:48
rmw_service_endpoint_info_t rcl_service_endpoint_info_t
Definition: graph.h:56
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Handle for a rcl guard condition.