/
z_liveliness.rs
73 lines (66 loc) · 2.03 KB
/
z_liveliness.rs
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
//
// 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 async_std::task::sleep;
use clap::Parser;
use futures::prelude::*;
use std::time::Duration;
use zenoh::config::Config;
use zenoh::prelude::r#async::*;
use zenoh_examples::CommonArgs;
#[async_std::main]
async fn main() {
// Initiate logging
env_logger::init();
let (config, key_expr) = parse_args();
println!("Opening session...");
let session = zenoh::open(config).res().await.unwrap();
println!("Declaring LivelinessToken on '{}'...", &key_expr);
let mut token = Some(
session
.liveliness()
.declare_token(&key_expr)
.res()
.await
.unwrap(),
);
println!("Enter 'd' to undeclare LivelinessToken, 'q' to quit...");
let mut stdin = async_std::io::stdin();
let mut input = [0_u8];
loop {
let _ = stdin.read_exact(&mut input).await;
match input[0] {
b'q' => break,
b'd' => {
if let Some(token) = token.take() {
println!("Undeclaring LivelinessToken...");
token.undeclare().res().await.unwrap();
}
}
0 => sleep(Duration::from_secs(1)).await,
_ => (),
}
}
}
#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
struct Args {
#[arg(short, long, default_value = "group1/zenoh-rs")]
/// The key expression of the liveliness token.
key: KeyExpr<'static>,
#[command(flatten)]
common: CommonArgs,
}
fn parse_args() -> (Config, KeyExpr<'static>) {
let args = Args::parse();
(args.common.into(), args.key)
}