From dc5bb6ac486c16c2995e7a63c059945c747cf585 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Thu, 7 Dec 2017 14:28:37 -0800 Subject: [PATCH 1/2] add node name to user data --- rmw_connext_shared_cpp/src/node.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rmw_connext_shared_cpp/src/node.cpp b/rmw_connext_shared_cpp/src/node.cpp index a70fc625..a50733a9 100644 --- a/rmw_connext_shared_cpp/src/node.cpp +++ b/rmw_connext_shared_cpp/src/node.cpp @@ -53,6 +53,23 @@ create_node( // This String_dup is not matched with a String_free because DDS appears to // free this automatically. participant_qos.participant_name.name = DDS::String_dup(name); + // since the participant name is not part of the DDS spec + // the node name is also set in the user_data + DDS_Long name_length = static_cast(strlen(name)); + const char prefix[6] = "name="; + bool success = participant_qos.user_data.value.length(name_length + sizeof(prefix)); + if (!success) { + RMW_SET_ERROR_MSG("failed to resize participant user_data"); + return NULL; + } + memcpy(participant_qos.user_data.value.get_contiguous_buffer(), prefix, sizeof(prefix) - 1); + { + for (DDS_Long i = 0; i < name_length; ++i) { + participant_qos.user_data.value[sizeof(prefix) - 1 + i] = name[i]; + } + participant_qos.user_data.value[sizeof(prefix) - 1 + name_length] = ';'; + } + // forces local traffic to be sent over loopback, // even if a more efficient transport (such as shared memory) is installed // (in which case traffic will be sent over both transports) From 74e28fbdeb67fea45b34e4771c006b3a9a9ab4ff Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Mon, 8 Jan 2018 11:05:33 -0800 Subject: [PATCH 2/2] get participant name from user data first --- rmw_connext_shared_cpp/src/node_names.cpp | 30 +++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/rmw_connext_shared_cpp/src/node_names.cpp b/rmw_connext_shared_cpp/src/node_names.cpp index c91ffc90..f7c7ffe2 100644 --- a/rmw_connext_shared_cpp/src/node_names.cpp +++ b/rmw_connext_shared_cpp/src/node_names.cpp @@ -12,11 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include +#include + #include "rcutils/logging_macros.h" #include "rcutils/strdup.h" #include "rmw/convert_rcutils_ret_to_rmw_ret.h" #include "rmw/error_handling.h" +#include "rmw/impl/cpp/key_value.hpp" #include "rmw/sanity_checks.h" #include "rmw_connext_shared_cpp/ndds_include.hpp" @@ -67,15 +71,31 @@ get_node_names( return RMW_RET_BAD_ALLOC; } - for (auto i = 1; i < length; ++i) { DDS::ParticipantBuiltinTopicData pbtd; auto dds_ret = participant->get_discovered_participant_data(pbtd, handles[i - 1]); - const char * name = pbtd.participant_name.name; - if (!name || dds_ret != DDS_RETCODE_OK) { - name = "(no name)"; + std::string name; + if (DDS_RETCODE_OK == dds_ret) { + auto data = static_cast(pbtd.user_data.value.get_contiguous_buffer()); + std::vector kv(data, data + pbtd.user_data.value.length()); + auto map = rmw::impl::cpp::parse_key_value(kv); + auto found = map.find("name"); + if (found != map.end()) { + name = std::string(found->second.begin(), found->second.end()); + } + if (name.empty()) { + // use participant name if no name was found in the user data + if (pbtd.participant_name.name) { + name = pbtd.participant_name.name; + } + } + } + if (name.empty()) { + // ignore discovered participants without a name + node_names->data[i] = nullptr; + continue; } - node_names->data[i] = rcutils_strdup(name, allocator); + node_names->data[i] = rcutils_strdup(name.c_str(), allocator); if (!node_names->data[i]) { RMW_SET_ERROR_MSG("could not allocate memory for node name") rcutils_ret = rcutils_string_array_fini(node_names);