diff --git a/rclcpp/include/rclcpp/node_interfaces/node_graph.hpp b/rclcpp/include/rclcpp/node_interfaces/node_graph.hpp index 54b03f3..a8c463b 100644 --- a/rclcpp/include/rclcpp/node_interfaces/node_graph.hpp +++ b/rclcpp/include/rclcpp/node_interfaces/node_graph.hpp @@ -1,4 +1,4 @@ -// Copyright 2016 Open Source Robotics Foundation, Inc. +// Copyright 2016-2017 Open Source Robotics Foundation, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -59,6 +59,11 @@ public: std::map get_topic_names_and_types() const; + RCLCPP_PUBLIC + virtual + std::vector + get_node_names() const; + RCLCPP_PUBLIC virtual size_t diff --git a/rclcpp/include/rclcpp/node_interfaces/node_graph_interface.hpp b/rclcpp/include/rclcpp/node_interfaces/node_graph_interface.hpp index e2fe760..a7f4c0a 100644 --- a/rclcpp/include/rclcpp/node_interfaces/node_graph_interface.hpp +++ b/rclcpp/include/rclcpp/node_interfaces/node_graph_interface.hpp @@ -1,4 +1,4 @@ -// Copyright 2016 Open Source Robotics Foundation, Inc. +// Copyright 2016-2017 Open Source Robotics Foundation, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ #include #include #include +#include #include "rcl/guard_condition.h" @@ -46,6 +47,12 @@ public: std::map get_topic_names_and_types() const = 0; + /// Return a vector of existing node names (string). + RCLCPP_PUBLIC + virtual + std::vector + get_node_names() const = 0; + /// Return the number of publishers that are advertised on a given topic. RCLCPP_PUBLIC virtual diff --git a/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp b/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp index 693ab49..567c919 100644 --- a/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp +++ b/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp @@ -16,6 +16,7 @@ #include #include +#include #include "rcl/graph.h" #include "rclcpp/exceptions.hpp" @@ -75,6 +76,37 @@ NodeGraph::get_topic_names_and_types() const return topics; } +std::vector +NodeGraph::get_node_names() const +{ + rcl_string_array_t node_names_c = + rcl_get_zero_initialized_string_array(); + + auto ret = rcl_get_node_names(node_base_->get_rcl_node_handle(), + &node_names_c); + if (ret != RMW_RET_OK) { + if (rmw_destroy_node_names(&node_names_c) != RMW_RET_OK) { + RMW_SET_ERROR_MSG("Fatal: Leaking node_name memory."); + } + // *INDENT-OFF* + throw std::runtime_error( + std::string("could not get node names: ") + rmw_get_error_string_safe()); + // *INDENT-ON* + } + + std::vector node_names(&node_names_c.data[0], + &node_names_c.data[0 + node_names_c.size]); + ret = rmw_destroy_node_names(&node_names_c); + if (ret != RMW_RET_OK) { + // *INDENT-OFF* + throw std::runtime_error( + std::string("could not destroy node names: ") + rmw_get_error_string_safe()); + // *INDENT-ON* + } + + return node_names; +} + size_t NodeGraph::count_publishers(const std::string & topic_name) const {