-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add liveliness bindings #189
Merged
Merged
Changes from all 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// Copyright (c) 2023 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 <stdio.h> | ||
#include <string.h> | ||
|
||
#include "zenoh.h" | ||
|
||
int main(int argc, char **argv) { | ||
char *expr = "group1/**"; | ||
if (argc > 1) { | ||
expr = argv[1]; | ||
} | ||
|
||
z_keyexpr_t keyexpr = z_keyexpr(expr); | ||
if (!z_check(keyexpr)) { | ||
printf("%s is not a valid key expression\n", expr); | ||
exit(-1); | ||
} | ||
|
||
z_owned_config_t config = z_config_default(); | ||
if (argc > 2) { | ||
if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[2]) < 0) { | ||
printf( | ||
"Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " | ||
"JSON-serialized list of strings\n", | ||
argv[2], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY); | ||
exit(-1); | ||
} | ||
} | ||
|
||
printf("Opening session...\n"); | ||
z_owned_session_t s = z_open(z_move(config)); | ||
if (!z_check(s)) { | ||
printf("Unable to open session!\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("Sending liveliness query '%s'...\n", expr); | ||
z_owned_reply_channel_t channel = zc_reply_fifo_new(16); | ||
zc_liveliness_get(z_loan(s), keyexpr, z_move(channel.send), NULL); | ||
z_owned_reply_t reply = z_reply_null(); | ||
for (z_call(channel.recv, &reply); z_check(reply); z_call(channel.recv, &reply)) { | ||
if (z_reply_is_ok(&reply)) { | ||
z_sample_t sample = z_reply_ok(&reply); | ||
z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); | ||
printf(">> Alive token ('%s')\n", z_loan(keystr)); | ||
z_drop(z_move(keystr)); | ||
} else { | ||
printf("Received an error\n"); | ||
} | ||
} | ||
z_drop(z_move(reply)); | ||
z_drop(z_move(channel)); | ||
z_close(z_move(s)); | ||
return 0; | ||
} |
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,76 @@ | ||
// | ||
// Copyright (c) 2023 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 <stdio.h> | ||
#include <string.h> | ||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) | ||
#include <windows.h> | ||
#define sleep(x) Sleep(x * 1000) | ||
#else | ||
#include <unistd.h> | ||
#endif | ||
#include "zenoh.h" | ||
|
||
int main(int argc, char **argv) { | ||
char *expr = "group1/zenoh-rs"; | ||
if (argc > 1) { | ||
expr = argv[1]; | ||
} | ||
|
||
z_keyexpr_t keyexpr = z_keyexpr(expr); | ||
if (!z_check(keyexpr)) { | ||
printf("%s is not a valid key expression\n", expr); | ||
exit(-1); | ||
} | ||
|
||
z_owned_config_t config = z_config_default(); | ||
if (argc > 2) { | ||
if (zc_config_insert_json(z_loan(config), Z_CONFIG_CONNECT_KEY, argv[2]) < 0) { | ||
printf( | ||
"Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " | ||
"JSON-serialized list of strings\n", | ||
argv[2], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY); | ||
exit(-1); | ||
} | ||
} | ||
|
||
printf("Opening session...\n"); | ||
z_owned_session_t s = z_open(z_move(config)); | ||
if (!z_check(s)) { | ||
printf("Unable to open session!\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("Declaring liveliness token '%s'...\n", expr); | ||
zc_owned_liveliness_token_t token = zc_liveliness_declare_token(z_loan(s), keyexpr, NULL); | ||
if (!z_check(token)) { | ||
printf("Unable to create liveliness token!\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("Enter 'd' to undeclare liveliness token, 'q' to quit...\n"); | ||
char c = 0; | ||
while (c != 'q') { | ||
c = getchar(); | ||
if (c == -1) { | ||
sleep(1); | ||
} else if (c == 'd') { | ||
printf("Undeclaring liveliness token...\n"); | ||
z_drop(z_move(token)); | ||
} | ||
} | ||
|
||
z_drop(z_move(token)); | ||
z_close(z_move(s)); | ||
return 0; | ||
} |
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,86 @@ | ||
// | ||
// Copyright (c) 2023 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 <stdio.h> | ||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) | ||
#include <windows.h> | ||
#define sleep(x) Sleep(x * 1000) | ||
#else | ||
#include <unistd.h> | ||
#endif | ||
#include "zenoh.h" | ||
|
||
void data_handler(const z_sample_t *sample, void *arg) { | ||
z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); | ||
switch (sample->kind) { | ||
case Z_SAMPLE_KIND_PUT: | ||
printf(">> [LivelinessSubscriber] New alive token ('%s')\n", z_loan(keystr)); | ||
break; | ||
case Z_SAMPLE_KIND_DELETE: | ||
printf(">> [LivelinessSubscriber] Dropped token ('%s')\n", z_loan(keystr)); | ||
break; | ||
} | ||
z_drop(z_move(keystr)); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
char *expr = "group1/**"; | ||
if (argc > 1) { | ||
expr = argv[1]; | ||
} | ||
|
||
z_keyexpr_t keyexpr = z_keyexpr(expr); | ||
if (!z_check(keyexpr)) { | ||
printf("%s is not a valid key expression\n", expr); | ||
exit(-1); | ||
} | ||
|
||
z_owned_config_t config = z_config_default(); | ||
if (argc > 2) { | ||
if (zc_config_insert_json(z_loan(config), Z_CONFIG_LISTEN_KEY, argv[2]) < 0) { | ||
printf( | ||
"Couldn't insert value `%s` in configuration at `%s`. This is likely because `%s` expects a " | ||
"JSON-serialized list of strings\n", | ||
argv[2], Z_CONFIG_LISTEN_KEY, Z_CONFIG_LISTEN_KEY); | ||
exit(-1); | ||
} | ||
} | ||
|
||
printf("Opening session...\n"); | ||
z_owned_session_t s = z_open(z_move(config)); | ||
if (!z_check(s)) { | ||
printf("Unable to open session!\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("Declaring liveliness subscriber on '%s'...\n", expr); | ||
z_owned_closure_sample_t callback = z_closure(data_handler); | ||
z_owned_subscriber_t sub = zc_liveliness_declare_subscriber(z_loan(s), keyexpr, z_move(callback), NULL); | ||
if (!z_check(sub)) { | ||
printf("Unable to declare liveliness subscriber.\n"); | ||
exit(-1); | ||
} | ||
|
||
printf("Enter 'q' to quit...\n"); | ||
char c = 0; | ||
while (c != 'q') { | ||
c = getchar(); | ||
if (c == -1) { | ||
sleep(1); | ||
} | ||
} | ||
|
||
z_undeclare_subscriber(z_move(sub)); | ||
z_close(z_move(s)); | ||
return 0; | ||
} |
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.
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.
The
zenoh-commons.h
header is meant to be common acrosszenoh-c
andzenoh-pico
.All functions starting with
zc_
are inherently meant to be specific tozenoh-c
.All these function declarations should hence go into zenoh-concrete.h.
@p-avital @milyin I see that other
zc_
functions slipped inzenoh-commons.h
in the past. I think they should be moved intozenoh-concrete.h
as well. Opinions?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.
Yeah, the original goal was to be able to copy-paste this header into pico to ensure we always have the same signatures, but it didn't end up working out that way.
I think we can make the splitter logic a bit better if we want to stick to the original ideal. Otherwise, we could remove the splitter entirely, or just leave it in that state. Any odd these solutions is fine by me :)
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.
zenoh-pico
has evolved quite a bit since we initially made that split and I see how the split could hardly work out in practice. Therefore I'm more in favour of unifying all the includes in a single file rather than introducing artificial splits (although correct in principle).For what concerns the scope of this specific PR, let's not change anything for the time being... A different PR may follow in the future.
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.
Agreed, though note that we'll never be single filed because the zenoh-macros file is still hand-written