Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit c800708

Browse files
committed
get participant name from user data first
1 parent 8be510a commit c800708

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

rmw_connext_shared_cpp/src/node_names.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <string>
16+
#include <vector>
17+
1518
#include "rcutils/logging_macros.h"
1619
#include "rcutils/strdup.h"
1720

1821
#include "rmw/convert_rcutils_ret_to_rmw_ret.h"
1922
#include "rmw/error_handling.h"
23+
#include "rmw/impl/cpp/key_value.hpp"
2024
#include "rmw/sanity_checks.h"
2125

2226
#include "rmw_connext_shared_cpp/ndds_include.hpp"
@@ -67,15 +71,29 @@ get_node_names(
6771
return RMW_RET_BAD_ALLOC;
6872
}
6973

70-
7174
for (auto i = 1; i < length; ++i) {
7275
DDS::ParticipantBuiltinTopicData pbtd;
7376
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;
7795
}
78-
node_names->data[i] = rcutils_strdup(name, allocator);
96+
node_names->data[i] = rcutils_strdup(name.c_str(), allocator);
7997
if (!node_names->data[i]) {
8098
RMW_SET_ERROR_MSG("could not allocate memory for node name")
8199
rcutils_ret = rcutils_string_array_fini(node_names);

0 commit comments

Comments
 (0)