Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NodeGraph exposure with test cases #1484

Merged
merged 13 commits into from
Sep 23, 2021
Merged
25 changes: 25 additions & 0 deletions rclcpp/include/rclcpp/node_interfaces/node_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <memory>
#include <mutex>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -70,10 +71,34 @@ class NodeGraph : public NodeGraphInterface
const std::string & node_name,
const std::string & namespace_) const override;

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 override;

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 override;

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 override;

RCLCPP_PUBLIC
std::vector<std::string>
get_node_names() const override;

RCLCPP_PUBLIC
std::vector<std::tuple<std::string, std::string, std::string>>
get_node_names_with_enclaves() const override;

RCLCPP_PUBLIC
std::vector<std::pair<std::string, std::string>>
get_node_names_and_namespaces() const override;
Expand Down
84 changes: 79 additions & 5 deletions rclcpp/include/rclcpp/node_interfaces/node_graph_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <chrono>
#include <map>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -146,7 +147,9 @@ class NodeGraphInterface
/**
* A topic is considered to exist when at least one publisher or subscriber
* exists for it, whether they be local or remote to this process.
* The returned names are the actual names used and do not have remap rules applied.
* The returned names are the actual names of the topics, either announced by another nodes or by this one.
* Attempting to create publishers or subscribers using names returned by this function may not
* result in the desired topic name being used depending on the remap rules in use.
*
* \param[in] no_demangle if true, topic names and types are not demangled
*/
Expand All @@ -160,7 +163,9 @@ class NodeGraphInterface
* A service is considered to exist when at least one service server or
* service client exists for it, whether they be local or remote to this
* process.
* The returned names are the actual names used and do not have remap rules applied.
* The returned names are the actual names of the services, either announced by another nodes or by this one.
* Attempting to create clients or services using names returned by this function may not result in
* the desired service name being used depending on the remap rules in use.
*/
RCLCPP_PUBLIC
virtual
Expand All @@ -170,7 +175,9 @@ class NodeGraphInterface
/// Return a map of existing service names to list of service types for a specific node.
/**
* This function only considers services - not clients.
* The returned names are the actual names used and do not have remap rules applied.
* The returned names are the actual names after remap rules applied.
* Attempting to create service clients using names returned by this function may not
* result in the desired service name being used depending on the remap rules in use.
*
* \param[in] node_name name of the node
* \param[in] namespace_ namespace of the node
Expand All @@ -182,18 +189,85 @@ class NodeGraphInterface
const std::string & node_name,
const std::string & namespace_) const = 0;

/// Return a map of existing service names and types with a specific node.
/**
* This function only considers clients - not service servers.
* The returned names are the actual names after remap rules applied.
* Attempting to create service servers using names returned by this function may not
* result in the desired service name being used depending on the remap rules in use.
*
* \param[in] node_name name of the node
* \param[in] namespace_ namespace of the node
*/
RCLCPP_PUBLIC
virtual
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 topic names to list of topic types for a specific node.
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
/**
* This function only considers publishers - not subscribers.
* The returned names are the actual names after remap rules applied.
* Attempting to create publishers or subscribers using names returned by this function may not
* result in the desired topic name being used depending on the remap rules in use.
*
* \param[in] node_name name of the node
* \param[in] namespace_ namespace of the node
* \param[in] no_demangle if true, topic names and types are not demangled
*/
RCLCPP_PUBLIC
virtual
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.
/**
* This function only considers subscribers - not publishers.
* The returned names are the actual names after remap rules applied.
* Attempting to create publishers or subscribers using names returned by this function may not
* result in the desired topic name being used depending on the remap rules in use.
*
* \param[in] node_name name of the node
* \param[in] namespace_ namespace of the node
* \param[in] no_demangle if true, topic names and types are not demangled
*/
RCLCPP_PUBLIC
virtual
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 vector of existing node names (string).
/*
* The returned names are the actual names used and do not have remap rules applied.
* The returned names are the actual names after remap rules applied.
*/
RCLCPP_PUBLIC
virtual
std::vector<std::string>
get_node_names() const = 0;

