Skip to content

Commit

Permalink
Include topic type in keyexpr
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Jun 20, 2024
1 parent d3396a5 commit 5700488
Showing 1 changed file with 68 additions and 35 deletions.
103 changes: 68 additions & 35 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,42 @@

namespace
{

//==============================================================================
// A function to take ros topic names and convert them to valid Zenoh keys.
// A function that generates a key expression for message transport of the format
// <ros_domain_id>/<topic_name>/<topic_type>/<topic_hash>

// In particular, Zenoh keys cannot start or end with a /, so this function
// will strip them out.
// The Zenoh key is also prefixed with the ros_domain_id.
// Performance note: at present, this function allocates a new string and copies
// 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)
{
const std::string keyexpr_str = std::to_string(domain_id) +
"/" +
rmw_zenoh_cpp::liveliness::mangle_name(topic_name);
z_owned_keyexpr_t ros_topic_name_to_zenoh_key(
const std::size_t domain_id,
const char * const topic_name,
const char * const topic_type)
{
auto strip_slashes =
[](const char * const str) -> std::string
{
std::string ret = std::string(str);
const std::size_t len = strlen(str);
std::size_t start = 0;
std::size_t end = len - 1;
if (str[0] == '/') {
++start;
}
if (str[end] == '/') {
--end;
}
return ret.substr(start, end - start);
};

const std::string keyexpr_str = std::to_string(domain_id) + "/" +
strip_slashes(topic_name) + "/" +
std::string(topic_type);

return z_keyexpr_new(keyexpr_str.c_str());
}

Expand Down Expand Up @@ -568,7 +589,9 @@ rmw_create_publisher(
});

z_owned_keyexpr_t keyexpr = ros_topic_name_to_zenoh_key(
topic_name, node->context->actual_domain_id);
node->context->actual_domain_id,
topic_name,
publisher_data->type_support->get_name());
auto always_free_ros_keyexpr = rcpputils::make_scope_exit(
[&keyexpr]() {
z_keyexpr_drop(z_move(keyexpr));
Expand Down Expand Up @@ -1360,7 +1383,9 @@ rmw_create_subscription(

z_owned_closure_sample_t callback = z_closure(rmw_zenoh_cpp::sub_data_handler, nullptr, sub_data);
z_owned_keyexpr_t keyexpr = ros_topic_name_to_zenoh_key(
topic_name, node->context->actual_domain_id);
node->context->actual_domain_id,
topic_name,
sub_data->type_support->get_name());
auto always_free_ros_keyexpr = rcpputils::make_scope_exit(
[&keyexpr]() {
z_keyexpr_drop(z_move(keyexpr));
Expand Down Expand Up @@ -2055,17 +2080,6 @@ rmw_create_client(
allocator->deallocate(const_cast<char *>(rmw_client->service_name), allocator->state);
});

client_data->keyexpr = ros_topic_name_to_zenoh_key(
rmw_client->service_name, node->context->actual_domain_id);
auto free_ros_keyexpr = rcpputils::make_scope_exit(
[client_data]() {
z_keyexpr_drop(z_move(client_data->keyexpr));
});
if (!z_keyexpr_check(&client_data->keyexpr)) {
RMW_SET_ERROR_MSG("unable to create zenoh keyexpr.");
return nullptr;
}

// Note: Service request/response types will contain a suffix Request_ or Response_.
// We remove the suffix when appending the type to the liveliness tokens for
// better reusability within GraphCache.
Expand All @@ -2080,6 +2094,21 @@ rmw_create_client(
service_type.c_str(), rmw_client->service_name);
return nullptr;
}


client_data->keyexpr = ros_topic_name_to_zenoh_key(
node->context->actual_domain_id,
rmw_client->service_name,
service_type.c_str());
auto free_ros_keyexpr = rcpputils::make_scope_exit(
[client_data]() {
z_keyexpr_drop(z_move(client_data->keyexpr));
});
if (!z_keyexpr_check(&client_data->keyexpr)) {
RMW_SET_ERROR_MSG("unable to create zenoh keyexpr.");
return nullptr;
}

client_data->entity = rmw_zenoh_cpp::liveliness::Entity::make(
z_info_zid(z_loan(node->context->impl->session)),
std::to_string(node_data->id),
Expand Down Expand Up @@ -2590,8 +2619,26 @@ rmw_create_service(
[rmw_service, allocator]() {
allocator->deallocate(const_cast<char *>(rmw_service->service_name), allocator->state);
});

// Note: Service request/response types will contain a suffix Request_ or Response_.
// We remove the suffix when appending the type to the liveliness tokens for
// better reusability within GraphCache.
std::string service_type = service_data->response_type_support->get_name();
size_t suffix_substring_position = service_type.find("Response_");
if (std::string::npos != suffix_substring_position) {
service_type = service_type.substr(0, suffix_substring_position);
} else {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unexpected type %s for service %s. Report this bug",
service_type.c_str(), rmw_service->service_name);
return nullptr;
}

service_data->keyexpr = ros_topic_name_to_zenoh_key(
rmw_service->service_name, node->context->actual_domain_id);
node->context->actual_domain_id,
rmw_service->service_name,
service_type.c_str());
auto free_ros_keyexpr = rcpputils::make_scope_exit(
[service_data]() {
if (service_data) {
Expand Down Expand Up @@ -2624,20 +2671,6 @@ rmw_create_service(
z_undeclare_queryable(z_move(service_data->qable));
});

// Note: Service request/response types will contain a suffix Request_ or Response_.
// We remove the suffix when appending the type to the liveliness tokens for
// better reusability within GraphCache.
std::string service_type = service_data->response_type_support->get_name();
size_t suffix_substring_position = service_type.find("Response_");
if (std::string::npos != suffix_substring_position) {
service_type = service_type.substr(0, suffix_substring_position);
} else {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unexpected type %s for service %s. Report this bug",
service_type.c_str(), rmw_service->service_name);
return nullptr;
}
service_data->entity = rmw_zenoh_cpp::liveliness::Entity::make(
z_info_zid(z_loan(node->context->impl->session)),
std::to_string(node_data->id),
Expand Down

0 comments on commit 5700488

Please sign in to comment.