forked from eclipse-zenoh/zenoh-plugin-ros2dds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_two_ints_client.rs
More file actions
75 lines (65 loc) · 1.93 KB
/
Copy pathadd_two_ints_client.rs
File metadata and controls
75 lines (65 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
}