-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Liveliness subscriber implementation
- Loading branch information
Showing
10 changed files
with
335 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// | ||
// Copyright (c) 2024 ZettaScale Technology | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// | ||
// Contributors: | ||
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
|
||
#include <ctype.h> | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_LIVELINESS == 1 | ||
|
||
static int msg_nb = 0; | ||
|
||
void data_handler(z_loaned_sample_t *sample, void *ctx) { | ||
(void)(ctx); | ||
z_view_string_t key_string; | ||
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string); | ||
switch (z_sample_kind(sample)) { | ||
case Z_SAMPLE_KIND_PUT: | ||
printf(">> [LivelinessSubscriber] New alive token ('%.*s')\n", (int)z_string_len(z_loan(key_string)), | ||
z_string_data(z_loan(key_string))); | ||
break; | ||
case Z_SAMPLE_KIND_DELETE: | ||
printf(">> [LivelinessSubscriber] Dropped token ('%.*s')\n", (int)z_string_len(z_loan(key_string)), | ||
z_string_data(z_loan(key_string))); | ||
break; | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
const char *keyexpr = "group1/**"; | ||
const char *mode = "client"; | ||
char *clocator = NULL; | ||
char *llocator = NULL; | ||
int n = 0; | ||
|
||
int opt; | ||
while ((opt = getopt(argc, argv, "k:e:m:l:n:")) != -1) { | ||
switch (opt) { | ||
case 'k': | ||
keyexpr = optarg; | ||
break; | ||
case 'e': | ||
clocator = optarg; | ||
break; | ||
case 'm': | ||
mode = optarg; | ||
break; | ||
case 'l': | ||
llocator = optarg; | ||
break; | ||
case 'n': | ||
n = atoi(optarg); | ||
break; | ||
case '?': | ||
if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'l' || optopt == 'n') { | ||
fprintf(stderr, "Option -%c requires an argument.\n", optopt); | ||
} else { | ||
fprintf(stderr, "Unknown option `-%c'.\n", optopt); | ||
} | ||
return 1; | ||
default: | ||
return -1; | ||
} | ||
} | ||
|
||
z_owned_config_t config; | ||
z_config_default(&config); | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, mode); | ||
if (clocator != NULL) { | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, clocator); | ||
} | ||
if (llocator != NULL) { | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, llocator); | ||
} | ||
|
||
printf("Opening session...\n"); | ||
z_owned_session_t s; | ||
if (z_open(&s, z_move(config), NULL) < 0) { | ||
printf("Unable to open session!\n"); | ||
return -1; | ||
} | ||
|
||
// Start read and lease tasks for zenoh-pico | ||
if (zp_start_read_task(z_loan_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { | ||
printf("Unable to start read and lease tasks\n"); | ||
z_session_drop(z_session_move(&s)); | ||
return -1; | ||
} | ||
|
||
printf("Declaring liveliness subscriber on '%s'...\n", keyexpr); | ||
z_owned_closure_sample_t callback; | ||
z_closure(&callback, data_handler, NULL, NULL); | ||
z_owned_subscriber_t sub; | ||
|
||
z_view_keyexpr_t ke; | ||
z_view_keyexpr_from_str(&ke, keyexpr); | ||
if (z_liveliness_declare_subscriber(z_loan(s), &sub, z_loan(ke), z_move(callback), NULL) < 0) { | ||
printf("Unable to declare liveliness subscriber.\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("Press CTRL-C to quit...\n"); | ||
while (1) { | ||
z_sleep_s(1); | ||
} | ||
printf("Press CTRL-C to quit...\n"); | ||
while (1) { | ||
if ((n != 0) && (msg_nb >= n)) { | ||
break; | ||
} | ||
sleep(1); | ||
} | ||
// Clean up | ||
z_drop(z_move(sub)); | ||
z_drop(z_move(s)); | ||
return 0; | ||
} | ||
#else | ||
int main(void) { | ||
printf( | ||
"ERROR: Zenoh pico was compiled without Z_FEATURE_SUBSCRIPTION and Z_FEATURE_LIVELINESS but this example " | ||
"requires it.\n"); | ||
return -2; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.