Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Hennzau committed Nov 28, 2024
1 parent 659aaca commit 1b0945a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
9 changes: 9 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
28 changes: 10 additions & 18 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,30 @@ Alternatively, we can create a single library of a zenoh-flow node with the foll
cargo build --example <node>
```

### 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.
56 changes: 56 additions & 0 deletions examples/examples/topic-sender/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>,

/// 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(())
}
4 changes: 2 additions & 2 deletions examples/flows/getting-started.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
name: getting-started

vars:
TARGET_DIR: "/path/to/zenoh-flow/target"
TARGET_DIR: "/Users/enzolevan/Offline/zenoh-flow/target"
BUILD: "debug"
DLL_EXTENSION: "so"
DLL_EXTENSION: "dylib"
OUT_FILE: "/tmp/greetings.txt"

sources:
Expand Down

0 comments on commit 1b0945a

Please sign in to comment.