diff --git a/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp index 4efdb16f..c9033c6c 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp @@ -193,7 +193,7 @@ std::shared_ptr ClientData::make( response_type_support }); - if (!client_data->init(z_loan(session->_0))) { + if (!client_data->init(session)) { // init() already set the error. return nullptr; } @@ -226,31 +226,18 @@ ClientData::ClientData( } ///============================================================================= -bool ClientData::init(const z_loaned_session_t * session) +bool ClientData::init(const std::shared_ptr & session) { std::string topic_keyexpr = this->entity_->topic_info().value().topic_keyexpr_; - auto free_ros_keyexpr = rcpputils::make_scope_exit( - [this]() { - z_drop(z_move(this->keyexpr_)); - }); - if (z_keyexpr_from_str(&this->keyexpr_, topic_keyexpr.c_str()) != Z_OK) { - RMW_SET_ERROR_MSG("unable to create zenoh keyexpr."); - return false; - } + keyexpr_ = zenoh::KeyExpr(topic_keyexpr); std::string liveliness_keyexpr = this->entity_->liveliness_keyexpr(); - z_view_keyexpr_t liveliness_ke; - z_view_keyexpr_from_str(&liveliness_ke, liveliness_keyexpr.c_str()); - auto free_token = rcpputils::make_scope_exit( - [this]() { - z_drop(z_move(this->token_)); - }); - if(zc_liveliness_declare_token( - session, - &this->token_, - z_loan(liveliness_ke), - NULL - ) != Z_OK) + zenoh::ZResult err; + this->token_ = session->liveliness_declare_token( + zenoh::KeyExpr(liveliness_keyexpr), + zenoh::Session::LivelinessDeclarationOptions::create_default(), + &err); + if (err != Z_OK) { RMW_ZENOH_LOG_ERROR_NAMED( "rmw_zenoh_cpp", @@ -258,9 +245,6 @@ bool ClientData::init(const z_loaned_session_t * session) return false; } - free_ros_keyexpr.cancel(); - free_token.cancel(); - return true; } @@ -289,7 +273,7 @@ void ClientData::add_new_reply(std::unique_ptr reply) { // Log warning if message is discarded due to hitting the queue depth z_view_string_t keystr; - z_keyexpr_as_view_string(z_loan(keyexpr_), &keystr); + z_keyexpr_as_view_string(z_loan(keyexpr_.value()._0), &keystr); RMW_ZENOH_LOG_ERROR_NAMED( "rmw_zenoh_cpp", "Query queue depth of %ld reached, discarding oldest Query " @@ -451,7 +435,7 @@ rmw_ret_t ClientData::send_request( z_closure(&callback, client_data_handler, client_data_drop, this); z_get( context_impl->session(), - z_loan(keyexpr_), "", + z_loan(keyexpr_.value()._0), "", z_move(callback), &opts); @@ -510,9 +494,15 @@ void ClientData::_shutdown() } // Unregister this node from the ROS graph. - zc_liveliness_undeclare_token(z_move(token_)); - - z_drop(z_move(keyexpr_)); + zenoh::ZResult err; + std::move(token_).value().undeclare(&err); + if (err != Z_OK) + { + RMW_ZENOH_LOG_ERROR_NAMED( + "rmw_zenoh_cpp", + "Unable to undeclare liveliness token"); + return; + } is_shutdown_ = true; } diff --git a/rmw_zenoh_cpp/src/detail/rmw_client_data.hpp b/rmw_zenoh_cpp/src/detail/rmw_client_data.hpp index 4b3398a9..b8a387d7 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_client_data.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_client_data.hpp @@ -109,7 +109,7 @@ class ClientData final : public std::enable_shared_from_this std::shared_ptr response_type_support); // Initialize the Zenoh objects for this entity. - bool init(const z_loaned_session_t * session); + bool init(const std::shared_ptr & session); // Shutdown this client (the mutex is expected to be held by the caller). void _shutdown(); @@ -122,9 +122,9 @@ class ClientData final : public std::enable_shared_from_this // The Entity generated for the service. std::shared_ptr entity_; // An owned keyexpression. - z_owned_keyexpr_t keyexpr_; + std::optional keyexpr_; // Liveliness token for the service. - zc_owned_liveliness_token_t token_; + std::optional token_; // Type support fields. const void * request_type_support_impl_; const void * response_type_support_impl_; diff --git a/rmw_zenoh_cpp/src/detail/rmw_node_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_node_data.cpp index 10f3cd1a..c847e5d1 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_node_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_node_data.cpp @@ -58,20 +58,18 @@ std::shared_ptr NodeData::make( // Create the liveliness token. std::string liveliness_keyexpr = entity->liveliness_keyexpr(); - z_view_keyexpr_t liveliness_ke; - z_view_keyexpr_from_str(&liveliness_ke, liveliness_keyexpr.c_str()); - zc_owned_liveliness_token_t token; - auto free_token = rcpputils::make_scope_exit( - [&token]() { - z_drop(z_move(token)); - }); - if (zc_liveliness_declare_token(z_loan(session->_0), &token, z_loan(liveliness_ke), NULL) != Z_OK) { + zenoh::ZResult err; + auto token = session->liveliness_declare_token( + zenoh::KeyExpr(liveliness_keyexpr), + zenoh::Session::LivelinessDeclarationOptions::create_default(), + &err); + if (err != Z_OK) + { RMW_ZENOH_LOG_ERROR_NAMED( "rmw_zenoh_cpp", "Unable to create liveliness token for the node."); return nullptr; } - free_token.cancel(); return std::shared_ptr( new NodeData{ @@ -87,7 +85,7 @@ NodeData::NodeData( const rmw_node_t * const node, std::size_t id, std::shared_ptr entity, - zc_owned_liveliness_token_t token) + zenoh::LivelinessToken token) : node_(node), id_(std::move(id)), entity_(std::move(entity)), @@ -452,7 +450,15 @@ rmw_ret_t NodeData::shutdown() } // Unregister this node from the ROS graph. - zc_liveliness_undeclare_token(z_move(token_)); + zenoh::ZResult err; + std::move(token_).value().undeclare(&err); + if (err != Z_OK) + { + RMW_ZENOH_LOG_ERROR_NAMED( + "rmw_zenoh_cpp", + "Unable to undeclare liveliness token"); + return RMW_RET_ERROR; + } is_shutdown_ = true; return ret; diff --git a/rmw_zenoh_cpp/src/detail/rmw_node_data.hpp b/rmw_zenoh_cpp/src/detail/rmw_node_data.hpp index ecf53718..5e1a7714 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_node_data.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_node_data.hpp @@ -126,7 +126,7 @@ class NodeData final const rmw_node_t * const node, std::size_t id, std::shared_ptr entity, - zc_owned_liveliness_token_t token); + zenoh::LivelinessToken token); // Internal mutex. mutable std::mutex mutex_; // The rmw_node_t associated with this NodeData. @@ -137,7 +137,7 @@ class NodeData final // The Entity generated for the node. std::shared_ptr entity_; // Liveliness token for the node. - zc_owned_liveliness_token_t token_; + std::optional token_; // Shutdown flag. bool is_shutdown_; // Map of publishers. diff --git a/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp index 092df1b2..f6d63d63 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp @@ -166,16 +166,12 @@ std::shared_ptr PublisherData::make( } std::string liveliness_keyexpr = entity->liveliness_keyexpr(); - z_view_keyexpr_t liveliness_ke; - z_view_keyexpr_from_str(&liveliness_ke, liveliness_keyexpr.c_str()); - zc_owned_liveliness_token_t token; - auto free_token = rcpputils::make_scope_exit( - [&token]() { - z_drop(z_move(token)); - }); - if (zc_liveliness_declare_token( - z_loan(session->_0), &token, z_loan(liveliness_ke), - NULL) != Z_OK) + zenoh::ZResult err; + auto token = session->liveliness_declare_token( + zenoh::KeyExpr(liveliness_keyexpr), + zenoh::Session::LivelinessDeclarationOptions::create_default(), + &err); + if (err != Z_OK) { RMW_ZENOH_LOG_ERROR_NAMED( "rmw_zenoh_cpp", @@ -183,7 +179,6 @@ std::shared_ptr PublisherData::make( return nullptr; } - free_token.cancel(); undeclare_z_publisher_cache.cancel(); undeclare_z_publisher.cancel(); @@ -205,7 +200,7 @@ PublisherData::PublisherData( std::shared_ptr entity, z_owned_publisher_t pub, std::optional pub_cache, - zc_owned_liveliness_token_t token, + zenoh::LivelinessToken token, const void * type_support_impl, std::unique_ptr type_support) : rmw_node_(rmw_node), @@ -428,7 +423,15 @@ rmw_ret_t PublisherData::shutdown() } // Unregister this publisher from the ROS graph. - zc_liveliness_undeclare_token(z_move(token_)); + zenoh::ZResult err; + std::move(token_).value().undeclare(&err); + if (err != Z_OK) + { + RMW_ZENOH_LOG_ERROR_NAMED( + "rmw_zenoh_cpp", + "Unable to undeclare liveliness token"); + return RMW_RET_ERROR; + } if (pub_cache_.has_value()) { z_drop(z_move(pub_cache_.value())); } diff --git a/rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp b/rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp index 1893360b..f8aeb0ad 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp @@ -85,7 +85,7 @@ class PublisherData final std::shared_ptr entity, z_owned_publisher_t pub, std::optional pub_cache, - zc_owned_liveliness_token_t token, + zenoh::LivelinessToken token, const void * type_support_impl, std::unique_ptr type_support); @@ -100,7 +100,7 @@ class PublisherData final // Optional publication cache when durability is transient_local. std::optional pub_cache_; // Liveliness token for the publisher. - zc_owned_liveliness_token_t token_; + std::optional token_; // Type support fields const void * type_support_impl_; std::unique_ptr type_support_; diff --git a/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp index a32e4e11..ff32a8ad 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp @@ -178,20 +178,12 @@ std::shared_ptr ServiceData::make( } std::string liveliness_keyexpr = service_data->entity_->liveliness_keyexpr(); - z_view_keyexpr_t liveliness_ke; - if (z_view_keyexpr_from_str(&liveliness_ke, liveliness_keyexpr.c_str()) != Z_OK) { - RMW_SET_ERROR_MSG("unable to create zenoh keyexpr."); - return nullptr; - } - auto free_token = rcpputils::make_scope_exit( - [service_data]() { - if (service_data != nullptr) { - z_drop(z_move(service_data->token_)); - } - }); - if (zc_liveliness_declare_token( - z_loan(session->_0), &service_data->token_, z_loan(liveliness_ke), - NULL) != Z_OK) + zenoh::ZResult err; + service_data->token_ = session->liveliness_declare_token( + zenoh::KeyExpr(liveliness_keyexpr), + zenoh::Session::LivelinessDeclarationOptions::create_default(), + &err); + if (err != Z_OK) { RMW_ZENOH_LOG_ERROR_NAMED( "rmw_zenoh_cpp", @@ -200,7 +192,6 @@ std::shared_ptr ServiceData::make( } undeclare_z_queryable.cancel(); - free_token.cancel(); return service_data; } @@ -492,7 +483,15 @@ rmw_ret_t ServiceData::shutdown() } // Unregister this node from the ROS graph. - zc_liveliness_undeclare_token(z_move(token_)); + zenoh::ZResult err; + std::move(token_).value().undeclare(&err); + if (err != Z_OK) + { + RMW_ZENOH_LOG_ERROR_NAMED( + "rmw_zenoh_cpp", + "Unable to undeclare liveliness token"); + return RMW_RET_ERROR; + } z_undeclare_queryable(z_move(qable_)); is_shutdown_ = true; diff --git a/rmw_zenoh_cpp/src/detail/rmw_service_data.hpp b/rmw_zenoh_cpp/src/detail/rmw_service_data.hpp index 7ba3d276..431cdde4 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_service_data.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_service_data.hpp @@ -106,7 +106,7 @@ class ServiceData final // An owned queryable. z_owned_queryable_t qable_; // Liveliness token for the service. - zc_owned_liveliness_token_t token_; + std::optional token_; // Type support fields. const void * request_type_support_impl_; const void * response_type_support_impl_; diff --git a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp index e40f041f..462c1770 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp @@ -320,15 +320,12 @@ bool SubscriptionData::init() // Publish to the graph that a new subscription is in town. std::string liveliness_keyexpr = entity_->liveliness_keyexpr(); - z_view_keyexpr_t liveliness_ke; - z_view_keyexpr_from_str(&liveliness_ke, liveliness_keyexpr.c_str()); - - auto free_token = rcpputils::make_scope_exit( - [this]() { - z_drop(z_move(token_)); - }); - if (zc_liveliness_declare_token( - context_impl->session(), &token_, z_loan(liveliness_ke), NULL) != Z_OK) + zenoh::ZResult err; + token_ = context_impl->session_cpp()->liveliness_declare_token( + zenoh::KeyExpr(liveliness_keyexpr), + zenoh::Session::LivelinessDeclarationOptions::create_default(), + &err); + if (err != Z_OK) { RMW_ZENOH_LOG_ERROR_NAMED( "rmw_zenoh_cpp", @@ -337,7 +334,6 @@ bool SubscriptionData::init() } undeclare_z_sub.cancel(); - free_token.cancel(); initialized_ = true; @@ -396,7 +392,15 @@ rmw_ret_t SubscriptionData::shutdown() graph_cache_->remove_qos_event_callbacks(entity_->keyexpr_hash()); // Unregister this subscription from the ROS graph. - zc_liveliness_undeclare_token(z_move(token_)); + zenoh::ZResult err; + std::move(token_).value().undeclare(&err); + if (err != Z_OK) + { + RMW_ZENOH_LOG_ERROR_NAMED( + "rmw_zenoh_cpp", + "Unable to undeclare liveliness token"); + return RMW_RET_ERROR; + } z_owned_subscriber_t * sub = std::get_if(&sub_); if (sub != nullptr) { diff --git a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp index dd90499a..7eeb0fa7 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.hpp @@ -139,7 +139,7 @@ class SubscriptionData final : public std::enable_shared_from_this sub_; // Liveliness token for the subscription. - zc_owned_liveliness_token_t token_; + std::optional token_; // Type support fields const void * type_support_impl_; std::unique_ptr type_support_;