Skip to content

Commit

Permalink
adding transcoding example
Browse files Browse the repository at this point in the history
Signed-off-by: gabrik <gabriele.baldoni@gmail.com>
  • Loading branch information
gabrik committed Aug 4, 2023
1 parent 56d126b commit ffb5336
Show file tree
Hide file tree
Showing 11 changed files with 552 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ The purpose of this example is to demonstrate how Zenoh-Flow can handle a
complex dataflow graph, like a robotic application.

Go to the [README](./montblanc/README.md) for instructions on how to run it.


#### Transcoding

The purpose of this example is to demonstrate how Zenoh-Flow can handle be used within a Zenoh
router to transcode live data.

Go to the [README](./transcoding/README.md) for instructions on how to run it.
119 changes: 119 additions & 0 deletions transcoding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

# Zenoh-Flow for data transcoding.

This document will guide you in building, installing and configuring Zenoh-Flow together with Zenoh for data transcoding.

Note: this guide has been tested on Ubuntu 22.04 LTS
## Prerequisites

In order to be able to build and run Zenoh-Flow the following dependencies are needed:

- build-essentials
- python3-dev
- python3-pip
- python3-venv
- clang
- libclang-dev
- rust
- pkg-config

Please make sure those dependencies are installed before proceeding.

## Build Zenoh and Zenoh-Flow

Clone the repositories and build:
```
cd ~
git clone https://github.com/eclipse-zenoh/zenoh -b 0.7.2-rc
cd zenoh
cargo build --release --all-targets --features shared-memory
cd ..
git clone https://github.com/eclipse-zenoh/zenoh-flow -b v0.5.0-alpha.1
cd zenoh-flow
cargo build --release --all-targets
cd ..
git clone https://github.com/eclipse-zenoh/zenoh-flow-python -b v0.5.0-alpha.1
cd zenoh-flow-python
cargo build --release --all-targets
cd zenoh-flow-python
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements-dev.txt
maturin build --release
deactivate
```

## Install

Install Zenoh and Zenoh-Flow

```
cd ~
sudo mkdir -p /etc/zenoh/
sudo mkdir -p /var/zenoh-flow/python
sudo mkdir -p /var/zenoh-flow/flows
sudo mkdir -p /etc/zenoh-flow/extensions.d
sudo cp zenoh/target/release/zenohd /usr/bin/
sudo cp zenoh/target/release/libzenoh_plugin_*.so /usr/lib/
sudo cp zenoh-flow/target/release/libzenoh_plugin_zenoh_flow.so /usr/lib/
sudo cp zenoh-flow/target/release/zfctl /usr/bin/
sudo cp zenoh-flow-python/target/release/libzenoh_flow_python_*_wrapper.so /var/zenoh-flow/python
sudo cp zenoh-flow-python/01-python.zfext /etc/zenoh-flow/extensions.d/
sudo cp zenoh-flow/zfctl/.config/zfctl-zenoh.json /etc/zenoh-flow/
pip3 install ./zenoh-flow-python/target/wheels/eclipse_zenoh_flow-0.5.0a1-cp37-abi3-manylinux_2_34_x86_64.whl
```

## Start Runtime

Copy the `zenoh-config.json` from this folder to `/etc/zenoh/zenoh.json`.

Now you can start the Zenoh router with the Zenoh-Flow plugin.
Open a terminal and run: `RUST_LOG=debug zenohd -c /etc/zenoh/zenoh.json`

Then on another terminal run: `zfctl list runtimes`

You should get an output similar to this:
```
+----------------------------------+--------------------+--------+
| UUID | Name | Status |
+----------------------------------+--------------------+--------+
| bb4a456d6c0948bfae21a6e8c9051d6b | protoc-client-test | Ready |
+----------------------------------+--------------------+--------+
```

This means that the zenoh-flow runtime is was loaded and it is ready.

## The transcoding application.

Copy the content of this folder in: `/var/zenoh-flow/flows` and run `pip3 install -r /var/zenoh-flow/flows/requirements.txt`.


