From 1164ceae56fc7f507ea91c64ee2a4acd8678d592 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Mon, 8 Jan 2018 11:05:33 -0800 Subject: [PATCH] get participant name from user data first --- rmw_connext_shared_cpp/src/node_names.cpp | 27 ++++++++++++++++++----- 1 file changed, 22 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..d50b7381 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,28 @@ 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()) { + 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);