From 29ca3efd9bd109c09653bfcef816fa3cd410d92c Mon Sep 17 00:00:00 2001 From: Enzo Le Van Date: Thu, 28 Nov 2024 18:17:15 +0100 Subject: [PATCH] Update examples Update examples --- Cargo.lock | 3 ++ README.md | 2 +- examples/Cargo.toml | 9 ++++ examples/README.md | 28 ++++------- examples/examples/topic-sender/src/main.rs | 56 ++++++++++++++++++++++ 5 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 examples/examples/topic-sender/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 564a5c39..ada9be6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3337,9 +3337,12 @@ dependencies = [ name = "zenoh-flow-examples" version = "0.6.0-dev" dependencies = [ + "anyhow", "async-std", "async-trait", + "clap", "prost 0.11.9", + "zenoh", "zenoh-flow-nodes", ] diff --git a/README.md b/README.md index 30bc3d33..d5c6231b 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,6 @@ The best way to learn Zenoh-Flow is to go through our [getting started guide](ht ## Examples -We encourage you to look at the examples available in our [examples repository](https://github.com/ZettaScaleLabs/zenoh-flow-examples). +We encourage you to look at the examples available in our [examples folder](./examples). 🚗 If you still want more, we also ported an [Autonomous Driving Pipeline](https://github.com/ZettaScaleLabs/stunt)! diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 10f772c1..23ffa1f8 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -29,6 +29,11 @@ async-trait = { workspace = true } prost = "0.11" zenoh-flow-nodes = { workspace = true } +[dev-dependencies] +zenoh = { workspace = true } +clap = { workspace = true, features = ["derive", "wrap_help"] } +anyhow = { workspace = true } + [[example]] name = "greetings-maker" path = "examples/greetings-maker/src/lib.rs" @@ -38,3 +43,7 @@ crate-type = ["cdylib"] name = "file-writer" path = "examples/file-writer/src/lib.rs" crate-type = ["cdylib"] + +[[example]] +name = "topic-sender" +path = "examples/topic-sender/src/main.rs" diff --git a/examples/README.md b/examples/README.md index e5bbf16d..79f856e1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -14,38 +14,30 @@ Alternatively, we can create a single library of a zenoh-flow node with the foll cargo build --example ``` -### Configure and run the examples +### Getting Started -We first have to update all the occurrences of `{{ BASE_DIR }}` in the YAML descriptors to match our system. +You can find the YAML descriptor for the `getting-started` example in the `flows` folder. In order to run it, you have +to modify the `{{TARGET_DIR}}` placeholder in the YAML descriptor to match your system. You also have to modify the `{{DLL_EXTENSION}}` placeholder to match the extension of the dynamic library generated by the build system of your system. #### Launch the flow ```shell -./target/debug/zfctl launch ~/dev/zenoh-flow/examples/data-flow.yaml +./target/debug/zfctl runtime run examples/flows/getting-started.yaml ``` -If you have enabled the REST plugin of Zenoh -```shell -curl -X PUT -d 'world' http://localhost:8000/zf/getting-started/hello -``` - -For the "period-miss-detector" example: - -```shell -curl -X PUT -d '2340' http://localhost:8000/zf/period-miss-detector -``` #### Show the result: -The Sink node used in both examples creates a text file where the node writes the strings it receives. -We can see the "getting-started" test file with: +The Sink node used in the example creates a text file where the node writes the strings it receives. You can see the file with: ``` tail -f /tmp/greetings.txt ``` -For the "period-miss-detector" example: +In another terminal, you can send a string to the source node with: -``` -tail -f /tmp/period-log.txt +```shell +./target/debug/examples/topic-sender zf/getting-started/hello world ``` +**Note**: This assumes that you built the examples in DEBUG mode. If you built them in RELEASE mode, you should replace `debug` with `release` in the paths above **and** modify +the `{{BUILD}}` placeholder in the YAML descriptor to match the build mode you used. diff --git a/examples/examples/topic-sender/src/main.rs b/examples/examples/topic-sender/src/main.rs new file mode 100644 index 00000000..f48606be --- /dev/null +++ b/examples/examples/topic-sender/src/main.rs @@ -0,0 +1,56 @@ +use std::path::PathBuf; + +use anyhow::anyhow; +use clap::Parser; + +use zenoh::{ + prelude::{r#async::AsyncResolve, *}, + Result, +}; + +#[derive(Parser)] +struct TopicSender { + /// The path to a Zenoh configuration to manage the connection to the Zenoh + /// network. + /// + /// If no configuration is provided, `zfctl` will default to connecting as + /// a peer with multicast scouting enabled. + #[arg(short = 'z', long, verbatim_doc_comment)] + zenoh_configuration: Option, + + /// The key_expression to use for the `put` operation. + #[arg(verbatim_doc_comment)] + key_expression: KeyExpr<'static>, + + /// The payload to use for the `put` operation. + #[arg(verbatim_doc_comment)] + payload: String, +} + +#[async_std::main] +async fn main() -> Result<()> { + let args = TopicSender::parse(); + + let zenoh_config = match args.zenoh_configuration { + Some(path) => zenoh::prelude::Config::from_file(path.clone()).map_err(|e| { + anyhow!( + "Failed to parse the Zenoh configuration from < {} >:\n{e:?}", + path.display() + ) + })?, + None => zenoh::config::peer(), + }; + + let session = zenoh::open(zenoh_config) + .res() + .await + .map_err(|e| anyhow!("Failed to open Zenoh session:\n{:?}", e))?; + + session + .put(&args.key_expression, args.payload) + .res() + .await + .map_err(|e| anyhow!("Failed to put a sample:\n{:?}", e))?; + + Ok(()) +}