-
Notifications
You must be signed in to change notification settings - Fork 52
Added ROS2-like Rust Examples #104
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
935c116
[WIP] Restructured Examples directory Python
imstevenpmwork 9709d95
[WIP] Added rust examples sub & pub
imstevenpmwork 1081cf0
[WIP] Added add_two_ints_client Rust
imstevenpmwork d379fc7
[WIP] Changed prints Python
imstevenpmwork b640e44
[WIP] Added Fibonnacci Rust
imstevenpmwork 9a82244
[WIP] typo python readme + link
imstevenpmwork 27f1f02
[WIP] Push to zenoh 0.10.1 + rust readme + consistent prints
imstevenpmwork c03cb89
fix toolchain & clap version
imstevenpmwork 76700fe
Remove install zenoh-rust step in readme
imstevenpmwork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "rust_examples" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
| [workspace] | ||
| [dependencies] | ||
| async-std = { version = "1.12.0" } | ||
| futures = { version = "0.3.28" } | ||
| zenoh = { version = "0.10.0-rc" } | ||
| clap = { version = "3.2.23" } | ||
| env_logger = { version = "0.10.0" } | ||
| serde = {version = "1" } | ||
| serde_derive = {version = "1"} | ||
| cdr = {version = "0.2.4"} | ||
| log = { version = "0.4.21"} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| TODO | ||
|
imstevenpmwork marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // | ||
| // Copyright (c) 2023 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> | ||
| // | ||
| use cdr::{CdrLe, Infinite}; | ||
| use clap::{App, Arg}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use zenoh::config::Config; | ||
| use zenoh::prelude::r#async::*; | ||
|
|
||
| #[derive(Serialize, PartialEq, Debug)] | ||
| struct AddTwoIntsRequest { | ||
| a: i64, | ||
| b: i64, | ||
| } | ||
|
|
||
| #[derive(Deserialize, PartialEq, Debug)] | ||
| struct AddTwoIntsResponse { | ||
| sum: i64, | ||
| } | ||
|
|
||
| #[async_std::main] | ||
| async fn main() { | ||
| env_logger::init(); | ||
|
|
||
| let config = parse_args(); | ||
|
|
||
| let session = zenoh::open(config).res().await.unwrap(); | ||
|
|
||
| let req = AddTwoIntsRequest { a: 2, b: 3 }; | ||
| let buf = cdr::serialize::<_, _, CdrLe>(&req, Infinite).unwrap(); | ||
| let replies = session | ||
| .get("add_two_ints") | ||
| .with_value(buf) | ||
| .res() | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| while let Ok(reply) = replies.recv_async().await { | ||
| match cdr::deserialize_from::<_, AddTwoIntsResponse, _>( | ||
| reply.sample.unwrap().payload.reader(), | ||
| cdr::size::Infinite, | ||
| ) { | ||
| Ok(res) => { | ||
| println!("Result of add_two_ints: {}", res.sum); | ||
| } | ||
| Err(e) => log::warn!("Error decoding message: {}", e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn parse_args() -> Config { | ||
| let args = App::new("zenoh sub example") | ||
| .arg(Arg::from_usage( | ||
| "-c, --config=[FILE] 'A configuration file.'", | ||
| )) | ||
| .get_matches(); | ||
|
|
||
| let config = if let Some(conf_file) = args.value_of("config") { | ||
| Config::from_file(conf_file).unwrap() | ||
| } else { | ||
| Config::default() | ||
| }; | ||
|
|
||
| config | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // | ||
| // Copyright (c) 2023 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> | ||
| // | ||
| use cdr::{CdrLe, Infinite}; | ||
| use clap::{App, Arg}; | ||
| use serde::{Deserialize, Serialize}; | ||
| use zenoh::config::Config; | ||
| use zenoh::prelude::r#async::*; | ||
|
|
||
| #[derive(Deserialize, PartialEq, Debug)] | ||
| struct Time { | ||
| sec: u32, | ||
| nsec: u32, | ||
| } | ||
|
|
||
| #[derive(Serialize, PartialEq, Debug)] | ||
| struct FibonacciSendGoalRequest { | ||
| goal_id: [u8; 16], | ||
| order: i32, | ||
| } | ||
|
|
||
| #[derive(Deserialize, PartialEq, Debug)] | ||
| struct FibonacciSendGoalResponse { | ||
| accepted: bool, | ||
| stamp: Time, | ||
| } | ||
|
|
||
| #[derive(Serialize, PartialEq, Debug)] | ||
| struct FibonacciGetResultRequest { | ||
| goal_id: [u8; 16], | ||
| } | ||
|
|
||
| #[derive(Deserialize, PartialEq, Debug)] | ||
| struct FibonacciGetResultResponse { | ||
| status: i8, | ||
| sequence: Vec<i32>, | ||
| } | ||
|
|
||
| #[derive(Deserialize, PartialEq, Debug)] | ||
| struct FibonacciFeedback { | ||
| goal_id: [u8; 16], | ||
| partial_sequence: Vec<i32>, | ||
| } | ||
|
|
||
| #[async_std::main] | ||
| async fn main() { | ||
| env_logger::init(); | ||
|
|
||
| let config = parse_args(); | ||
|
|
||
| let session = zenoh::open(config).res().await.unwrap(); | ||
|
|
||
| let _subscriber = session | ||
| .declare_subscriber("fibonacci/_action/feedback") | ||
| .callback(|sample| { | ||
| match cdr::deserialize_from::<_, FibonacciFeedback, _>( | ||
| sample.value.payload.reader(), | ||
| cdr::size::Infinite, | ||
| ) { | ||
| Ok(msg) => { | ||
| println!( | ||
| "Next number in sequence received: {:?}", | ||
| msg.partial_sequence | ||
| ); | ||
| } | ||
| Err(e) => log::warn!("Error decoding message: {}", e), | ||
| }; | ||
| }) | ||
| .res() | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| let goal_id: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | ||
| let req = FibonacciSendGoalRequest { | ||
| goal_id: goal_id, | ||
| order: 10, | ||
| }; | ||
|
|
||
| let buf = cdr::serialize::<_, _, CdrLe>(&req, Infinite).unwrap(); | ||
| println!("Sending goal"); | ||
| let replies = session | ||
| .get("fibonacci/_action/send_goal") | ||
| .with_value(buf) | ||
| .res() | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| while let Ok(reply) = replies.recv_async().await { | ||
| match cdr::deserialize_from::<_, FibonacciSendGoalResponse, _>( | ||
| reply.sample.unwrap().payload.reader(), | ||
| cdr::size::Infinite, | ||
| ) { | ||
| Ok(res) => { | ||
| if res.accepted { | ||
| println!("Goal accepted by server, waiting for result"); | ||
| } else { | ||
| println!("Goal rejected :("); | ||
| return; | ||
| } | ||
| } | ||
| Err(e) => log::warn!("Error decoding message: {}", e), | ||
| } | ||
| } | ||
|
|
||
| let req = FibonacciGetResultRequest { goal_id: goal_id }; | ||
| let buf = cdr::serialize::<_, _, CdrLe>(&req, Infinite).unwrap(); | ||
| let replies = session | ||
| .get("fibonacci/_action/get_result") | ||
| .with_value(buf) | ||
| .res() | ||
| .await | ||
| .unwrap(); | ||
| while let Ok(reply) = replies.recv_async().await { | ||
| match cdr::deserialize_from::<_, FibonacciGetResultResponse, _>( | ||
| reply.sample.unwrap().payload.reader(), | ||
| cdr::size::Infinite, | ||
| ) { | ||
| Ok(res) => { | ||
| println!("Result: {:?}", res.sequence); | ||
| } | ||
| Err(e) => log::warn!("Error decoding message: {}", e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn parse_args() -> Config { | ||
| let args = App::new("zenoh sub example") | ||
| .arg(Arg::from_usage( | ||
| "-c, --config=[FILE] 'A configuration file.'", | ||
| )) | ||
| .get_matches(); | ||
|
|
||
| let config = if let Some(conf_file) = args.value_of("config") { | ||
| Config::from_file(conf_file).unwrap() | ||
| } else { | ||
| Config::default() | ||
| }; | ||
|
|
||
| config | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // | ||
| // Copyright (c) 2023 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> | ||
| // | ||
| use clap::{App, Arg}; | ||
| use serde::Deserialize; | ||
| use zenoh::config::Config; | ||
| use zenoh::prelude::r#async::*; | ||
|
|
||
| #[derive(Deserialize, PartialEq, Debug)] | ||
| struct Message { | ||
| data: String, | ||
| } | ||
| #[async_std::main] | ||
| async fn main() { | ||
| // Initiate logging | ||
| env_logger::init(); | ||
|
|
||
| let config = parse_args(); | ||
|
|
||
| println!("Opening session..."); | ||
| let session = zenoh::open(config).res().await.unwrap(); | ||
|
|
||
| let key_expr = "chatter"; | ||
| println!("Declaring Subscriber on '{}'...", &key_expr); | ||
| let subscriber = session.declare_subscriber(key_expr).res().await.unwrap(); | ||
|
imstevenpmwork marked this conversation as resolved.
Outdated
|
||
|
|
||
| while let Ok(sample) = subscriber.recv_async().await { | ||
| match cdr::deserialize_from::<_, Message, _>( | ||
| sample.value.payload.reader(), | ||
| cdr::size::Infinite, | ||
| ) { | ||
| Ok(msg) => { | ||
| println!("I heard: [{}]", msg.data); | ||
| } | ||
| Err(e) => log::warn!("Error decoding message: {}", e), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn parse_args() -> Config { | ||
| let args = App::new("zenoh sub example") | ||
| .arg(Arg::from_usage( | ||
| "-c, --config=[FILE] 'A configuration file.'", | ||
| )) | ||
| .get_matches(); | ||
|
|
||
| let config = if let Some(conf_file) = args.value_of("config") { | ||
| Config::from_file(conf_file).unwrap() | ||
| } else { | ||
| Config::default() | ||
| }; | ||
|
|
||
| config | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.