/// Return a vector of existing node names, namespaces and enclaves (tuple of string).
/*
* The returned names are the actual names after remap rules applied.
* The enclaves contain the runtime security artifacts, those can be
* used to establish secured network.
* See https://design.ros2.org/articles/ros2_security_enclaves.html
*/
RCLCPP_PUBLIC
virtual
std::vector<std::tuple<std::string, std::string, std::string>>
get_node_names_with_enclaves() const = 0;

/// Return a vector of existing node names and namespaces (pair of string).
/*
* The returned names are the actual names used and do not have remap rules applied.
* The returned names are the actual names after remap rules applied.
*/
RCLCPP_PUBLIC
virtual
Expand Down
187 changes: 186 additions & 1 deletion rclcpp/src/rclcpp/node_interfaces/node_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <algorithm>
#include <map>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

Expand All @@ -27,6 +28,7 @@
#include "rclcpp/expand_topic_or_service_name.hpp"
#include "rclcpp/graph_listener.hpp"
#include "rclcpp/node_interfaces/node_graph_interface.hpp"
#include "rcpputils/scope_exit.hpp"

using rclcpp::node_interfaces::NodeGraph;
using rclcpp::exceptions::throw_from_rcl_error;
Expand Down Expand Up @@ -178,6 +180,115 @@ NodeGraph::get_service_names_and_types_by_node(
return services_and_types;
}

std::map<std::string, std::vector<std::string>>
NodeGraph::get_client_names_and_types_by_node(
const std::string & node_name,
const std::string & namespace_) const
{
rcl_names_and_types_t service_names_and_types = rcl_get_zero_initialized_names_and_types();
auto service_names_and_types_finalizer = rcpputils::make_scope_exit(
[&service_names_and_types]() {
if (rcl_names_and_types_fini(&service_names_and_types) != RCL_RET_OK) {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp"), "could not destroy service names and types");
}
});
rcl_allocator_t allocator = rcl_get_default_allocator();
fujitatomoya marked this conversation as resolved.
Show resolved Hide resolved
rcl_ret_t ret = rcl_get_client_names_and_types_by_node(
node_base_->get_rcl_node_handle(),
&allocator,
node_name.c_str(),
namespace_.c_str(),
&service_names_and_types);
if (ret != RCL_RET_OK) {
throw_from_rcl_error(ret, "failed to get service names and types by node");
}

std::map<std::string, std::vector<std::string>> services_and_types;
for (size_t i = 0; i < service_names_and_types.names.size; ++i) {
std::string service_name = service_names_and_types.names.data[i];
for (size_t j = 0; j < service_names_and_types.types[i].size; ++j) {
services_and_types[service_name].emplace_back(service_names_and_types.types[i].data[j]);
}
}

return services_and_types;
}

std::map<std::string, std::vector<std::string>>
NodeGraph::get_publisher_names_and_types_by_node(
const std::string & node_name,
const std::string & namespace_,
bool no_demangle) const
{
rcl_names_and_types_t topic_names_and_types = rcl_get_zero_initialized_names_and_types();
auto topic_names_and_types_finalizer = rcpputils::make_scope_exit(
[&topic_names_and_types]() {
if (rcl_names_and_types_fini(&topic_names_and_types) != RCL_RET_OK) {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp"), "could not destroy topic names and types");
}
});
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_ret_t ret = rcl_get_publisher_names_and_types_by_node(
node_base_->get_rcl_node_handle(),
&allocator,
no_demangle,
node_name.c_str(),
namespace_.c_str(),
&topic_names_and_types);
if (ret != RCL_RET_OK) {
throw_from_rcl_error(ret, "failed to get topic names and types by node");
}

std::map<std::string, std::vector<std::string>> topics_and_types;
for (size_t i = 0; i < topic_names_and_types.names.size; ++i) {
std::string topic_name = topic_names_and_types.names.data[i];
for (size_t j = 0; j < topic_names_and_types.types[i].size; ++j) {
topics_and_types[topic_name].emplace_back(topic_names_and_types.types[i].data[j]);
}
}

return topics_and_types;
}

