From 300b814ae8ef19b0fa7eb1e7db74efa80d300d4c Mon Sep 17 00:00:00 2001 From: Shivang Vijay Date: Sat, 9 Mar 2024 21:38:15 +0530 Subject: [PATCH 1/8] Attempt to solve issue #111 --- rmw_zenoh_cpp/src/detail/liveliness_utils.cpp | 18 ++++++++++++++++++ rmw_zenoh_cpp/src/detail/liveliness_utils.hpp | 3 +++ rmw_zenoh_cpp/src/rmw_zenoh.cpp | 8 +++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp b/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp index 997f76b3..306c60a0 100644 --- a/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp +++ b/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp @@ -446,6 +446,24 @@ std::string mangle_name(const std::string & input) return output; } +///============================================================================= +char * mangle_name(const char * input) +{ + size_t input_length = strlen(input); + char * output = new char[input_length + 1]; + + for (size_t i = 0; i < input_length; ++i) { + if (input[i] == '/') { + output[i] = SLASH_REPLACEMENT; + } else { + output[i] = input[i]; + } + } + output[input_length] = '\0'; + + return output; +} + ///============================================================================= std::string demangle_name(const std::string & input) { diff --git a/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp b/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp index 390ecc68..40d9c80a 100644 --- a/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp +++ b/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp @@ -122,6 +122,9 @@ class Entity /// Replace "/" instances with "%". std::string mangle_name(const std::string & input); +/// Replace "/" instances with "%". +char * mangle_name(const char * input); + /// Replace "%" instances with "/". std::string demangle_name(const std::string & input); diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index ae62e7be..de6edd11 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -79,7 +79,7 @@ namespace // the old string into it. If this becomes a performance problem, we could consider // modifying the topic_name in place. But this means we need to be much more // careful about who owns the string. -z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * const topic_name, size_t domain_id) +z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * topic_name, size_t domain_id) { std::string d = std::to_string(domain_id); @@ -87,12 +87,14 @@ z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * const topic_name, siz size_t topic_name_len = strlen(topic_name); size_t end_offset = topic_name_len; + topic_name = liveliness::mangle_name(topic_name); + if (topic_name_len > 0) { - if (topic_name[0] == '/') { + if (topic_name[0] == '%') { // Strip the leading '/' start_offset = 1; } - if (topic_name[end_offset - 1] == '/') { + if (topic_name[end_offset - 1] == '%') { // Strip the trailing '/' end_offset -= 1; } From a136fe2bdb653adaa7ea817203abe1e576b29ac6 Mon Sep 17 00:00:00 2001 From: Shivang Vijay Date: Sun, 10 Mar 2024 11:47:27 +0530 Subject: [PATCH 2/8] Remove code duplication, update ros_topic_name_to_zenoh_key function --- rmw_zenoh_cpp/src/detail/liveliness_utils.cpp | 18 ------------------ rmw_zenoh_cpp/src/detail/liveliness_utils.hpp | 3 --- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 16 +++------------- 3 files changed, 3 insertions(+), 34 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp b/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp index 306c60a0..997f76b3 100644 --- a/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp +++ b/rmw_zenoh_cpp/src/detail/liveliness_utils.cpp @@ -446,24 +446,6 @@ std::string mangle_name(const std::string & input) return output; } -///============================================================================= -char * mangle_name(const char * input) -{ - size_t input_length = strlen(input); - char * output = new char[input_length + 1]; - - for (size_t i = 0; i < input_length; ++i) { - if (input[i] == '/') { - output[i] = SLASH_REPLACEMENT; - } else { - output[i] = input[i]; - } - } - output[input_length] = '\0'; - - return output; -} - ///============================================================================= std::string demangle_name(const std::string & input) { diff --git a/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp b/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp index 40d9c80a..390ecc68 100644 --- a/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp +++ b/rmw_zenoh_cpp/src/detail/liveliness_utils.hpp @@ -122,9 +122,6 @@ class Entity /// Replace "/" instances with "%". std::string mangle_name(const std::string & input); -/// Replace "/" instances with "%". -char * mangle_name(const char * input); - /// Replace "%" instances with "/". std::string demangle_name(const std::string & input); diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index de6edd11..fa120931 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -87,22 +87,12 @@ z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * topic_name, size_t do size_t topic_name_len = strlen(topic_name); size_t end_offset = topic_name_len; - topic_name = liveliness::mangle_name(topic_name); - - if (topic_name_len > 0) { - if (topic_name[0] == '%') { - // Strip the leading '/' - start_offset = 1; - } - if (topic_name[end_offset - 1] == '%') { - // Strip the trailing '/' - end_offset -= 1; - } - } + std::string topic_nm(topic_name); + topic_nm = liveliness::mangle_name(topic_nm); return z_keyexpr_join( z_keyexpr(d.c_str()), - zc_keyexpr_from_slice(&topic_name[start_offset], end_offset - start_offset)); + z_keyexpr(topic_nm.c_str())); } //============================================================================== From 11f3613709d3ced45942e8fcb419902d86fcb6e4 Mon Sep 17 00:00:00 2001 From: Shivang Vijay Date: Tue, 12 Mar 2024 13:04:53 +0530 Subject: [PATCH 3/8] Update rmw_zenoh_cpp/src/rmw_zenoh.cpp Co-authored-by: Yadu Signed-off-by: Shivang Vijay --- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index fa120931..8fcc55b3 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -87,8 +87,7 @@ z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * topic_name, size_t do size_t topic_name_len = strlen(topic_name); size_t end_offset = topic_name_len; - std::string topic_nm(topic_name); - topic_nm = liveliness::mangle_name(topic_nm); + topic_nm = liveliness::mangle_name(topic_name); return z_keyexpr_join( z_keyexpr(d.c_str()), From f95fee8b0077a006b32ea95b90dbc80bd8874189 Mon Sep 17 00:00:00 2001 From: Shivang Vijay Date: Tue, 12 Mar 2024 13:19:04 +0530 Subject: [PATCH 4/8] Added const in front of topic name and declare topic_nm --- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 8fcc55b3..a5200a6d 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -79,7 +79,7 @@ namespace // the old string into it. If this becomes a performance problem, we could consider // modifying the topic_name in place. But this means we need to be much more // careful about who owns the string. -z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * topic_name, size_t domain_id) +z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * const topic_name, size_t domain_id) { std::string d = std::to_string(domain_id); @@ -87,7 +87,7 @@ z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * topic_name, size_t do size_t topic_name_len = strlen(topic_name); size_t end_offset = topic_name_len; - topic_nm = liveliness::mangle_name(topic_name); + std::string topic_nm = liveliness::mangle_name(topic_name); return z_keyexpr_join( z_keyexpr(d.c_str()), From 60d27b18b3acee0d12b12020fb5899b3e920df4a Mon Sep 17 00:00:00 2001 From: Shivang Vijay Date: Tue, 12 Mar 2024 14:01:15 +0530 Subject: [PATCH 5/8] Adding proper variable name Co-authored-by: Yadu Signed-off-by: Shivang Vijay --- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index a5200a6d..51704ebe 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -87,7 +87,7 @@ z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * const topic_name, siz size_t topic_name_len = strlen(topic_name); size_t end_offset = topic_name_len; - std::string topic_nm = liveliness::mangle_name(topic_name); + const std::string mangled_topic_name = liveliness::mangle_name(topic_name); return z_keyexpr_join( z_keyexpr(d.c_str()), From 4556929a9eda696c3d784af1104933cad934ca84 Mon Sep 17 00:00:00 2001 From: Shivang Vijay Date: Tue, 12 Mar 2024 14:08:53 +0530 Subject: [PATCH 6/8] Added appropriate variable name --- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 51704ebe..345c7927 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -91,7 +91,7 @@ z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * const topic_name, siz return z_keyexpr_join( z_keyexpr(d.c_str()), - z_keyexpr(topic_nm.c_str())); + z_keyexpr(mangled_topic_name.c_str())); } //============================================================================== From 2c2c8d85542b2c8e8321eee9a6bd2e12c7634381 Mon Sep 17 00:00:00 2001 From: Shivang Vijay Date: Thu, 4 Apr 2024 00:36:28 +0530 Subject: [PATCH 7/8] / separater --- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 345c7927..d84911de 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -81,17 +81,8 @@ namespace // careful about who owns the string. z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * const topic_name, size_t domain_id) { - std::string d = std::to_string(domain_id); - - size_t start_offset = 0; - size_t topic_name_len = strlen(topic_name); - size_t end_offset = topic_name_len; - - const std::string mangled_topic_name = liveliness::mangle_name(topic_name); - - return z_keyexpr_join( - z_keyexpr(d.c_str()), - z_keyexpr(mangled_topic_name.c_str())); + const std::string keyexpr_str = std::to_string(domain_id) + "/" + liveliness::mangle_name(topic_name); + return z_keyexpr_new(keyexpr_str.c_str()); } //============================================================================== From dc10b2c9611b410a2933e2e9b30b97bbe50cfd9e Mon Sep 17 00:00:00 2001 From: Yadunund Date: Thu, 18 Apr 2024 22:32:27 +0800 Subject: [PATCH 8/8] Style Signed-off-by: Yadunund --- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index d84911de..58995aa9 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -81,7 +81,8 @@ namespace // careful about who owns the string. z_owned_keyexpr_t ros_topic_name_to_zenoh_key(const char * const topic_name, size_t domain_id) { - const std::string keyexpr_str = std::to_string(domain_id) + "/" + liveliness::mangle_name(topic_name); + const std::string keyexpr_str = std::to_string(domain_id) + "/" + liveliness::mangle_name( + topic_name); return z_keyexpr_new(keyexpr_str.c_str()); }