From 4df1107d137ace2d0f475a0b61eac8cbf5537e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Guimar=C3=A3es?= Date: Wed, 5 Jul 2023 15:31:53 +0200 Subject: [PATCH] Fix ping-pong examples (#226) (#227) --- examples/unix/c11/z_ping.c | 27 ++++++++++++++++++++++++--- examples/unix/c11/z_pong.c | 25 +++++++++++++++++++++++-- examples/unix/c99/z_ping.c | 36 +++++++++++++++++++++++++++++------- examples/unix/c99/z_pong.c | 35 +++++++++++++++++++++++++++++------ examples/windows/z_ping.c | 28 ++++++++++++++++++++++++---- examples/windows/z_pong.c | 25 +++++++++++++++++++++++-- 6 files changed, 152 insertions(+), 24 deletions(-) diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index 2472196fe..b048737ff 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -46,11 +46,32 @@ int main(int argc, char** argv) { _z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_owned_closure_sample_t respond = z_closure(callback, drop, NULL); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; @@ -79,8 +100,8 @@ int main(int argc, char** argv) { _z_mutex_unlock(&mutex); z_free(results); z_free(data); - z_drop(z_move(sub)); - z_drop(z_move(pub)); + z_undeclare_publisher(z_move(pub)); + z_undeclare_subscriber(z_move(sub)); z_close(z_move(session)); } diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index 7f0731478..dde80e160 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -19,13 +19,34 @@ int main(int argc, char** argv) { (void)argv; z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + while (getchar() != 'q') { } - z_drop(z_move(sub)); + + z_undeclare_subscriber(z_move(sub)); z_close(z_move(session)); } \ No newline at end of file diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 9bbb18695..3ddd8db99 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -44,12 +44,34 @@ int main(int argc, char** argv) { _z_mutex_init(&mutex); _z_condvar_init(&cond); z_owned_config_t config = z_config_default(); - z_owned_session_t session = z_open(z_move(config)); + z_owned_session_t session = z_open(z_config_move(&config)); + if (!z_session_check(&session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_session_loan(&session), NULL) < 0 || + zp_start_lease_task(z_session_loan(&session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); - z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), ping, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); - z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), pong, z_move(respond), NULL); + if (!z_publisher_check(&pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_closure_sample_t respond = z_closure_sample(callback, drop, NULL); + z_owned_subscriber_t sub = + z_declare_subscriber(z_session_loan(&session), pong, z_closure_sample_move(&respond), NULL); + if (!z_subscriber_check(&sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; @@ -78,9 +100,9 @@ int main(int argc, char** argv) { _z_mutex_unlock(&mutex); z_free(results); z_free(data); - z_undeclare_subscriber(z_move(sub)); - z_undeclare_publisher(z_move(pub)); - z_close(z_move(session)); + z_undeclare_subscriber(z_subscriber_move(&sub)); + z_undeclare_publisher(z_publisher_move(&pub)); + z_close(z_session_move(&session)); } char* getopt(int argc, char** argv, char option) { diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index 49c99c488..7b5cb9ff1 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -19,14 +19,37 @@ int main(int argc, char** argv) { (void)argc; (void)argv; z_owned_config_t config = z_config_default(); - z_owned_session_t session = z_open(z_move(config)); - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_owned_session_t session = z_open(z_config_move(&config)); + if (!z_session_check(&session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_session_loan(&session), NULL) < 0 || + zp_start_lease_task(z_session_loan(&session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_session_loan(&session), pong, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); - z_owned_subscriber_t sub = z_declare_subscriber(z_session_loan(&session), ping, z_move(respond), NULL); + if (!z_publisher_check(&pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + z_owned_closure_sample_t respond = z_closure_sample(callback, drop, (void*)z_publisher_move(&pub)); + z_owned_subscriber_t sub = + z_declare_subscriber(z_session_loan(&session), ping, z_closure_sample_move(&respond), NULL); + if (!z_subscriber_check(&sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + while (getchar() != 'q') { } - z_undeclare_subscriber(z_move(sub)); - z_close(z_move(session)); + + z_undeclare_subscriber(z_subscriber_move(&sub)); + z_close(z_session_move(&session)); } \ No newline at end of file diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 1ce6759ef..4fa5b9e1e 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -44,11 +44,31 @@ int main(int argc, char** argv) { _z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); - z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); - z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); + z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; @@ -77,8 +97,8 @@ int main(int argc, char** argv) { _z_mutex_unlock(&mutex); z_free(results); z_free(data); - z_drop(z_move(sub)); - z_drop(z_move(pub)); + z_undeclare_pubscriber(z_move(pub)); + z_undeclare_subscriber(z_move(sub)); z_close(z_move(session)); } diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index 7f0731478..dde80e160 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -19,13 +19,34 @@ int main(int argc, char** argv) { (void)argv; z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); - z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); + if (!z_check(session)) { + printf("Unable to open session!\n"); + return -1; + } + + if (zp_start_read_task(z_loan(session), NULL) < 0 || zp_start_lease_task(z_loan(session), NULL) < 0) { + printf("Unable to start read and lease tasks"); + return -1; + } + z_keyexpr_t pong = z_keyexpr_unchecked("test/pong"); z_owned_publisher_t pub = z_declare_publisher(z_loan(session), pong, NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + return -1; + } + + z_keyexpr_t ping = z_keyexpr_unchecked("test/ping"); z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)z_move(pub)); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), ping, z_move(respond), NULL); + if (!z_check(sub)) { + printf("Unable to declare subscriber for key expression.\n"); + return -1; + } + while (getchar() != 'q') { } - z_drop(z_move(sub)); + + z_undeclare_subscriber(z_move(sub)); z_close(z_move(session)); } \ No newline at end of file