std::map<std::string, std::vector<std::string>>
NodeGraph::get_subscriber_names_and_types_by_node(
const std::string & node_name,
const std::string & namespace_,
bool no_demangle) const
{
rcl_names_and_types_t topic_names_and_types = rcl_get_zero_initialized_names_and_types();
auto topic_names_and_types_finalizer = rcpputils::make_scope_exit(
[&topic_names_and_types]() {
if (rcl_names_and_types_fini(&topic_names_and_types) != RCL_RET_OK) {
RCLCPP_ERROR(
rclcpp::get_logger("rclcpp"), "could not destroy topic names and types");
}
});
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_ret_t ret = rcl_get_subscriber_names_and_types_by_node(
node_base_->get_rcl_node_handle(),
&allocator,
no_demangle,
node_name.c_str(),
namespace_.c_str(),
&topic_names_and_types);
if (ret != RCL_RET_OK) {
throw_from_rcl_error(ret, "failed to get topic names and types by node");
}

std::map<std::string, std::vector<std::string>> topics_and_types;
for (size_t i = 0; i < topic_names_and_types.names.size; ++i) {
std::string topic_name = topic_names_and_types.names.data[i];
for (size_t j = 0; j < topic_names_and_types.types[i].size; ++j) {
topics_and_types[topic_name].emplace_back(topic_names_and_types.types[i].data[j]);
}
}

return topics_and_types;
}

std::vector<std::string>
NodeGraph::get_node_names() const
{
Expand Down Expand Up @@ -206,6 +317,81 @@ NodeGraph::get_node_names() const
return nodes;
}

std::vector<std::tuple<std::string, std::string, std::string>>
NodeGraph::get_node_names_with_enclaves() const
{
rcutils_string_array_t node_names_c =
rcutils_get_zero_initialized_string_array();
rcutils_string_array_t node_namespaces_c =
rcutils_get_zero_initialized_string_array();
rcutils_string_array_t node_enclaves_c =
rcutils_get_zero_initialized_string_array();

auto allocator = rcl_get_default_allocator();
auto ret = rcl_get_node_names_with_enclaves(
node_base_->get_rcl_node_handle(),
allocator,
&node_names_c,
&node_namespaces_c,
&node_enclaves_c);
if (ret != RCL_RET_OK) {
auto error_msg =
std::string("failed to get node names with enclaves: ") + rcl_get_error_string().str;
rcl_reset_error();
if (rcutils_string_array_fini(&node_names_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node names, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
if (rcutils_string_array_fini(&node_namespaces_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node namespaces, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
if (rcutils_string_array_fini(&node_enclaves_c) != RCUTILS_RET_OK) {
error_msg += std::string(", failed also to cleanup node enclaves, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
throw std::runtime_error(error_msg);
}

std::vector<std::tuple<std::string, std::string, std::string>> node_tuples;
for (size_t i = 0; i < node_names_c.size; ++i) {
if (node_names_c.data[i] && node_namespaces_c.data[i] && node_enclaves_c.data[i]) {
node_tuples.emplace_back(
std::make_tuple(node_names_c.data[i], node_namespaces_c.data[i], node_enclaves_c.data[i]));
}
}

std::string error("failed to finalize array");
rcl_ret_t ret_names = rcutils_string_array_fini(&node_names_c);
if (ret_names != RCUTILS_RET_OK) {
error += std::string(", could not destroy node names, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}
rcl_ret_t ret_ns = rcutils_string_array_fini(&node_namespaces_c);
if (ret_ns != RCUTILS_RET_OK) {
error += std::string(", could not destroy node namespaces, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}

rcl_ret_t ret_ecv = rcutils_string_array_fini(&node_enclaves_c);
if (ret_ecv != RCUTILS_RET_OK) {
error += std::string(", could not destroy node enclaves, leaking memory: ") +
rcl_get_error_string().str;
rcl_reset_error();
}

if (ret_names != RCUTILS_RET_OK || ret_ns != RCUTILS_RET_OK || ret_ecv != RCUTILS_RET_OK) {
throw std::runtime_error(error);
}

return node_tuples;
}

std::vector<std::pair<std::string, std::string>>
NodeGraph::get_node_names_and_namespaces() const
{
Expand Down Expand Up @@ -237,7 +423,6 @@ NodeGraph::get_node_names_and_namespaces() const
throw std::runtime_error(error_msg);
}


std::vector<std::pair<std::string, std::string>> node_names;
node_names.reserve(node_names_c.size);
for (size_t i = 0; i < node_names_c.size; ++i) {
Expand Down
Loading