Skip to content

Commit

Permalink
Have rmw_context_impl_s return a shared_ptr to GraphCache
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Sep 3, 2024
1 parent 1591129 commit 7cc52bd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 265 deletions.
181 changes: 5 additions & 176 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ rmw_context_impl_s::Data::Data(
z_owned_session_t session,
std::optional<zc_owned_shm_manager_t> shm_manager,
const std::string & liveliness_str,
std::unique_ptr<rmw_zenoh_cpp::GraphCache> graph_cache,
std::shared_ptr<rmw_zenoh_cpp::GraphCache> graph_cache,
rmw_guard_condition_t * graph_guard_condition)
: allocator_(allocator),
enclave_(std::move(enclave)),
Expand Down Expand Up @@ -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<rmw_zenoh_cpp::GraphCache>(zid);
auto graph_cache = std::make_shared<rmw_zenoh_cpp::GraphCache>(zid);
// Setup liveliness subscriptions for discovery.
std::string liveliness_str = rmw_zenoh_cpp::liveliness::subscription_token(
domain_id);
Expand Down Expand Up @@ -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_zenoh_cpp::GraphCache> rmw_context_impl_s::graph_cache()
{
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(data_->mutex_);
return data_->graph_cache_->set_querying_subscriber_callback(
std::move(keyexpr),
std::move(cb));
return data_->graph_cache_;
}
73 changes: 4 additions & 69 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,73 +75,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<rmw_zenoh_cpp::GraphCache> graph_cache();

private:
// Bundle all class members into a data struct which can be passed as a
Expand All @@ -156,7 +91,7 @@ class rmw_context_impl_s final
z_owned_session_t session,
std::optional<zc_owned_shm_manager_t> shm_manager,
const std::string & liveliness_str,
std::unique_ptr<rmw_zenoh_cpp::GraphCache> graph_cache,
std::shared_ptr<rmw_zenoh_cpp::GraphCache> graph_cache,
rmw_guard_condition_t * graph_guard_condition);

// Subscribe to the ROS graph.
Expand All @@ -182,7 +117,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<rmw_zenoh_cpp::GraphCache> graph_cache_;
std::shared_ptr<rmw_zenoh_cpp::GraphCache> graph_cache_;
// ROS graph liveliness subscriber.
z_owned_subscriber_t graph_subscriber_;
// Equivalent to rmw_dds_common::Context's guard condition
Expand Down
4 changes: 2 additions & 2 deletions rmw_zenoh_cpp/src/rmw_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions rmw_zenoh_cpp/src/rmw_get_node_info_and_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rmw_context_impl_t *>(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,
Expand Down Expand Up @@ -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<rmw_context_impl_t *>(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,
Expand Down Expand Up @@ -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<rmw_context_impl_t *>(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,
Expand Down Expand Up @@ -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<rmw_context_impl_t *>(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,
Expand Down
2 changes: 1 addition & 1 deletion rmw_zenoh_cpp/src/rmw_get_service_names_and_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ rmw_get_service_names_and_types(
rmw_context_impl_t * context_impl = static_cast<rmw_context_impl_t *>(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"
4 changes: 2 additions & 2 deletions rmw_zenoh_cpp/src/rmw_get_topic_endpoint_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rmw_context_impl_t *>(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,
Expand Down Expand Up @@ -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<rmw_context_impl_t *>(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,
Expand Down
2 changes: 1 addition & 1 deletion rmw_zenoh_cpp/src/rmw_get_topic_names_and_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rmw_get_topic_names_and_types(

rmw_context_impl_t * context_impl = static_cast<rmw_context_impl_t *>(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"
Loading

0 comments on commit 7cc52bd

Please sign in to comment.