Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Jan 29, 2025
1 parent 8dbcbd2 commit 434ba84
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion examples/unix/c11/z_querier.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@

#if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 && defined Z_FEATURE_UNSTABLE_API

#if Z_FEATURE_MATCHING == 1
void matching_status_handler(const z_matching_status_t *matching_status, void *arg) {
(void)arg;
if (matching_status->matching) {
printf("Querier has matching queryable.\n");
} else {
printf("Querier has NO MORE matching queryables.\n");
}
}
#endif

int main(int argc, char **argv) {
const char *selector = "demo/example/**";
const char *mode = "client";
Expand All @@ -27,9 +38,10 @@ int main(int argc, char **argv) {
const char *value = NULL;
int n = INT_MAX;
int timeout_ms = 0;
bool add_matching_listener = false;

int opt;
while ((opt = getopt(argc, argv, "s:e:m:v:l:n:t:")) != -1) {
while ((opt = getopt(argc, argv, "s:e:m:v:l:n:t:a")) != -1) {
switch (opt) {
case 's':
selector = optarg;
Expand All @@ -52,6 +64,9 @@ int main(int argc, char **argv) {
case 't':
timeout_ms = atoi(optarg);
break;
case 'a':
add_matching_listener = true;
break;
case '?':
if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'v' || optopt == 'l' ||
optopt == 'n' || optopt == 't') {
Expand Down Expand Up @@ -115,6 +130,17 @@ int main(int argc, char **argv) {
exit(-1);
}

if (add_matching_listener) {
#if Z_FEATURE_MATCHING == 1
z_owned_closure_matching_status_t callback;
z_closure(&callback, matching_status_handler, NULL, NULL);
z_querier_declare_background_matching_listener(z_loan(querier), z_move(callback));
#else
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_MATCHING but this example requires it.\n");
return -2;
#endif
}

printf("Press CTRL-C to quit...\n");
char buf[256];
for (int idx = 0; idx != n; ++idx) {
Expand Down

0 comments on commit 434ba84

Please sign in to comment.