Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zc_publisher_matching_listener update #581

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build-resources/opaque-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
============

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 @@ -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.
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