diff --git a/CMakeLists.txt b/CMakeLists.txt index f9fd2e845..e5a7e6a45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -243,6 +243,7 @@ set(Z_FEATURE_TCP_NODELAY 1 CACHE STRING "Toggle TCP_NODELAY") set(Z_FEATURE_LOCAL_SUBSCRIBER 0 CACHE STRING "Toggle local subscriptions") set(Z_FEATURE_PUBLISHER_SESSION_CHECK 1 CACHE STRING "Toggle publisher session check") set(Z_FEATURE_BATCHING 1 CACHE STRING "Toggle batching") +set(Z_FEATURE_MATCHING 1 CACHE STRING "Toggle matching feature") set(Z_FEATURE_RX_CACHE 0 CACHE STRING "Toggle RX_CACHE") set(Z_FEATURE_AUTO_RECONNECT 1 CACHE STRING "Toggle automatic reconnection") diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index ddd1dc11d..9b6a7adcf 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -20,9 +20,19 @@ #include #include -#include "zenoh-pico/system/platform.h" - #if Z_FEATURE_PUBLICATION == 1 + +#if defined(Z_FEATURE_MATCHING) +void matching_status_handler(const z_matching_status_t *matching_status, void *arg) { + (void)arg; + if (matching_status->matching) { + printf("Publisher has matching subscribers.\n"); + } else { + printf("Publisher has NO MORE matching subscribers.\n"); + } +} +#endif + int main(int argc, char **argv) { const char *keyexpr = "demo/example/zenoh-pico-pub"; char *const default_value = "Pub from Pico!"; @@ -31,9 +41,10 @@ int main(int argc, char **argv) { char *clocator = NULL; char *llocator = NULL; int n = 2147483647; // max int value by default + bool add_matching_listener = false; int opt; - while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) { + while ((opt = getopt(argc, argv, "k:v:e:m:l:n:a")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -53,6 +64,9 @@ int main(int argc, char **argv) { case 'n': n = atoi(optarg); break; + case 'a': + add_matching_listener = true; + break; case '?': if (optopt == 'k' || optopt == 'v' || optopt == 'e' || optopt == 'm' || optopt == 'l' || optopt == 'n') { @@ -104,6 +118,17 @@ int main(int argc, char **argv) { return -1; } + if (add_matching_listener) { +#if defined(Z_FEATURE_MATCHING) + z_owned_closure_matching_status_t callback; + z_closure(&callback, matching_status_handler, NULL, NULL); + z_publisher_declare_background_matching_listener(z_loan(pub), z_move(callback)); +#else + printf("ERROR: Zenoh pico was compiled without Z_FEATURE_MATCHING but this example requires it.\n"); + return -2; +#endif + } + // Publish data printf("Press CTRL-C to quit...\n"); char buf[256]; diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index f60ba22bd..51355df25 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -37,42 +37,44 @@ */ #define z_loan(x) _Generic((x), \ - z_owned_keyexpr_t : z_keyexpr_loan, \ - z_view_keyexpr_t : z_view_keyexpr_loan, \ - z_owned_config_t : z_config_loan, \ - z_owned_session_t : z_session_loan, \ - z_owned_subscriber_t : z_subscriber_loan, \ - z_owned_publisher_t : z_publisher_loan, \ - z_owned_queryable_t : z_queryable_loan, \ - z_owned_liveliness_token_t : z_liveliness_token_loan, \ - z_owned_reply_t : z_reply_loan, \ - z_owned_hello_t : z_hello_loan, \ - z_owned_string_t : z_string_loan, \ - z_view_string_t : z_view_string_loan, \ - z_owned_string_array_t : z_string_array_loan, \ - z_owned_sample_t : z_sample_loan, \ - z_owned_query_t : z_query_loan, \ - z_owned_slice_t : z_slice_loan, \ - z_view_slice_t : z_view_slice_loan, \ - z_owned_bytes_t : z_bytes_loan, \ - z_owned_encoding_t : z_encoding_loan, \ - z_owned_task_t : z_task_loan, \ - z_owned_mutex_t : z_mutex_loan, \ - z_owned_condvar_t : z_condvar_loan, \ - z_owned_fifo_handler_query_t : z_fifo_handler_query_loan, \ - z_owned_fifo_handler_reply_t : z_fifo_handler_reply_loan, \ - z_owned_fifo_handler_sample_t : z_fifo_handler_sample_loan, \ - z_owned_ring_handler_query_t : z_ring_handler_query_loan, \ - z_owned_ring_handler_reply_t : z_ring_handler_reply_loan, \ - z_owned_ring_handler_sample_t : z_ring_handler_sample_loan, \ - z_owned_reply_err_t : z_reply_err_loan, \ - z_owned_closure_sample_t : z_closure_sample_loan, \ - z_owned_closure_reply_t : z_closure_reply_loan, \ - z_owned_closure_query_t : z_closure_query_loan, \ - z_owned_closure_hello_t : z_closure_hello_loan, \ - z_owned_closure_zid_t : z_closure_zid_loan, \ - ze_owned_serializer_t : ze_serializer_loan, \ - z_owned_bytes_writer_t : z_bytes_writer_loan \ + z_owned_keyexpr_t : z_keyexpr_loan, \ + z_view_keyexpr_t : z_view_keyexpr_loan, \ + z_owned_config_t : z_config_loan, \ + z_owned_session_t : z_session_loan, \ + z_owned_subscriber_t : z_subscriber_loan, \ + z_owned_publisher_t : z_publisher_loan, \ + z_owned_matching_listener_t : z_matching_listener_loan, \ + z_owned_queryable_t : z_queryable_loan, \ + z_owned_liveliness_token_t : z_liveliness_token_loan, \ + z_owned_reply_t : z_reply_loan, \ + z_owned_hello_t : z_hello_loan, \ + z_owned_string_t : z_string_loan, \ + z_view_string_t : z_view_string_loan, \ + z_owned_string_array_t : z_string_array_loan, \ + z_owned_sample_t : z_sample_loan, \ + z_owned_query_t : z_query_loan, \ + z_owned_slice_t : z_slice_loan, \ + z_view_slice_t : z_view_slice_loan, \ + z_owned_bytes_t : z_bytes_loan, \ + z_owned_encoding_t : z_encoding_loan, \ + z_owned_task_t : z_task_loan, \ + z_owned_mutex_t : z_mutex_loan, \ + z_owned_condvar_t : z_condvar_loan, \ + z_owned_fifo_handler_query_t : z_fifo_handler_query_loan, \ + z_owned_fifo_handler_reply_t : z_fifo_handler_reply_loan, \ + z_owned_fifo_handler_sample_t : z_fifo_handler_sample_loan, \ + z_owned_ring_handler_query_t : z_ring_handler_query_loan, \ + z_owned_ring_handler_reply_t : z_ring_handler_reply_loan, \ + z_owned_ring_handler_sample_t : z_ring_handler_sample_loan, \ + z_owned_reply_err_t : z_reply_err_loan, \ + z_owned_closure_sample_t : z_closure_sample_loan, \ + z_owned_closure_reply_t : z_closure_reply_loan, \ + z_owned_closure_query_t : z_closure_query_loan, \ + z_owned_closure_hello_t : z_closure_hello_loan, \ + z_owned_closure_zid_t : z_closure_zid_loan, \ + z_owned_closure_matching_status_t : z_closure_matching_status_loan, \ + ze_owned_serializer_t : ze_serializer_loan, \ + z_owned_bytes_writer_t : z_bytes_writer_loan \ )(&x) #define z_loan_mut(x) _Generic((x), \ @@ -80,6 +82,7 @@ z_owned_config_t : z_config_loan_mut, \ z_owned_session_t : z_session_loan_mut, \ z_owned_publisher_t : z_publisher_loan_mut, \ + z_owned_matching_listener_t : z_matching_listener_loan_mut, \ z_owned_queryable_t : z_queryable_loan_mut, \ z_owned_liveliness_token_t : z_liveliness_token_loan_mut, \ z_owned_subscriber_t : z_subscriber_loan_mut, \ @@ -108,39 +111,41 @@ * x: The instance to drop. */ #define z_drop(x) _Generic((x), \ - z_moved_keyexpr_t* : z_keyexpr_drop, \ - z_moved_config_t* : z_config_drop, \ - z_moved_session_t* : z_session_drop, \ - z_moved_subscriber_t* : z_subscriber_drop, \ - z_moved_publisher_t* : z_publisher_drop, \ - z_moved_queryable_t* : z_queryable_drop, \ - z_moved_liveliness_token_t* : z_liveliness_token_drop, \ - z_moved_reply_t* : z_reply_drop, \ - z_moved_hello_t* : z_hello_drop, \ - z_moved_string_t* : z_string_drop, \ - z_moved_string_array_t* : z_string_array_drop, \ - z_moved_sample_t* : z_sample_drop, \ - z_moved_query_t* : z_query_drop, \ - z_moved_encoding_t* : z_encoding_drop, \ - z_moved_slice_t* : z_slice_drop, \ - z_moved_bytes_t* : z_bytes_drop, \ - z_moved_closure_sample_t* : z_closure_sample_drop, \ - z_moved_closure_query_t* : z_closure_query_drop, \ - z_moved_closure_reply_t* : z_closure_reply_drop, \ - z_moved_closure_hello_t* : z_closure_hello_drop, \ - z_moved_closure_zid_t* : z_closure_zid_drop, \ - z_moved_task_t* : z_task_join, \ - z_moved_mutex_t* : z_mutex_drop, \ - z_moved_condvar_t* : z_condvar_drop, \ - z_moved_fifo_handler_query_t* : z_fifo_handler_query_drop, \ - z_moved_fifo_handler_reply_t* : z_fifo_handler_reply_drop, \ - z_moved_fifo_handler_sample_t* : z_fifo_handler_sample_drop, \ - z_moved_ring_handler_query_t* : z_ring_handler_query_drop, \ - z_moved_ring_handler_reply_t* : z_ring_handler_reply_drop, \ - z_moved_ring_handler_sample_t* : z_ring_handler_sample_drop, \ - z_moved_reply_err_t* : z_reply_err_drop, \ - ze_moved_serializer_t* : ze_serializer_drop, \ - z_moved_bytes_writer_t* : z_bytes_writer_drop \ + z_moved_keyexpr_t* : z_keyexpr_drop, \ + z_moved_config_t* : z_config_drop, \ + z_moved_session_t* : z_session_drop, \ + z_moved_subscriber_t* : z_subscriber_drop, \ + z_moved_publisher_t* : z_publisher_drop, \ + z_moved_matching_listener_t* : z_matching_listener_drop, \ + z_moved_queryable_t* : z_queryable_drop, \ + z_moved_liveliness_token_t* : z_liveliness_token_drop, \ + z_moved_reply_t* : z_reply_drop, \ + z_moved_hello_t* : z_hello_drop, \ + z_moved_string_t* : z_string_drop, \ + z_moved_string_array_t* : z_string_array_drop, \ + z_moved_sample_t* : z_sample_drop, \ + z_moved_query_t* : z_query_drop, \ + z_moved_encoding_t* : z_encoding_drop, \ + z_moved_slice_t* : z_slice_drop, \ + z_moved_bytes_t* : z_bytes_drop, \ + z_moved_closure_sample_t* : z_closure_sample_drop, \ + z_moved_closure_query_t* : z_closure_query_drop, \ + z_moved_closure_reply_t* : z_closure_reply_drop, \ + z_moved_closure_hello_t* : z_closure_hello_drop, \ + z_moved_closure_zid_t* : z_closure_zid_drop, \ + z_moved_closure_matching_status_t* : z_closure_matching_status_drop, \ + z_moved_task_t* : z_task_join, \ + z_moved_mutex_t* : z_mutex_drop, \ + z_moved_condvar_t* : z_condvar_drop, \ + z_moved_fifo_handler_query_t* : z_fifo_handler_query_drop, \ + z_moved_fifo_handler_reply_t* : z_fifo_handler_reply_drop, \ + z_moved_fifo_handler_sample_t* : z_fifo_handler_sample_drop, \ + z_moved_ring_handler_query_t* : z_ring_handler_query_drop, \ + z_moved_ring_handler_reply_t* : z_ring_handler_reply_drop, \ + z_moved_ring_handler_sample_t* : z_ring_handler_sample_drop, \ + z_moved_reply_err_t* : z_reply_err_drop, \ + ze_moved_serializer_t* : ze_serializer_drop, \ + z_moved_bytes_writer_t* : z_bytes_writer_drop \ )(x) /** @@ -154,30 +159,32 @@ */ #define z_internal_check(x) _Generic((x), \ - z_owned_keyexpr_t : z_internal_keyexpr_check, \ - z_owned_reply_err_t : z_internal_reply_err_check, \ - z_owned_config_t : z_internal_config_check, \ - z_owned_session_t : z_internal_session_check, \ - z_owned_subscriber_t : z_internal_subscriber_check, \ - z_owned_publisher_t : z_internal_publisher_check, \ - z_owned_queryable_t : z_internal_queryable_check, \ - z_owned_liveliness_token_t : z_internal_liveliness_token_check, \ - z_owned_reply_t : z_internal_reply_check, \ - z_owned_hello_t : z_internal_hello_check, \ - z_owned_string_t : z_internal_string_check, \ - z_owned_string_array_t : z_internal_string_array_check, \ - z_owned_closure_sample_t : z_internal_closure_sample_check, \ - z_owned_closure_query_t : z_internal_closure_query_check, \ - z_owned_closure_reply_t : z_internal_closure_reply_check, \ - z_owned_closure_hello_t : z_internal_closure_hello_check, \ - z_owned_closure_zid_t : z_internal_closure_zid_check, \ - z_owned_slice_t : z_internal_slice_check, \ - z_owned_bytes_t : z_internal_bytes_check, \ - z_owned_sample_t : z_internal_sample_check, \ - z_owned_query_t : z_internal_query_check, \ - z_owned_encoding_t : z_internal_encoding_check, \ - ze_owned_serializer_t : ze_internal_serializer_check, \ - z_owned_bytes_writer_t : z_internal_bytes_writer_check \ + z_owned_keyexpr_t : z_internal_keyexpr_check, \ + z_owned_reply_err_t : z_internal_reply_err_check, \ + z_owned_config_t : z_internal_config_check, \ + z_owned_session_t : z_internal_session_check, \ + z_owned_subscriber_t : z_internal_subscriber_check, \ + z_owned_publisher_t : z_internal_publisher_check, \ + z_owned_matching_listener_t : z_internal_matching_listener_check, \ + z_owned_queryable_t : z_internal_queryable_check, \ + z_owned_liveliness_token_t : z_internal_liveliness_token_check, \ + z_owned_reply_t : z_internal_reply_check, \ + z_owned_hello_t : z_internal_hello_check, \ + z_owned_string_t : z_internal_string_check, \ + z_owned_string_array_t : z_internal_string_array_check, \ + z_owned_closure_sample_t : z_internal_closure_sample_check, \ + z_owned_closure_query_t : z_internal_closure_query_check, \ + z_owned_closure_reply_t : z_internal_closure_reply_check, \ + z_owned_closure_hello_t : z_internal_closure_hello_check, \ + z_owned_closure_zid_t : z_internal_closure_zid_check, \ + z_owned_closure_matching_status_t : z_internal_closure_matching_status_check, \ + z_owned_slice_t : z_internal_slice_check, \ + z_owned_bytes_t : z_internal_bytes_check, \ + z_owned_sample_t : z_internal_sample_check, \ + z_owned_query_t : z_internal_query_check, \ + z_owned_encoding_t : z_internal_encoding_check, \ + ze_owned_serializer_t : ze_internal_serializer_check, \ + z_owned_bytes_writer_t : z_internal_bytes_writer_check \ )(&x) /** @@ -187,11 +194,12 @@ * x: The closure to call */ #define z_call(x, ...) \ - _Generic((x), z_loaned_closure_sample_t : z_closure_sample_call, \ - z_loaned_closure_query_t : z_closure_query_call, \ - z_loaned_closure_reply_t : z_closure_reply_call, \ - z_loaned_closure_hello_t : z_closure_hello_call, \ - z_loaned_closure_zid_t : z_closure_zid_call \ + _Generic((x), z_loaned_closure_sample_t : z_closure_sample_call, \ + z_loaned_closure_query_t : z_closure_query_call, \ + z_loaned_closure_reply_t : z_closure_reply_call, \ + z_loaned_closure_hello_t : z_closure_hello_call, \ + z_loaned_closure_zid_t : z_closure_zid_call, \ + z_loaned_closure_matching_status_t : z_closure_matching_status_call \ ) (&x, __VA_ARGS__) #define z_try_recv(x, ...) \ @@ -224,39 +232,41 @@ * Returns the instance associated with `x`. */ #define z_move(x) _Generic((x), \ - z_owned_keyexpr_t : z_keyexpr_move, \ - z_owned_config_t : z_config_move, \ - z_owned_session_t : z_session_move, \ - z_owned_subscriber_t : z_subscriber_move, \ - z_owned_publisher_t : z_publisher_move, \ - z_owned_queryable_t : z_queryable_move, \ - z_owned_liveliness_token_t : z_liveliness_token_move, \ - z_owned_reply_t : z_reply_move, \ - z_owned_hello_t : z_hello_move, \ - z_owned_string_t : z_string_move, \ - z_owned_string_array_t : z_string_array_move, \ - z_owned_closure_sample_t : z_closure_sample_move, \ - z_owned_closure_query_t : z_closure_query_move, \ - z_owned_closure_reply_t : z_closure_reply_move, \ - z_owned_closure_hello_t : z_closure_hello_move, \ - z_owned_closure_zid_t : z_closure_zid_move, \ - z_owned_sample_t : z_sample_move, \ - z_owned_query_t : z_query_move, \ - z_owned_slice_t : z_slice_move, \ - z_owned_bytes_t : z_bytes_move, \ - z_owned_encoding_t : z_encoding_move, \ - z_owned_task_t : z_task_move, \ - z_owned_mutex_t : z_mutex_move, \ - z_owned_condvar_t : z_condvar_move, \ - z_owned_ring_handler_query_t : z_ring_handler_query_move, \ - z_owned_ring_handler_reply_t : z_ring_handler_reply_move, \ - z_owned_ring_handler_sample_t : z_ring_handler_sample_move, \ - z_owned_fifo_handler_query_t : z_fifo_handler_query_move, \ - z_owned_fifo_handler_reply_t : z_fifo_handler_reply_move, \ - z_owned_fifo_handler_sample_t : z_fifo_handler_sample_move, \ - z_owned_reply_err_t : z_reply_err_move, \ - ze_owned_serializer_t : ze_serializer_move, \ - z_owned_bytes_writer_t : z_bytes_writer_move \ + z_owned_keyexpr_t : z_keyexpr_move, \ + z_owned_config_t : z_config_move, \ + z_owned_session_t : z_session_move, \ + z_owned_subscriber_t : z_subscriber_move, \ + z_owned_publisher_t : z_publisher_move, \ + z_owned_matching_listener_t: z_matching_listener_move, \ + z_owned_queryable_t : z_queryable_move, \ + z_owned_liveliness_token_t : z_liveliness_token_move, \ + z_owned_reply_t : z_reply_move, \ + z_owned_hello_t : z_hello_move, \ + z_owned_string_t : z_string_move, \ + z_owned_string_array_t : z_string_array_move, \ + z_owned_closure_sample_t : z_closure_sample_move, \ + z_owned_closure_query_t : z_closure_query_move, \ + z_owned_closure_reply_t : z_closure_reply_move, \ + z_owned_closure_hello_t : z_closure_hello_move, \ + z_owned_closure_zid_t : z_closure_zid_move, \ + z_owned_closure_matching_status_t : z_closure_matching_status_move, \ + z_owned_sample_t : z_sample_move, \ + z_owned_query_t : z_query_move, \ + z_owned_slice_t : z_slice_move, \ + z_owned_bytes_t : z_bytes_move, \ + z_owned_encoding_t : z_encoding_move, \ + z_owned_task_t : z_task_move, \ + z_owned_mutex_t : z_mutex_move, \ + z_owned_condvar_t : z_condvar_move, \ + z_owned_ring_handler_query_t : z_ring_handler_query_move, \ + z_owned_ring_handler_reply_t : z_ring_handler_reply_move, \ + z_owned_ring_handler_sample_t : z_ring_handler_sample_move, \ + z_owned_fifo_handler_query_t : z_fifo_handler_query_move, \ + z_owned_fifo_handler_reply_t : z_fifo_handler_reply_move, \ + z_owned_fifo_handler_sample_t : z_fifo_handler_sample_move, \ + z_owned_reply_err_t : z_reply_err_move, \ + ze_owned_serializer_t : ze_serializer_move, \ + z_owned_bytes_writer_t : z_bytes_writer_move \ )(&x) /** @@ -269,40 +279,42 @@ * Returns: * Returns the instance associated with `x`. */ -#define z_take(this_, x) \ - _Generic((this_), \ - z_owned_bytes_t *: z_bytes_take, \ - z_owned_closure_hello_t *: z_closure_hello_take, \ - z_owned_closure_query_t *: z_closure_query_take, \ - z_owned_closure_reply_t *: z_closure_reply_take, \ - z_owned_closure_sample_t *: z_closure_sample_take, \ - z_owned_closure_zid_t * : z_closure_zid_take, \ - z_owned_condvar_t *: z_condvar_take, \ - z_owned_config_t *: z_config_take, \ - z_owned_encoding_t *: z_encoding_take, \ - z_owned_fifo_handler_query_t *: z_fifo_handler_query_take, \ - z_owned_fifo_handler_reply_t *: z_fifo_handler_reply_take, \ - z_owned_fifo_handler_sample_t *: z_fifo_handler_sample_take,\ - z_owned_hello_t *: z_hello_take, \ - z_owned_keyexpr_t *: z_keyexpr_take, \ - z_owned_mutex_t *: z_mutex_take, \ - z_owned_publisher_t *: z_publisher_take, \ - z_owned_query_t *: z_query_take, \ - z_owned_queryable_t *: z_queryable_take, \ - z_owned_liveliness_token_t *: z_liveliness_token_take, \ - z_owned_reply_t *: z_reply_take, \ - z_owned_reply_err_t *: z_reply_err_take, \ - z_owned_ring_handler_query_t *: z_ring_handler_query_take, \ - z_owned_ring_handler_reply_t *: z_ring_handler_reply_take, \ - z_owned_ring_handler_sample_t *: z_ring_handler_sample_take,\ - z_owned_sample_t *: z_sample_take, \ - z_owned_session_t *: z_session_take, \ - z_owned_slice_t *: z_slice_take, \ - z_owned_string_array_t *: z_string_array_take, \ - z_owned_string_t *: z_string_take, \ - z_owned_subscriber_t *: z_subscriber_take, \ - ze_owned_serializer_t *: ze_serializer_take, \ - z_owned_bytes_writer_t *: z_bytes_writer_take \ +#define z_take(this_, x) \ + _Generic((this_), \ + z_owned_bytes_t *: z_bytes_take, \ + z_owned_closure_hello_t *: z_closure_hello_take, \ + z_owned_closure_query_t *: z_closure_query_take, \ + z_owned_closure_reply_t *: z_closure_reply_take, \ + z_owned_closure_sample_t *: z_closure_sample_take, \ + z_owned_closure_zid_t * : z_closure_zid_take, \ + z_owned_closure_matching_status_t * : z_closure_matching_status_take, \ + z_owned_condvar_t *: z_condvar_take, \ + z_owned_config_t *: z_config_take, \ + z_owned_encoding_t *: z_encoding_take, \ + z_owned_fifo_handler_query_t *: z_fifo_handler_query_take, \ + z_owned_fifo_handler_reply_t *: z_fifo_handler_reply_take, \ + z_owned_fifo_handler_sample_t *: z_fifo_handler_sample_take, \ + z_owned_hello_t *: z_hello_take, \ + z_owned_keyexpr_t *: z_keyexpr_take, \ + z_owned_mutex_t *: z_mutex_take, \ + z_owned_publisher_t *: z_publisher_take, \ + z_owned_matching_listener_t *: z_matching_listener_take, \ + z_owned_query_t *: z_query_take, \ + z_owned_queryable_t *: z_queryable_take, \ + z_owned_liveliness_token_t *: z_liveliness_token_take, \ + z_owned_reply_t *: z_reply_take, \ + z_owned_reply_err_t *: z_reply_err_take, \ + z_owned_ring_handler_query_t *: z_ring_handler_query_take, \ + z_owned_ring_handler_reply_t *: z_ring_handler_reply_take, \ + z_owned_ring_handler_sample_t *: z_ring_handler_sample_take, \ + z_owned_sample_t *: z_sample_take, \ + z_owned_session_t *: z_session_take, \ + z_owned_slice_t *: z_slice_take, \ + z_owned_string_array_t *: z_string_array_take, \ + z_owned_string_t *: z_string_take, \ + z_owned_subscriber_t *: z_subscriber_take, \ + ze_owned_serializer_t *: ze_serializer_take, \ + z_owned_bytes_writer_t *: z_bytes_writer_take \ )(this_, x) /** @@ -337,30 +349,32 @@ * x: The instance to nullify. */ #define z_internal_null(x) _Generic((x), \ - z_owned_session_t * : z_internal_session_null, \ - z_owned_publisher_t * : z_internal_publisher_null, \ - z_owned_keyexpr_t * : z_internal_keyexpr_null, \ - z_owned_config_t * : z_internal_config_null, \ - z_owned_subscriber_t * : z_internal_subscriber_null, \ - z_owned_queryable_t * : z_internal_queryable_null, \ - z_owned_liveliness_token_t * : z_internal_liveliness_token_null, \ - z_owned_query_t * : z_internal_query_null, \ - z_owned_reply_t * : z_internal_reply_null, \ - z_owned_hello_t * : z_internal_hello_null, \ - z_owned_string_t * : z_internal_string_null, \ - z_owned_string_array_t * : z_internal_string_array_null, \ - z_owned_slice_t *: z_internal_slice_null, \ - z_owned_bytes_t *: z_internal_bytes_null, \ - z_owned_closure_sample_t * : z_internal_closure_sample_null, \ - z_owned_closure_query_t * : z_internal_closure_query_null, \ - z_owned_closure_reply_t * : z_internal_closure_reply_null, \ - z_owned_closure_hello_t * : z_internal_closure_hello_null, \ - z_owned_closure_zid_t * : z_internal_closure_zid_null, \ - z_owned_sample_t * : z_internal_sample_null, \ - z_owned_encoding_t * : z_internal_encoding_null, \ - z_owned_reply_err_t * : z_internal_reply_err_null, \ - ze_owned_serializer_t * : ze_internal_serializer_null, \ - z_owned_bytes_writer_t * : z_internal_bytes_writer_null \ + z_owned_session_t * : z_internal_session_null, \ + z_owned_publisher_t * : z_internal_publisher_null, \ + z_owned_matching_listener_t * : z_internal_matching_listener_null, \ + z_owned_keyexpr_t * : z_internal_keyexpr_null, \ + z_owned_config_t * : z_internal_config_null, \ + z_owned_subscriber_t * : z_internal_subscriber_null, \ + z_owned_queryable_t * : z_internal_queryable_null, \ + z_owned_liveliness_token_t * : z_internal_liveliness_token_null, \ + z_owned_query_t * : z_internal_query_null, \ + z_owned_reply_t * : z_internal_reply_null, \ + z_owned_hello_t * : z_internal_hello_null, \ + z_owned_string_t * : z_internal_string_null, \ + z_owned_string_array_t * : z_internal_string_array_null, \ + z_owned_slice_t *: z_internal_slice_null, \ + z_owned_bytes_t *: z_internal_bytes_null, \ + z_owned_closure_sample_t * : z_internal_closure_sample_null, \ + z_owned_closure_query_t * : z_internal_closure_query_null, \ + z_owned_closure_reply_t * : z_internal_closure_reply_null, \ + z_owned_closure_hello_t * : z_internal_closure_hello_null, \ + z_owned_closure_zid_t * : z_internal_closure_zid_null, \ + z_owned_closure_matching_status_t * : z_internal_closure_matching_status_null, \ + z_owned_sample_t * : z_internal_sample_null, \ + z_owned_encoding_t * : z_internal_encoding_null, \ + z_owned_reply_err_t * : z_internal_reply_err_null, \ + ze_owned_serializer_t * : ze_internal_serializer_null, \ + z_owned_bytes_writer_t * : z_internal_bytes_writer_null \ )(x) // clang-format on @@ -396,6 +410,7 @@ inline const z_loaned_config_t* z_loan(const z_owned_config_t& x) { return z_con inline const z_loaned_session_t* z_loan(const z_owned_session_t& x) { return z_session_loan(&x); } inline const z_loaned_subscriber_t* z_loan(const z_owned_subscriber_t& x) { return z_subscriber_loan(&x); } inline const z_loaned_publisher_t* z_loan(const z_owned_publisher_t& x) { return z_publisher_loan(&x); } +inline const z_loaned_matching_listener_t* z_loan(const z_owned_matching_listener_t& x) { return z_matching_listener_loan(&x); } inline const z_loaned_queryable_t* z_loan(const z_owned_queryable_t& x) { return z_queryable_loan(&x); } inline const z_loaned_liveliness_token_t* z_loan(const z_owned_liveliness_token_t& x) { return z_liveliness_token_loan(&x); } inline const z_loaned_reply_t* z_loan(const z_owned_reply_t& x) { return z_reply_loan(&x); } @@ -418,6 +433,7 @@ inline const z_loaned_closure_reply_t* z_loan(const z_owned_closure_reply_t& x) inline const z_loaned_closure_query_t* z_loan(const z_owned_closure_query_t& x) { return z_closure_query_loan(&x); } inline const z_loaned_closure_hello_t* z_loan(const z_owned_closure_hello_t& x) { return z_closure_hello_loan(&x); } inline const z_loaned_closure_zid_t* z_loan(const z_owned_closure_zid_t& x) { return z_closure_zid_loan(&x); } +inline const z_loaned_closure_matching_status_t* z_loan(const z_owned_closure_matching_status_t& x) { return z_closure_matching_status_loan(&x); } inline const z_loaned_fifo_handler_query_t* z_loan(const z_owned_fifo_handler_query_t& x) { return z_fifo_handler_query_loan(&x); } inline const z_loaned_fifo_handler_reply_t* z_loan(const z_owned_fifo_handler_reply_t& x) { return z_fifo_handler_reply_loan(&x); } inline const z_loaned_fifo_handler_sample_t* z_loan(const z_owned_fifo_handler_sample_t& x) { return z_fifo_handler_sample_loan(&x); } @@ -433,6 +449,7 @@ inline z_loaned_keyexpr_t* z_loan_mut(z_view_keyexpr_t& x) { return z_view_keyex inline z_loaned_config_t* z_loan_mut(z_owned_config_t& x) { return z_config_loan_mut(&x); } inline z_loaned_session_t* z_loan_mut(z_owned_session_t& x) { return z_session_loan_mut(&x); } inline z_loaned_publisher_t* z_loan_mut(z_owned_publisher_t& x) { return z_publisher_loan_mut(&x); } +inline z_loaned_matching_listener_t* z_loan_mut(z_owned_matching_listener_t& x) { return z_matching_listener_loan_mut(&x); } inline z_loaned_queryable_t* z_loan_mut(z_owned_queryable_t& x) { return z_queryable_loan_mut(&x); } inline z_loaned_liveliness_token_t* z_loan_mut(z_owned_liveliness_token_t& x) { return z_liveliness_token_loan_mut(&x); } inline z_loaned_subscriber_t* z_loan_mut(z_owned_subscriber_t& x) { return z_subscriber_loan_mut(&x); } @@ -457,6 +474,7 @@ inline ze_loaned_serializer_t* z_loan_mut(ze_owned_serializer_t& x) { return ze_ // z_drop definition inline void z_drop(z_moved_session_t* v) { z_session_drop(v); } inline void z_drop(z_moved_publisher_t* v) { z_publisher_drop(v); } +inline void z_drop(z_moved_matching_listener_t* v) { z_matching_listener_drop(v); } inline void z_drop(z_moved_keyexpr_t* v) { z_keyexpr_drop(v); } inline void z_drop(z_moved_config_t* v) { z_config_drop(v); } inline void z_drop(z_moved_subscriber_t* v) { z_subscriber_drop(v); } @@ -479,6 +497,7 @@ inline void z_drop(z_moved_closure_query_t* v) { z_closure_query_drop(v); } inline void z_drop(z_moved_closure_reply_t* v) { z_closure_reply_drop(v); } inline void z_drop(z_moved_closure_hello_t* v) { z_closure_hello_drop(v); } inline void z_drop(z_moved_closure_zid_t* v) { z_closure_zid_drop(v); } +inline void z_drop(z_moved_closure_matching_status_t* v) { z_closure_matching_status_drop(v); } inline void z_drop(z_moved_ring_handler_sample_t* v) { z_ring_handler_sample_drop(v); } inline void z_drop(z_moved_fifo_handler_sample_t* v) { z_fifo_handler_sample_drop(v); } inline void z_drop(z_moved_ring_handler_query_t* v) { z_ring_handler_query_drop(v); } @@ -491,6 +510,7 @@ inline void z_drop(ze_moved_serializer_t* v) { ze_serializer_drop(v); } // z_internal_null definition inline void z_internal_null(z_owned_session_t* v) { z_internal_session_null(v); } inline void z_internal_null(z_owned_publisher_t* v) { z_internal_publisher_null(v); } +inline void z_internal_null(z_owned_matching_listener_t* v) { z_internal_matching_listener_null(v); } inline void z_internal_null(z_owned_keyexpr_t* v) { z_internal_keyexpr_null(v); } inline void z_internal_null(z_owned_config_t* v) { z_internal_config_null(v); } inline void z_internal_null(z_owned_subscriber_t* v) { z_internal_subscriber_null(v); } @@ -509,6 +529,7 @@ inline void z_internal_null(z_owned_closure_query_t* v) { z_internal_closure_que inline void z_internal_null(z_owned_closure_reply_t* v) { z_internal_closure_reply_null(v); } inline void z_internal_null(z_owned_closure_hello_t* v) { z_internal_closure_hello_null(v); } inline void z_internal_null(z_owned_closure_zid_t* v) { z_internal_closure_zid_null(v); } +inline void z_internal_null(z_owned_closure_matching_status_t* v) { z_internal_closure_matching_status_null(v); } inline void z_internal_null(z_owned_ring_handler_query_t* v) { return z_internal_ring_handler_query_null(v); } inline void z_internal_null(z_owned_ring_handler_reply_t* v) { return z_internal_ring_handler_reply_null(v); } inline void z_internal_null(z_owned_ring_handler_sample_t* v) { return z_internal_ring_handler_sample_null(v); } @@ -521,6 +542,7 @@ inline void z_internal_null(ze_owned_serializer_t* v) { return ze_internal_seria // z_internal_check definition inline bool z_internal_check(const z_owned_session_t& v) { return z_internal_session_check(&v); } inline bool z_internal_check(const z_owned_publisher_t& v) { return z_internal_publisher_check(&v); } +inline bool z_internal_check(const z_owned_matching_listener_t& v) { return z_internal_matching_listener_check(&v); } inline bool z_internal_check(const z_owned_keyexpr_t& v) { return z_internal_keyexpr_check(&v); } inline bool z_internal_check(const z_owned_config_t& v) { return z_internal_config_check(&v); } inline bool z_internal_check(const z_owned_subscriber_t& v) { return z_internal_subscriber_check(&v); } @@ -554,6 +576,8 @@ inline void z_call(const z_loaned_closure_hello_t &closure, z_loaned_hello_t *he { z_closure_hello_call(&closure, hello); } inline void z_call(const z_loaned_closure_zid_t &closure, const z_id_t *zid) { z_closure_zid_call(&closure, zid); } +inline void z_call(const z_loaned_closure_matching_status_t &closure, const z_id_t *zid) + { z_closure_matching_status_call(&closure, zid); } inline void z_closure( z_owned_closure_hello_t* closure, @@ -600,6 +624,15 @@ inline void z_closure( closure->_val.drop = drop; closure->_val.call = call; } +inline void z_closure( + z_owned_closure_matching_status_t* closure, + void (*call)(const z_id_t*, void*), + void (*drop)(void*), + void *context) { + closure->_val.context = context; + closure->_val.drop = drop; + closure->_val.call = call; +} inline z_result_t z_try_recv(const z_loaned_fifo_handler_query_t* this_, z_owned_query_t* query) { return z_fifo_handler_query_try_recv(this_, query); @@ -648,12 +681,16 @@ inline z_moved_closure_query_t* z_move(z_owned_closure_query_t& closure) { retur inline z_moved_closure_reply_t* z_move(z_owned_closure_reply_t& closure) { return z_closure_reply_move(&closure); } inline z_moved_closure_sample_t* z_move(z_owned_closure_sample_t& closure) { return z_closure_sample_move(&closure); } inline z_moved_closure_zid_t* z_move(z_owned_closure_zid_t& closure) { return z_closure_zid_move(&closure); } +inline z_moved_closure_matching_status_t* z_move(z_owned_closure_matching_status_t& closure) { + return z_closure_matching_status_move(&closure); +} inline z_moved_config_t* z_move(z_owned_config_t& x) { return z_config_move(&x); } inline z_moved_encoding_t* z_move(z_owned_encoding_t& x) { return z_encoding_move(&x); } inline z_moved_reply_err_t* z_move(z_owned_reply_err_t& x) { return z_reply_err_move(&x); } inline z_moved_hello_t* z_move(z_owned_hello_t& x) { return z_hello_move(&x); } inline z_moved_keyexpr_t* z_move(z_owned_keyexpr_t& x) { return z_keyexpr_move(&x); } inline z_moved_publisher_t* z_move(z_owned_publisher_t& x) { return z_publisher_move(&x); } +inline z_moved_matching_listener_t* z_move(z_owned_matching_listener_t& x) { return z_matching_listener_move(&x); } inline z_moved_query_t* z_move(z_owned_query_t& x) { return z_query_move(&x); } inline z_moved_queryable_t* z_move(z_owned_queryable_t& x) { return z_queryable_move(&x); } inline z_moved_liveliness_token_t* z_move(z_owned_liveliness_token_t& x) { return z_liveliness_token_move(&x); } @@ -680,6 +717,9 @@ inline ze_moved_serializer_t* z_move(ze_owned_serializer_t& x) { return ze_seria // z_take definition inline void z_take(z_owned_session_t* this_, z_moved_session_t* v) { return z_session_take(this_, v); } inline void z_take(z_owned_publisher_t* this_, z_moved_publisher_t* v) { return z_publisher_take(this_, v); } +inline void z_take(z_owned_matching_listener_t* this_, z_moved_matching_listener_t* v) { + return z_matching_listener_take(this_, v); +} inline void z_take(z_owned_keyexpr_t* this_, z_moved_keyexpr_t* v) { z_keyexpr_take(this_, v); } inline void z_take(z_owned_config_t* this_, z_moved_config_t* v) { z_config_take(this_, v); } inline void z_take(z_owned_subscriber_t* this_, z_moved_subscriber_t* v) { return z_subscriber_take(this_, v); } @@ -704,6 +744,9 @@ inline void z_take(z_owned_closure_query_t* this_, z_moved_closure_query_t* v) { inline void z_take(z_owned_closure_reply_t* this_, z_moved_closure_reply_t* v) { z_closure_reply_take(this_, v); } inline void z_take(z_owned_closure_hello_t* this_, z_moved_closure_hello_t* v) { z_closure_hello_take(this_, v); } inline void z_take(z_owned_closure_zid_t* this_, z_moved_closure_zid_t* v) { z_closure_zid_take(this_, v); } +inline void z_take(z_owned_closure_matching_status_t* this_, z_moved_closure_matching_status_t* v) { + z_closure_matching_status_take(this_, v); +} inline void z_take(z_owned_ring_handler_sample_t* this_, z_moved_ring_handler_sample_t* v) { z_ring_handler_sample_take(this_, v); } @@ -803,6 +846,13 @@ template <> struct z_owned_to_loaned_type_t { typedef z_loaned_publisher_t type; }; +struct z_loaned_to_owned_type_t { + typedef z_owned_matching_listener_t type; +}; +template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_matching_listener_t type; +}; template <> struct z_loaned_to_owned_type_t { typedef z_owned_query_t type; @@ -924,6 +974,14 @@ struct z_loaned_to_owned_type_t { typedef z_owned_closure_zid_t type; }; template <> +struct z_owned_to_loaned_type_t { + typedef z_loaned_closure_matching_status_t type; +}; +template <> +struct z_loaned_to_owned_type_t { + typedef z_owned_closure_matching_status_t type; +}; +template <> struct z_loaned_to_owned_type_t { typedef z_owned_fifo_handler_query_t type; }; diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index 9cce79d99..4e951369d 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -1111,6 +1111,16 @@ z_result_t z_closure_zid(z_owned_closure_zid_t *closure, z_closure_zid_callback_ */ void z_closure_zid_call(const z_loaned_closure_zid_t *closure, const z_id_t *id); +/** + * Calls a matching status closure. + * + * Parameters: + * closure: Pointer to the :c:type:`z_loaned_closure_matching_status_t` to call. + * status: Pointer to the :c:type:`z_matching_status_t` to pass to the closure. + */ +void z_closure_matching_status_call(const z_loaned_closure_matching_status_t *closure, + const z_matching_status_t *status); + /**************** Loans ****************/ _Z_OWNED_FUNCTIONS_DEF(string) _Z_OWNED_FUNCTIONS_DEF(keyexpr) @@ -1118,6 +1128,7 @@ _Z_OWNED_FUNCTIONS_DEF(config) _Z_OWNED_FUNCTIONS_NO_COPY_DEF(session) _Z_OWNED_FUNCTIONS_NO_COPY_DEF(subscriber) _Z_OWNED_FUNCTIONS_NO_COPY_DEF(publisher) +_Z_OWNED_FUNCTIONS_NO_COPY_DEF(matching_listener) _Z_OWNED_FUNCTIONS_NO_COPY_DEF(queryable) _Z_OWNED_FUNCTIONS_DEF(hello) _Z_OWNED_FUNCTIONS_DEF(reply) @@ -1135,6 +1146,7 @@ _Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_query) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_reply) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_hello) _Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_zid) +_Z_OWNED_FUNCTIONS_CLOSURE_DEF(closure_matching_status) _Z_VIEW_FUNCTIONS_DEF(keyexpr) _Z_VIEW_FUNCTIONS_DEF(string) @@ -1661,7 +1673,54 @@ z_result_t z_publisher_delete(const z_loaned_publisher_t *pub, const z_publisher * The keyexpr wrapped as a :c:type:`z_loaned_keyexpr_t`. */ const z_loaned_keyexpr_t *z_publisher_keyexpr(const z_loaned_publisher_t *publisher); -#endif + +#if Z_FEATURE_MATCHING == 1 +/** + * Declares a matching listener, registering a callback for notifying subscribers matching with a given publisher. + * The callback will be run in the background until the corresponding publisher is dropped. + * + * Parameters: + * 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). + * + * Return: + * ``0`` if execution was successful, ``negative value`` otherwise. + * + * .. warning:: This API has been marked as unstable: it works as advertised, but it may be changed in a future release. + */ +z_result_t z_publisher_declare_background_matching_listener(const z_loaned_publisher_t *publisher, + z_moved_closure_matching_status_t *callback); +/** + * Constructs matching listener, registering a callback for notifying subscribers matching with a given publisher. + * + * Parameters: + * publisher: A publisher to associate with matching listener. + * matching_listener: An uninitialized memory location where matching listener will be constructed. The matching + * listener's callback will be automatically dropped when the publisher is dropped. 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`` if execution was successful, ``negative value`` otherwise. + * + * .. warning:: This API has been marked as unstable: it works as advertised, but it may be changed in a future release. + */ +z_result_t z_publisher_declare_matching_listener(const z_loaned_publisher_t *publisher, + z_owned_matching_listener_t *matching_listener, + z_moved_closure_matching_status_t *callback); +/** + * Gets publisher matching status - i.e. if there are any subscribers matching its key expression. + * + * Return: + * ``0`` if execution was successful, ``negative value`` otherwise. + * + * .. warning:: This API has been marked as unstable: it works as advertised, but it may be changed in a future release. + */ +z_result_t z_publisher_get_matching_status(const z_loaned_publisher_t *publisher, z_matching_status_t *matching_status); +#endif // Z_FEATURE_MATCHING == 1 + +#endif // Z_FEATURE_PUBLICATION == 1 #if Z_FEATURE_QUERY == 1 /** diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index c5a76dbd8..9d1519cbb 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -22,6 +22,7 @@ #include "zenoh-pico/collections/slice.h" #include "zenoh-pico/collections/string.h" #include "zenoh-pico/net/encoding.h" +#include "zenoh-pico/net/matching.h" #include "zenoh-pico/net/publish.h" #include "zenoh-pico/net/query.h" #include "zenoh-pico/net/reply.h" @@ -110,6 +111,11 @@ _Z_OWNED_TYPE_VALUE(_z_subscriber_t, subscriber) */ _Z_OWNED_TYPE_VALUE(_z_publisher_t, publisher) +/** + * Represents a Zenoh Matching listener entity. + */ +_Z_OWNED_TYPE_VALUE(_z_matching_listener_t, matching_listener) + /** * Represents a Zenoh Queryable entity. */ @@ -130,6 +136,16 @@ _Z_OWNED_TYPE_VALUE(_z_encoding_t, encoding) */ _Z_OWNED_TYPE_VALUE(_z_value_t, reply_err) +#if defined(Z_FEATURE_UNSTABLE_API) +/** + * A struct that indicates if there exist Subscribers matching the Publisher's key expression or Queryables matching + * Querier's key expression and target. + */ +typedef struct { + bool matching; // true if there exist matching Zenoh entities, false otherwise. +} z_matching_status_t; +#endif + /** * Represents the configuration used to configure a subscriber upon declaration :c:func:`z_declare_subscriber`. */ @@ -476,6 +492,21 @@ typedef struct { */ _Z_OWNED_TYPE_VALUE(_z_closure_zid_t, closure_zid) +#if defined(Z_FEATURE_UNSTABLE_API) +typedef void (*z_closure_matching_status_callback_t)(const z_matching_status_t *status, void *arg); + +typedef struct { + void *context; + z_closure_matching_status_callback_t call; + z_closure_drop_callback_t drop; +} _z_closure_matching_status_t; + +/** + * Represents the matching status callback closure. + */ +_Z_OWNED_TYPE_VALUE(_z_closure_matching_status_t, closure_matching_status) +#endif + #ifdef __cplusplus } #endif diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 3542c7d1d..abce70efd 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -47,6 +47,7 @@ #define Z_FEATURE_LOCAL_SUBSCRIBER 0 #define Z_FEATURE_PUBLISHER_SESSION_CHECK 1 #define Z_FEATURE_BATCHING 1 +#define Z_FEATURE_MATCHING 1 #define Z_FEATURE_RX_CACHE 0 #define Z_FEATURE_AUTO_RECONNECT 1 // End of CMake generation diff --git a/include/zenoh-pico/config.h.in b/include/zenoh-pico/config.h.in index a3748c432..897526cc0 100644 --- a/include/zenoh-pico/config.h.in +++ b/include/zenoh-pico/config.h.in @@ -47,6 +47,7 @@ #define Z_FEATURE_LOCAL_SUBSCRIBER @Z_FEATURE_LOCAL_SUBSCRIBER@ #define Z_FEATURE_PUBLISHER_SESSION_CHECK @Z_FEATURE_PUBLISHER_SESSION_CHECK@ #define Z_FEATURE_BATCHING @Z_FEATURE_BATCHING@ +#define Z_FEATURE_MATCHING @Z_FEATURE_MATCHING@ #define Z_FEATURE_RX_CACHE @Z_FEATURE_RX_CACHE@ #define Z_FEATURE_AUTO_RECONNECT @Z_FEATURE_AUTO_RECONNECT@ // End of CMake generation diff --git a/include/zenoh-pico/net/matching.h b/include/zenoh-pico/net/matching.h new file mode 100644 index 000000000..c880c3c7d --- /dev/null +++ b/include/zenoh-pico/net/matching.h @@ -0,0 +1,59 @@ +// +// Copyright (c) 2025 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#ifndef INCLUDE_ZENOH_PICO_NET_MATCHING_H +#define INCLUDE_ZENOH_PICO_NET_MATCHING_H + +#include "zenoh-pico/net/filtering.h" +#include "zenoh-pico/net/session.h" +#include "zenoh-pico/protocol/core.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Return type when declaring a matching. + */ +typedef struct _z_matching_listener_t { + _z_keyexpr_t _key; + _z_session_weak_t _zn; + /* +_z_zint_t _id; +_z_encoding_t _encoding; +z_congestion_control_t _congestion_control; +z_priority_t _priority; +z_reliability_t reliability; +bool _is_express; +#if Z_FEATURE_INTEREST == 1 +_z_write_filter_t _filter; +#endif +*/ +} _z_matching_listener_t; + +#if Z_FEATURE_PUBLICATION == 1 +// Warning: None of the sub-types require a non-0 initialization. Add a init function if it changes. +static inline _z_matching_listener_t _z_matching_listener_null(void) { return (_z_matching_listener_t){0}; } +static inline bool _z_matching_listener_check(const _z_matching_listener_t *matching_listener) { + return !_Z_RC_IS_NULL(&matching_listener->_zn); +} +void _z_matching_listener_clear(_z_matching_listener_t *pub); +void _z_matching_listener_free(_z_matching_listener_t **pub); +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* INCLUDE_ZENOH_PICO_NET_MATCHING_H */ diff --git a/src/api/api.c b/src/api/api.c index f6a209ac9..e6cef7419 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -477,6 +477,13 @@ void z_closure_zid_call(const z_loaned_closure_zid_t *closure, const z_id_t *id) } } +void z_closure_matching_status_call(const z_loaned_closure_matching_status_t *closure, + const z_matching_status_t *status) { + if (closure->call != NULL) { + (closure->call)(status, closure->context); + } +} + bool _z_config_check(const _z_config_t *config) { return !_z_str_intmap_is_empty(config); } _z_config_t _z_config_null(void) { return _z_str_intmap_make(); } z_result_t _z_config_copy(_z_config_t *dst, const _z_config_t *src) { @@ -570,6 +577,8 @@ _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_query, _z_closure_query_callback_t, z_cl _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_reply, _z_closure_reply_callback_t, z_closure_drop_callback_t) _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_hello, z_closure_hello_callback_t, z_closure_drop_callback_t) _Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_zid, z_closure_zid_callback_t, z_closure_drop_callback_t) +_Z_OWNED_FUNCTIONS_CLOSURE_IMPL(closure_matching_status, z_closure_matching_status_callback_t, + z_closure_drop_callback_t) /************* Primitives **************/ typedef struct __z_hello_handler_wrapper_t { @@ -1096,7 +1105,36 @@ z_result_t z_publisher_delete(const z_loaned_publisher_t *pub, const z_publisher const z_loaned_keyexpr_t *z_publisher_keyexpr(const z_loaned_publisher_t *publisher) { return (const z_loaned_keyexpr_t *)&publisher->_key; } -#endif + +#if Z_FEATURE_MATCHING == 1 +z_result_t z_publisher_declare_background_matching_listener(const z_loaned_publisher_t *publisher, + z_moved_closure_matching_status_t *callback) { + (void)publisher; + (void)callback; + // TODO(sashacmc): Implement + return _Z_RES_OK; +} + +z_result_t z_publisher_declare_matching_listener(const z_loaned_publisher_t *publisher, + z_owned_matching_listener_t *matching_listener, + z_moved_closure_matching_status_t *callback) { + (void)publisher; + (void)matching_listener; + (void)callback; + // TODO(sashacmc): Implement + return _Z_RES_OK; +} + +z_result_t z_publisher_get_matching_status(const z_loaned_publisher_t *publisher, + z_matching_status_t *matching_status) { + (void)publisher; + (void)matching_status; + // TODO(sashacmc): Implement + return _Z_RES_OK; +} +#endif // Z_FEATURE_MATCHING == 1 + +#endif // Z_FEATURE_PUBLICATION == 1 #if Z_FEATURE_QUERY == 1 bool _z_reply_check(const _z_reply_t *reply) { @@ -1519,6 +1557,17 @@ z_result_t zp_batch_stop(const z_loaned_session_t *zs) { } #endif +#ifdef Z_FEATURE_MATCHING +void _z_matching_listener_drop(_z_matching_listener_t *listener) { + //_z_undeclare_matching_listener(pub); + _z_matching_listener_clear(listener); +} + +_Z_OWNED_FUNCTIONS_VALUE_NO_COPY_IMPL(_z_matching_listener_t, matching_listener, _z_matching_listener_check, + _z_matching_listener_null, _z_matching_listener_drop) + +#endif + /**************** Tasks ****************/ void zp_task_read_options_default(zp_task_read_options_t *options) { #if Z_FEATURE_MULTI_THREAD == 1 diff --git a/src/net/matching.c b/src/net/matching.c new file mode 100644 index 000000000..6e5b040a4 --- /dev/null +++ b/src/net/matching.c @@ -0,0 +1,37 @@ +// +// Copyright (c) 2025 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// + +#include + +#include "zenoh-pico/net/matching.h" + +#if Z_FEATURE_MATCHING == 1 +void _z_matching_listener_clear(_z_matching_listener_t *listener) { + _z_keyexpr_clear(&listener->_key); + _z_session_weak_drop(&listener->_zn); + *listener = _z_matching_listener_null(); +} + +void _z_matching_listener_free(_z_matching_listener_t **listener) { + _z_matching_listener_t *ptr = *listener; + + if (ptr != NULL) { + _z_matching_listener_clear(ptr); + + z_free(ptr); + *listener = NULL; + } +} + +#endif