From c38dfb5be27ae0d903468ea369f3048edd8eab00 Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Thu, 22 Feb 2024 15:40:29 +0100 Subject: [PATCH] Remove reading from stdin, align example implementations --- examples/z_liveliness.c | 20 +++++++++----------- examples/z_pub.c | 1 + examples/z_pub_attachment.c | 1 + examples/z_pub_cache.c | 1 + examples/z_pub_shm.c | 1 + examples/z_pub_thr.c | 1 + examples/z_pull.c | 14 +++++--------- examples/z_query_sub.c | 10 +++------- examples/z_queryable.c | 10 +++------- examples/z_queryable_with_channels.c | 2 +- examples/z_sub.c | 10 +++------- examples/z_sub_attachment.c | 10 +++------- examples/z_sub_liveliness.c | 10 +++------- examples/z_sub_thr.c | 13 +++++++++---- 14 files changed, 44 insertions(+), 60 deletions(-) diff --git a/examples/z_liveliness.c b/examples/z_liveliness.c index b21cc40bc..f8fd5707b 100644 --- a/examples/z_liveliness.c +++ b/examples/z_liveliness.c @@ -58,19 +58,17 @@ int main(int argc, char **argv) { 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"); - z_drop(z_move(token)); - } + printf("Press CTRL-C to undeclare liveliness token and quit...\n"); + while (1) { + sleep(1); } - z_drop(z_move(token)); + // LivelinessTokens are automatically closed when dropped + // Use the code below to manually undeclare it if needed + // + // printf("Undeclaring liveliness token...\n"); + // z_drop(z_move(token)); + z_close(z_move(s)); return 0; } diff --git a/examples/z_pub.c b/examples/z_pub.c index 73fd02287..b5a565e66 100644 --- a/examples/z_pub.c +++ b/examples/z_pub.c @@ -70,6 +70,7 @@ int main(int argc, char **argv) { listener = zcu_publisher_matching_listener_callback(z_loan(pub), z_move(callback)); } + printf("Press CTRL-C to quit...\n"); char buf[256]; for (int idx = 0; 1; ++idx) { sleep(1); diff --git a/examples/z_pub_attachment.c b/examples/z_pub_attachment.c index b75f245c3..535cda9ea 100644 --- a/examples/z_pub_attachment.c +++ b/examples/z_pub_attachment.c @@ -66,6 +66,7 @@ int main(int argc, char **argv) { // add some value z_bytes_map_insert_by_alias(&map, z_bytes_from_str("source"), z_bytes_from_str("C")); + printf("Press CTRL-C to quit...\n"); char buf[256]; char buf_ind[16]; for (int idx = 0; 1; ++idx) { diff --git a/examples/z_pub_cache.c b/examples/z_pub_cache.c index 8c1d36221..d003f5cf2 100644 --- a/examples/z_pub_cache.c +++ b/examples/z_pub_cache.c @@ -64,6 +64,7 @@ int main(int argc, char **argv) { exit(-1); } + printf("Press CTRL-C to quit...\n"); char buf[256]; for (int idx = 0; 1; ++idx) { sleep(1); diff --git a/examples/z_pub_shm.c b/examples/z_pub_shm.c index f26c65c08..e12f01bf9 100644 --- a/examples/z_pub_shm.c +++ b/examples/z_pub_shm.c @@ -70,6 +70,7 @@ int main(int argc, char **argv) { exit(-1); } + printf("Press CTRL-C to quit...\n"); for (int idx = 0; true; ++idx) { zc_owned_shmbuf_t shmbuf = zc_shm_alloc(&manager, 256); if (!z_check(shmbuf)) { diff --git a/examples/z_pub_thr.c b/examples/z_pub_thr.c index 8686c33eb..54b992d3c 100644 --- a/examples/z_pub_thr.c +++ b/examples/z_pub_thr.c @@ -53,6 +53,7 @@ int main(int argc, char **argv) { exit(-1); } + printf("Press CTRL-C to quit...\n"); while (1) { z_publisher_put(z_loan(pub), (const uint8_t *)value, len, NULL); } diff --git a/examples/z_pull.c b/examples/z_pull.c index d7039e902..8052f577b 100644 --- a/examples/z_pull.c +++ b/examples/z_pull.c @@ -61,15 +61,11 @@ int main(int argc, char **argv) { exit(-1); } - printf("Press to pull data...\n"); - char c = 0; - while (c != 'q') { - c = getchar(); - if (c == -1) { - sleep(1); - } else { - z_subscriber_pull(z_loan(sub)); - } + printf("Press CTRL-C to quit...\n"); + for (int idx=0; 1; ++idx) { + sleep(1); + printf("[%4d] Pulling...\n", idx); + z_subscriber_pull(z_loan(sub)); } z_undeclare_pull_subscriber(z_move(sub)); diff --git a/examples/z_query_sub.c b/examples/z_query_sub.c index 680cfd973..5b3a2c7bb 100644 --- a/examples/z_query_sub.c +++ b/examples/z_query_sub.c @@ -63,13 +63,9 @@ int main(int argc, char **argv) { exit(-1); } - printf("Enter 'q' to quit...\n"); - char c = 0; - while (c != 'q') { - c = getchar(); - if (c == -1) { - sleep(1); - } + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_drop(z_move(sub)); diff --git a/examples/z_queryable.c b/examples/z_queryable.c index f74f5d355..4d6d6321d 100644 --- a/examples/z_queryable.c +++ b/examples/z_queryable.c @@ -76,13 +76,9 @@ int main(int argc, char **argv) { exit(-1); } - printf("Enter 'q' to quit...\n"); - char c = 0; - while (c != 'q') { - c = getchar(); - if (c == -1) { - sleep(1); - } + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_queryable(z_move(qable)); diff --git a/examples/z_queryable_with_channels.c b/examples/z_queryable_with_channels.c index 80c98d689..189710b37 100644 --- a/examples/z_queryable_with_channels.c +++ b/examples/z_queryable_with_channels.c @@ -68,7 +68,7 @@ int main(int argc, char **argv) { exit(-1); } - printf("^C to quit...\n"); + printf("Press CTRL-C to quit...\n"); z_owned_query_t oquery = z_query_null(); for (z_call(channel.recv, &oquery); z_check(oquery); z_call(channel.recv, &oquery)) { z_query_t query = z_loan(oquery); diff --git a/examples/z_sub.c b/examples/z_sub.c index ccfb32825..36de7ed70 100644 --- a/examples/z_sub.c +++ b/examples/z_sub.c @@ -68,13 +68,9 @@ int main(int argc, char **argv) { exit(-1); } - printf("Enter 'q' to quit...\n"); - char c = 0; - while (c != 'q') { - c = getchar(); - if (c == -1) { - sleep(1); - } + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_subscriber(z_move(sub)); diff --git a/examples/z_sub_attachment.c b/examples/z_sub_attachment.c index 26cc95ed8..e6e1f68a5 100644 --- a/examples/z_sub_attachment.c +++ b/examples/z_sub_attachment.c @@ -79,13 +79,9 @@ int main(int argc, char **argv) { exit(-1); } - printf("Enter 'q' to quit...\n"); - char c = 0; - while (c != 'q') { - c = getchar(); - if (c == -1) { - sleep(1); - } + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_subscriber(z_move(sub)); diff --git a/examples/z_sub_liveliness.c b/examples/z_sub_liveliness.c index f67ac5a68..5b8d0f96e 100644 --- a/examples/z_sub_liveliness.c +++ b/examples/z_sub_liveliness.c @@ -71,13 +71,9 @@ int main(int argc, char **argv) { exit(-1); } - printf("Enter 'q' to quit...\n"); - char c = 0; - while (c != 'q') { - c = getchar(); - if (c == -1) { - sleep(1); - } + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_subscriber(z_move(sub)); diff --git a/examples/z_sub_thr.c b/examples/z_sub_thr.c index d32480174..cd6b277c4 100644 --- a/examples/z_sub_thr.c +++ b/examples/z_sub_thr.c @@ -13,7 +13,12 @@ // #include #include - +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) +#include +#define sleep(x) Sleep(x * 1000) +#else +#include +#endif #include "zenoh.h" #define N 1000000 @@ -94,9 +99,9 @@ int main(int argc, char **argv) { exit(-1); } - char c = 0; - while (c != 'q') { - c = fgetc(stdin); + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_subscriber(z_move(sub));