|
| 1 | +// |
| 2 | +// Copyright (c) 2023 ZettaScale Technology |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +// |
| 9 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | +// |
| 11 | +// Contributors: |
| 12 | +// ZettaScale Zenoh Team, <zenoh@zettascale.tech> |
| 13 | +// |
| 14 | +use cdr::{CdrLe, Infinite}; |
| 15 | +use clap::{App, Arg}; |
| 16 | +use serde::{Deserialize, Serialize}; |
| 17 | +use zenoh::config::Config; |
| 18 | +use zenoh::prelude::r#async::*; |
| 19 | + |
| 20 | +#[derive(Deserialize, PartialEq, Debug)] |
| 21 | +struct Time { |
| 22 | + sec: u32, |
| 23 | + nsec: u32, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Serialize, PartialEq, Debug)] |
| 27 | +struct FibonacciSendGoalRequest { |
| 28 | + goal_id: [u8; 16], |
| 29 | + order: i32, |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Deserialize, PartialEq, Debug)] |
| 33 | +struct FibonacciSendGoalResponse { |
| 34 | + accepted: bool, |
| 35 | + stamp: Time, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Serialize, PartialEq, Debug)] |
| 39 | +struct FibonacciGetResultRequest { |
| 40 | + goal_id: [u8; 16], |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Deserialize, PartialEq, Debug)] |
| 44 | +struct FibonacciGetResultResponse { |
| 45 | + status: i8, |
| 46 | + sequence: Vec<i32>, |
| 47 | +} |
| 48 | + |
| 49 | +#[derive(Deserialize, PartialEq, Debug)] |
| 50 | +struct FibonacciFeedback { |
| 51 | + goal_id: [u8; 16], |
| 52 | + partial_sequence: Vec<i32>, |
| 53 | +} |
| 54 | + |
| 55 | +#[async_std::main] |
| 56 | +async fn main() { |
| 57 | + env_logger::init(); |
| 58 | + |
| 59 | + let config = parse_args(); |
| 60 | + |
| 61 | + let session = zenoh::open(config).res().await.unwrap(); |
| 62 | + |
| 63 | + let _subscriber = session |
| 64 | + .declare_subscriber("fibonacci/_action/feedback") |
| 65 | + .callback(|sample| { |
| 66 | + match cdr::deserialize_from::<_, FibonacciFeedback, _>( |
| 67 | + sample.value.payload.reader(), |
| 68 | + cdr::size::Infinite, |
| 69 | + ) { |
| 70 | + Ok(msg) => { |
| 71 | + println!( |
| 72 | + "Next number in sequence received: {:?}", |
| 73 | + msg.partial_sequence |
| 74 | + ); |
| 75 | + } |
| 76 | + Err(e) => log::warn!("Error decoding message: {}", e), |
| 77 | + }; |
| 78 | + }) |
| 79 | + .res() |
| 80 | + .await |
| 81 | + .unwrap(); |
| 82 | + |
| 83 | + let goal_id: [u8; 16] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 84 | + let req = FibonacciSendGoalRequest { |
| 85 | + goal_id: goal_id, |
| 86 | + order: 10, |
| 87 | + }; |
| 88 | + |
| 89 | + let buf = cdr::serialize::<_, _, CdrLe>(&req, Infinite).unwrap(); |
| 90 | + println!("Sending goal"); |
| 91 | + let replies = session |
| 92 | + .get("fibonacci/_action/send_goal") |
| 93 | + .with_value(buf) |
| 94 | + .res() |
| 95 | + .await |
| 96 | + .unwrap(); |
| 97 | + |
| 98 | + while let Ok(reply) = replies.recv_async().await { |
| 99 | + match cdr::deserialize_from::<_, FibonacciSendGoalResponse, _>( |
| 100 | + reply.sample.unwrap().payload.reader(), |
| 101 | + cdr::size::Infinite, |
| 102 | + ) { |
| 103 | + Ok(res) => { |
| 104 | + if res.accepted { |
| 105 | + println!("Goal accepted by server, waiting for result"); |
| 106 | + } else { |
| 107 | + println!("Goal rejected :("); |
| 108 | + return; |
| 109 | + } |
| 110 | + } |
| 111 | + Err(e) => log::warn!("Error decoding message: {}", e), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + let req = FibonacciGetResultRequest { goal_id: goal_id }; |
| 116 | + let buf = cdr::serialize::<_, _, CdrLe>(&req, Infinite).unwrap(); |
| 117 | + let replies = session |
| 118 | + .get("fibonacci/_action/get_result") |
| 119 | + .with_value(buf) |
| 120 | + .res() |
| 121 | + .await |
| 122 | + .unwrap(); |
| 123 | + while let Ok(reply) = replies.recv_async().await { |
| 124 | + match cdr::deserialize_from::<_, FibonacciGetResultResponse, _>( |
| 125 | + reply.sample.unwrap().payload.reader(), |
| 126 | + cdr::size::Infinite, |
| 127 | + ) { |
| 128 | + Ok(res) => { |
| 129 | + println!("Result: {:?}", res.sequence); |
| 130 | + } |
| 131 | + Err(e) => log::warn!("Error decoding message: {}", e), |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +fn parse_args() -> Config { |
| 137 | + let args = App::new("zenoh sub example") |
| 138 | + .arg(Arg::from_usage( |
| 139 | + "-c, --config=[FILE] 'A configuration file.'", |
| 140 | + )) |
| 141 | + .get_matches(); |
| 142 | + |
| 143 | + let config = if let Some(conf_file) = args.value_of("config") { |
| 144 | + Config::from_file(conf_file).unwrap() |
| 145 | + } else { |
| 146 | + Config::default() |
| 147 | + }; |
| 148 | + |
| 149 | + config |
| 150 | +} |
0 commit comments