-
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.
- Loading branch information
Showing
15 changed files
with
454 additions
and
23 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
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,129 @@ | ||
// | ||
// 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 <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 | ||
|
||
int main(int argc, char **argv) { | ||
const char *keyexpr = "demo/example/**"; | ||
const char *mode = "client"; | ||
const char *clocator = NULL; | ||
const char *llocator = NULL; | ||
const char *value = NULL; | ||
|
||
int opt; | ||
while ((opt = getopt(argc, argv, "k:e:m:v: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 'v': | ||
value = 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(); | ||
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); | ||
if (clocator != NULL) { | ||
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); | ||
} | ||
if (llocator != NULL) { | ||
zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); | ||
} | ||
|
||
printf("Opening session...\n"); | ||
z_owned_session_t s = z_open(z_move(config)); | ||
if (!z_check(s)) { | ||
printf("Unable to open session!\n"); | ||
return -1; | ||
} | ||
|
||
// Start read and lease tasks for zenoh-pico | ||
if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { | ||
printf("Unable to start read and lease tasks\n"); | ||
z_close(z_session_move(&s)); | ||
return -1; | ||
} | ||
|
||
z_keyexpr_t ke = z_keyexpr(keyexpr); | ||
if (!z_check(ke)) { | ||
printf("%s is not a valid key expression", keyexpr); | ||
return -1; | ||
} | ||
|
||
printf("Sending Query '%s'...\n", keyexpr); | ||
z_get_options_t opts = z_get_options_default(); | ||
if (value != NULL) { | ||
opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); | ||
} | ||
z_owned_reply_ring_channel_t channel = z_reply_ring_channel_new(1); | ||
if (z_get(z_loan(s), ke, "", z_move(channel.send), &opts) < 0) { | ||
printf("Unable to send query.\n"); | ||
return -1; | ||
} | ||
|
||
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(">> Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample.payload.len, sample.payload.start); | ||
z_drop(z_move(keystr)); | ||
} else { | ||
printf(">> Received an error\n"); | ||
} | ||
} | ||
|
||
z_drop(z_move(channel)); | ||
|
||
// Stop read and lease tasks for zenoh-pico | ||
zp_stop_read_task(z_loan(s)); | ||
zp_stop_lease_task(z_loan(s)); | ||
|
||
z_close(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 |
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,130 @@ | ||
// | ||
// 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 <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_QUERYABLE == 1 | ||
const char *keyexpr = "demo/example/zenoh-pico-queryable"; | ||
const char *value = "Queryable from Pico!"; | ||
|
||
int main(int argc, char **argv) { | ||
const char *mode = "client"; | ||
char *clocator = NULL; | ||
char *llocator = NULL; | ||
|
||
int opt; | ||
while ((opt = getopt(argc, argv, "k:e:m:v: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 'v': | ||
value = 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(); | ||
zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); | ||
if (clocator != NULL) { | ||
zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); | ||
} | ||
if (llocator != NULL) { | ||
zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); | ||
} | ||
|
||
printf("Opening session...\n"); | ||
z_owned_session_t s = z_open(z_move(config)); | ||
if (!z_check(s)) { | ||
printf("Unable to open session!\n"); | ||
return -1; | ||
} | ||
|
||
// Start read and lease tasks for zenoh-pico | ||
if (zp_start_read_task(z_loan(s), NULL) < 0 || zp_start_lease_task(z_loan(s), NULL) < 0) { | ||
printf("Unable to start read and lease tasks\n"); | ||
z_close(z_session_move(&s)); | ||
return -1; | ||
} | ||
|
||
z_keyexpr_t ke = z_keyexpr(keyexpr); | ||
if (!z_check(ke)) { | ||
printf("%s is not a valid key expression", keyexpr); | ||
return -1; | ||
} | ||
|
||
printf("Creating Queryable on '%s'...\n", keyexpr); | ||
z_owned_query_ring_channel_t channel = z_query_ring_channel_new(10); | ||
z_owned_queryable_t qable = z_declare_queryable(z_loan(s), ke, z_move(channel.send), NULL); | ||
if (!z_check(qable)) { | ||
printf("Unable to create queryable.\n"); | ||
return -1; | ||
} | ||
|
||
z_owned_query_t query = z_query_null(); | ||
for (z_call(channel.recv, &query); z_check(query); z_call(channel.recv, &query)) { | ||
z_query_t q = z_loan(query); | ||
z_owned_str_t keystr = z_keyexpr_to_string(z_query_keyexpr(&q)); | ||
z_bytes_t pred = z_query_parameters(&q); | ||
z_value_t payload_value = z_query_value(&q); | ||
printf(" >> [Queryable handler] Received Query '%s?%.*s'\n", z_loan(keystr), (int)pred.len, pred.start); | ||
if (payload_value.payload.len > 0) { | ||
printf(" with value '%.*s'\n", (int)payload_value.payload.len, payload_value.payload.start); | ||
} | ||
z_query_reply_options_t options = z_query_reply_options_default(); | ||
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); | ||
z_query_reply(&q, z_keyexpr(keyexpr), (const unsigned char *)value, strlen(value), &options); | ||
z_drop(z_move(keystr)); | ||
z_drop(z_move(query)); | ||
} | ||
|
||
z_drop(z_move(channel)); | ||
z_undeclare_queryable(z_move(qable)); | ||
|
||
// Stop read and lease tasks for zenoh-pico | ||
zp_stop_read_task(z_loan(s)); | ||
zp_stop_lease_task(z_loan(s)); | ||
|
||
z_close(z_move(s)); | ||
|
||
return 0; | ||
} | ||
#else | ||
int main(void) { | ||
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_QUERYABLE 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
Oops, something went wrong.