Skip to content

Commit

Permalink
rename zc_publisher_matching_listener_callback -> zc_publisher_matchi…
Browse files Browse the repository at this point in the history
…ng_listener_declare; (#581)

added zc_publisher_get_matching_status;
  • Loading branch information
DenisBiryukov91 authored Aug 8, 2024
1 parent 04cad65 commit ad54bcb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,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
============

Expand Down
2 changes: 1 addition & 1 deletion examples/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pub_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 15 additions & 5 deletions include/zenoh_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -4516,20 +4516,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.
Expand Down
32 changes: 28 additions & 4 deletions src/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<zc_owned_matching_listener_t>,
publisher: &'static z_loaned_publisher_t,
callback: &mut zc_owned_closure_matching_status_t,
Expand Down Expand Up @@ -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<zc_matching_status_t>,
) -> 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.
Expand All @@ -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
Expand Down

0 comments on commit ad54bcb

Please sign in to comment.