-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Align examples and remove reading from stdin #359
Changes from 4 commits
07c7040
cb09cff
f0a1576
7f94ce9
cc5475e
54e8596
28e4b04
0196aa7
1e3e6a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,13 +27,17 @@ | |
#endif | ||
|
||
#define KEYEXPR "demo/example/**" | ||
#define N 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should align the behaviour with all other bindings, I believe we don't wait for just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other bindings do not implement single-threaded pub/sub examples, therefore we only need to align the configuration of these examples across their different plateform-specific implementations. In this PR, the configuration is as follows:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the other comment on this file, I'll update all implementations to start with a default number of samples of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behaviour of a |
||
|
||
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) { | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,16 +12,30 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
|
||
#include <ctype.h> | ||
#include <errno.h> | ||
#include <pthread.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_QUERY == 1 | ||
#define QUERY_TIMEOUT 10 // query timeout in seconds | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: we'll have to revise this example when timeout is exposed by zenoh pico |
||
#define handle_error_en(en, msg) \ | ||
do { \ | ||
errno = en; \ | ||
perror(msg); \ | ||
exit(EXIT_FAILURE); \ | ||
} while (0) | ||
pthread_cond_t cond; | ||
pthread_mutex_t mutex; | ||
|
||
void reply_dropper(void *ctx) { | ||
(void)(ctx); | ||
printf(">> Received query final notification\n"); | ||
pthread_cond_signal(&cond); | ||
pthread_cond_destroy(&cond); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the public sync mechanisms found in |
||
} | ||
|
||
void reply_handler(z_owned_reply_t *reply, void *ctx) { | ||
|
@@ -73,6 +87,9 @@ int main(int argc, char **argv) { | |
} | ||
} | ||
|
||
pthread_mutex_init(&mutex, NULL); | ||
pthread_cond_init(&cond, NULL); | ||
|
||
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) { | ||
|
@@ -102,27 +119,25 @@ 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; | ||
} | ||
pthread_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; | ||
} | ||
struct timespec query_timeout; | ||
clock_gettime(CLOCK_REALTIME, &query_timeout); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use the public timer mechanisms found in |
||
query_timeout.tv_sec += QUERY_TIMEOUT; | ||
int err = pthread_cond_timedwait(&cond, &mutex, &query_timeout); | ||
if (err != 0) { | ||
handle_error_en(err, "pthread_cond_timedwait"); | ||
} | ||
pthread_mutex_unlock(&mutex); | ||
|
||
// Stop read and lease tasks for zenoh-pico | ||
zp_stop_read_task(z_loan(s)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case after
10
publications the example will exit, however all other examples kepp publishing indefinitely.