From 94acae874a088d6877e1552f3bc5d794dbef2ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20H=C3=A9cart?= Date: Mon, 28 Sep 2020 14:36:59 +0200 Subject: [PATCH] Remove zenoh-ffi (moved to eclipse-zenoh/zenoh-c) --- Cargo.toml | 1 - zenoh-ffi/Cargo.toml | 58 --- zenoh-ffi/cbindgen.toml | 164 ------- zenoh-ffi/examples/.gitignore | 10 - zenoh-ffi/examples/Makefile | 42 -- zenoh-ffi/examples/zn_eval.c | 61 --- zenoh-ffi/examples/zn_info.c | 49 -- zenoh-ffi/examples/zn_pub.c | 60 --- zenoh-ffi/examples/zn_pub_thr.c | 45 -- zenoh-ffi/examples/zn_pull.c | 58 --- zenoh-ffi/examples/zn_query.c | 50 -- zenoh-ffi/examples/zn_scout.c | 74 --- zenoh-ffi/examples/zn_sub.c | 56 --- zenoh-ffi/examples/zn_sub_thr.c | 75 --- zenoh-ffi/examples/zn_write.c | 44 -- zenoh-ffi/gencbind.sh | 17 - zenoh-ffi/include/zenoh-ffi.h | 412 ---------------- zenoh-ffi/include/zenoh-types.h | 6 - zenoh-ffi/src/lib.rs | 804 -------------------------------- 19 files changed, 2086 deletions(-) delete mode 100644 zenoh-ffi/Cargo.toml delete mode 100644 zenoh-ffi/cbindgen.toml delete mode 100644 zenoh-ffi/examples/.gitignore delete mode 100644 zenoh-ffi/examples/Makefile delete mode 100644 zenoh-ffi/examples/zn_eval.c delete mode 100644 zenoh-ffi/examples/zn_info.c delete mode 100644 zenoh-ffi/examples/zn_pub.c delete mode 100644 zenoh-ffi/examples/zn_pub_thr.c delete mode 100644 zenoh-ffi/examples/zn_pull.c delete mode 100644 zenoh-ffi/examples/zn_query.c delete mode 100644 zenoh-ffi/examples/zn_scout.c delete mode 100644 zenoh-ffi/examples/zn_sub.c delete mode 100644 zenoh-ffi/examples/zn_sub_thr.c delete mode 100644 zenoh-ffi/examples/zn_write.c delete mode 100755 zenoh-ffi/gencbind.sh delete mode 100644 zenoh-ffi/include/zenoh-ffi.h delete mode 100644 zenoh-ffi/include/zenoh-types.h delete mode 100644 zenoh-ffi/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 5083c8e0f..62aa4a725 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ members = [ "zenoh", - "zenoh-ffi", "zenoh-protocol", "zenoh-router", "zenoh-util", diff --git a/zenoh-ffi/Cargo.toml b/zenoh-ffi/Cargo.toml deleted file mode 100644 index a945eb9c5..000000000 --- a/zenoh-ffi/Cargo.toml +++ /dev/null @@ -1,58 +0,0 @@ -# -# Copyright (c) 2017, 2020 ADLINK Technology Inc. -# -# 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: -# ADLINK zenoh team, -# -[package] -name = "zenoh-ffi" -version = "0.5.0-beta.2" -repository = "https://github.com/eclipse-zenoh/zenoh/tree/rust-master" -homepage = "http://zenoh.io" -authors = ["kydos ", - "Julien Enoch ", - "Olivier Hécart ", - "Luca Cominardi "] -edition = "2018" -license = " EPL-2.0 OR Apache-2.0" -categories = ["network-programming"] -description = "Zenoh: Zero Overhead Pub/sub, Store/Query and Compute." - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[badges] -travis-ci = { repository = "...", branch = "rust-master" } -maintenance = { status = "actively-developed" } - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - - -[dependencies] -zenoh = { version = "0.5.0-beta.2", path = "../zenoh" } -zenoh-protocol = { version = "0.5.0-beta.2", path = "../zenoh-protocol" } -zenoh-util = { version = "0.5.0-beta.2", path = "../zenoh-util" } -futures = "0.3.5" -async-trait = "0.1.38" -spin = "0.5.2" -rand = "0.7.3" -cbindgen = "0.14.4" -libc = "0.2.76" -clap = "2.33.3" -env_logger = "0.7.1" - -[dependencies.smol] -version = "0.3.3" - -[dependencies.async-std] -version = "=1.6.4" -features = ["unstable"] - -[lib] -crate-type = ["cdylib"] diff --git a/zenoh-ffi/cbindgen.toml b/zenoh-ffi/cbindgen.toml deleted file mode 100644 index aa3f9bc03..000000000 --- a/zenoh-ffi/cbindgen.toml +++ /dev/null @@ -1,164 +0,0 @@ -# -# Copyright (c) 2017, 2020 ADLINK Technology Inc. -# -# 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: -# ADLINK zenoh team, -# - -# This is a template cbindgen.toml file with all of the default values. -# Some values are commented out because their absence is the real default. -# -# See https://github.com/eqrion/cbindgen/blob/master/docs.md#cbindgentoml -# for detailed documentation of every option here. - - - -language = "C" - - - -############## Options for Wrapping the Contents of the Header ################# - -# header = "/* Text to put at the beginning of the generated file. Probably a license. */" -# trailer = "/* Text to put at the end of the generated file */" -include_guard = "ZENOH_NET_FFI_" -# pragma_once = true -# autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */" -include_version = false -# namespace = "my_namespace" -namespaces = [] -using_namespaces = [] -sys_includes = [] -includes = ["zenoh-types.h"] -no_includes = false -after_includes = "" - - - - -############################ Code Style Options ################################ - -braces = "SameLine" -line_length = 100 -tab_width = 2 -documentation_style = "auto" - - - - - -############################# Codegen Options ################################## - -style = "both" - - - -[defines] -# "target_os = freebsd" = "DEFINE_FREEBSD" -# "feature = serde" = "DEFINE_SERDE" - - - -[export] -include = [] -exclude = [] -# prefix = "CAPI_" -item_types = [] -renaming_overrides_prefixing = false - - - -[export.rename] - - - -[export.body] - - - - -[fn] -rename_args = "None" -# must_use = "MUST_USE_FUNC" -# prefix = "START_FUNC" -# postfix = "END_FUNC" -args = "auto" -sort_by = "Name" - - - - -[struct] -rename_fields = "None" -# must_use = "MUST_USE_STRUCT" -derive_constructor = false -derive_eq = false -derive_neq = false -derive_lt = false -derive_lte = false -derive_gt = false -derive_gte = false - - - - -[enum] -rename_variants = "None" -# must_use = "MUST_USE_ENUM" -add_sentinel = false -prefix_with_name = true -derive_helper_methods = false -derive_const_casts = false -derive_mut_casts = false -# cast_assert_name = "ASSERT" -derive_tagged_enum_destructor = false -derive_tagged_enum_copy_constructor = false -enum_class = true -private_default_tagged_enum_constructor = false - - - - -[const] -allow_static_const = true -allow_constexpr = false - - - - -[macro_expansion] -bitflags = false - - - - - - -############## Options for How Your Rust library Should Be Parsed ############## - -[parse] -parse_deps = false -# include = [] -exclude = [] -clean = false -extra_bindings = [] - - - -[parse.expand] -crates = [] -all_features = false -default_features = true -features = [] - - - - - diff --git a/zenoh-ffi/examples/.gitignore b/zenoh-ffi/examples/.gitignore deleted file mode 100644 index e80629f3d..000000000 --- a/zenoh-ffi/examples/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ -zn_sub -zn_pub -zn_write -zn_query -zn_eval -zn_pull -zn_info -zn_scout -zn_sub_thr -zn_pub_thr \ No newline at end of file diff --git a/zenoh-ffi/examples/Makefile b/zenoh-ffi/examples/Makefile deleted file mode 100644 index e76adab2c..000000000 --- a/zenoh-ffi/examples/Makefile +++ /dev/null @@ -1,42 +0,0 @@ -# -# Copyright (c) 2017, 2020 ADLINK Technology Inc. -# -# 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: -# ADLINK zenoh team, -# - -debug: - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_sub.c -o zn_sub - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_pub.c -o zn_pub - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_write.c -o zn_write - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_query.c -o zn_query - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_eval.c -o zn_eval - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_pull.c -o zn_pull - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_info.c -o zn_info - clang -I ../include -L ../../target/debug/ -lzenoh_ffi zn_scout.c -o zn_scout - -release: - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_sub_thr.c -o zn_sub_thr - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_pub_thr.c -o zn_pub_thr - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_sub.c -o zn_sub - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_pub.c -o zn_pub - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_write.c -o zn_write - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_query.c -o zn_query - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_eval.c -o zn_eval - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_pull.c -o zn_pull - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_info.c -o zn_info - clang -O3 -I ../include -L ../../target/release/ -lzenoh_ffi zn_scout.c -o zn_scout - -all: - make debug - make release - -clean: - $(RM) zn_sub zn_pub zn_write zn_query zn_eval zn_pull zn_info zn_scout zn_sub_thr zn_pub_thr \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_eval.c b/zenoh-ffi/examples/zn_eval.c deleted file mode 100644 index 18fdfa55e..000000000 --- a/zenoh-ffi/examples/zn_eval.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include "zenoh-ffi.h" -#include -#include -#include -#include - -char *uri = "/demo/example/zenoh-c-eval"; -char *value = "Eval from C!"; - -void query_handler(ZNQuery *query) { - const zn_string *res = zn_query_res_name(query); - const zn_string *pred = zn_query_predicate(query); - printf(">> [Query handler] Handling '%.*s?%.*s'\n", res->len, res->val, pred->len, pred->val); - zn_send_reply(query, uri, (const unsigned char *)value, strlen(value)); -} - -int main(int argc, char** argv) { - if (argc > 1) { - uri = argv[1]; - } - char *locator = 0; - if (argc > 2) { - locator = argv[2]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - printf("Declaring Queryable on '%s'...\n", uri); - ZNQueryable *qable = zn_declare_queryable(s, uri, EVAL, query_handler); - if (qable == 0) { - printf("Unable to declare queryable.\n"); - exit(-1); - } - - char c = 0; - while (c != 'q') { - c = fgetc(stdin); - } - - zn_undeclare_queryable(qable); - zn_close(s); - return 0; -} diff --git a/zenoh-ffi/examples/zn_info.c b/zenoh-ffi/examples/zn_info.c deleted file mode 100644 index 5a3e8376e..000000000 --- a/zenoh-ffi/examples/zn_info.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include "zenoh-ffi.h" -#include -#include -#include -#include - -int main(int argc, char** argv) { - char *locator = 0; - if (argc > 1) { - locator = argv[1]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - ZNProperties *ps = zn_info(s); - int n = zn_properties_len(ps); - int id; - - for (int i = 0; i < n; ++i) { - id = zn_property_id(ps, i); - const zn_bytes *bs = zn_property_value(ps, i); - printf(" %d : ", id); - for (int j = 0; j < bs->len; j++) { - printf("%02X", (int)bs->val[j]); - } - printf("\n"); - } - - zn_properties_free(ps); - zn_close(s); -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_pub.c b/zenoh-ffi/examples/zn_pub.c deleted file mode 100644 index a7f2c07d8..000000000 --- a/zenoh-ffi/examples/zn_pub.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include -#include -#include -#include "zenoh-ffi.h" - -int main(int argc, char **argv) { - char *uri = "/demo/example/zenoh-c-pub"; - if (argc > 1) { - uri = argv[1]; - } - char *value = "Pub from C!"; - if (argc > 2) { - value = argv[2]; - } - char *locator = 0; - if (argc > 3) { - locator = argv[3]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - // long rid = zn_declare_resource(s, uri); - - // printf("Declaring Publisher on '%s'...\n", uri); - // ZNPublisher *pub = zn_declare_publisher(s, uri); - // if (sub == 0) { - // printf("Unable to declare publisher.\n"); - // exit(-1); - // } - - char buf[256]; - for(int idx = 0; 1; ++idx) { - sleep(1); - sprintf(buf, "[%4d] %s", idx, value); - printf("Writing Data ('%s': '%s')...\n", uri, buf); - zn_write(s, uri, buf, strlen(buf)); - } - - // zn_undeclare_publisher(pub); - zn_close(s); - return 0; -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_pub_thr.c b/zenoh-ffi/examples/zn_pub_thr.c deleted file mode 100644 index 2d1473680..000000000 --- a/zenoh-ffi/examples/zn_pub_thr.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include "zenoh-ffi.h" -#include -#include -#include -#include - -int main(int argc, char** argv) { - char *locator = 0; - if (argc < 2) { - printf("USAGE:\n\tzn_pub_thr []\n\n"); - exit(-1); - } - size_t len = atoi(argv[1]); - printf("Running throughput test for payload of %zu bytes.\n", len); - if (argc > 2) { - locator = argv[2]; - } - - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - char *data = (char*) malloc(len); - memset(data, 1, len); - - size_t rid = zn_declare_resource(s, "/test/thr"); - while (1) { - zn_write_wrid(s, rid, data, len); - } -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_pull.c b/zenoh-ffi/examples/zn_pull.c deleted file mode 100644 index 23d9d5090..000000000 --- a/zenoh-ffi/examples/zn_pull.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include -#include -#include "zenoh-ffi.h" - -void data_handler(const zn_sample *sample) { - printf(">> [Subscription listener] Received (%.*s, %.*s)\n", - sample->key.len, sample->key.val, - sample->value.len, sample->value.val); -} - -int main(int argc, char **argv) { - char *uri = "/demo/example/**"; - if (argc > 1) { - uri = argv[1]; - } - char *locator = 0; - if (argc > 2) { - locator = argv[2]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - printf("Declaring Subscriber on '%s'...\n", uri); - ZNSubscriber *sub = zn_declare_subscriber(s, uri, zn_subinfo_pull(), data_handler); - if (sub == 0) { - printf("Unable to declare subscriber.\n"); - exit(-1); - } - - printf("Press to pull data...\n"); - char c = 0; - while (c != 'q') { - c = fgetc(stdin); - zn_pull(sub); - } - - zn_undeclare_subscriber(sub); - zn_close(s); - return 0; -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_query.c b/zenoh-ffi/examples/zn_query.c deleted file mode 100644 index 9613cd205..000000000 --- a/zenoh-ffi/examples/zn_query.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include "zenoh-ffi.h" -#include -#include -#include -#include - -void reply_handler(const zn_source_info *info, const zn_sample *sample) { - printf(">> [Reply handler] received (%.*s, %.*s)\n", - sample->key.len, sample->key.val, - sample->value.len, sample->value.val); -} - -int main(int argc, char** argv) { - char *uri = "/demo/example/**"; - if (argc > 1) { - uri = argv[1]; - } - char *locator = 0; - if (argc > 2) { - locator = argv[2]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - printf("Sending Query '%s'...\n", uri); - zn_query(s, uri, "", zn_query_target_default(), zn_query_consolidation_default(), reply_handler); - - sleep(1); - - zn_close(s); - return 0; -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_scout.c b/zenoh-ffi/examples/zn_scout.c deleted file mode 100644 index 8b6386929..000000000 --- a/zenoh-ffi/examples/zn_scout.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include -#include -#include "zenoh-ffi.h" - -void fprintpid(FILE *stream, const unsigned char *peerid, int len) { - if (peerid == NULL) { - fprintf(stream, "None"); - } else { - fprintf(stream, "Some("); - for (int i = 0; i < len; i++) { - fprintf(stream, "%02X", (int)peerid[i]); - } - fprintf(stream, ")"); - } -} - -void fprintwhatami(FILE *stream, unsigned int whatami) { - if (whatami == ROUTER) { fprintf(stream, "\"Router\""); } - else if (whatami == PEER) { fprintf(stream, "\"Peer\""); } - else { fprintf(stream, "\"Other\""); } -} - -void fprintlocators(FILE *stream, ZNLocators *locs) { - fprintf(stream, "["); - for (int i = 0; i < zn_scout_locators_len(locs); i++) { - fprintf(stream, "\""); - fprintf(stream, "%s", zn_scout_locator_get(locs, i)); - fprintf(stream, "\""); - if (i < zn_scout_locators_len(locs) - 1) { - fprintf(stream, ", "); - } - } - fprintf(stream, "]"); -} - -void fprinthello(FILE *stream, ZNScout *scout, unsigned int idx) { - fprintf(stream, "Hello { pid: "); - fprintpid(stream, zn_scout_peerid(scout, idx), zn_scout_peerid_len(scout, idx)); - fprintf(stream, ", whatami: "); - fprintwhatami(stream, zn_scout_whatami(scout, idx)); - fprintf(stream, ", locators: "); - ZNLocators *locs = zn_scout_locators(scout, idx); - fprintlocators(stream, locs); - zn_scout_locators_free(locs); - fprintf(stream, " }"); -} - -int main(int argc, char** argv) { - printf("Scouting...\n"); - ZNScout *scout = zn_scout(ROUTER | PEER, "auto", 1000); - if (zn_scout_len(scout) > 0) { - for (unsigned int i = 0; i < zn_scout_len(scout); ++i) { - fprinthello(stdout, scout, i); - fprintf(stdout, "\n"); - } - } else { - printf("Did not find any zenoh process.\n"); - } - zn_scout_free(scout); - return 0; -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_sub.c b/zenoh-ffi/examples/zn_sub.c deleted file mode 100644 index a8bdd3358..000000000 --- a/zenoh-ffi/examples/zn_sub.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include -#include -#include "zenoh-ffi.h" - -void data_handler(const zn_sample *sample) { - printf(">> [Subscription listener] Received (%.*s, %.*s)\n", - sample->key.len, sample->key.val, - sample->value.len, sample->value.val); -} - -int main(int argc, char **argv) { - char *uri = "/demo/example/**"; - if (argc > 1) { - uri = argv[1]; - } - char *locator = 0; - if (argc > 2) { - locator = argv[2]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - printf("Declaring Subscriber on '%s'...\n", uri); - ZNSubscriber *sub = zn_declare_subscriber(s, uri, zn_subinfo_default(), data_handler); - if (sub == 0) { - printf("Unable to declare subscriber.\n"); - exit(-1); - } - - char c = 0; - while (c != 'q') { - c = fgetc(stdin); - } - - zn_undeclare_subscriber(sub); - zn_close(s); - return 0; -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_sub_thr.c b/zenoh-ffi/examples/zn_sub_thr.c deleted file mode 100644 index 2716d0751..000000000 --- a/zenoh-ffi/examples/zn_sub_thr.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include -#include -#include -#include "zenoh-ffi.h" - -#define N 100000 - -volatile unsigned long long int count = 0; -volatile struct timeval start; -volatile struct timeval stop; - -void print_stats(volatile struct timeval *start, volatile struct timeval *stop) { - double t0 = start->tv_sec + ((double)start->tv_usec / 1000000.0); - double t1 = stop->tv_sec + ((double)stop->tv_usec / 1000000.0); - double thpt = N / (t1 - t0); - printf("%f msgs/sec\n", thpt); -} - -void data_handler(const zn_sample *sample) { - struct timeval tv; - if (count == 0) { - gettimeofday(&tv, 0); - start = tv; - count++; - } else if (count < N) { - count++; - } else { - gettimeofday(&tv, 0); - stop = tv; - print_stats(&start, &stop); - count = 0; - } -} - -int main(int argc, char **argv) { - char *locator = 0; - if (argc > 1) { - locator = argv[1]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - ZNSubscriber *sub = zn_declare_subscriber(s, "/test/thr", zn_subinfo_default(), data_handler); - if (sub == 0) { - printf("Unable to declare subscriber.\n"); - exit(-1); - } - - char c = 0; - while (c != 'q') { - c = fgetc(stdin); - } - - zn_undeclare_subscriber(sub); - zn_close(s); - return 0; -} \ No newline at end of file diff --git a/zenoh-ffi/examples/zn_write.c b/zenoh-ffi/examples/zn_write.c deleted file mode 100644 index e5c9063b1..000000000 --- a/zenoh-ffi/examples/zn_write.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2017, 2020 ADLINK Technology Inc. - * - * 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: - * ADLINK zenoh team, - */ -#include -#include -#include -#include "zenoh-ffi.h" - -int main(int argc, char **argv) { - char *uri = "/demo/example/zenoh-c-write"; - if (argc > 1) { - uri = argv[1]; - } - char *value = "Write from C!"; - if (argc > 2) { - value = argv[2]; - } - char *locator = 0; - if (argc > 3) { - locator = argv[3]; - } - - printf("Openning session...\n"); - ZNSession *s = zn_open(PEER, locator, 0); - if (s == 0) { - printf("Unable to open session!\n"); - exit(-1); - } - - zn_write(s, uri, value, strlen(value)); - - zn_close(s); - return 0; -} \ No newline at end of file diff --git a/zenoh-ffi/gencbind.sh b/zenoh-ffi/gencbind.sh deleted file mode 100755 index fe843cb32..000000000 --- a/zenoh-ffi/gencbind.sh +++ /dev/null @@ -1,17 +0,0 @@ -# -# Copyright (c) 2017, 2020 ADLINK Technology Inc. -# -# 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: -# ADLINK zenoh team, -# - -#!/bin/bash - -cbindgen --config cbindgen.toml --crate zenoh-ffi --output ./include/zenoh-ffi.h \ No newline at end of file diff --git a/zenoh-ffi/include/zenoh-ffi.h b/zenoh-ffi/include/zenoh-ffi.h deleted file mode 100644 index 155dcc3dc..000000000 --- a/zenoh-ffi/include/zenoh-ffi.h +++ /dev/null @@ -1,412 +0,0 @@ -#ifndef ZENOH_NET_FFI_ -#define ZENOH_NET_FFI_ - -#include -#include -#include -#include -#include "zenoh-types.h" - - -typedef struct ZNLocators ZNLocators; - -typedef struct ZNProperties ZNProperties; - -typedef struct ZNQuery ZNQuery; - -typedef struct ZNQueryConsolidation ZNQueryConsolidation; - -typedef struct ZNQueryTarget ZNQueryTarget; - -typedef struct ZNQueryable ZNQueryable; - -typedef struct ZNScout ZNScout; - -typedef struct ZNSession ZNSession; - -typedef struct ZNSubInfo ZNSubInfo; - -typedef struct ZNSubscriber ZNSubscriber; - -typedef struct zn_string { - const char *val; - unsigned int len; -} zn_string; - -typedef struct zn_bytes { - const unsigned char *val; - unsigned int len; -} zn_bytes; - -typedef struct zn_sample { - zn_string key; - zn_bytes value; -} zn_sample; - -typedef struct zn_source_info { - unsigned int kind; - zn_bytes id; -} zn_source_info; - -extern const unsigned int ALL_KINDS; - -extern const unsigned int BROKER; - -extern const unsigned int CLIENT; - -extern const unsigned int EVAL; - -extern const unsigned int PEER; - -extern const unsigned int ROUTER; - -extern const unsigned int STORAGE; - -extern const unsigned int ZN_INFO_PEER_PID_KEY; - -extern const unsigned int ZN_INFO_PID_KEY; - -extern const unsigned int ZN_INFO_ROUTER_PID_KEY; - -/** - * Close a zenoh session - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_close(ZNSession *session); - -/** - * Notifies the zenoh runtime that there won't be any more replies sent for this - * query. - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_close_query(ZNQuery *query); - -/** - * Declares a zenoh queryable entity - * - * Returns the queryable entity or null if the creation was unsuccessful. - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -ZNQueryable *zn_declare_queryable(ZNSession *session, - const char *r_name, - unsigned int kind, - void (*callback)(ZNQuery*)); - -/** - * Declare a zenoh resource - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -unsigned long zn_declare_resource(ZNSession *session, const char *r_name); - -/** - * Declare a zenoh resource with a suffix - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -unsigned long zn_declare_resource_ws(ZNSession *session, unsigned long rid, const char *suffix); - -/** - * Declares a zenoh subscriber - * - * Returns the created subscriber or null if the declaration failed. - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -ZNSubscriber *zn_declare_subscriber(ZNSession *session, - const char *r_name, - ZNSubInfo *sub_info, - void (*callback)(const zn_sample*)); - -/** - * Return information on currently open session along with the the kind of entity for which the - * session has been established. - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -ZNProperties *zn_info(ZNSession *session); - -/** - * Initialise the zenoh runtime logger - * - */ -void zn_init_logger(void); - -/** - * Open a zenoh session - * - * Returns the created session or null if the creation did not succeed - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -ZNSession *zn_open(int mode, const char *locator, const ZNProperties *_ps); - -/** - * Add a property - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -ZNProperties *zn_properties_add(ZNProperties *ps, unsigned long id, const char *value); - -/** - * Add a property - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_properties_free(ZNProperties *ps); - -/** - * Get the properties length - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -unsigned int zn_properties_len(ZNProperties *ps); - -ZNProperties *zn_properties_make(void); - -/** - * Get the properties n-th property ID - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -unsigned int zn_property_id(ZNProperties *ps, unsigned int n); - -/** - * Get the properties n-th property value - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -const zn_bytes *zn_property_value(ZNProperties *ps, unsigned int n); - -/** - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_pull(ZNSubscriber *sub); - -/** - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_query(ZNSession *session, - const char *key_expr, - const char *predicate, - ZNQueryTarget *target, - ZNQueryConsolidation *consolidation, - void (*callback)(const zn_source_info*, const zn_sample*)); - -ZNQueryConsolidation *zn_query_consolidation_default(void); - -ZNQueryConsolidation *zn_query_consolidation_incremental(void); - -ZNQueryConsolidation *zn_query_consolidation_last_hop(void); - -ZNQueryConsolidation *zn_query_consolidation_none(void); - -/** - * Return the predicate for this query - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -const zn_string *zn_query_predicate(ZNQuery *query); - -/** - * Return the resource name for this query - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -const zn_string *zn_query_res_name(ZNQuery *query); - -ZNQueryTarget *zn_query_target_default(void); - -/** - * The scout mask allows to specify what to scout for. - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -ZNScout *zn_scout(unsigned int what, const char *iface, unsigned long scout_period); - -/** - * Frees the ZNScout by releasing its associated memory. - * - * # Safety - * The main reason for this function to be unsafe is that it does of a pointer into a box. - */ -void zn_scout_free(ZNScout *s); - -/** - * Get the number of entities scouted and available as part of - * the ZNScout - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -unsigned int zn_scout_len(ZNScout *si); - -/** - * Get the locator at the given index. - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -const char *zn_scout_locator_get(ZNLocators *ls, unsigned int idx); - -/** - * Get the locators for the scouted. - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -ZNLocators *zn_scout_locators(ZNScout *si, unsigned int idx); - -/** - * Frees the locators - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -void zn_scout_locators_free(ZNLocators *ls); - -/** - * Get the number of locators for the scouted entity. - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -unsigned int zn_scout_locators_len(ZNLocators *ls); - -/** - * Get the peer-id for the scouted entity at the given index - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -const unsigned char *zn_scout_peerid(ZNScout *si, unsigned int idx); - -/** - * Get the length of the peer-id for the scouted entity at the given index - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -unsigned int zn_scout_peerid_len(ZNScout *si, unsigned int idx); - -/** - * Get the whatami for the scouted entity at the given index - * - * # Safety - * The main reason for this function to be unsafe is that it dereferences a pointer. - * - */ -unsigned int zn_scout_whatami(ZNScout *si, unsigned int idx); - -/** - * Sends a reply to a query. - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_send_reply(ZNQuery *query, const char *key, const unsigned char *payload, unsigned int len); - -/** - * Create the default subscriber info. - * - * This describes a reliable push subscriber without any negotiated - * schedule. Starting from this default variants can be created. - */ -ZNSubInfo *zn_subinfo_default(void); - -/** - * Create a subscriber info for a pull subscriber - * - * This describes a reliable pull subscriber without any negotiated - * schedule. - */ -ZNSubInfo *zn_subinfo_pull(void); - -/** - * Un-declares a zenoh queryable - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_undeclare_queryable(ZNQueryable *sub); - -/** - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -void zn_undeclare_subscriber(ZNSubscriber *sub); - -/** - * Writes a named resource. - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -int zn_write(ZNSession *session, const char *r_name, const char *payload, unsigned int len); - -/** - * Writes a named resource using a resource id. This is the most wire efficient way of writing in zenoh. - * - * # Safety - * The main reason for this function to be unsafe is that it does casting of a pointer into a box. - * - */ -int zn_write_wrid(ZNSession *session, - unsigned long r_id, - const char *payload, - unsigned int len); - -#endif /* ZENOH_NET_FFI_ */ diff --git a/zenoh-ffi/include/zenoh-types.h b/zenoh-ffi/include/zenoh-types.h deleted file mode 100644 index 2ddab0fcb..000000000 --- a/zenoh-ffi/include/zenoh-types.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef ZENOH_TYPES_H_ -#define ZENOH_TYPES_H_ - -// Included by default by zenoh-ffh.h - -#endif /* ZENOH_TYPES_H_ */ \ No newline at end of file diff --git a/zenoh-ffi/src/lib.rs b/zenoh-ffi/src/lib.rs deleted file mode 100644 index 3057303eb..000000000 --- a/zenoh-ffi/src/lib.rs +++ /dev/null @@ -1,804 +0,0 @@ -// -// Copyright (c) 2017, 2020 ADLINK Technology Inc. -// -// 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: -// ADLINK zenoh team, -// -#![recursion_limit = "256"] - -use async_std::prelude::FutureExt; -use async_std::sync::{channel, Arc, Sender}; -use async_std::task; -use futures::prelude::*; -use futures::select; -use libc::{c_char, c_int, c_uchar, c_uint, c_ulong}; -use std::convert::TryFrom; -use std::ffi::{CStr, CString}; -use std::slice; -use zenoh::net::Config; -use zenoh::net::*; -use zenoh_protocol::core::ZInt; -use zenoh_util::to_zint; - -#[no_mangle] -pub static ROUTER: c_uint = whatami::ROUTER as c_uint; -#[no_mangle] -pub static PEER: c_uint = whatami::PEER as c_uint; -#[no_mangle] -pub static CLIENT: c_uint = whatami::CLIENT as c_uint; - -// Flags used in Queryable declaration and in queries -#[no_mangle] -pub static ALL_KINDS: c_uint = zenoh::net::queryable::ALL_KINDS as c_uint; -#[no_mangle] -pub static STORAGE: c_uint = zenoh::net::queryable::STORAGE as c_uint; -#[no_mangle] -pub static EVAL: c_uint = zenoh::net::queryable::EVAL as c_uint; - -// Properties returned by zn_info() -#[no_mangle] -pub static ZN_INFO_PID_KEY: c_uint = 0x00 as c_uint; -#[no_mangle] -pub static ZN_INFO_PEER_PID_KEY: c_uint = 0x01 as c_uint; -#[no_mangle] -pub static ZN_INFO_ROUTER_PID_KEY: c_uint = 0x02 as c_uint; - -pub struct ZNSession(zenoh::net::Session); - -pub struct ZNProperties(zenoh::net::Properties); - -enum ZnSubOps { - Pull, - Close, -} - -pub struct ZNSubscriber(Option>>); - -pub struct ZNQueryTarget(zenoh::net::QueryTarget); - -pub struct ZNQueryConsolidation(zenoh::net::QueryConsolidation); - -pub struct ZNQueryable(Option>>); - -pub struct ZNQuery(zenoh::net::Query); - -pub struct ZNSubInfo(zenoh::net::SubInfo); - -pub struct ZNScout(std::vec::Vec); - -pub struct ZNLocators(std::vec::Vec); - -#[repr(C)] -pub struct zn_string { - val: *const c_char, - len: c_uint, -} - -#[repr(C)] -pub struct zn_bytes { - val: *const c_uchar, - len: c_uint, -} - -#[repr(C)] -pub struct zn_sample { - key: zn_string, - value: zn_bytes, -} - -#[repr(C)] -pub struct zn_source_info { - kind: c_uint, - id: zn_bytes, -} - -#[no_mangle] -pub extern "C" fn zn_query_target_default() -> *mut ZNQueryTarget { - Box::into_raw(Box::new(ZNQueryTarget(QueryTarget::default()))) -} - -#[no_mangle] -pub extern "C" fn zn_query_consolidation_default() -> *mut ZNQueryConsolidation { - Box::into_raw(Box::new( - ZNQueryConsolidation(QueryConsolidation::default()), - )) -} - -#[no_mangle] -pub extern "C" fn zn_query_consolidation_none() -> *mut ZNQueryConsolidation { - Box::into_raw(Box::new(ZNQueryConsolidation(QueryConsolidation::None))) -} - -#[no_mangle] -pub extern "C" fn zn_query_consolidation_incremental() -> *mut ZNQueryConsolidation { - Box::into_raw(Box::new(ZNQueryConsolidation( - QueryConsolidation::Incremental, - ))) -} - -#[no_mangle] -pub extern "C" fn zn_query_consolidation_last_hop() -> *mut ZNQueryConsolidation { - Box::into_raw(Box::new(ZNQueryConsolidation(QueryConsolidation::LastHop))) -} - -#[no_mangle] -pub extern "C" fn zn_properties_make() -> *mut ZNProperties { - Box::into_raw(Box::new(ZNProperties(zenoh::net::Properties::new()))) -} - -/// Get the properties length -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_properties_len(ps: *mut ZNProperties) -> c_uint { - (*ps).0.len() as c_uint -} - -/// Get the properties n-th property ID -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_property_id(ps: *mut ZNProperties, n: c_uint) -> c_uint { - (*ps).0[n as usize].0 as c_uint -} - -/// Get the properties n-th property value -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_property_value(ps: *mut ZNProperties, n: c_uint) -> *const zn_bytes { - let ptr = (*ps).0[n as usize].1.as_ptr(); - let value = Box::new(zn_bytes { - val: ptr as *const c_uchar, - len: (*ps).0[n as usize].1.len() as c_uint, - }); - Box::into_raw(value) -} - -/// Add a property -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_properties_add( - ps: *mut ZNProperties, - id: c_ulong, - value: *const c_char, -) -> *mut ZNProperties { - let bs = CStr::from_ptr(value).to_bytes(); - (*ps).0.push((to_zint!(id), Vec::from(bs))); - ps -} - -/// Add a property -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_properties_free(ps: *mut ZNProperties) { - let bps = Box::from_raw(ps); - drop(bps); -} - -/// Return the resource name for this query -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_query_res_name(query: *mut ZNQuery) -> *const zn_string { - let rn = zn_string { - val: (*query).0.res_name.as_ptr() as *const c_char, - len: (*query).0.res_name.len() as c_uint, - }; - Box::into_raw(Box::new(rn)) -} - -/// Return the predicate for this query -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_query_predicate(query: *mut ZNQuery) -> *const zn_string { - let pred = zn_string { - val: (*query).0.predicate.as_ptr() as *const c_char, - len: (*query).0.predicate.len() as c_uint, - }; - Box::into_raw(Box::new(pred)) -} - -/// Create the default subscriber info. -/// -/// This describes a reliable push subscriber without any negotiated -/// schedule. Starting from this default variants can be created. -#[no_mangle] -pub extern "C" fn zn_subinfo_default() -> *mut ZNSubInfo { - Box::into_raw(Box::new(ZNSubInfo(SubInfo::default()))) -} - -/// Create a subscriber info for a pull subscriber -/// -/// This describes a reliable pull subscriber without any negotiated -/// schedule. -#[no_mangle] -pub extern "C" fn zn_subinfo_pull() -> *mut ZNSubInfo { - let si = SubInfo { - reliability: Reliability::Reliable, - mode: SubMode::Pull, - period: None, - }; - Box::into_raw(Box::new(ZNSubInfo(si))) -} - -/// Get the number of entities scouted and available as part of -/// the ZNScout -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_len(si: *mut ZNScout) -> c_uint { - (*si).0.len() as c_uint -} - -/// Get the whatami for the scouted entity at the given index -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_whatami(si: *mut ZNScout, idx: c_uint) -> c_uint { - match (*si).0[idx as usize].whatami { - Some(w) => w as c_uint, - None => ROUTER as c_uint, - } -} - -/// Get the peer-id for the scouted entity at the given index -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_peerid(si: *mut ZNScout, idx: c_uint) -> *const c_uchar { - match &(*si).0[idx as usize].pid { - Some(v) => v.as_slice().as_ptr() as *const c_uchar, - None => std::ptr::null(), - } -} - -/// Get the length of the peer-id for the scouted entity at the given index -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_peerid_len(si: *mut ZNScout, idx: c_uint) -> c_uint { - match &(*si).0[idx as usize].pid { - Some(v) => v.as_slice().len() as c_uint, - None => 0, - } -} - -/// Get the locators for the scouted. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_locators(si: *mut ZNScout, idx: c_uint) -> *mut ZNLocators { - let mut vs = vec![]; - match &(*si).0[idx as usize].locators { - Some(ls) => { - for l in ls { - vs.push(CString::new(format!("{}", l)).unwrap()) - } - } - None => (), - } - Box::into_raw(Box::new(ZNLocators(vs))) -} - -/// Get the number of locators for the scouted entity. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_locators_len(ls: *mut ZNLocators) -> c_uint { - (*ls).0.len() as c_uint -} - -/// Get the locator at the given index. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_locator_get(ls: *mut ZNLocators, idx: c_uint) -> *const c_char { - (*ls).0[idx as usize].as_ptr() -} - -/// Frees the locators -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout_locators_free(ls: *mut ZNLocators) { - drop(Box::from_raw(ls)) -} - -/// The scout mask allows to specify what to scout for. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it dereferences a pointer. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_scout( - what: c_uint, - iface: *const c_char, - scout_period: c_ulong, -) -> *mut ZNScout { - let w = what as ZInt; - let i = CStr::from_ptr(iface).to_str().unwrap(); - - let hellos = task::block_on(async move { - let mut hs = std::vec::Vec::::new(); - let mut stream = zenoh::net::scout(w, i).await; - let scout = async { - while let Some(hello) = stream.next().await { - hs.push(hello) - } - }; - let timeout = async_std::task::sleep(std::time::Duration::from_millis(scout_period as u64)); - FutureExt::race(scout, timeout).await; - hs - }); - Box::into_raw(Box::new(ZNScout(hellos))) -} - -/// Frees the ZNScout by releasing its associated memory. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does of a pointer into a box. -#[no_mangle] -pub unsafe extern "C" fn zn_scout_free(s: *mut ZNScout) { - drop(Box::from_raw(s)) -} - -/// Initialise the zenoh runtime logger -/// -#[no_mangle] -pub extern "C" fn zn_init_logger() { - env_logger::init(); -} - -/// Open a zenoh session -/// -/// Returns the created session or null if the creation did not succeed -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_open( - mode: c_int, - locator: *const c_char, - _ps: *const ZNProperties, -) -> *mut ZNSession { - let s = task::block_on(async move { - let c: Config = Default::default(); - let config = if !locator.is_null() { - c.mode(to_zint!(mode)) - .add_peer(CStr::from_ptr(locator).to_str().unwrap()) - } else { - c.mode(to_zint!(mode)) - }; - - open(config, None).await - }); - match s { - Ok(v) => Box::into_raw(Box::new(ZNSession(v))), - Err(_) => std::ptr::null_mut(), - } -} - -/// Return information on currently open session along with the the kind of entity for which the -/// session has been established. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_info(session: *mut ZNSession) -> *mut ZNProperties { - let ps = task::block_on((*session).0.info()); - let bps = Box::new(ZNProperties(ps)); - Box::into_raw(bps) -} -/// Close a zenoh session -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_close(session: *mut ZNSession) { - task::block_on((*Box::from_raw(session)).0.close()).unwrap(); -} - -/// Declare a zenoh resource -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_declare_resource( - session: *mut ZNSession, - r_name: *const c_char, -) -> c_ulong { - if r_name.is_null() { - return 0; - } - let name = CStr::from_ptr(r_name).to_str().unwrap(); - task::block_on( - (*session) - .0 - .declare_resource(&ResKey::RName(name.to_string())), - ) - .unwrap() as c_ulong -} - -/// Declare a zenoh resource with a suffix -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_declare_resource_ws( - session: *mut ZNSession, - rid: c_ulong, - suffix: *const c_char, -) -> c_ulong { - if suffix.is_null() { - return 0; - } - let sfx = CStr::from_ptr(suffix).to_str().unwrap(); - task::block_on( - (*session) - .0 - .declare_resource(&ResKey::RIdWithSuffix(to_zint!(rid), sfx.to_string())), - ) - .unwrap() as c_ulong -} - -/// Writes a named resource. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_write( - session: *mut ZNSession, - r_name: *const c_char, - payload: *const c_char, - len: c_uint, -) -> c_int { - if r_name.is_null() { - return -1; - } - - let name = CStr::from_ptr(r_name).to_str().unwrap(); - let r = ResKey::RName(name.to_string()); - match task::block_on((*session).0.write( - &r, - slice::from_raw_parts(payload as *const u8, len as usize).into(), - )) { - Ok(()) => 0, - _ => 1, - } -} - -/// Writes a named resource using a resource id. This is the most wire efficient way of writing in zenoh. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_write_wrid( - session: *mut ZNSession, - r_id: c_ulong, - payload: *const c_char, - len: c_uint, -) -> c_int { - let r = ResKey::RId(to_zint!(r_id)); - match smol::block_on((*session).0.write( - &r, - slice::from_raw_parts(payload as *const u8, len as usize).into(), - )) { - Ok(()) => 0, - _ => 1, - } -} - -/// Declares a zenoh subscriber -/// -/// Returns the created subscriber or null if the declaration failed. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_declare_subscriber( - session: *mut ZNSession, - r_name: *const c_char, - sub_info: *mut ZNSubInfo, - callback: extern "C" fn(*const zn_sample), -) -> *mut ZNSubscriber { - if session.is_null() || r_name.is_null() { - return std::ptr::null_mut(); - } - - let si = Box::from_raw(sub_info); - let name = CStr::from_ptr(r_name).to_str().unwrap(); - - let (tx, rx) = channel::(8); - let rsub = ZNSubscriber(Some(Arc::new(tx))); - let s = Box::from_raw(session); - let mut sub: Subscriber = task::block_on( - (*session) - .0 - .declare_subscriber(&ResKey::RName(name.to_string()), &si.0), - ) - .unwrap(); - // Note: This is done to ensure that even if the call-back into C - // does any blocking call we do not incour the risk of blocking - // any of the task resolving futures. - task::spawn_blocking(move || { - task::block_on(async move { - let key = zn_string { - val: std::ptr::null(), - len: 0, - }; - let value = zn_bytes { - val: std::ptr::null(), - len: 0, - }; - let mut sample = zn_sample { key, value }; - - loop { - select!( - s = sub.stream().next().fuse() => { - // This is a bit brutal but avoids an allocation and - // a copy that would be otherwise required to add the - // C string terminator. See the test_sub.c to find out how to deal - // with non null terminated strings. - let us = s.unwrap(); - let data = us.payload.to_vec(); - sample.key.val = us.res_name.as_ptr() as *const c_char; - sample.key.len = us.res_name.len() as c_uint; - sample.value.val = data.as_ptr() as *const c_uchar; - sample.value.len = data.len() as c_uint; - callback(&sample) - }, - op = rx.recv().fuse() => { - match op { - Ok(ZnSubOps::Pull) => { - let _ = sub.pull().await; - () - }, - - Ok(ZnSubOps::Close) => { - let _ = sub.undeclare().await; - Box::into_raw(s); - return () - }, - _ => return () - } - } - ) - } - }) - }); - Box::into_raw(Box::new(rsub)) -} - -// Pulls data on a zenoh pull subscriber -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_pull(sub: *mut ZNSubscriber) { - let sub = Box::from_raw(sub); - match *sub { - ZNSubscriber(Some(ref tx)) => smol::block_on(tx.send(ZnSubOps::Pull)), - ZNSubscriber(None) => (), - } - Box::into_raw(sub); -} - -// Un-declares a zenoh subscriber -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_undeclare_subscriber(sub: *mut ZNSubscriber) { - match *Box::from_raw(sub) { - ZNSubscriber(Some(tx)) => smol::block_on(tx.send(ZnSubOps::Close)), - ZNSubscriber(None) => (), - } -} - -// Issues a zenoh query -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_query( - session: *mut ZNSession, - key_expr: *const c_char, - predicate: *const c_char, - target: *mut ZNQueryTarget, - consolidation: *mut ZNQueryConsolidation, - callback: extern "C" fn(*const zn_source_info, *const zn_sample), -) { - let s = Box::from_raw(session); - let ke = CStr::from_ptr(key_expr).to_str().unwrap(); - let p = CStr::from_ptr(predicate).to_str().unwrap(); - let qt = Box::from_raw(target); - let qc = Box::from_raw(consolidation); - - task::spawn_blocking(move || { - task::block_on(async move { - let mut q = s.0.query(&ke.into(), p, qt.0, qc.0).await.unwrap(); - let key = zn_string { - val: std::ptr::null(), - len: 0, - }; - let value = zn_bytes { - val: std::ptr::null(), - len: 0, - }; - let mut sample = zn_sample { key, value }; - let id = zn_bytes { - val: std::ptr::null(), - len: 0, - }; - let mut source_info = zn_source_info { kind: 0, id }; - - while let Some(reply) = q.next().await { - source_info.kind = reply.source_kind as c_uint; - source_info.id.val = reply.replier_id.as_slice().as_ptr() as *const c_uchar; - source_info.id.len = reply.replier_id.as_slice().len() as c_uint; - sample.key.val = reply.data.res_name.as_ptr() as *const c_char; - sample.key.len = reply.data.res_name.len() as c_uint; - let data = reply.data.payload.to_vec(); - sample.value.val = data.as_ptr() as *const c_uchar; - sample.value.len = data.len() as c_uint; - - callback(&source_info, &sample) - } - let _ = Box::into_raw(s); - }) - }); -} - -/// Declares a zenoh queryable entity -/// -/// Returns the queryable entity or null if the creation was unsuccessful. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_declare_queryable( - session: *mut ZNSession, - r_name: *const c_char, - kind: c_uint, - callback: extern "C" fn(*mut ZNQuery), -) -> *mut ZNQueryable { - if session.is_null() || r_name.is_null() { - return std::ptr::null_mut(); - } - - let s = Box::from_raw(session); - let name = CStr::from_ptr(r_name).to_str().unwrap(); - - let (tx, rx) = channel::(1); - let r = ZNQueryable(Some(Arc::new(tx))); - - // Note: This is done to ensure that even if the call-back into C - // does any blocking call we do not incour the risk of blocking - // any of the task resolving futures. - task::spawn_blocking(move || { - task::block_on(async move { - let mut queryable: zenoh::net::Queryable = - s.0.declare_queryable(&ResKey::RName(name.to_string()), kind as ZInt) - .await - .unwrap(); - loop { - select!( - query = queryable.stream().next().fuse() => { - // This is a bit brutal but avoids an allocation and - // a copy that would be otherwise required to add the - // C string terminator. See the test_sub.c to find out how to deal - // with non null terminated strings. - let bquery = Box::new(ZNQuery(query.unwrap())); - let rbquery = Box::into_raw(bquery); - callback(rbquery); - Box::from_raw(rbquery); - }, - _ = rx.recv().fuse() => { - let _ = queryable.undeclare().await; - Box::into_raw(s); - return () - }) - } - }) - }); - Box::into_raw(Box::new(r)) -} - -/// Un-declares a zenoh queryable -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_undeclare_queryable(sub: *mut ZNQueryable) { - match *Box::from_raw(sub) { - ZNQueryable(Some(tx)) => smol::block_on(tx.send(true)), - ZNQueryable(None) => (), - } -} - -/// Sends a reply to a query. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_send_reply( - query: *mut ZNQuery, - key: *const c_char, - payload: *const c_uchar, - len: c_uint, -) { - let name = CStr::from_ptr(key).to_str().unwrap(); - let s = Sample { - res_name: name.to_string(), - payload: slice::from_raw_parts(payload as *const u8, len as usize).into(), - data_info: None, - }; - task::block_on((*query).0.replies_sender.send(s)); -} - -/// Notifies the zenoh runtime that there won't be any more replies sent for this -/// query. -/// -/// # Safety -/// The main reason for this function to be unsafe is that it does casting of a pointer into a box. -/// -#[no_mangle] -pub unsafe extern "C" fn zn_close_query(query: *mut ZNQuery) { - let bq = Box::from_raw(query); - std::mem::drop(bq); -}