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

Adding Zenoh-Flow Examples Target #167

Merged
merged 31 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a6210e1
Update README.md
PhilipTamb Sep 22, 2023
571287b
zenoh-flow examples first commit
PhilipTamb Sep 25, 2023
22057f4
zenoh-flow examples aggiunta README modifica .yaml
PhilipTamb Sep 25, 2023
b05fdda
modified the flows
PhilipTamb Sep 26, 2023
7910546
other small modification
PhilipTamb Sep 26, 2023
336b3dc
zenoh-flow with configuration: file: name_file
PhilipTamb Sep 26, 2023
d074f68
update readme
PhilipTamb Sep 28, 2023
18bd8aa
update commit
PhilipTamb Sep 28, 2023
b092735
small modification
PhilipTamb Sep 29, 2023
2f9b20d
commit before to delite the path and put the general path
PhilipTamb Sep 29, 2023
55dc750
code with generic path
PhilipTamb Sep 29, 2023
40a5cdd
remove src
PhilipTamb Sep 29, 2023
30b6185
fix conflict
PhilipTamb Sep 29, 2023
1036526
adjustments after first PR
PhilipTamb Oct 3, 2023
4e1ead2
update Cargo.lock
PhilipTamb Oct 4, 2023
7b58c8b
Cargo.lock deleated
PhilipTamb Oct 4, 2023
99d0861
Update the README files
PhilipTamb Oct 4, 2023
8ee9471
README file modification
PhilipTamb Oct 4, 2023
90dbebf
other small README modification
PhilipTamb Oct 4, 2023
a596780
merge with the master branch
PhilipTamb Oct 4, 2023
6fa24f4
remove profiles settings from Cargo.toml
PhilipTamb Oct 4, 2023
cbfa469
typo: single quote character
J-Loudet Oct 5, 2023
90e2e48
doc: rewording
J-Loudet Oct 5, 2023
2967e77
doc: fix typo in curl url
J-Loudet Oct 5, 2023
547d4dc
README update
PhilipTamb Oct 5, 2023
22ab504
untrack Cargo.lock fix
PhilipTamb Oct 5, 2023
dcf8f41
README fix
PhilipTamb Oct 5, 2023
e1af8c2
README fix
PhilipTamb Oct 5, 2023
c2c6b32
delete Cargo.lock
PhilipTamb Oct 5, 2023
204ff55
fix Cargo.lock
PhilipTamb Oct 5, 2023
059e8e7
docs: revert README
PhilipTamb Oct 6, 2023
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"zenoh-flow-derive",
"zenoh-flow-plugin",
"zfctl",
"examples",
]

[workspace.package]
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,4 @@ 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).

