Skip to content
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

examples' command line arguments aligned with those in rust. Failures in shared memory examples fixed #844

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/parse_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,27 @@ z_query_target_t parse_query_target(const char* arg) {
exit(-1);
}
}

z_priority_t parse_priority(const char* arg) {
int p = atoi(arg);
if (p < Z_PRIORITY_INTERACTIVE_HIGH || p > Z_PRIORITY_BACKGROUND) {
printf("Unsupported priority value [%s]\n", arg);
exit(-1);
}
return (z_priority_t)p;
}

#define _Z_PARSE_ARG(VALUE, ID, FUNC, DEFAULT_VALUE) \
do { \
const char* arg_val = parse_opt(argc, argv, ID, true); \
if (!arg_val) { \
VALUE = DEFAULT_VALUE; \
} else { \
VALUE = FUNC(arg_val); \
} \
} while (0)

#define _Z_CHECK_FLAG(VALUE, ID) \
do { \
VALUE = (parse_opt(argc, argv, ID, false) != NULL); \
} while (0)
31 changes: 9 additions & 22 deletions examples/z_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ int main(int argc, char** argv) {

z_view_keyexpr_t keyexpr;

if (z_view_keyexpr_from_str(&keyexpr, ke) < 0) {
printf("%s is not a valid key expression", ke);
if (z_view_keyexpr_from_substr(&keyexpr, ke, ke_len) < 0) {
printf("%.*s is not a valid key expression", (int)ke_len, ke);
exit(-1);
}

Expand Down Expand Up @@ -121,24 +121,12 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
print_help();
exit(1);
}
const char* selector = parse_opt(argc, argv, "s", true);
if (!selector) {
selector = DEFAULT_SELECTOR;
}
const char* value = parse_opt(argc, argv, "p", true);
if (!value) {
value = DEFAULT_VALUE;
}
const char* timeout_arg = parse_opt(argc, argv, "o", true);
uint64_t timeout_ms = DEFAULT_TIMEOUT_MS;
if (timeout_arg) {
timeout_ms = atoi(timeout_arg);
}
const char* target_arg = parse_opt(argc, argv, "t", true);
z_query_target_t target = z_query_target_default();
if (target_arg) {
target = parse_query_target(target_arg);
}
struct args_t args;
_Z_PARSE_ARG(args.selector, "s", (char*), (char*)DEFAULT_SELECTOR);
_Z_PARSE_ARG(args.value, "p", (char*), (char*)DEFAULT_VALUE);
_Z_PARSE_ARG(args.timeout_ms, "o", atoi, DEFAULT_TIMEOUT_MS);
_Z_PARSE_ARG(args.target, "t", parse_query_target, z_query_target_default());

parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
Expand All @@ -152,6 +140,5 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
exit(-1);
}
free(pos_args);
return (struct args_t){
.selector = (char*)selector, .value = (char*)value, .timeout_ms = timeout_ms, .target = target};
return args;
}
24 changes: 15 additions & 9 deletions examples/z_get_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#include "zenoh.h"

#define DEFAULT_KEYEXPR "group1/**"
#define DEFAULT_TIMEOUT_MS 10000

struct args_t {
char* keyexpr; // -k
char* keyexpr; // -k
uint64_t timeout_ms; // -o
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

Expand All @@ -47,7 +49,10 @@ int main(int argc, char** argv) {
z_owned_fifo_handler_reply_t handler;
z_owned_closure_reply_t closure;
z_fifo_channel_reply_new(&closure, &handler, 16);
z_liveliness_get(z_loan(s), z_loan(keyexpr), z_move(closure), NULL);
z_liveliness_get_options_t opts;
z_liveliness_get_options_default(&opts);
opts.timeout_ms = args.timeout_ms;
z_liveliness_get(z_loan(s), z_loan(keyexpr), z_move(closure), &opts);
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))) {
Expand All @@ -71,8 +76,9 @@ void print_help() {
"\
Usage: z_get_liveliness [OPTIONS]\n\n\
Options:\n\
-k <KEY> (optional, string, default='%s'): The key expression to query\n",
DEFAULT_KEYEXPR);
-k <KEY> (optional, string, default='%s'): The key expression to query\n\
-o <TIMEOUT_MS> (optional, number, default = '%d'): Query timeout in milliseconds\n",
DEFAULT_KEYEXPR, DEFAULT_TIMEOUT_MS);
printf(COMMON_HELP);
printf(
"\
Expand All @@ -84,10 +90,10 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
print_help();
exit(1);
}
const char* keyexpr = parse_opt(argc, argv, "k", true);
if (!keyexpr) {
keyexpr = DEFAULT_KEYEXPR;
}
struct args_t args;
_Z_PARSE_ARG(args.keyexpr, "k", (char*), (char*)DEFAULT_KEYEXPR);
_Z_PARSE_ARG(args.timeout_ms, "o", atoi, DEFAULT_TIMEOUT_MS);

parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
Expand All @@ -101,5 +107,5 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
exit(-1);
}
free(pos_args);
return (struct args_t){.keyexpr = (char*)keyexpr};
return args;
}
115 changes: 80 additions & 35 deletions examples/z_get_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,46 @@
#include <stdio.h>
#include <string.h>

#include "parse_args.h"
#include "zenoh.h"

#define DEFAULT_SELECTOR "demo/example/**"
#define DEFAULT_VALUE NULL
#define DEFAULT_TIMEOUT_MS 10000

struct args_t {
char* selector; // -s
char* value; // -p
z_query_target_t target; // -t
uint64_t timeout_ms; // -o
};
struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

int main(int argc, char** argv) {
char* expr = "demo/example/**";
char* value = NULL;
switch (argc) {
default:
case 3:
value = argv[2];
case 2:
expr = argv[1];
break;
case 1:
value = "Test Value";
break;
zc_init_log_from_env_or("error");
z_owned_config_t config;

struct args_t args = parse_args(argc, argv, &config);
if (!args.value) {
args.value = "Get from Rust SHM!";
}
size_t value_len = value ? strlen(value) : 0;

zc_init_log_from_env_or("error");
const char* ke = args.selector;
size_t ke_len = strlen(ke);
const char* params = strchr(args.selector, '?');
if (params != NULL) {
ke_len = params - ke;
params += 1;
}

size_t value_len = args.value ? strlen(args.value) : 0;

z_view_keyexpr_t keyexpr;
if (z_view_keyexpr_from_str(&keyexpr, expr) < 0) {
printf("%s is not a valid key expression", expr);
if (z_view_keyexpr_from_substr(&keyexpr, ke, ke_len) < 0) {
printf("%.*s is not a valid key expression", (int)ke_len, ke);
exit(-1);
}

z_owned_config_t config;
z_config_default(&config);

if (argc > 3) {
if (zc_config_insert_json5(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, argv[3]) < 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[3], Z_CONFIG_CONNECT_KEY, Z_CONFIG_CONNECT_KEY);
exit(-1);
}
}

printf("Opening session...\n");
z_owned_session_t s;
if (z_open(&s, z_move(config), NULL)) {
Expand All @@ -76,29 +77,30 @@ int main(int argc, char** argv) {
}
// Fill SHM Buffer with data
uint8_t* data = z_shm_mut_data_mut(z_loan_mut(alloc.buf));
memcpy(data, value, value_len);
memcpy(data, args.value, value_len);
// Convert mutable SHM Buffer into immutable one (to be able to make it's ref copies)
z_owned_shm_t shm;
z_shm_from_mut(&shm, z_move(alloc.buf));
const z_loaned_shm_t* loaned_shm = z_loan(shm);

printf("Sending Query '%s'...\n", expr);
printf("Sending Query '%s'...\n", args.selector);
z_owned_fifo_handler_reply_t handler;
z_owned_closure_reply_t closure;
z_fifo_channel_reply_new(&closure, &handler, 16);

z_get_options_t opts;
z_get_options_default(&opts);
opts.target = args.target;
opts.timeout_ms = args.timeout_ms;

z_owned_bytes_t payload;
if (value != NULL) {
if (!z_bytes_from_shm(&payload, z_move(shm))) {
if (args.value != NULL) {
if (z_bytes_from_shm(&payload, z_move(shm)) != Z_OK) {
printf("Unexpected failure during SHM buffer serialization...\n");
return -1;
}
opts.payload = z_move(payload);
}
z_get(z_loan(s), z_loan(keyexpr), "", z_move(closure),
z_get(z_loan(s), z_loan(keyexpr), params, z_move(closure),
&opts); // here, the send is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;

Expand Down Expand Up @@ -129,3 +131,46 @@ int main(int argc, char** argv) {
z_drop(z_move(layout));
return 0;
}

void print_help() {
printf(
"\
Usage: z_get [OPTIONS]\n\n\
Options:\n\
-s <SELECTOR> (optional, string, default='%s'): The selection of resources to query\n\
-p <PAYLOAD> (optional, string): An optional value to put in the query\n\
-t <TARGET> (optional, BEST_MATCHING | ALL | ALL_COMPLETE): Query target\n\
-o <TIMEOUT_MS> (optional, number, default = '%d'): Query timeout in milliseconds\n",
DEFAULT_SELECTOR, DEFAULT_TIMEOUT_MS);
printf(COMMON_HELP);
printf(
"\
-h: print help\n");
}

struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
if (parse_opt(argc, argv, "h", false)) {
print_help();
exit(1);
}
struct args_t args;
_Z_PARSE_ARG(args.selector, "s", (char*), (char*)DEFAULT_SELECTOR);
_Z_PARSE_ARG(args.value, "p", (char*), (char*)DEFAULT_VALUE);
_Z_PARSE_ARG(args.timeout_ms, "o", atoi, DEFAULT_TIMEOUT_MS);
_Z_PARSE_ARG(args.target, "t", parse_query_target, z_query_target_default());

parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
printf("Unknown option %s\n", arg);
exit(-1);
}
char** pos_args = parse_pos_args(argc, argv, 1);
if (!pos_args || pos_args[0]) {
printf("Unexpected positional arguments\n");
free(pos_args);
exit(-1);
}
free(pos_args);
return args;
}
43 changes: 31 additions & 12 deletions examples/z_non_blocking_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
#include "zenoh.h"

#define DEFAULT_SELECTOR "demo/example/**"
#define DEFAULT_VALUE NULL
#define DEFAULT_TIMEOUT_MS 10000

struct args_t {
char* selector; // -s
char* selector; // -s
char* value; // -v
z_query_target_t target; // -t
uint64_t timeout_ms; // -o
};

struct args_t parse_args(int argc, char** argv, z_owned_config_t* config);

int main(int argc, char** argv) {
Expand All @@ -30,9 +36,17 @@ int main(int argc, char** argv) {
z_owned_config_t config;
struct args_t args = parse_args(argc, argv, &config);

const char* ke = args.selector;
size_t ke_len = strlen(ke);
const char* params = strchr(args.selector, '?');
if (params != NULL) {
ke_len = params - ke;
params += 1;
}

z_view_keyexpr_t keyexpr;
if (z_view_keyexpr_from_str(&keyexpr, args.selector) < 0) {
printf("%s is not a valid key expression", args.selector);
if (z_view_keyexpr_from_substr(&keyexpr, ke, ke_len) < 0) {
printf("%.*s is not a valid key expression", (int)ke_len, ke);
exit(-1);
}

Expand All @@ -50,7 +64,7 @@ int main(int argc, char** argv) {
z_owned_fifo_handler_reply_t handler;
z_owned_closure_reply_t closure;
z_fifo_channel_reply_new(&closure, &handler, 16);
z_get(z_loan(s), z_loan(keyexpr), "", z_move(closure),
z_get(z_loan(s), z_loan(keyexpr), params, z_move(closure),
&opts); // here, the closure is moved and will be dropped by zenoh when adequate
z_owned_reply_t reply;
for (z_result_t res = z_try_recv(z_loan(handler), &reply); res != Z_CHANNEL_DISCONNECTED;
Expand Down Expand Up @@ -81,10 +95,13 @@ int main(int argc, char** argv) {
void print_help() {
printf(
"\
Usage: z_non_blocking_get [OPTIONS]\n\n\
Usage: z_get [OPTIONS]\n\n\
Options:\n\
-s <SELECTOR> (optional, string, default='%s'): The selection of resources to query\n",
DEFAULT_SELECTOR);
-s <SELECTOR> (optional, string, default='%s'): The selection of resources to query\n\
-p <PAYLOAD> (optional, string): An optional value to put in the query\n\
-t <TARGET> (optional, BEST_MATCHING | ALL | ALL_COMPLETE): Query target\n\
-o <TIMEOUT_MS> (optional, number, default = '%d'): Query timeout in milliseconds\n",
DEFAULT_SELECTOR, DEFAULT_TIMEOUT_MS);
printf(COMMON_HELP);
printf(
"\
Expand All @@ -96,10 +113,12 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
print_help();
exit(1);
}
const char* selector = parse_opt(argc, argv, "s", true);
if (!selector) {
selector = DEFAULT_SELECTOR;
}
struct args_t args;
_Z_PARSE_ARG(args.selector, "s", (char*), (char*)DEFAULT_SELECTOR);
_Z_PARSE_ARG(args.value, "p", (char*), (char*)DEFAULT_VALUE);
_Z_PARSE_ARG(args.timeout_ms, "o", atoi, DEFAULT_TIMEOUT_MS);
_Z_PARSE_ARG(args.target, "t", parse_query_target, z_query_target_default());

parse_zenoh_common_args(argc, argv, config);
const char* arg = check_unknown_opts(argc, argv);
if (arg) {
Expand All @@ -113,5 +132,5 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config) {
exit(-1);
}
free(pos_args);
return (struct args_t){.selector = (char*)selector};
return args;
}
Loading
Loading