From 2bb459b446cbaafffad9843af817839b78872709 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Thu, 8 Aug 2024 11:26:04 +0200 Subject: [PATCH] rename zc_publisher_matching_listener_callback -> zc_publisher_matching_listener_declare; added zc_publisher_get_matching_status; --- build-resources/opaque-types/src/lib.rs | 1 - docs/api.rst | 4 ++++ examples/z_pub.c | 2 +- examples/z_pub_shm.c | 2 +- include/zenoh_commons.h | 20 ++++++++++++---- src/publisher.rs | 32 +++++++++++++++++++++---- 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/build-resources/opaque-types/src/lib.rs b/build-resources/opaque-types/src/lib.rs index b0370ceef..1c1a8b7c6 100644 --- a/build-resources/opaque-types/src/lib.rs +++ b/build-resources/opaque-types/src/lib.rs @@ -63,7 +63,6 @@ pub struct CSlice { _context: *mut c_void, } - get_opaque_type_data!(CSlice, z_owned_slice_t); /// A contiguous sequence of bytes owned by some other entity. get_opaque_type_data!(CSlice, z_view_slice_t); diff --git a/docs/api.rst b/docs/api.rst index ddfe3e550..66c57d550 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -573,6 +573,10 @@ Functions .. doxygenfunction:: zc_closure_matching_status_drop .. doxygenfunction:: zc_closure_matching_status_call +.. doxygenfunction:: zc_publisher_get_matching_status +.. doxygenfunction:: zc_publisher_matching_listener_declare +.. doxygenfunction:: zc_publisher_matching_listener_undeclare + Subscription ============ diff --git a/examples/z_pub.c b/examples/z_pub.c index 08e9a98f2..05d5ae2ab 100644 --- a/examples/z_pub.c +++ b/examples/z_pub.c @@ -68,7 +68,7 @@ int main(int argc, char **argv) { if (add_matching_listener) { zc_owned_closure_matching_status_t callback; z_closure(&callback, matching_status_handler, NULL, NULL); - zc_publisher_matching_listener_callback(&listener, z_loan(pub), z_move(callback)); + zc_publisher_matching_listener_declare(&listener, z_loan(pub), z_move(callback)); } #else if (add_matching_listener) { diff --git a/examples/z_pub_shm.c b/examples/z_pub_shm.c index 728b086cf..bf43406c1 100644 --- a/examples/z_pub_shm.c +++ b/examples/z_pub_shm.c @@ -79,7 +79,7 @@ int main(int argc, char **argv) { if (add_matching_listener) { zc_owned_closure_matching_status_t callback; z_closure(&callback, matching_status_handler, NULL, NULL); - zc_publisher_matching_listener_callback(&listener, z_loan(pub), z_move(callback)); + zc_publisher_matching_listener_declare(&listener, z_loan(pub), z_move(callback)); } #else if (add_matching_listener) { diff --git a/include/zenoh_commons.h b/include/zenoh_commons.h index ae89b03b6..e668fbeaf 100644 --- a/include/zenoh_commons.h +++ b/include/zenoh_commons.h @@ -4419,20 +4419,30 @@ ZENOHC_API z_result_t zc_liveliness_undeclare_token(zc_owned_liveliness_token_t #if defined(UNSTABLE) ZENOHC_API enum zc_locality_t zc_locality_default(void); #endif +/** + * Gets publisher matching status - i.e. if there are any subscribers matching its key expression. + * + * @return 0 in case of success, negative error code otherwise (in this case matching_status is not updated). + */ +#if defined(UNSTABLE) +ZENOHC_API +z_result_t zc_publisher_get_matching_status(const struct z_loaned_publisher_t *this_, + struct zc_matching_status_t *matching_status); +#endif /** * Constructs matching listener, registering a callback for notifying subscribers matching with a given publisher. * * @param this_: An unitilized memory location where matching listener will be constructed. The matching listener will be automatically dropped when publisher is dropped. - * @publisher: A publisher to associate with matching listener. - * @callback: A closure that will be called every time the matching status of the publisher changes (If last subscriber, disconnects or when the first subscriber connects). + * @param publisher: A publisher to associate with matching listener. + * @param callback: A closure that will be called every time the matching status of the publisher changes (If last subscriber, disconnects or when the first subscriber connects). * * @return 0 in case of success, negative error code otherwise. */ #if defined(UNSTABLE) ZENOHC_API -z_result_t zc_publisher_matching_listener_callback(zc_owned_matching_listener_t *this_, - const struct z_loaned_publisher_t *publisher, - struct zc_owned_closure_matching_status_t *callback); +z_result_t zc_publisher_matching_listener_declare(zc_owned_matching_listener_t *this_, + const struct z_loaned_publisher_t *publisher, + struct zc_owned_closure_matching_status_t *callback); #endif /** * Undeclares the given matching listener, droping and invalidating it. diff --git a/src/publisher.rs b/src/publisher.rs index 094d75ad0..3877c47a3 100644 --- a/src/publisher.rs +++ b/src/publisher.rs @@ -315,13 +315,13 @@ pub struct zc_matching_status_t { /// Constructs matching listener, registering a callback for notifying subscribers matching with a given publisher. /// /// @param this_: An unitilized memory location where matching listener will be constructed. The matching listener will be automatically dropped when publisher is dropped. -/// @publisher: A publisher to associate with matching listener. -/// @callback: A closure that will be called every time the matching status of the publisher changes (If last subscriber, disconnects or when the first subscriber connects). +/// @param publisher: A publisher to associate with matching listener. +/// @param callback: A closure that will be called every time the matching status of the publisher changes (If last subscriber, disconnects or when the first subscriber connects). /// /// @return 0 in case of success, negative error code otherwise. #[no_mangle] #[allow(clippy::missing_safety_doc)] -pub extern "C" fn zc_publisher_matching_listener_callback( +pub extern "C" fn zc_publisher_matching_listener_declare( this: &mut MaybeUninit, publisher: &'static z_loaned_publisher_t, callback: &mut zc_owned_closure_matching_status_t, @@ -369,6 +369,30 @@ pub extern "C" fn zc_publisher_matching_listener_undeclare( result::Z_OK } +#[cfg(feature = "unstable")] +/// Gets publisher matching status - i.e. if there are any subscribers matching its key expression. +/// +/// @return 0 in case of success, negative error code otherwise (in this case matching_status is not updated). +#[no_mangle] +#[allow(clippy::missing_safety_doc)] +pub extern "C" fn zc_publisher_get_matching_status( + this: &'static z_loaned_publisher_t, + matching_status: &mut MaybeUninit, +) -> result::z_result_t { + match this.as_rust_type_ref().matching_status().wait() { + Ok(s) => { + matching_status.write(zc_matching_status_t { + matching: s.matching_subscribers(), + }); + result::Z_OK + } + Err(e) => { + tracing::error!("{}", e); + result::Z_ENETWORK + } + } +} + /// Undeclares the given publisher, droping and invalidating it. /// /// @return 0 in case of success, negative error code otherwise. @@ -378,7 +402,7 @@ pub extern "C" fn z_undeclare_publisher(this: &mut z_owned_publisher_t) -> resul if let Some(p) = this.as_rust_type_mut().take() { if let Err(e) = p.undeclare().wait() { tracing::error!("{}", e); - return result::Z_EGENERIC; + return result::Z_ENETWORK; } } result::Z_OK