🚗 If you still want more, we also ported an [Autonomous Driving Pipeline](https://github.com/ZettaScaleLabs/stunt)!
compile the examples using "cargo build --examples"
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "zenoh-flow-examples"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
homepage.workspace = true
categories.workspace = true
description.workspace = true
license.workspace = true
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = { workspace = true }
async-trait = { workspace = true }
prost = "0.11"
zenoh-flow = { path = "../zenoh-flow/" }

[[example]]
name = "greetings-maker"
path = "examples/greetings-maker/src/lib.rs"
crate-type = ["cdylib"]

[[example]]
name = "file-writer"
path = "examples/file-writer/src/lib.rs"
crate-type = ["cdylib"]

[[example]]
name = "period-miss-detector"
path = "examples/period-miss-detector/src/lib.rs"
crate-type = ["cdylib"]
51 changes: 51 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Zenoh-Flow examples

## How to run

### Build

We can create all the zenoh-flow node libraries used in the examples with the following command:
```bash
cargo build --examples
```

Alternatively, we can create a single library of a zenoh-flow node with the following command:
```bash
cargo build --example <node>
```

### Configure and run the examples

To configure the examples see the [documentation](https://github.com/eclipse-zenoh/zenoh-flow/wiki/Installation-(v0.4.0)#zenoh-plugin).
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved

#### Launch the flow

```shell
./target/debug/zfctl launch ~/dev/zenoh-flow/examples/data-flow.yaml
```

If you have enabled the REST plugin of Zenoh
```shell
curl -X PUT -d '"Hello World!"' http://localhost:8000/zf/gettig-started/hello
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
```

For the "period-miss-detector" example:

```shell
curl -X PUT -d ‘2340' http://localhost:8000/zf/period-miss-detector
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
```
#### 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:

```
tail -f /tmp/greetings.txt
```

For the "period-miss-detector" example:

```
tail -f /tmp/period-log.txt
```

14 changes: 14 additions & 0 deletions examples/examples/file-writer/file-writer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id: file-writer

vars:
BASE_DIR: "/path/to/zenoh-flow"

# Do not forget to change the extension depending on your operating system!
# Linux -> .so
# Windows -> .dll (and remove the "lib" in front)
# MacOS -> .dylib
uri: "file://{{ BASE_DIR }}/target/debug/examples/libfile_writer.dylib"
# If the compilation is in release:
# uri: file:///absolute/path/to/target/release/libfile_writer.so

inputs: [in]
71 changes: 71 additions & 0 deletions examples/examples/file-writer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// 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:
// ZettaScale Zenoh Team, <[email protected]>
//

use async_std::{fs::File, io::WriteExt, sync::Mutex};
use prost::Message as pMessage;
use zenoh_flow::{anyhow, prelude::*};

#[export_sink]
pub struct FileWriter {
input: Input<String>,
file: Mutex<File>,
}

#[async_trait::async_trait]
impl Node for FileWriter {
async fn iteration(&self) -> Result<()> {
let (message, _) = self.input.recv().await?;

if let Message::Data(greeting) = message {
let mut file = self.file.lock().await;
file.write_all(greeting.as_bytes())
.await
.map_err(|e| zferror!(ErrorKind::IOError, "{:?}", e))?;
return file
.flush()
.await
.map_err(|e| zferror!(ErrorKind::IOError, "{:?}", e).into());
}

Ok(())
}
}

#[async_trait::async_trait]
impl Sink for FileWriter {
async fn new(
_context: Context,
configuration: Option<Configuration>,
mut inputs: Inputs,
) -> Result<Self> {
let file_path = match &configuration {
Some(conf) => conf["file"]
.as_str()
.ok_or_else(|| zferror!(ErrorKind::ConfigurationError))?,
None => "/tmp/greetings.txt",
};

Ok(FileWriter {
file: Mutex::new(
File::create(file_path)
.await
.unwrap_or_else(|e| panic!("Could not create '{}'", e)),
),
input: inputs
.take("in")
.expect("No Input called 'in' found")
.typed(|bytes| String::decode(bytes).map_err(|e| anyhow!(e))),
})
}
}
15 changes: 15 additions & 0 deletions examples/examples/greetings-maker/greetings-maker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id: greetings-maker

vars:
BASE_DIR: "/path/to/zenoh-flow"

# Do not forget to change the extension depending on your operating system!
# Linux -> .so
# Windows -> .dll (and remove the "lib" in front)
# MacOS -> .dylib
uri: "file://{{ BASE_DIR }}/target/debug/examples/libgreetings_maker.dylib"
# If the compilation is in release:
# uri: file:///absolute/path/to/target/release/libgreetings_maker.so

inputs: [name]
outputs: [greeting]
64 changes: 64 additions & 0 deletions examples/examples/greetings-maker/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// 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:
// ZettaScale Zenoh Team, <[email protected]>
//

use prost::Message as pMessage;
use zenoh_flow::{anyhow, prelude::*};

#[export_operator]
pub struct GreetingsMaker {
input: Input<String>,
output: Output<String>,
}

#[async_trait::async_trait]
impl Operator for GreetingsMaker {
async fn new(
_context: Context,
_configuration: Option<Configuration>,
mut inputs: Inputs,
mut outputs: Outputs,
) -> Result<Self> {
Ok(GreetingsMaker {
input: inputs
.take("name")
.expect("No input 'name' found")
.typed(|bytes| String::from_utf8(bytes.into()).map_err(|e| anyhow!(e))),
output: outputs
.take("greeting")
.expect("No output 'greeting' found")
.typed(|buffer, data: &String| data.encode(buffer).map_err(|e| anyhow!(e))),
})
}
}

#[async_trait::async_trait]
impl Node for GreetingsMaker {
async fn iteration(&self) -> Result<()> {
let (message, _) = self.input.recv().await?;
if let Message::Data(characters) = message {
let name = characters.trim_end();

let greetings = match name {
"Sofia" | "Leonardo" => format!("Ciao, {}!\n", name),
"Lucia" | "Martin" => format!("¡Hola, {}!\n", name),
"Jade" | "Gabriel" => format!("Bonjour, {} !\n", name),
_ => format!("Hello, {}!\n", name),
};

return self.output.send(greetings, None).await;
}

Ok(())
}
}
15 changes: 15 additions & 0 deletions examples/examples/period-miss-detector/period-miss-detector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
id: period-miss-detector

vars:
BASE_DIR: "/path/to/zenoh-flow"

# Do not forget to change the extension depending on your operating system!
# Linux -> .so
# Windows -> .dll (and remove the "lib" in front)
# MacOS -> .dylib
uri: "file://{{ BASE_DIR }}/target/debug/examples/libperiod_miss_detector.dylib"
# If the compilation is in release:
# uri: file:///absolute/path/to/target/release/libperiod_miss_detector.dylib

inputs: [in]
outputs: [out]
Loading
Loading