Skip to content

Commit

Permalink
Use zc APIs instead of macros to resolve liveliness api issues
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Nov 16, 2023
1 parent c6bc79b commit 66ee215
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
3 changes: 1 addition & 2 deletions rmw_zenoh_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ find_package(rcutils REQUIRED)
find_package(rosidl_typesupport_fastrtps_c REQUIRED)
find_package(rosidl_typesupport_fastrtps_cpp REQUIRED)
find_package(rmw REQUIRED)
find_package(yaml_cpp_vendor REQUIRED)
find_package(zenoh_c_vendor REQUIRED)
find_package(zenohc REQUIRED)

Expand All @@ -47,6 +46,7 @@ add_library(rmw_zenoh_cpp SHARED
src/rmw_zenoh.cpp
)

add_dependencies(rmw_zenoh_cpp zenohc::lib)
target_link_libraries(rmw_zenoh_cpp
PRIVATE
ament_index_cpp::ament_index_cpp
Expand All @@ -56,7 +56,6 @@ target_link_libraries(rmw_zenoh_cpp
rosidl_typesupport_fastrtps_c::rosidl_typesupport_fastrtps_c
rosidl_typesupport_fastrtps_cpp::rosidl_typesupport_fastrtps_cpp
rmw::rmw
yaml-cpp
zenohc::lib
)

Expand Down
3 changes: 1 addition & 2 deletions rmw_zenoh_cpp/src/rmw_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ rmw_init(const rmw_init_options_t * options, rmw_context_t * context)
z_keyexpr(liveliness_str.c_str()),
z_move(callback),
&sub_options);
// TODO(Yadunund): Uncomment once linker issue is resolved.
// z_drop(z_move(sub_options));
zc_liveliness_subscriber_options_drop(z_move(sub_options));
auto undeclare_z_sub = rcpputils::make_scope_exit(
[context]() {
z_undeclare_subscriber(z_move(context->impl->graph_subscriber));
Expand Down
36 changes: 17 additions & 19 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ rmw_create_node(
// zenohd is not running.
// Put metadata into node->data.
node->data = allocator->zero_allocate(1, sizeof(rmw_node_data_t), allocator->state);
rmw_node_data_t * node_data = static_cast<rmw_node_data_t *>(node->data);
RMW_CHECK_FOR_NULL_WITH_MSG(
node->data,
"unable to allocate memory for node data",
Expand All @@ -183,7 +182,6 @@ rmw_create_node(
node->implementation_identifier = rmw_zenoh_identifier;
node->context = context;


// Uncomment and rely on #if #endif blocks to enable this feature when building with
// zenoh-pico since liveliness is only available in zenoh-c.
// Publish to the graph that a new node is in town
Expand All @@ -195,6 +193,7 @@ rmw_create_node(
// return nullptr;
// }
// Initialize liveliness token for the node to advertise that a new node is in town.
rmw_node_data_t * node_data = static_cast<rmw_node_data_t *>(node->data);
node_data->token = zc_liveliness_declare_token(
z_loan(node->context->impl->session),
z_keyexpr(GenerateToken::node(context->actual_domain_id, namespace_, name).c_str()),
Expand All @@ -207,13 +206,12 @@ rmw_create_node(
z_drop(z_move(node_data->token));
}
});
// TODO(Yadunund): Uncomment this after resolving build error.
// if (!z_check(node_data->token)) {
// RCUTILS_LOG_ERROR_NAMED(
// "rmw_zenoh_cpp",
// "Unable to create liveliness token for the node.");
// return nullptr;
// }
if (!zc_liveliness_token_check(&node_data->token)) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to create liveliness token for the node.");
return nullptr;
}

free_node_data.cancel();
free_namespace.cancel();
Expand Down Expand Up @@ -251,7 +249,8 @@ rmw_destroy_node(rmw_node_t * node)

// Undeclare liveliness token for the node to advertise that the node has ridden off into the sunset.
rmw_node_data_t * node_data = static_cast<rmw_node_data_t *>(node->data);
z_drop(z_move(node_data->token));
zc_liveliness_undeclare_token(z_move(node_data->token));
// z_drop(z_move(node_data->token));

rcutils_allocator_t * allocator = &node->context->options.allocator;

Expand Down Expand Up @@ -585,13 +584,12 @@ rmw_create_publisher(
z_drop(z_move(publisher_data->token));
}
});
// TODO(Yadunund): Uncomment this after resolving build error.
// if (!z_check(publisher_data->token)) {
// RCUTILS_LOG_ERROR_NAMED(
// "rmw_zenoh_cpp",
// "Unable to create liveliness token for the publisher.");
// return nullptr;
// }
if (!zc_liveliness_token_check(&publisher_data->token)) {
RCUTILS_LOG_ERROR_NAMED(
"rmw_zenoh_cpp",
"Unable to create liveliness token for the publisher.");
return nullptr;
}

publisher_data->graph_cache_handle = node->context->impl->graph_cache.add_publisher(
rmw_publisher->topic_name, node->name, node->namespace_,
Expand Down Expand Up @@ -655,8 +653,8 @@ rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher)
// // TODO(Yadunund): Should this really return an error?
// return RMW_RET_ERROR;
// }
// TODO(Yadunund): Fix linker error.
z_drop(z_move(publisher_data->token));
zc_liveliness_undeclare_token(z_move(publisher_data->token));

node->context->impl->graph_cache.remove_publisher(publisher_data->graph_cache_handle);

RMW_TRY_DESTRUCTOR(publisher_data->type_support->~MessageTypeSupport(), MessageTypeSupport, );
Expand Down

0 comments on commit 66ee215

Please sign in to comment.