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 13 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
4,351 changes: 4,351 additions & 0 deletions Cargo.lock
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ The best way to learn Zenoh-Flow is to go through our [getting started guide](ht
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
42 changes: 42 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "zenoh-flow-examples"
version.workspace = true
repository.workspace = true
homepage.workspace = true
categories.workspace = true
description.workspace = true
edition.workspace = true
license.workspace = true
readme = "README.md"

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

[profile.dev]
debug = true

[profile.release]
debug = true
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
async-std = "1.12"
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
async-trait = "0.1"
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
prost = "0.11"
zenoh-flow = { path = "../zenoh-flow/" }

[build-dependencies]
rustc_version = "0.4.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this build dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have the prost = "0.11" row because in our libraries we use prost::Message so we need to specify this dependency.

The zenoh-flow = { path = "../zenoh-flow/" } row it's useful to specify the Zenoh-Flow version declared Cargo.toml root folder file

The rows26 and 27 can be removed.


[[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"]
76 changes: 76 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# 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 name_node

i.e.
cargo build --example greetings-maker
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```bash
cargo build --example name_node
i.e.
cargo build --example greetings-maker
```
```bash
cargo build --example <node>


### Update the paths

For each YAML file in the list below, check that the paths and filenames are
correct:
- flows/period-miss-detector.yaml
- flows/getting-started.yaml
- examples/file-writer/file-writer.yaml
- examples/greetings-maker/greetings-maker.yaml
- examples/period-miss-detector/period-miss-detector.yaml

### Launch

#### 1st terminal: Zenoh

```shell
cd ~/dev/zenoh && ./target/debug/zenohd -c ~/.config/zenoh-flow/zenoh.json
```

#### 2nd terminal: Zenoh-Flow daemon

```shell
cd ~/dev/zenoh-flow/ && ./target/debug/zenoh-flow-daemon -c ~/.config/zenoh-flow/runtime.yaml
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge these 2 paragraphs into one, and point to the documentation: https://github.com/eclipse-zenoh/zenoh-flow/wiki/Installation-(v0.4.0)#zenoh-plugin


#### 3rd terminal: launch the flow

```shell
cd ~/dev/zenoh-flow && ./target/debug/zfctl launch ~/dev/zenoh-flow/examples/data-flow.yaml
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Then, if the flow was successfully launched, put values at regular intervals into the "greetings-maker" example:

```shell
# If you have compiled the `z_put` example of Zenoh in debug
$ZENOH/target/debug/examples/z_put -k "zf/getting-started/hello" -v "Alice"
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this command with the CURL equivalent (adapt the key expression):

curl -X PUT -d '"Hello World!"' http://localhost:8000/demo/example/test

It is only possible to do so if the Zenoh router loaded the REST plugin.


Alternatively, in the "period-miss-detector" example, put values at regular intervals with:

```shell
cd ~/dev/zenoh && ./target/debug/examples/z_put -k "zf/period-miss-detector" -v "3.1416"
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, replace this command with the CURL equivalent (adapt the key expression):

curl -X PUT -d '"Hello World!"' http://localhost:8000/demo/example/test

#### 4rd terminal:

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
```

Alternatively we can run the following command to see "period-miss-detector" text result:
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved

```
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::InvalidState))?,
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
None => "/tmp/greetings.txt",
};

Ok(FileWriter {
file: Mutex::new(
File::create(file_path) //File::create("/tmp/greetings.txt")
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
.await
.expect("Could not create '/tmp/greetings.txt'"),
J-Loudet marked this conversation as resolved.
Show resolved Hide resolved
),
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