-
Notifications
You must be signed in to change notification settings - Fork 74
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
Implement liveliness support #632
Open
sashacmc
wants to merge
4
commits into
eclipse-zenoh:main
Choose a base branch
from
ZettaScaleLabs:liveliness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,165
−149
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// | ||
// 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 <stddef.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_LIVELINESS == 1 && Z_FEATURE_QUERY == 1 | ||
|
||
int main(int argc, char **argv) { | ||
const char *keyexpr = "group1/**"; | ||
const char *mode = "client"; | ||
const char *clocator = NULL; | ||
const char *llocator = NULL; | ||
|
||
int opt; | ||
while ((opt = getopt(argc, argv, "k:e:m:l:")) != -1) { | ||
switch (opt) { | ||
case 'k': | ||
keyexpr = optarg; | ||
break; | ||
case 'e': | ||
clocator = optarg; | ||
break; | ||
case 'm': | ||
mode = optarg; | ||
break; | ||
case 'l': | ||
llocator = optarg; | ||
break; | ||
case '?': | ||
if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'v' || optopt == 'l') { | ||
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; | ||
} | ||
|
||
z_view_keyexpr_t ke; | ||
if (z_view_keyexpr_from_str(&ke, keyexpr) < 0) { | ||
printf("%s is not a valid key expression", keyexpr); | ||
return -1; | ||
} | ||
|
||
printf("Sending liveliness query '%s'...\n", keyexpr); | ||
z_owned_fifo_handler_reply_t handler; | ||
z_owned_closure_reply_t closure; | ||
z_fifo_channel_reply_new(&closure, &handler, 16); | ||
if (z_liveliness_get(z_loan(s), z_loan(ke), z_move(closure), NULL) < 0) { | ||
printf("Liveliness query failed"); | ||
return -1; | ||
} | ||
z_owned_reply_t reply; | ||
for (z_result_t res = z_recv(z_loan(handler), &reply); res == Z_OK; res = z_recv(z_loan(handler), &reply)) { | ||
if (z_reply_is_ok(z_loan(reply))) { | ||
const z_loaned_sample_t *sample = z_reply_ok(z_loan(reply)); | ||
z_view_string_t key_str; | ||
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_str); | ||
printf(">> Alive token ('%.*s')\n", (int)z_string_len(z_loan(key_str)), z_string_data(z_loan(key_str))); | ||
} else { | ||
printf("Received an error\n"); | ||
} | ||
} | ||
|
||
z_drop(z_move(reply)); | ||
z_drop(z_move(handler)); | ||
z_drop(z_move(s)); | ||
return 0; | ||
} | ||
#else | ||
int main(void) { | ||
printf( | ||
"ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY or Z_FEATURE_LIVELINESS but this example requires " | ||
"them.\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// | ||
// 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 <signal.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_LIVELINESS == 1 | ||
|
||
static volatile int keepRunning = 1; | ||
|
||
void intHandler(int dummy) { | ||
(void)dummy; | ||
keepRunning = 0; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
const char *keyexpr = "group1/zenoh-pico"; | ||
const char *mode = "client"; | ||
const char *clocator = NULL; | ||
const char *llocator = NULL; | ||
|
||
int opt; | ||
while ((opt = getopt(argc, argv, "k:e:m:l:")) != -1) { | ||
switch (opt) { | ||
case 'k': | ||
keyexpr = optarg; | ||
break; | ||
case 'e': | ||
clocator = optarg; | ||
break; | ||
case 'm': | ||
mode = optarg; | ||
break; | ||
case 'l': | ||
llocator = optarg; | ||
break; | ||
case '?': | ||
if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'v' || optopt == 'l') { | ||
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; | ||
} | ||
|
||
z_view_keyexpr_t ke; | ||
if (z_view_keyexpr_from_str(&ke, keyexpr) < 0) { | ||
printf("%s is not a valid key expression", keyexpr); | ||
return -1; | ||
} | ||
|
||
printf("Declaring liveliness token '%s'...\n", keyexpr); | ||
z_owned_liveliness_token_t token; | ||
if (z_liveliness_declare_token(z_loan(s), &token, z_loan(ke), NULL) < 0) { | ||
printf("Unable to create liveliness token!\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("Press CTRL-C to undeclare liveliness token and quit...\n"); | ||
signal(SIGINT, intHandler); | ||
while (keepRunning) { | ||
z_sleep_s(1); | ||
} | ||
|
||
// 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_drop(z_move(s)); | ||
return 0; | ||
} | ||
#else | ||
int main(void) { | ||
printf( | ||
"ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY or Z_FEATURE_MULTI_THREAD but this example requires " | ||
"them.\n"); | ||
return -2; | ||
} | ||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we really need that ?
I'm not sure to see the added value.
(We don't have this in rust example)
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.
Without this, neither session closing nor token undeclaration is called at all, the example with zenoh-pico is simply interrupted. Yes, after the session loss timeout, the router will report the loss of the token, but for example in multicast peer-to-peer this will not happen.