From e084b33f2050cc22873944c5be7aa75db6b8e170 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 8 Nov 2023 18:00:35 +0100 Subject: [PATCH 1/7] Add z_sub_liveliness sample --- examples/z_sub_liveliness.c | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 examples/z_sub_liveliness.c diff --git a/examples/z_sub_liveliness.c b/examples/z_sub_liveliness.c new file mode 100644 index 000000000..b7ada51b4 --- /dev/null +++ b/examples/z_sub_liveliness.c @@ -0,0 +1,80 @@ +// +// Copyright (c) 2023 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 +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +#include +#define sleep(x) Sleep(x * 1000) +#else +#include +#endif +#include "zenoh.h" + +void data_handler(const z_sample_t *sample, void *arg) { + z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); + switch (sample->kind) { + case Z_SAMPLE_KIND_PUT: + printf(">> [LivelinessSubscriber] New alive token ('%s')\n", z_loan(keystr)); + break; + case Z_SAMPLE_KIND_DELETE: + printf(">> [LivelinessSubscriber] Dropped token ('%s')\n", z_loan(keystr)); + break; + } + z_drop(z_move(keystr)); +} + +int main(int argc, char **argv) { + char *expr = "group1/**"; + if (argc > 1) { + expr = argv[1]; + } + + z_owned_config_t config = z_config_default(); + if (argc > 2) { + if (zc_config_insert_json(z_loan(config), Z_CONFIG_LISTEN_KEY, argv[2]) < 0) { + printf( + "Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " + "JSON-serialized list of strings\n", + argv[2], Z_CONFIG_LISTEN_KEY, Z_CONFIG_LISTEN_KEY); + exit(-1); + } + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + exit(-1); + } + + z_owned_closure_sample_t callback = z_closure(data_handler); + printf("Declaring liveliness subscriber on '%s'...\n", expr); + z_owned_subscriber_t sub = zc_liveliness_declare_subscriber(z_loan(s), z_keyexpr(expr), z_move(callback), NULL); + if (!z_check(sub)) { + printf("Unable to declare liveliness subscriber.\n"); + exit(-1); + } + + printf("Enter 'q' to quit...\n"); + char c = 0; + while (c != 'q') { + c = getchar(); + if (c == -1) { + sleep(1); + } + } + + z_undeclare_subscriber(z_move(sub)); + z_close(z_move(s)); + return 0; +} From be42f76ea34c19847395aa464882801534b3ef3f Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 8 Nov 2023 18:46:33 +0100 Subject: [PATCH 2/7] Add z_get_liveliness sample --- examples/z_get_liveliness.c | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 examples/z_get_liveliness.c diff --git a/examples/z_get_liveliness.c b/examples/z_get_liveliness.c new file mode 100644 index 000000000..a70474904 --- /dev/null +++ b/examples/z_get_liveliness.c @@ -0,0 +1,67 @@ +// +// Copyright (c) 2023 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 + +#include "zenoh.h" + +int main(int argc, char **argv) { + char *expr = "group1/**"; + if (argc > 1) { + expr = argv[1]; + } + + z_keyexpr_t keyexpr = z_keyexpr(expr); + if (!z_check(keyexpr)) { + printf("%s is not a valid key expression", expr); + exit(-1); + } + z_owned_config_t config = z_config_default(); + if (argc > 3) { + if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[3]) < 0) { + printf( + "Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " + "JSON-serialized list of strings\n", + argv[3], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY); + exit(-1); + } + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + exit(-1); + } + + printf("Sending liveliness query '%s'...\n", expr); + z_owned_reply_channel_t channel = zc_reply_fifo_new(16); + z_get_options_t opts = z_get_options_default(); + zc_liveliness_get(z_loan(s), keyexpr, z_move(channel.send), NULL); + z_owned_reply_t reply = z_reply_null(); + for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { + if (z_reply_is_ok(&reply)) { + z_sample_t sample = z_reply_ok(&reply); + z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); + printf(">> Alive token ('%s')\n", z_loan(keystr)); + z_drop(z_move(keystr)); + } else { + printf("Received an error\n"); + } + } + z_drop(z_move(reply)); + z_drop(z_move(channel)); + z_close(z_move(s)); + return 0; +} From 7b34eb29bf8f77566b1626b39cdbd228313fe815 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 8 Nov 2023 19:32:04 +0100 Subject: [PATCH 3/7] Fix args parsing --- examples/z_get_liveliness.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/z_get_liveliness.c b/examples/z_get_liveliness.c index a70474904..96ee8b56d 100644 --- a/examples/z_get_liveliness.c +++ b/examples/z_get_liveliness.c @@ -28,8 +28,8 @@ int main(int argc, char **argv) { exit(-1); } z_owned_config_t config = z_config_default(); - if (argc > 3) { - if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[3]) < 0) { + if (argc > 2) { + if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[2]) < 0) { printf( "Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " "JSON-serialized list of strings\n", From d4bf4670ef16b52e7c1fa9e0332fd71acad4f1bb Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 8 Nov 2023 19:34:28 +0100 Subject: [PATCH 4/7] Add z_liveliness sample --- examples/z_liveliness.c | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 examples/z_liveliness.c diff --git a/examples/z_liveliness.c b/examples/z_liveliness.c new file mode 100644 index 000000000..33cdd7422 --- /dev/null +++ b/examples/z_liveliness.c @@ -0,0 +1,76 @@ +// +// Copyright (c) 2023 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 +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +#include +#define sleep(x) Sleep(x * 1000) +#else +#include +#endif +#include "zenoh.h" + +const char *expr = "group1/zenoh-rs"; +z_keyexpr_t keyexpr; + +int main(int argc, char **argv) { + if (argc > 1) { + expr = argv[1]; + } + z_owned_config_t config = z_config_default(); + if (argc > 2) { + if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[2]) < 0) { + printf( + "Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " + "JSON-serialized list of strings\n", + argv[2], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY); + exit(-1); + } + } + + printf("Opening session...\n"); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + exit(-1); + } + keyexpr = z_keyexpr(expr); + if (!z_check(keyexpr)) { + printf("%s is not a valid key expression", expr); + exit(-1); + } + + printf("Declaring liveliness token '%s'...\n", expr); + zc_owned_liveliness_token_t token = zc_liveliness_declare_token(z_loan(s), keyexpr, NULL); + if (!zc_liveliness_token_check(&token)) { + printf("Unable to create liveliness token.\n"); + exit(-1); + } + + printf("Enter 'd' to undeclare liveliness token, 'q' to quit...\n"); + char c = 0; + while (c != 'q') { + c = getchar(); + if (c == -1) { + sleep(1); + } else if (c == 'd') { + printf("Undeclaring liveliness token...\n"); + zc_liveliness_undeclare_token(&token); + } + } + + zc_liveliness_undeclare_token(z_move(token)); + z_close(z_move(s)); + return 0; +} From 4e147974542cd011468d6ee240ccdb58b6bb255b Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Wed, 8 Nov 2023 19:58:10 +0100 Subject: [PATCH 5/7] Code cleaning up --- examples/z_get_liveliness.c | 6 +++--- examples/z_liveliness.c | 18 +++++++++--------- examples/z_sub_liveliness.c | 10 ++++++++-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/examples/z_get_liveliness.c b/examples/z_get_liveliness.c index 96ee8b56d..c667cb03a 100644 --- a/examples/z_get_liveliness.c +++ b/examples/z_get_liveliness.c @@ -24,16 +24,17 @@ int main(int argc, char **argv) { z_keyexpr_t keyexpr = z_keyexpr(expr); if (!z_check(keyexpr)) { - printf("%s is not a valid key expression", expr); + printf("%s is not a valid key expression\n", expr); exit(-1); } + z_owned_config_t config = z_config_default(); if (argc > 2) { if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[2]) < 0) { printf( "Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " "JSON-serialized list of strings\n", - argv[3], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY); + argv[2], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY); exit(-1); } } @@ -47,7 +48,6 @@ int main(int argc, char **argv) { printf("Sending liveliness query '%s'...\n", expr); z_owned_reply_channel_t channel = zc_reply_fifo_new(16); - z_get_options_t opts = z_get_options_default(); zc_liveliness_get(z_loan(s), keyexpr, z_move(channel.send), NULL); z_owned_reply_t reply = z_reply_null(); for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { diff --git a/examples/z_liveliness.c b/examples/z_liveliness.c index 33cdd7422..3df2f4383 100644 --- a/examples/z_liveliness.c +++ b/examples/z_liveliness.c @@ -21,13 +21,18 @@ #endif #include "zenoh.h" -const char *expr = "group1/zenoh-rs"; -z_keyexpr_t keyexpr; - int main(int argc, char **argv) { + char *expr = "group1/zenoh-rs"; if (argc > 1) { expr = argv[1]; } + + z_keyexpr_t keyexpr = z_keyexpr(expr); + if (!z_check(keyexpr)) { + printf("%s is not a valid key expression\n", expr); + exit(-1); + } + z_owned_config_t config = z_config_default(); if (argc > 2) { if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[2]) < 0) { @@ -45,16 +50,11 @@ int main(int argc, char **argv) { printf("Unable to open session!\n"); exit(-1); } - keyexpr = z_keyexpr(expr); - if (!z_check(keyexpr)) { - printf("%s is not a valid key expression", expr); - exit(-1); - } printf("Declaring liveliness token '%s'...\n", expr); zc_owned_liveliness_token_t token = zc_liveliness_declare_token(z_loan(s), keyexpr, NULL); if (!zc_liveliness_token_check(&token)) { - printf("Unable to create liveliness token.\n"); + printf("Unable to create liveliness token!\n"); exit(-1); } diff --git a/examples/z_sub_liveliness.c b/examples/z_sub_liveliness.c index b7ada51b4..f67ac5a68 100644 --- a/examples/z_sub_liveliness.c +++ b/examples/z_sub_liveliness.c @@ -39,6 +39,12 @@ int main(int argc, char **argv) { expr = argv[1]; } + z_keyexpr_t keyexpr = z_keyexpr(expr); + if (!z_check(keyexpr)) { + printf("%s is not a valid key expression\n", expr); + exit(-1); + } + z_owned_config_t config = z_config_default(); if (argc > 2) { if (zc_config_insert_json(z_loan(config), Z_CONFIG_LISTEN_KEY, argv[2]) < 0) { @@ -57,9 +63,9 @@ int main(int argc, char **argv) { exit(-1); } - z_owned_closure_sample_t callback = z_closure(data_handler); printf("Declaring liveliness subscriber on '%s'...\n", expr); - z_owned_subscriber_t sub = zc_liveliness_declare_subscriber(z_loan(s), z_keyexpr(expr), z_move(callback), NULL); + z_owned_closure_sample_t callback = z_closure(data_handler); + z_owned_subscriber_t sub = zc_liveliness_declare_subscriber(z_loan(s), keyexpr, z_move(callback), NULL); if (!z_check(sub)) { printf("Unable to declare liveliness subscriber.\n"); exit(-1); From 9cada9ff80879c6ea5393e90b4d5e2a58d849fcc Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Thu, 9 Nov 2023 10:24:35 +0100 Subject: [PATCH 6/7] Add zenoh macroses for liveliness --- examples/z_liveliness.c | 2 +- include/zenoh_macros.h | 38 ++++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/examples/z_liveliness.c b/examples/z_liveliness.c index 3df2f4383..9b9a0965c 100644 --- a/examples/z_liveliness.c +++ b/examples/z_liveliness.c @@ -53,7 +53,7 @@ int main(int argc, char **argv) { printf("Declaring liveliness token '%s'...\n", expr); zc_owned_liveliness_token_t token = zc_liveliness_declare_token(z_loan(s), keyexpr, NULL); - if (!zc_liveliness_token_check(&token)) { + if (!z_check(token)) { printf("Unable to create liveliness token!\n"); exit(-1); } diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index d817fb6ad..4bb00e95e 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -62,27 +62,29 @@ z_owned_reply_channel_t * : z_reply_channel_null, \ zc_owned_payload_t * : zc_payload_null, \ zc_owned_shmbuf_t * : zc_shmbuf_null, \ - zc_owned_shm_manager_t * : zc_shm_manager_null \ + zc_owned_shm_manager_t * : zc_shm_manager_null, \ + zc_owned_liveliness_token_t * : zc_liveliness_token_null \ )()) #define z_check(x) \ - _Generic((x), z_owned_session_t : z_session_check, \ - z_owned_publisher_t : z_publisher_check, \ - z_owned_keyexpr_t : z_keyexpr_check, \ - z_keyexpr_t : z_keyexpr_is_initialized, \ - z_owned_config_t : z_config_check, \ - z_owned_scouting_config_t : z_scouting_config_check, \ - z_bytes_t : z_bytes_check, \ - z_owned_subscriber_t : z_subscriber_check, \ - z_owned_pull_subscriber_t : z_pull_subscriber_check, \ - z_owned_queryable_t : z_queryable_check, \ - z_owned_encoding_t : z_encoding_check, \ - z_owned_reply_t : z_reply_check, \ - z_owned_hello_t : z_hello_check, \ - z_owned_str_t : z_str_check, \ - zc_owned_payload_t : zc_payload_check, \ - zc_owned_shmbuf_t : zc_shmbuf_check, \ - zc_owned_shm_manager_t : zc_shm_manager_check \ + _Generic((x), z_owned_session_t : z_session_check, \ + z_owned_publisher_t : z_publisher_check, \ + z_owned_keyexpr_t : z_keyexpr_check, \ + z_keyexpr_t : z_keyexpr_is_initialized, \ + z_owned_config_t : z_config_check, \ + z_owned_scouting_config_t : z_scouting_config_check, \ + z_bytes_t : z_bytes_check, \ + z_owned_subscriber_t : z_subscriber_check, \ + z_owned_pull_subscriber_t : z_pull_subscriber_check, \ + z_owned_queryable_t : z_queryable_check, \ + z_owned_encoding_t : z_encoding_check, \ + z_owned_reply_t : z_reply_check, \ + z_owned_hello_t : z_hello_check, \ + z_owned_str_t : z_str_check, \ + zc_owned_payload_t : zc_payload_check, \ + zc_owned_shmbuf_t : zc_shmbuf_check, \ + zc_owned_shm_manager_t : zc_shm_manager_check, \ + zc_owned_liveliness_token_t : zc_liveliness_token_check \ )(&x) #define z_call(x, ...) \ From 4928b11750c3b0e7d51662675ff6dc57a929dfb2 Mon Sep 17 00:00:00 2001 From: Alexander Bushnev Date: Thu, 9 Nov 2023 12:17:01 +0100 Subject: [PATCH 7/7] Add z_drop for liveliness --- examples/z_liveliness.c | 4 ++-- include/zenoh_macros.h | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/z_liveliness.c b/examples/z_liveliness.c index 9b9a0965c..b21cc40bc 100644 --- a/examples/z_liveliness.c +++ b/examples/z_liveliness.c @@ -66,11 +66,11 @@ int main(int argc, char **argv) { sleep(1); } else if (c == 'd') { printf("Undeclaring liveliness token...\n"); - zc_liveliness_undeclare_token(&token); + z_drop(z_move(token)); } } - zc_liveliness_undeclare_token(z_move(token)); + z_drop(z_move(token)); z_close(z_move(s)); return 0; } diff --git a/include/zenoh_macros.h b/include/zenoh_macros.h index 4bb00e95e..2a2c23e51 100644 --- a/include/zenoh_macros.h +++ b/include/zenoh_macros.h @@ -37,7 +37,8 @@ z_owned_reply_channel_t * : z_reply_channel_drop, \ zc_owned_payload_t * : zc_payload_drop, \ zc_owned_shmbuf_t * : zc_shmbuf_drop, \ - zc_owned_shm_manager_t * : zc_shm_manager_drop \ + zc_owned_shm_manager_t * : zc_shm_manager_drop, \ + zc_owned_liveliness_token_t * : zc_liveliness_undeclare_token \ )(x) #define z_null(x) (*x = \