-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from sashacmc/liveliness_sample
Add liveliness samples
- Loading branch information
Showing
4 changed files
with
251 additions
and
19 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,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