On a terminal start the publisher side: `cd /var/zenoh-flow/flows && python3 pub-proto.py`
On a new terminal start the subscriber side: `cd /var/zenoh-flow/flows && python3 pub-cdr.py`

The subscriber will not receive any data as the transcoding is not yet deployed.

On a 3rd terminal instruct zenoh-flow to launch the transcoding flow: `zfctl launch /var/zenoh-flow/flows/dataflow.yml` it will return the instance id.

Now you should see the data being transcoded and received by your subscriber.

Once you are done you can list the current running flow instances: `zfctl list instances` and delete the running one with `zfctl destroy <instance uuid>`.

Once the instance is delete you will see that the subscriber is not going to receive any data.








35 changes: 35 additions & 0 deletions transcoding/dataflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
flow: Transcoder
vars:
BASE_DIR: "/var/zenoh-flow/flows"

operators:
- id : Conversion
descriptor: "file://{{ BASE_DIR }}/transcoder.yml"
sources:
- id : ZenohSrc
configuration:
key-expressions:
proto: data/proto
descriptor: "builtin://zenoh"

sinks:
- id : ZenohSink
configuration:
key-expressions:
cdr: data/cdr
descriptor: "builtin://zenoh"

links:
- from:
node : ZenohSrc
output : proto
to:
node : Conversion
input : in

- from:
node : Conversion
output : out
to:
node : ZenohSink
input : cdr
6 changes: 6 additions & 0 deletions transcoding/message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

message MyMsg {
uint64 u_value = 1;
string s_value = 2;
}
34 changes: 34 additions & 0 deletions transcoding/message_pb2.py

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

89 changes: 89 additions & 0 deletions transcoding/pub-proto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#
# 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, <zenoh@zettascale.tech>
#

import sys
import time
import argparse
import itertools
import json
import zenoh
from zenoh import config
from message_pb2 import MyMsg

# --- Command line argument parsing --- --- --- --- --- ---
parser = argparse.ArgumentParser(
prog='z_pub',
description='zenoh pub example')
parser.add_argument('--mode', '-m', dest='mode',
choices=['peer', 'client'],
type=str,
help='The zenoh session mode.')
parser.add_argument('--connect', '-e', dest='connect',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to connect to.')
parser.add_argument('--listen', '-l', dest='listen',
metavar='ENDPOINT',
action='append',
type=str,
help='Endpoints to listen on.')
parser.add_argument('--key', '-k', dest='key',
default='data/proto',
type=str,
help='The key expression to publish onto.')
parser.add_argument('--value', '-v', dest='value',
default='Pub from Python!',
type=str,
help='The value to publish.')
parser.add_argument("--iter", dest="iter", type=int,
help="How many puts to perform")
parser.add_argument('--config', '-c', dest='config',
metavar='FILE',
type=str,
help='A configuration file.')

args = parser.parse_args()
conf = zenoh.Config.from_file(args.config) if args.config is not None else zenoh.Config()
if args.mode is not None:
conf.insert_json5(zenoh.config.MODE_KEY, json.dumps(args.mode))
if args.connect is not None:
conf.insert_json5(zenoh.config.CONNECT_KEY, json.dumps(args.connect))
if args.listen is not None:
conf.insert_json5(zenoh.config.LISTEN_KEY, json.dumps(args.listen))
key = args.key
value = args.value

# initiate logging
zenoh.init_logger()

print("Opening session...")
session = zenoh.open(conf)

print(f"Declaring Publisher on '{key}'...")
pub = session.declare_publisher(key)

for idx in itertools.count() if args.iter is None else range(args.iter):
time.sleep(1)

msg = MyMsg(
u_value = idx,
s_value = value
)

print(f"Putting Data ('{key}': '{msg}')...")
pub.put(msg.SerializeToString())

pub.undeclare()
session.close()
3 changes: 3 additions & 0 deletions transcoding/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
grpcio-tools==1.54.2
pycdr2==1.0.0
eclipse-zenoh==0.7.2rc0
Loading

0 comments on commit ffb5336

Please sign in to comment.