diff --git a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp index c4941450..f49ed2df 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp @@ -45,7 +45,7 @@ void rmw_context_impl_s::graph_sub_data_handler(const z_sample_t * sample, void if (data_ptr == nullptr) { RMW_ZENOH_LOG_ERROR_NAMED( "rmw_zenoh_cpp", - "[graph_sub_data_handler] Unable to lock data_wp." + "[graph_sub_data_handler] Invalid data_ptr." ); return; } @@ -83,7 +83,7 @@ rmw_context_impl_s::Data::Data( z_owned_session_t session, std::optional shm_manager, const std::string & liveliness_str, - std::unique_ptr graph_cache, + std::shared_ptr graph_cache, rmw_guard_condition_t * graph_guard_condition) : allocator_(allocator), enclave_(std::move(enclave)), @@ -227,7 +227,7 @@ rmw_context_impl_s::rmw_context_impl_s( // Initialize the graph cache. const z_id_t zid = z_info_zid(z_loan(session)); - auto graph_cache = std::make_unique(zid); + auto graph_cache = std::make_shared(zid); // Setup liveliness subscriptions for discovery. std::string liveliness_str = rmw_zenoh_cpp::liveliness::subscription_token( domain_id); @@ -417,179 +417,8 @@ bool rmw_context_impl_s::session_is_valid() const } ///============================================================================= -rmw_ret_t rmw_context_impl_s::get_node_names( - rcutils_string_array_t * node_names, - rcutils_string_array_t * node_namespaces, - rcutils_string_array_t * enclaves, - rcutils_allocator_t * allocator) const +std::shared_ptr rmw_context_impl_s::graph_cache() { std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->get_node_names( - node_names, - node_namespaces, - enclaves, - allocator); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::get_topic_names_and_types( - rcutils_allocator_t * allocator, - bool no_demangle, - rmw_names_and_types_t * topic_names_and_types) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->get_topic_names_and_types( - allocator, - no_demangle, - topic_names_and_types); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::publisher_count_matched_subscriptions( - const rmw_publisher_t * publisher, - size_t * subscription_count) -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->publisher_count_matched_subscriptions( - publisher, - subscription_count); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::subscription_count_matched_publishers( - const rmw_subscription_t * subscription, - size_t * publisher_count) -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->subscription_count_matched_publishers( - subscription, - publisher_count); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::get_service_names_and_types( - rcutils_allocator_t * allocator, - rmw_names_and_types_t * service_names_and_types) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->get_service_names_and_types( - allocator, - service_names_and_types); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::count_publishers( - const char * topic_name, - size_t * count) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->count_publishers( - topic_name, - count); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::count_subscriptions( - const char * topic_name, - size_t * count) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->count_subscriptions( - topic_name, - count); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::count_services( - const char * service_name, - size_t * count) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->count_services( - service_name, - count); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::count_clients( - const char * service_name, - size_t * count) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->count_clients( - service_name, - count); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::get_entity_names_and_types_by_node( - rmw_zenoh_cpp::liveliness::EntityType entity_type, - rcutils_allocator_t * allocator, - const char * node_name, - const char * node_namespace, - bool no_demangle, - rmw_names_and_types_t * names_and_types) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->get_entity_names_and_types_by_node( - entity_type, - allocator, - node_name, - node_namespace, - no_demangle, - names_and_types); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::get_entities_info_by_topic( - rmw_zenoh_cpp::liveliness::EntityType entity_type, - rcutils_allocator_t * allocator, - const char * topic_name, - bool no_demangle, - rmw_topic_endpoint_info_array_t * endpoints_info) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->get_entities_info_by_topic( - entity_type, - allocator, - topic_name, - no_demangle, - endpoints_info); -} - -///============================================================================= -rmw_ret_t rmw_context_impl_s::service_server_is_available( - const char * service_name, - const char * service_type, - bool * is_available) const -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->service_server_is_available( - service_name, - service_type, - is_available); -} - -///============================================================================= -void rmw_context_impl_s::set_qos_event_callback( - rmw_zenoh_cpp::liveliness::ConstEntityPtr entity, - const rmw_zenoh_cpp::rmw_zenoh_event_type_t & event_type, - GraphCacheEventCallback callback) -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->set_qos_event_callback( - std::move(entity), - event_type, - std::move(callback)); -} - -///============================================================================= -void rmw_context_impl_s::set_querying_subscriber_callback( - const std::string & keyexpr, - QueryingSubscriberCallback cb) -{ - std::lock_guard lock(data_->mutex_); - return data_->graph_cache_->set_querying_subscriber_callback( - std::move(keyexpr), - std::move(cb)); + return data_->graph_cache_; } diff --git a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp index 0d1296f9..aaa64827 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp @@ -32,9 +32,6 @@ class rmw_context_impl_s final { public: - using GraphCacheEventCallback = rmw_zenoh_cpp::GraphCache::GraphCacheEventCallback; - using QueryingSubscriberCallback = - rmw_zenoh_cpp::GraphCache::QueryingSubscriberCallback; // Constructor that internally initializees the Zenoh session and other artifacts. // Throws an std::runtime_error if any of the initializations fail. // The construction will block until a Zenoh router is detected. @@ -75,73 +72,8 @@ class rmw_context_impl_s final // Returns true if the Zenoh session is valid. bool session_is_valid() const; - rmw_ret_t get_node_names( - rcutils_string_array_t * node_names, - rcutils_string_array_t * node_namespaces, - rcutils_string_array_t * enclaves, - rcutils_allocator_t * allocator) const; - - rmw_ret_t get_topic_names_and_types( - rcutils_allocator_t * allocator, - bool no_demangle, - rmw_names_and_types_t * topic_names_and_types) const; - - rmw_ret_t publisher_count_matched_subscriptions( - const rmw_publisher_t * publisher, - size_t * subscription_count); - - rmw_ret_t subscription_count_matched_publishers( - const rmw_subscription_t * subscription, - size_t * publisher_count); - - rmw_ret_t get_service_names_and_types( - rcutils_allocator_t * allocator, - rmw_names_and_types_t * service_names_and_types) const; - - rmw_ret_t count_publishers( - const char * topic_name, - size_t * count) const; - - rmw_ret_t count_subscriptions( - const char * topic_name, - size_t * count) const; - - rmw_ret_t count_services( - const char * service_name, - size_t * count) const; - - rmw_ret_t count_clients( - const char * service_name, - size_t * count) const; - - rmw_ret_t get_entity_names_and_types_by_node( - rmw_zenoh_cpp::liveliness::EntityType entity_type, - rcutils_allocator_t * allocator, - const char * node_name, - const char * node_namespace, - bool no_demangle, - rmw_names_and_types_t * names_and_types) const; - - rmw_ret_t get_entities_info_by_topic( - rmw_zenoh_cpp::liveliness::EntityType entity_type, - rcutils_allocator_t * allocator, - const char * topic_name, - bool no_demangle, - rmw_topic_endpoint_info_array_t * endpoints_info) const; - - rmw_ret_t service_server_is_available( - const char * service_name, - const char * service_type, - bool * is_available) const; - - void set_qos_event_callback( - rmw_zenoh_cpp::liveliness::ConstEntityPtr entity, - const rmw_zenoh_cpp::rmw_zenoh_event_type_t & event_type, - GraphCacheEventCallback callback); - - void set_querying_subscriber_callback( - const std::string & keyexpr, - QueryingSubscriberCallback cb); + /// Return a shared_ptr to the GraphCache stored in this context. + std::shared_ptr graph_cache(); private: // Bundle all class members into a data struct which can be passed as a @@ -156,7 +88,7 @@ class rmw_context_impl_s final z_owned_session_t session, std::optional shm_manager, const std::string & liveliness_str, - std::unique_ptr graph_cache, + std::shared_ptr graph_cache, rmw_guard_condition_t * graph_guard_condition); // Subscribe to the ROS graph. @@ -182,7 +114,7 @@ class rmw_context_impl_s final // Liveliness keyexpr string to subscribe to for ROS graph changes. std::string liveliness_str_; // Graph cache. - std::unique_ptr graph_cache_; + std::shared_ptr graph_cache_; // ROS graph liveliness subscriber. z_owned_subscriber_t graph_subscriber_; // Equivalent to rmw_dds_common::Context's guard condition diff --git a/rmw_zenoh_cpp/src/rmw_event.cpp b/rmw_zenoh_cpp/src/rmw_event.cpp index 17280be7..6b1856c9 100644 --- a/rmw_zenoh_cpp/src/rmw_event.cpp +++ b/rmw_zenoh_cpp/src/rmw_event.cpp @@ -64,7 +64,7 @@ rmw_publisher_event_init( rmw_event->event_type = event_type; // Register the event with graph cache. - context_impl->set_qos_event_callback( + context_impl->graph_cache()->set_qos_event_callback( pub_data->entity, zenoh_event_type, [pub_data, @@ -126,7 +126,7 @@ rmw_subscription_event_init( return RMW_RET_OK; } - sub_data->context->impl->set_qos_event_callback( + context_impl->graph_cache()->set_qos_event_callback( sub_data->entity, zenoh_event_type, [sub_data, diff --git a/rmw_zenoh_cpp/src/rmw_get_node_info_and_types.cpp b/rmw_zenoh_cpp/src/rmw_get_node_info_and_types.cpp index 4aadc4d2..9e838aec 100644 --- a/rmw_zenoh_cpp/src/rmw_get_node_info_and_types.cpp +++ b/rmw_zenoh_cpp/src/rmw_get_node_info_and_types.cpp @@ -45,7 +45,7 @@ rmw_get_subscriber_names_and_types_by_node( RMW_CHECK_ARGUMENT_FOR_NULL(node->context->impl, RMW_RET_INVALID_ARGUMENT); rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_entity_names_and_types_by_node( + return context_impl->graph_cache()->get_entity_names_and_types_by_node( rmw_zenoh_cpp::liveliness::EntityType::Subscription, allocator, node_name, @@ -75,7 +75,7 @@ rmw_get_publisher_names_and_types_by_node( RMW_CHECK_ARGUMENT_FOR_NULL(node->context->impl, RMW_RET_INVALID_ARGUMENT); rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_entity_names_and_types_by_node( + return context_impl->graph_cache()->get_entity_names_and_types_by_node( rmw_zenoh_cpp::liveliness::EntityType::Publisher, allocator, node_name, @@ -104,7 +104,7 @@ rmw_get_service_names_and_types_by_node( RMW_CHECK_ARGUMENT_FOR_NULL(node->context->impl, RMW_RET_INVALID_ARGUMENT); rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_entity_names_and_types_by_node( + return context_impl->graph_cache()->get_entity_names_and_types_by_node( rmw_zenoh_cpp::liveliness::EntityType::Service, allocator, node_name, @@ -133,7 +133,7 @@ rmw_get_client_names_and_types_by_node( RMW_CHECK_ARGUMENT_FOR_NULL(node->context->impl, RMW_RET_INVALID_ARGUMENT); rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_entity_names_and_types_by_node( + return context_impl->graph_cache()->get_entity_names_and_types_by_node( rmw_zenoh_cpp::liveliness::EntityType::Client, allocator, node_name, diff --git a/rmw_zenoh_cpp/src/rmw_get_service_names_and_types.cpp b/rmw_zenoh_cpp/src/rmw_get_service_names_and_types.cpp index 5dc7cdf3..681b8ceb 100644 --- a/rmw_zenoh_cpp/src/rmw_get_service_names_and_types.cpp +++ b/rmw_zenoh_cpp/src/rmw_get_service_names_and_types.cpp @@ -38,7 +38,7 @@ rmw_get_service_names_and_types( rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_service_names_and_types( + return context_impl->graph_cache()->get_service_names_and_types( allocator, service_names_and_types); } } // extern "C" diff --git a/rmw_zenoh_cpp/src/rmw_get_topic_endpoint_info.cpp b/rmw_zenoh_cpp/src/rmw_get_topic_endpoint_info.cpp index d70ce0c7..3d7570cc 100644 --- a/rmw_zenoh_cpp/src/rmw_get_topic_endpoint_info.cpp +++ b/rmw_zenoh_cpp/src/rmw_get_topic_endpoint_info.cpp @@ -45,7 +45,7 @@ rmw_get_publishers_info_by_topic( RMW_CHECK_ARGUMENT_FOR_NULL(node->context->impl, RMW_RET_INVALID_ARGUMENT); rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_entities_info_by_topic( + return context_impl->graph_cache()->get_entities_info_by_topic( rmw_zenoh_cpp::liveliness::EntityType::Publisher, allocator, topic_name, @@ -73,7 +73,7 @@ rmw_get_subscriptions_info_by_topic( RMW_CHECK_ARGUMENT_FOR_NULL(node->context->impl, RMW_RET_INVALID_ARGUMENT); rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_entities_info_by_topic( + return context_impl->graph_cache()->get_entities_info_by_topic( rmw_zenoh_cpp::liveliness::EntityType::Subscription, allocator, topic_name, diff --git a/rmw_zenoh_cpp/src/rmw_get_topic_names_and_types.cpp b/rmw_zenoh_cpp/src/rmw_get_topic_names_and_types.cpp index 477dc7d9..9a3fb6c2 100644 --- a/rmw_zenoh_cpp/src/rmw_get_topic_names_and_types.cpp +++ b/rmw_zenoh_cpp/src/rmw_get_topic_names_and_types.cpp @@ -39,7 +39,7 @@ rmw_get_topic_names_and_types( rmw_context_impl_t * context_impl = static_cast(node->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->get_topic_names_and_types( + return context_impl->graph_cache()->get_topic_names_and_types( allocator, no_demangle, topic_names_and_types); } } // extern "C" diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 6b68b4bb..4e568317 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -1013,7 +1013,7 @@ rmw_publisher_count_matched_subscriptions( rmw_context_impl_t * context_impl = static_cast(pub_data->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->publisher_count_matched_subscriptions( + return context_impl->graph_cache()->publisher_count_matched_subscriptions( publisher, subscription_count); } @@ -1485,7 +1485,7 @@ rmw_create_subscription( } // Register the querying subscriber with the graph cache to get latest // messages from publishers that were discovered after their first publication. - context_impl->set_querying_subscriber_callback( + context_impl->graph_cache()->set_querying_subscriber_callback( sub_data->entity->topic_info()->topic_keyexpr_, [sub_data](const std::string & queryable_prefix) -> void { @@ -1653,7 +1653,7 @@ rmw_subscription_count_matched_publishers( rmw_context_impl_t * context_impl = static_cast(sub_data->context->impl); RMW_CHECK_ARGUMENT_FOR_NULL(context_impl, RMW_RET_INVALID_ARGUMENT); - return context_impl->subscription_count_matched_publishers( + return context_impl->graph_cache()->subscription_count_matched_publishers( subscription, publisher_count); } @@ -3649,7 +3649,7 @@ rmw_get_node_names( rcutils_allocator_t * allocator = &node->context->options.allocator; RMW_CHECK_ARGUMENT_FOR_NULL(allocator, RMW_RET_INVALID_ARGUMENT); - return node->context->impl->get_node_names( + return node->context->impl->graph_cache()->get_node_names( node_names, node_namespaces, nullptr, allocator); } @@ -3672,7 +3672,7 @@ rmw_get_node_names_with_enclaves( rcutils_allocator_t * allocator = &node->context->options.allocator; RMW_CHECK_ARGUMENT_FOR_NULL(allocator, RMW_RET_INVALID_ARGUMENT); - return node->context->impl->get_node_names( + return node->context->impl->graph_cache()->get_node_names( node_names, node_namespaces, enclaves, allocator); } @@ -3703,7 +3703,7 @@ rmw_count_publishers( } RMW_CHECK_ARGUMENT_FOR_NULL(count, RMW_RET_INVALID_ARGUMENT); - return node->context->impl->count_publishers(topic_name, count); + return node->context->impl->graph_cache()->count_publishers(topic_name, count); } //============================================================================== @@ -3733,7 +3733,7 @@ rmw_count_subscribers( } RMW_CHECK_ARGUMENT_FOR_NULL(count, RMW_RET_INVALID_ARGUMENT); - return node->context->impl->count_subscriptions(topic_name, count); + return node->context->impl->graph_cache()->count_subscriptions(topic_name, count); } //============================================================================== @@ -3763,7 +3763,7 @@ rmw_count_clients( } RMW_CHECK_ARGUMENT_FOR_NULL(count, RMW_RET_INVALID_ARGUMENT); - return node->context->impl->count_clients(service_name, count); + return node->context->impl->graph_cache()->count_clients(service_name, count); } //============================================================================== @@ -3793,7 +3793,7 @@ rmw_count_services( } RMW_CHECK_ARGUMENT_FOR_NULL(count, RMW_RET_INVALID_ARGUMENT); - return node->context->impl->count_services(service_name, count); + return node->context->impl->graph_cache()->count_services(service_name, count); } //============================================================================== @@ -3893,7 +3893,7 @@ rmw_service_server_is_available( return RMW_RET_INVALID_ARGUMENT; } - return node->context->impl->service_server_is_available( + return node->context->impl->graph_cache()->service_server_is_available( client->service_name, service_type.c_str(), is_available); }