diff --git a/Cargo.toml b/Cargo.toml index f2a6d8818..367ff9ed2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,7 +69,7 @@ phf = { version = "0.11.2", features = ["macros"] } [lib] path = "src/lib.rs" -name = "zenohcd" +name = "zenohc" crate-type = ["cdylib", "staticlib"] doctest = false diff --git a/examples/parse_args.h b/examples/parse_args.h index 1b0b52b12..7708ba6b5 100644 --- a/examples/parse_args.h +++ b/examples/parse_args.h @@ -137,7 +137,7 @@ char** parse_pos_args(const int argc, char** argv, const size_t nb_args) { * @param config_key: zenoh configuration key under which the parsed values will be inserted */ void parse_zenoh_json_list_config(int argc, char** argv, const char* opt, const char* config_key, - const z_owned_config_t* config) { + z_owned_config_t* config) { char* buf = (char*)calloc(1, sizeof(char)); const char* value = parse_opt(argc, argv, opt, true); while (value) { @@ -158,7 +158,7 @@ void parse_zenoh_json_list_config(int argc, char** argv, const char* opt, const char* json_list = (char*)malloc(json_list_len); snprintf(json_list, json_list_len, "[%s]", buf); // insert in config - if (zc_config_insert_json(z_loan(*config), config_key, json_list) < 0) { + if (zc_config_insert_json(z_loan_mut(*config), config_key, json_list) < 0) { printf( "Couldn't insert value `%s` in configuration at `%s`\n`%s` is either not a JSON-serialized list of " "strings, or values within the list do not respect expected format for `%s`\n", @@ -183,6 +183,8 @@ void parse_zenoh_common_args(const int argc, char** argv, z_owned_config_t* conf const char* config_file = parse_opt(argc, argv, "c", true); if (config_file) { zc_config_from_file(config, config_file); + } else { + z_config_default(config); } // -m: The Zenoh session mode [default: peer]. const char* mode = parse_opt(argc, argv, "m", true); @@ -190,7 +192,7 @@ void parse_zenoh_common_args(const int argc, char** argv, z_owned_config_t* conf size_t buflen = strlen(mode) + 3; // mode + quotes + nullbyte char* buf = (char*)malloc(buflen); snprintf(buf, buflen, "'%s'", mode); - if (zc_config_insert_json(z_loan(*config), Z_CONFIG_MODE_KEY, buf) < 0) { + if (zc_config_insert_json(z_loan_mut(*config), Z_CONFIG_MODE_KEY, buf) < 0) { printf( "Couldn't insert value `%s` in configuration at `%s`. Value must be one of: 'client', 'peer' or " "'router'\n", @@ -206,7 +208,8 @@ void parse_zenoh_common_args(const int argc, char** argv, z_owned_config_t* conf parse_zenoh_json_list_config(argc, argv, "l", Z_CONFIG_LISTEN_KEY, config); // --no-multicast-scrouting: Disable the multicast-based scouting mechanism. const char* no_multicast_scouting = parse_opt(argc, argv, "no-multicast-scouting", false); - if (no_multicast_scouting && zc_config_insert_json(z_loan(*config), Z_CONFIG_MULTICAST_SCOUTING_KEY, "false") < 0) { + if (no_multicast_scouting && + zc_config_insert_json(z_loan_mut(*config), Z_CONFIG_MULTICAST_SCOUTING_KEY, "false") < 0) { printf("Couldn't disable multicast-scouting.\n"); exit(-1); } diff --git a/examples/z_delete.c b/examples/z_delete.c index d0ea1712c..67c0f083a 100644 --- a/examples/z_delete.c +++ b/examples/z_delete.c @@ -26,7 +26,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); diff --git a/examples/z_get.c b/examples/z_get.c index 6dc8f6342..c26b9cbb9 100644 --- a/examples/z_get.c +++ b/examples/z_get.c @@ -28,7 +28,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); z_view_keyexpr_t keyexpr; diff --git a/examples/z_get_liveliness.c b/examples/z_get_liveliness.c index f4ad98ec8..60e101416 100644 --- a/examples/z_get_liveliness.c +++ b/examples/z_get_liveliness.c @@ -26,7 +26,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); z_view_keyexpr_t keyexpr; diff --git a/examples/z_info.c b/examples/z_info.c index 59fd032b2..be40b5254 100644 --- a/examples/z_info.c +++ b/examples/z_info.c @@ -27,7 +27,6 @@ void print_zid(const z_id_t* id, void* ctx) { int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); parse_args(argc, argv, &config); printf("Opening session...\n"); diff --git a/examples/z_liveliness.c b/examples/z_liveliness.c index 92fe6f159..de0dd95db 100644 --- a/examples/z_liveliness.c +++ b/examples/z_liveliness.c @@ -26,7 +26,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); z_view_keyexpr_t keyexpr; diff --git a/examples/z_non_blocking_get.c b/examples/z_non_blocking_get.c index c8a3ea9a1..1f834d769 100644 --- a/examples/z_non_blocking_get.c +++ b/examples/z_non_blocking_get.c @@ -26,11 +26,10 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); z_view_keyexpr_t keyexpr; - if (z_view_keyexpr_from_str(&keyexpr, args.keyexpr) < 0) { + if (z_view_keyexpr_from_str(&keyexpr, args.selector) < 0) { printf("%s is not a valid key expression", args.selector); exit(-1); } diff --git a/examples/z_ping.c b/examples/z_ping.c index 97162c9c2..436feeb6d 100644 --- a/examples/z_ping.c +++ b/examples/z_ping.c @@ -32,14 +32,8 @@ struct args_t { struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { - struct args_t args = parse_args(argc, argv, &config); - z_owned_config_t config; - if (args.config_path) { - zc_config_from_file(&config, args.config_path); - } else { - z_config_default(&config); - } + struct args_t args = parse_args(argc, argv, &config); z_mutex_init(&mutex); z_condvar_init(&cond); diff --git a/examples/z_pong.c b/examples/z_pong.c index 1e6c4a36e..f58a3fe96 100644 --- a/examples/z_pong.c +++ b/examples/z_pong.c @@ -25,12 +25,6 @@ int main(int argc, char** argv) { z_owned_config_t config; parse_args(argc, argv, &config); - if (args.config_path) { - zc_config_from_file(&config, args.config_path); - } else { - z_config_default(&config); - } - z_owned_session_t session; z_open(&session, z_move(config)); z_view_keyexpr_t ping; diff --git a/examples/z_pub.c b/examples/z_pub.c index 29ec2ccc4..6aa478a30 100644 --- a/examples/z_pub.c +++ b/examples/z_pub.c @@ -27,6 +27,7 @@ struct args_t { }; struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); +#ifdef UNSTABLE void matching_status_handler(const zc_matching_status_t* matching_status, void* arg) { if (matching_status->matching) { printf("Subscriber matched\n"); @@ -38,7 +39,6 @@ void matching_status_handler(const zc_matching_status_t* matching_status, void* int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); @@ -65,7 +65,7 @@ int main(int argc, char** argv) { zc_publisher_matching_listener_declare(&listener, z_loan(pub), z_move(callback)); } #else - if (add_matching_listener) { + if (args.add_matching_listener) { printf("To enable matching listener you must compile Zenoh-c with unstable feature support!\n"); exit(-1); } @@ -86,7 +86,7 @@ int main(int argc, char** argv) { z_publisher_put(z_loan(pub), z_move(payload), &options); } #ifdef UNSTABLE - if (add_matching_listener) { + if (args.add_matching_listener) { zc_publisher_matching_listener_undeclare(z_move(listener)); } #endif diff --git a/examples/z_pub_attachment.c b/examples/z_pub_attachment.c index b177777d4..ed947262b 100644 --- a/examples/z_pub_attachment.c +++ b/examples/z_pub_attachment.c @@ -51,7 +51,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); diff --git a/examples/z_pub_cache.c b/examples/z_pub_cache.c index c2796b6e8..e2de9caf9 100644 --- a/examples/z_pub_cache.c +++ b/examples/z_pub_cache.c @@ -30,7 +30,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); if (zc_config_insert_json(z_loan_mut(config), Z_CONFIG_ADD_TIMESTAMP_KEY, "true") < 0) { diff --git a/examples/z_pub_shm.c b/examples/z_pub_shm.c index 1bc29c167..461816aca 100644 --- a/examples/z_pub_shm.c +++ b/examples/z_pub_shm.c @@ -29,7 +29,6 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); diff --git a/examples/z_pub_thr.c b/examples/z_pub_thr.c index 3b6c13724..ebd1d7f0f 100644 --- a/examples/z_pub_thr.c +++ b/examples/z_pub_thr.c @@ -26,7 +26,6 @@ int main(int argc, char** argv) { char* keyexpr = "test/thr"; z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); uint8_t* value = (uint8_t*)z_malloc(args.size); memset(value, 1, args.size); @@ -50,8 +49,9 @@ int main(int argc, char** argv) { } printf("Press CTRL-C to quit...\n"); + z_owned_bytes_t payload; while (1) { - z_bytes_from_buf(&payload, value, len, NULL, NULL); + z_bytes_from_buf(&payload, value, args.size, NULL, NULL); z_publisher_put(z_loan(pub), z_move(payload), NULL); } diff --git a/examples/z_pull.c b/examples/z_pull.c index 09b9db274..722b67f12 100644 --- a/examples/z_pull.c +++ b/examples/z_pull.c @@ -38,7 +38,6 @@ void handle_sample(const z_loaned_sample_t* sample) { int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); @@ -52,9 +51,9 @@ int main(int argc, char** argv) { z_owned_ring_handler_sample_t handler; z_owned_closure_sample_t closure; - printf("Declaring Subscriber on '%s'...\n", expr); + printf("Declaring Subscriber on '%s'...\n", args.keyexpr); z_view_keyexpr_t ke; - z_view_keyexpr_from_str(&ke, expr); + z_view_keyexpr_from_str(&ke, args.keyexpr); z_owned_subscriber_t sub; if (z_declare_subscriber(&sub, z_loan(s), z_loan(ke), z_move(closure), NULL) < 0) { diff --git a/examples/z_put.c b/examples/z_put.c index 89d24c624..9117be365 100644 --- a/examples/z_put.c +++ b/examples/z_put.c @@ -27,10 +27,8 @@ struct args_t { struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); int main(int argc, char** argv) { - struct args_t args = parse_args(argc, argv, &config); - z_owned_config_t config; - z_config_default(&config); + struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); z_owned_session_t s; diff --git a/examples/z_query_sub.c b/examples/z_query_sub.c index bec0da0ec..e17a48b43 100644 --- a/examples/z_query_sub.c +++ b/examples/z_query_sub.c @@ -25,17 +25,20 @@ struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); const char* kind_to_str(z_sample_kind_t kind); -void data_handler(const z_sample_t* sample, void* arg) { - z_owned_str_t keystr; - z_keyexpr_to_string(&keystr, sample->keyexpr); - printf(">> [Subscriber] Received %s ('%s': '%.*s')\n", kind_to_str(sample->kind), z_loan(keystr), - (int)sample->payload.len, sample->payload.start); - z_drop(z_move(keystr)); +void data_handler(const z_loaned_sample_t* sample, void* arg) { + z_view_string_t key_string; + z_keyexpr_as_view_string(z_sample_keyexpr(sample), &key_string); + z_owned_string_t payload_string; + z_bytes_deserialize_into_string(z_sample_payload(sample), &payload_string); + + printf(">> [Subscriber] Received %s ('%.*s': '%.*s')\n", kind_to_str(z_sample_kind(sample)), + (int)z_string_len(z_loan(key_string)), z_string_data(z_loan(key_string)), + (int)z_string_len(z_loan(payload_string)), z_string_data(z_loan(payload_string))); + z_drop(z_move(payload_string)); } int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); @@ -48,12 +51,13 @@ int main(int argc, char** argv) { z_view_keyexpr_t ke; z_view_keyexpr_from_str(&ke, args.keyexpr); - ze_querying_subscriber_options_t sub_opts = ze_querying_subscriber_options_default(); + ze_querying_subscriber_options_t sub_opts; + ze_querying_subscriber_options_default(&sub_opts); z_owned_closure_sample_t callback; z_closure(&callback, data_handler, NULL, NULL); printf("Declaring querying subscriber on '%s'...\n", args.keyexpr); ze_owned_querying_subscriber_t sub; - if (ze_declare_querying_subscriber(z_loan(s), z_loan(ke), z_move(callback), &sub_opts) < 0) { + if (ze_declare_querying_subscriber(&sub, z_loan(s), z_loan(ke), z_move(callback), &sub_opts) < 0) { printf("Unable to declare querying subscriber.\n"); exit(-1); } diff --git a/examples/z_queryable.c b/examples/z_queryable.c index 62a3459e9..5f5f48b71 100644 --- a/examples/z_queryable.c +++ b/examples/z_queryable.c @@ -21,6 +21,15 @@ #define DEFAULT_VALUE "Queryable from C!" z_view_keyexpr_t ke; +struct args_t { + char *keyexpr; // -k + char *value; // -v +}; + +char *value; + +struct args_t parse_args(int argc, char **argv, z_owned_config_t *config); + void query_handler(const z_loaned_query_t *query, void *context) { z_view_string_t key_string; z_keyexpr_as_view_string(z_query_keyexpr(query), &key_string); @@ -55,7 +64,6 @@ void query_handler(const z_loaned_query_t *query, void *context) { int main(int argc, char **argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); value = args.value; diff --git a/examples/z_queryable_with_channels.c b/examples/z_queryable_with_channels.c index c2d334a07..4e8b6b856 100644 --- a/examples/z_queryable_with_channels.c +++ b/examples/z_queryable_with_channels.c @@ -30,7 +30,6 @@ z_view_keyexpr_t ke; int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); printf("Opening session...\n"); @@ -41,7 +40,7 @@ int main(int argc, char** argv) { } if (z_view_keyexpr_from_str(&ke, args.keyexpr) < 0) { - printf("%s is not a valid key expression", keyexpr); + printf("%s is not a valid key expression", args.keyexpr); exit(-1); } @@ -84,7 +83,7 @@ int main(int argc, char** argv) { z_query_reply_options_default(&options); z_owned_bytes_t reply_payload; - z_bytes_from_static_str(&reply_payload, value); + z_bytes_from_static_str(&reply_payload, args.value); z_query_reply(query, z_loan(ke), z_move(reply_payload), &options); z_drop(z_move(oquery)); } diff --git a/examples/z_sub.c b/examples/z_sub.c index 1834b705a..41dfdeff5 100644 --- a/examples/z_sub.c +++ b/examples/z_sub.c @@ -40,7 +40,6 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) { int main(int argc, char **argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); z_view_keyexpr_t ke; z_view_keyexpr_from_str(&ke, args.keyexpr); diff --git a/examples/z_sub_attachment.c b/examples/z_sub_attachment.c index 3705dc0da..20cef431e 100644 --- a/examples/z_sub_attachment.c +++ b/examples/z_sub_attachment.c @@ -23,6 +23,7 @@ struct args_t { char* keyexpr; // -k }; struct args_t parse_args(int argc, char** argv, z_owned_config_t* config); +const char* kind_to_str(z_sample_kind_t kind); void data_handler(const z_loaned_sample_t* sample, void* arg) { z_view_string_t key_string; @@ -63,7 +64,6 @@ void data_handler(const z_loaned_sample_t* sample, void* arg) { int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); z_view_keyexpr_t ke; z_view_keyexpr_from_str(&ke, args.keyexpr); diff --git a/examples/z_sub_liveliness.c b/examples/z_sub_liveliness.c index 83a83ed34..bb2d4fc0f 100644 --- a/examples/z_sub_liveliness.c +++ b/examples/z_sub_liveliness.c @@ -40,11 +40,10 @@ void data_handler(const z_loaned_sample_t* sample, void* arg) { int main(int argc, char** argv) { z_owned_config_t config; - z_config_default(&config); struct args_t args = parse_args(argc, argv, &config); z_view_keyexpr_t ke; - if (z_view_keyexpr_from_str(&ke, keyexpr) < 0) { - printf("%s is not a valid key expression\n", keyexpr); + if (z_view_keyexpr_from_str(&ke, args.keyexpr) < 0) { + printf("%s is not a valid key expression\n", args.keyexpr); exit(-1); } diff --git a/examples/z_sub_thr.c b/examples/z_sub_thr.c index e380711cf..75367f065 100644 --- a/examples/z_sub_thr.c +++ b/examples/z_sub_thr.c @@ -64,7 +64,6 @@ void drop_stats(void *context) { int main(int argc, char **argv) { z_owned_config_t config; - z_config_default(&config); parse_args(argc, argv, &config); #ifdef SHARED_MEMORY