|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +#include <string> |
| 16 | +#include <vector> |
| 17 | + |
15 | 18 | #include "rcutils/logging_macros.h" |
16 | 19 | #include "rcutils/strdup.h" |
17 | 20 |
|
18 | 21 | #include "rmw/convert_rcutils_ret_to_rmw_ret.h" |
19 | 22 | #include "rmw/error_handling.h" |
| 23 | +#include "rmw/impl/cpp/key_value.hpp" |
20 | 24 | #include "rmw/sanity_checks.h" |
21 | 25 |
|
22 | 26 | #include "rmw_connext_shared_cpp/ndds_include.hpp" |
@@ -67,15 +71,29 @@ get_node_names( |
67 | 71 | return RMW_RET_BAD_ALLOC; |
68 | 72 | } |
69 | 73 |
|
70 | | - |
71 | 74 | for (auto i = 1; i < length; ++i) { |
72 | 75 | DDS::ParticipantBuiltinTopicData pbtd; |
73 | 76 | auto dds_ret = participant->get_discovered_participant_data(pbtd, handles[i - 1]); |
74 | | - const char * name = pbtd.participant_name.name; |
75 | | - if (!name || dds_ret != DDS_RETCODE_OK) { |
76 | | - name = "(no name)"; |
| 77 | + std::string name; |
| 78 | + if (DDS_RETCODE_OK == dds_ret) { |
| 79 | + auto data = static_cast<unsigned char *>(pbtd.user_data.value.get_contiguous_buffer()); |
| 80 | + std::vector<uint8_t> kv(data, data + pbtd.user_data.value.length()); |
| 81 | + auto map = rmw::impl::cpp::parse_key_value(kv); |
| 82 | + auto found = map.find("name"); |
| 83 | + if (found != map.end()) { |
| 84 | + name = std::string(found->second.begin(), found->second.end()); |
| 85 | + } |
| 86 | + if (name.empty()) { |
| 87 | + // use participant name if no name was found in the user data |
| 88 | + name = pbtd.participant_name.name; |
| 89 | + } |
| 90 | + } |
| 91 | + if (name.empty()) { |
| 92 | + // ignore discovered participants without a name |
| 93 | + node_names->data[i] = nullptr; |
| 94 | + continue; |
77 | 95 | } |
78 | | - node_names->data[i] = rcutils_strdup(name, allocator); |
| 96 | + node_names->data[i] = rcutils_strdup(name.c_str(), allocator); |
79 | 97 | if (!node_names->data[i]) { |
80 | 98 | RMW_SET_ERROR_MSG("could not allocate memory for node name") |
81 | 99 | rcutils_ret = rcutils_string_array_fini(node_names); |
|
0 commit comments