diff --git a/rclcpp/include/rclcpp/node.hpp b/rclcpp/include/rclcpp/node.hpp index aac5825..0b49267 100644 --- a/rclcpp/include/rclcpp/node.hpp +++ b/rclcpp/include/rclcpp/node.hpp @@ -101,6 +101,12 @@ public: const char * get_name() const; + /// Get the namespace of the node. + /** \return The namespace of the node. */ + RCLCPP_PUBLIC + const char * + get_namespace() const; + /// Create and return a callback group. RCLCPP_PUBLIC rclcpp::callback_group::CallbackGroup::SharedPtr diff --git a/rclcpp/include/rclcpp/node_interfaces/node_base.hpp b/rclcpp/include/rclcpp/node_interfaces/node_base.hpp index 63d86d3..d3d7834 100644 --- a/rclcpp/include/rclcpp/node_interfaces/node_base.hpp +++ b/rclcpp/include/rclcpp/node_interfaces/node_base.hpp @@ -50,6 +50,11 @@ public: const char * get_name() const; + RCLCPP_PUBLIC + virtual + const char * + get_namespace() const; + RCLCPP_PUBLIC virtual rclcpp::context::Context::SharedPtr diff --git a/rclcpp/include/rclcpp/node_interfaces/node_base_interface.hpp b/rclcpp/include/rclcpp/node_interfaces/node_base_interface.hpp index 7c51102..23e397c 100644 --- a/rclcpp/include/rclcpp/node_interfaces/node_base_interface.hpp +++ b/rclcpp/include/rclcpp/node_interfaces/node_base_interface.hpp @@ -45,6 +45,13 @@ public: const char * get_name() const = 0; + /// Return the namespace of the node. + /** \return The namespace of the node. */ + RCLCPP_PUBLIC + virtual + const char * + get_namespace() const = 0; + /// Return the context of the node. /** \return SharedPtr to the node's context. */ RCLCPP_PUBLIC diff --git a/rclcpp/src/rclcpp/node.cpp b/rclcpp/src/rclcpp/node.cpp index 0630cfb..c6dee80 100644 --- a/rclcpp/src/rclcpp/node.cpp +++ b/rclcpp/src/rclcpp/node.cpp @@ -71,6 +71,12 @@ Node::get_name() const return node_base_->get_name(); } +const char * +Node::get_namespace() const +{ + return node_base_->get_namespace(); +} + rclcpp::callback_group::CallbackGroup::SharedPtr Node::create_callback_group( rclcpp::callback_group::CallbackGroupType group_type) diff --git a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp index 1775769..3c9c8ed 100644 --- a/rclcpp/src/rclcpp/node_interfaces/node_base.cpp +++ b/rclcpp/src/rclcpp/node_interfaces/node_base.cpp @@ -166,6 +166,12 @@ NodeBase::get_name() const return rcl_node_get_name(node_handle_.get()); } +const char * +NodeBase::get_namespace() const +{ + return rcl_node_get_namespace(node_handle_.get()); +} + rclcpp::context::Context::SharedPtr NodeBase::get_context() { diff --git a/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp b/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp index ce2963b..4416422 100644 --- a/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp +++ b/rclcpp/src/rclcpp/node_interfaces/node_graph.cpp @@ -120,10 +120,16 @@ NodeGraph::get_node_names() const size_t NodeGraph::count_publishers(const std::string & topic_name) const { + auto rmw_node_handle = rcl_node_get_rmw_handle(node_base_->get_rcl_node_handle()); + auto fqdn = rclcpp::expand_topic_or_service_name( + topic_name, + rmw_node_handle->name, + rmw_node_handle->namespace_, + false); // false = not a service + size_t count; // TODO(wjwwood): use the rcl equivalent methods - auto ret = rmw_count_publishers(rcl_node_get_rmw_handle(node_base_->get_rcl_node_handle()), - topic_name.c_str(), &count); + auto ret = rmw_count_publishers(rmw_node_handle, fqdn.c_str(), &count); if (ret != RMW_RET_OK) { // *INDENT-OFF* throw std::runtime_error( @@ -136,10 +142,16 @@ NodeGraph::count_publishers(const std::string & topic_name) const size_t NodeGraph::count_subscribers(const std::string & topic_name) const { + auto rmw_node_handle = rcl_node_get_rmw_handle(node_base_->get_rcl_node_handle()); + auto fqdn = rclcpp::expand_topic_or_service_name( + topic_name, + rmw_node_handle->name, + rmw_node_handle->namespace_, + false); // false = not a service + size_t count; // TODO(wjwwood): use the rcl equivalent methods - auto ret = rmw_count_subscribers(rcl_node_get_rmw_handle(node_base_->get_rcl_node_handle()), - topic_name.c_str(), &count); + auto ret = rmw_count_subscribers(rmw_node_handle, fqdn.c_str(), &count); if (ret != RMW_RET_OK) { // *INDENT-OFF* throw std::runtime_error( diff --git a/rclcpp/test/test_node.cpp b/rclcpp/test/test_node.cpp index 34d0e11..42d59f8 100644 --- a/rclcpp/test/test_node.cpp +++ b/rclcpp/test/test_node.cpp @@ -50,3 +50,26 @@ TEST_F(TestNode, construction_and_destruction) { }, rclcpp::exceptions::InvalidNamespaceError); } } + +TEST_F(TestNode, get_name_and_namespace) { + { + auto node = std::make_shared("my_node", "/ns"); + EXPECT_STREQ("my_node", node->get_name()); + EXPECT_STREQ("/ns", node->get_namespace()); + } + { + auto node = std::make_shared("my_node", "ns"); + EXPECT_STREQ("my_node", node->get_name()); + EXPECT_STREQ("/ns", node->get_namespace()); + } + { + auto node = std::make_shared("my_node", "/my/ns"); + EXPECT_STREQ("my_node", node->get_name()); + EXPECT_STREQ("/my/ns", node->get_namespace()); + } + { + auto node = std::make_shared("my_node", "my/ns"); + EXPECT_STREQ("my_node", node->get_name()); + EXPECT_STREQ("/my/ns", node->get_namespace()); + } +} diff --git a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp index c19a099..9913a92 100644 --- a/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp +++ b/rclcpp_lifecycle/include/rclcpp_lifecycle/lifecycle_node.hpp @@ -102,6 +102,12 @@ public: const char * get_name() const; + /// Get the namespace of the node. + // \return The namespace of the node. + RCLCPP_LIFECYCLE_PUBLIC + const char * + get_namespace() const; + /// Create and return a callback group. RCLCPP_LIFECYCLE_PUBLIC rclcpp::callback_group::CallbackGroup::SharedPtr diff --git a/rclcpp_lifecycle/src/lifecycle_node.cpp b/rclcpp_lifecycle/src/lifecycle_node.cpp index 09334cb..7ee3e34 100644 --- a/rclcpp_lifecycle/src/lifecycle_node.cpp +++ b/rclcpp_lifecycle/src/lifecycle_node.cpp @@ -89,6 +89,12 @@ LifecycleNode::get_name() const return node_base_->get_name(); } +const char * +LifecycleNode::get_namespace() const +{ + return node_base_->get_namespace(); +} + rclcpp::callback_group::CallbackGroup::SharedPtr LifecycleNode::create_callback_group( rclcpp::callback_group::CallbackGroupType group_type) diff --git a/rclcpp_lifecycle/test/test_lifecycle_node.cpp b/rclcpp_lifecycle/test/test_lifecycle_node.cpp index 227b6f0..c6f5e4e 100644 --- a/rclcpp_lifecycle/test/test_lifecycle_node.cpp +++ b/rclcpp_lifecycle/test/test_lifecycle_node.cpp @@ -123,7 +123,8 @@ rcl_lifecycle_ret_t MoodyLifecycleNode::on_error(const rclcpp_lifecycle TEST_F(TestDefaultStateMachine, empty_initializer) { auto test_node = std::make_shared("testnode"); - + EXPECT_STREQ("testnode", test_node->get_name()); + EXPECT_STREQ("/", test_node->get_namespace()); EXPECT_EQ(State::PRIMARY_STATE_UNCONFIGURED, test_node->get_current_state().id()); }