Skip to content

Commit

Permalink
Align examples and remove reading from stdin (#359)
Browse files Browse the repository at this point in the history
* Remove reading from stdin, align example implementations

* Update examples tests

* Adjust z_sub_thr output

* Fix z_get single query examples

* Replace pthread uses with z_mutex and z_condvar

* Add multi-thread feature condition to z_get examples

* Update error message for features absence

* Update z_get expected output in modularity test

* Update sample count for freertos single-thread examples
  • Loading branch information
oteffahi authored Mar 21, 2024
1 parent 0d9f25b commit 7c724c3
Show file tree
Hide file tree
Showing 43 changed files with 298 additions and 302 deletions.
3 changes: 1 addition & 2 deletions examples/arduino/z_pub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void setup() {
}

void loop() {
delay(1000);
char buf[256];
sprintf(buf, "[%4d] %s", idx++, VALUE);
Serial.print("Writing Data ('");
Expand All @@ -105,8 +106,6 @@ void loop() {
if (z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL) < 0) {
Serial.println("Error while publishing data");
}

delay(1000);
}
#else
void setup() {
Expand Down
6 changes: 5 additions & 1 deletion examples/arduino/z_pull.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define KEYEXPR "demo/example/**"

z_owned_pull_subscriber_t sub;
int idx = 0;

void data_handler(const z_sample_t *sample, void *arg) {
z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr);
Expand Down Expand Up @@ -106,7 +107,10 @@ void setup() {
}

void loop() {
delay(5000);
delay(1000);
char buf[256];
sprintf(buf, "[%4d] Pulling...", idx++);
Serial.println(buf);
z_subscriber_pull(z_pull_subscriber_loan(&sub));
}
#else
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void setup() {
delay(300);
}

void loop() { delay(5000); }
void loop() { delay(1000); }

#else
void setup() {
Expand Down
2 changes: 1 addition & 1 deletion examples/arduino/z_sub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void setup() {
delay(300);
}

void loop() { delay(5000); }
void loop() { delay(1000); }

#else
void setup() {
Expand Down
5 changes: 3 additions & 2 deletions examples/espidf/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ void app_main() {
}
printf("OK!\n");

int idx = 0;
while (1) {
sleep(5);
printf("Pulling data from '%s'...\n", KEYEXPR);
sleep(1);
printf("[%4d] Pulling...\n", idx++);
z_subscriber_pull(z_loan(sub));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void app_main() {
printf("Zenoh setup finished!\n");

while (1) {
sleep(5);
sleep(1);
}

printf("Closing Zenoh Session...");
Expand Down
2 changes: 1 addition & 1 deletion examples/espidf/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void app_main() {
printf("OK!\n");

while (1) {
sleep(5);
sleep(1);
}

printf("Closing Zenoh Session...");
Expand Down
3 changes: 2 additions & 1 deletion examples/freertos_plus_tcp/z_pub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#define KEYEXPR "demo/example/zenoh-pico-pub"
#define VALUE "[FreeRTOS-Plus-TCP] Pub from Zenoh-Pico!"
#define N 2147483647 // max int value by default

void app_main(void) {
z_owned_config_t config = z_config_default();
Expand All @@ -54,7 +55,7 @@ void app_main(void) {

char *buf = (char *)pvPortMalloc(256);
z_clock_t now = z_clock_now();
for (int idx = 0; 1;) {
for (int idx = 0; idx < N;) {
if (z_clock_elapsed_ms(&now) > 1000) {
snprintf(buf, 256, "[%4d] %s", idx, VALUE);
printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf);
Expand Down
5 changes: 3 additions & 2 deletions examples/freertos_plus_tcp/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ void app_main(void) {
return;
}

int idx = 0;
while (1) {
z_sleep_s(5);
printf("Pulling data from '%s'...\n", KEYEXPR);
z_sleep_s(1);
printf("[%4d] Pulling...\n", idx++);
z_subscriber_pull(z_loan(sub));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void app_main(void) {
}

while (1) {
z_sleep_s(5);
z_sleep_s(1);
}

z_undeclare_queryable(z_move(qable));
Expand Down
2 changes: 1 addition & 1 deletion examples/freertos_plus_tcp/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void app_main(void) {
}

while (1) {
z_sleep_s(5);
z_sleep_s(1);
}

z_undeclare_subscriber(z_move(sub));
Expand Down
7 changes: 6 additions & 1 deletion examples/freertos_plus_tcp/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
#endif

#define KEYEXPR "demo/example/**"
#define N 2147483647 // max int value by default

int msg_nb = 0;

void data_handler(const z_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len,
sample->payload.start);
z_drop(z_move(keystr));
msg_nb++;
}

void app_main(void) {
Expand All @@ -58,7 +62,8 @@ void app_main(void) {
return;
}

while (1) {
printf("Running until %d messages are received...\n", N);
while (msg_nb < N) {
zp_read(z_loan(s), NULL);
zp_send_keep_alive(z_loan(s), NULL);
zp_send_join(z_loan(s), NULL);
Expand Down
5 changes: 3 additions & 2 deletions examples/mbed/z_pull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ int main(int argc, char **argv) {
}
printf("OK!\n");

int idx = 0;
while (1) {
z_sleep_s(5);
printf("Pulling data from '%s'...\n", KEYEXPR);
z_sleep_s(1);
printf("[%4d] Pulling...\n", idx++);
z_subscriber_pull(z_pull_subscriber_loan(&sub));
}

Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_queryable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int main(int argc, char **argv) {
printf("Zenoh setup finished!\n");

while (1) {
z_sleep_s(5);
z_sleep_s(1);
}

printf("Closing Zenoh Session...");
Expand Down
2 changes: 1 addition & 1 deletion examples/mbed/z_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, char **argv) {
printf("OK!\n");

while (1) {
z_sleep_s(5);
z_sleep_s(1);
}

printf("Closing Zenoh Session...");
Expand Down
46 changes: 24 additions & 22 deletions examples/unix/c11/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
#include <unistd.h>
#include <zenoh-pico.h>

#if Z_FEATURE_QUERY == 1
#if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1
static z_condvar_t cond;
static z_mutex_t mutex;

void reply_dropper(void *ctx) {
(void)(ctx);
printf(">> Received query final notification\n");
z_condvar_signal(&cond);
z_condvar_free(&cond);
}

void reply_handler(z_owned_reply_t *reply, void *ctx) {
Expand Down Expand Up @@ -73,6 +78,9 @@ int main(int argc, char **argv) {
}
}

z_mutex_init(&mutex);
z_condvar_init(&cond);

z_owned_config_t config = z_config_default();
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode));
if (clocator != NULL) {
Expand Down Expand Up @@ -102,27 +110,19 @@ int main(int argc, char **argv) {
return -1;
}

printf("Enter any key to pull data or 'q' to quit...\n");
char c = '\0';
while (1) {
fflush(stdin);
int ret = scanf("%c", &c);
(void)ret; // Remove unused result warning
if (c == 'q') {
break;
}

printf("Sending Query '%s'...\n", keyexpr);
z_get_options_t opts = z_get_options_default();
if (value != NULL) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value));
}
z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper);
if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) {
printf("Unable to send query.\n");
return -1;
}
z_mutex_lock(&mutex);
printf("Sending Query '%s'...\n", keyexpr);
z_get_options_t opts = z_get_options_default();
if (value != NULL) {
opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value));
}
z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper);
if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) {
printf("Unable to send query.\n");
return -1;
}
z_condvar_wait(&cond, &mutex);
z_mutex_unlock(&mutex);

// Stop read and lease tasks for zenoh-pico
zp_stop_read_task(z_loan(s));
Expand All @@ -134,7 +134,9 @@ int main(int argc, char **argv) {
}
#else
int main(void) {
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY but this example requires it.\n");
printf(
"ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY or Z_FEATURE_MULTI_THREAD but this example requires "
"them.\n");
return -2;
}
#endif
10 changes: 6 additions & 4 deletions examples/unix/c11/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char **argv) {
const char *mode = "client";
char *clocator = NULL;
char *llocator = NULL;
int n = 10;
int n = 2147483647; // max int value by default

int opt;
while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) {
Expand Down Expand Up @@ -96,14 +96,16 @@ int main(int argc, char **argv) {
return -1;
}

printf("Press CTRL-C to quit...\n");
char buf[256];
for (int idx = 0; idx < n; ++idx) {
sleep(1);
(void)idx;
printf("Putting Data ('%s': '%s')...\n", keyexpr, value);
sprintf(buf, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, buf);

z_publisher_put_options_t options = z_publisher_put_options_default();
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
z_publisher_put(z_loan(pub), (const uint8_t *)value, strlen(value), &options);
z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options);
}

z_undeclare_publisher(z_move(pub));
Expand Down
10 changes: 6 additions & 4 deletions examples/unix/c11/z_pub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main(int argc, char **argv) {
const char *mode = "client";
char *clocator = NULL;
char *llocator = NULL;
int n = 10;
int n = 2147483647; // max int value by default

int opt;
while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) {
Expand Down Expand Up @@ -86,11 +86,13 @@ int main(int argc, char **argv) {
return -1;
}
// Main loop
printf("Press CTRL-C to quit...\n");
char buf[256];
for (int idx = 0; idx < n; idx++) {
sleep(1);
(void)idx;
printf("Putting Data ('%s': '%s')...\n", keyexpr, value);
z_publisher_put(z_loan(pub), (const uint8_t *)value, strlen(value), NULL);
sprintf(buf, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, buf);
z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), NULL);

zp_read(z_loan(s), NULL);
zp_send_keep_alive(z_loan(s), NULL);
Expand Down
12 changes: 4 additions & 8 deletions examples/unix/c11/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,11 @@ int main(int argc, char **argv) {
return -1;
}

printf("Enter any key to pull data or 'q' to quit...\n");
char c = '\0';
printf("Press CTRL-C to quit...\n");
int idx = 0;
while (1) {
fflush(stdin);
int ret = scanf("%c", &c);
(void)ret; // Remove unused result warning
if (c == 'q') {
break;
}
sleep(1);
printf("[%4d] Pulling...\n", idx++);
z_subscriber_pull(z_loan(sub));
}

Expand Down
9 changes: 3 additions & 6 deletions examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,9 @@ int main(int argc, char **argv) {
return -1;
}

printf("Enter 'q' to quit...\n");
char c = '\0';
while (c != 'q') {
fflush(stdin);
int ret = scanf("%c", &c);
(void)ret; // Remove unused result warning
printf("Press CTRL-C to quit...\n");
while (1) {
sleep(1);
}

z_undeclare_queryable(z_move(qable));
Expand Down
9 changes: 3 additions & 6 deletions examples/unix/c11/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,9 @@ int main(int argc, char **argv) {
return -1;
}

printf("Enter 'q' to quit...\n");
char c = '\0';
while (c != 'q') {
fflush(stdin);
int ret = scanf("%c", &c);
(void)ret; // Remove unused result warning
printf("Press CTRL-C to quit...\n");
while (1) {
sleep(1);
}

z_undeclare_subscriber(z_move(sub));
Expand Down
5 changes: 3 additions & 2 deletions examples/unix/c11/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int main(int argc, char **argv) {
const char *mode = "client";
char *clocator = NULL;
char *llocator = NULL;
int n = -1;
int n = 2147483647; // max int value by default

int opt;
while ((opt = getopt(argc, argv, "k:e:m:l:n:")) != -1) {
Expand Down Expand Up @@ -93,7 +93,8 @@ int main(int argc, char **argv) {
return -1;
}

while (msg_nb != n) {
printf("Press CTRL-C to quit...\n");
while (msg_nb < n) {
zp_read(z_loan(s), NULL);
zp_send_keep_alive(z_loan(s), NULL);
zp_send_join(z_loan(s), NULL);
Expand Down
Loading

0 comments on commit 7c724c3

Please sign in to comment.