From f87c08339bc2437c1241271aa0c7061fd95557b7 Mon Sep 17 00:00:00 2001 From: alpylmz <41516584+alpylmz@users.noreply.github.com> Date: Mon, 11 Jul 2022 12:30:49 +0300 Subject: [PATCH 1/5] add namespace environment variable support --- rcl/src/rcl/node.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rcl/src/rcl/node.c b/rcl/src/rcl/node.c index 4afc40f53..c70ac76d8 100644 --- a/rcl/src/rcl/node.c +++ b/rcl/src/rcl/node.c @@ -54,6 +54,7 @@ extern "C" #include "./context_impl.h" const char * const RCL_DISABLE_LOANED_MESSAGES_ENV_VAR = "ROS_DISABLE_LOANED_MESSAGES"; +const char * const RCL_NAMESPACE_ENV_VAR = "ROS2_NAMESPACE"; struct rcl_node_impl_s { @@ -121,6 +122,9 @@ rcl_node_init( rcl_context_t * context, const rcl_node_options_t * options) { + const char * env_error_str = NULL; + const char * env_val = NULL; + const rmw_guard_condition_t * rmw_graph_guard_condition = NULL; rcl_guard_condition_options_t graph_guard_condition_options = rcl_guard_condition_get_default_options(); @@ -128,6 +132,17 @@ rcl_node_init( rcl_ret_t fail_ret = RCL_RET_ERROR; char * remapped_node_name = NULL; + env_error_str = rcutils_get_env(RCL_NAMESPACE_ENV_VAR, &env_val); + if (NULL != env_error_str) { + RCL_SET_ERROR_MSG_WITH_FORMAT_STRING( + "Error getting env var: '" RCUTILS_STRINGIFY(RCL_NAMESPACE_ENV_VAR) "': %s\n", + env_error_str); + return RCL_RET_ERROR; + } + if (NULL != env_val or 0 != strlen(env_val)) { + namespace_ = env_val; + } + // Check options and allocator first, so allocator can be used for errors. RCL_CHECK_ARGUMENT_FOR_NULL(options, RCL_RET_INVALID_ARGUMENT); const rcl_allocator_t * allocator = &options->allocator; From e03b151e8ba170115df02a8e27442891a755e64e Mon Sep 17 00:00:00 2001 From: alpylmz <41516584+alpylmz@users.noreply.github.com> Date: Thu, 14 Jul 2022 10:49:13 +0300 Subject: [PATCH 2/5] condition typo fix --- rcl/src/rcl/node.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rcl/src/rcl/node.c b/rcl/src/rcl/node.c index c70ac76d8..9d8d52b1d 100644 --- a/rcl/src/rcl/node.c +++ b/rcl/src/rcl/node.c @@ -139,7 +139,7 @@ rcl_node_init( env_error_str); return RCL_RET_ERROR; } - if (NULL != env_val or 0 != strlen(env_val)) { + if (NULL != env_val && 0 != strlen(env_val)) { namespace_ = env_val; } From ab395766e83a38949cb55abce8ec36b06f05042e Mon Sep 17 00:00:00 2001 From: alpylmz <41516584+alpylmz@users.noreply.github.com> Date: Thu, 14 Jul 2022 10:49:27 +0300 Subject: [PATCH 3/5] a test is added for namespace override --- rcl/test/rcl/test_node.cpp | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/rcl/test/rcl/test_node.cpp b/rcl/test/rcl/test_node.cpp index 716fb5ff1..c7b48d185 100644 --- a/rcl/test/rcl/test_node.cpp +++ b/rcl/test/rcl/test_node.cpp @@ -1043,3 +1043,45 @@ TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_get_disable_loan EXPECT_FALSE(disable_loaned_message); } } + +/* Tests node namespace override + */ +TEST_F(CLASSNAME(TestNodeFixture, RMW_IMPLEMENTATION), test_rcl_node_namespace_override) { + { + const char * expected_namespace = "/bar/foo"; + ASSERT_TRUE(rcutils_set_env("ROS2_NAMESPACE", expected_namespace)); + + osrf_testing_tools_cpp::memory_tools::enable_monitoring_in_all_threads(); + rcl_ret_t ret; + // Initialize rcl with rcl_init(). + rcl_init_options_t init_options = rcl_get_zero_initialized_init_options(); + ret = rcl_init_options_init(&init_options, rcl_get_default_allocator()); + ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( + { + EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str; + }); + EXPECT_EQ(RCL_RET_OK, rcl_init_options_set_domain_id(&init_options, 42)); + const char * name = "test_rcl_node_namespace_override"; + const char * namespace_ = "/ns"; + rcl_node_options_t default_options = rcl_node_get_default_options(); + rcl_context_t context = rcl_get_zero_initialized_context(); + ret = rcl_init(0, nullptr, &init_options, &context); + ASSERT_EQ(RCL_RET_OK, ret); + // Create a normal node. + rcl_node_t node = rcl_get_zero_initialized_node(); + ret = rcl_node_init(&node, name, namespace_, &context, &default_options); + ASSERT_EQ(RCL_RET_OK, ret); + + // Check that the node namespace is overridden + ASSERT_STREQ(expected_namespace, rcl_node_get_namespace(&node)); + + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( + { + osrf_testing_tools_cpp::memory_tools::disable_monitoring_in_all_threads(); + ASSERT_EQ(RCL_RET_OK, rcl_node_fini(&node)); + ASSERT_EQ(RCL_RET_OK, rcl_shutdown(&context)); + ASSERT_EQ(RCL_RET_OK, rcl_context_fini(&context)); + }); + } +} From 7de72e2b84c49f999d6ebb949ff9921956af230b Mon Sep 17 00:00:00 2001 From: alpylmz <41516584+alpylmz@users.noreply.github.com> Date: Thu, 14 Jul 2022 21:42:34 +0300 Subject: [PATCH 4/5] environment variable changed from ROS2_NAMESPACE to ROS_NAMESPACE --- rcl/src/rcl/node.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rcl/src/rcl/node.c b/rcl/src/rcl/node.c index 9d8d52b1d..5f7ed3a03 100644 --- a/rcl/src/rcl/node.c +++ b/rcl/src/rcl/node.c @@ -54,7 +54,7 @@ extern "C" #include "./context_impl.h" const char * const RCL_DISABLE_LOANED_MESSAGES_ENV_VAR = "ROS_DISABLE_LOANED_MESSAGES"; -const char * const RCL_NAMESPACE_ENV_VAR = "ROS2_NAMESPACE"; +const char * const RCL_NAMESPACE_ENV_VAR = "ROS_NAMESPACE"; struct rcl_node_impl_s { From c44042f7c23ebec06dcae09a512ffe5fef5a3147 Mon Sep 17 00:00:00 2001 From: alpylmz <41516584+alpylmz@users.noreply.github.com> Date: Thu, 14 Jul 2022 21:46:03 +0300 Subject: [PATCH 5/5] const declarations moved close to actual use --- rcl/src/rcl/node.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rcl/src/rcl/node.c b/rcl/src/rcl/node.c index 5f7ed3a03..12d24b3a4 100644 --- a/rcl/src/rcl/node.c +++ b/rcl/src/rcl/node.c @@ -122,8 +122,6 @@ rcl_node_init( rcl_context_t * context, const rcl_node_options_t * options) { - const char * env_error_str = NULL; - const char * env_val = NULL; const rmw_guard_condition_t * rmw_graph_guard_condition = NULL; rcl_guard_condition_options_t graph_guard_condition_options = @@ -132,6 +130,8 @@ rcl_node_init( rcl_ret_t fail_ret = RCL_RET_ERROR; char * remapped_node_name = NULL; + const char * env_error_str = NULL; + const char * env_val = NULL; env_error_str = rcutils_get_env(RCL_NAMESPACE_ENV_VAR, &env_val); if (NULL != env_error_str) { RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(