-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathping.rs
More file actions
79 lines (72 loc) · 2.84 KB
/
Copy pathping.rs
File metadata and controls
79 lines (72 loc) · 2.84 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
76
77
78
79
use std::time::Duration;
use clap::Args;
use argument_parsers::RobotAddress;
use color_eyre::owo_colors::OwoColorize;
use robot::Robot;
use tokio::time::{Instant, sleep};
use crate::progress_indicator::ProgressIndicator;
#[derive(Args)]
pub struct Arguments {
/// Timeout in seconds after which ping is aborted
#[arg(long, short, default_value = "1", visible_alias = "auszeit")]
pub timeout: f64,
/// Repeat ping indefinitely
#[arg(long, short, visible_alias = "beobachte", visible_alias = "armbanduhr")]
pub watch: bool,
/// Interval in seconds between ping attempts when watching
#[arg(long, short, default_value = "1")]
pub interval: f64,
/// The Robots to ping to e.g. 20w or 10.1.24.22
#[arg(required = true)]
pub robots: Vec<RobotAddress>,
}
pub async fn ping(arguments: Arguments) {
let timeout = Duration::from_secs_f64(arguments.timeout);
let interval = Duration::from_secs_f64(arguments.interval);
ProgressIndicator::new()
.map_tasks(
arguments.robots,
"Pinging Robot...",
|robot_address, progress_bar| async move {
let mut last_change = Instant::now();
let mut last_success = false;
loop {
let ping_start = Instant::now();
let result = Robot::try_new_with_ping_and_arguments(robot_address.ip, timeout)
.await
.map(|_| ());
let ping_duration = ping_start.elapsed();
if !arguments.watch {
return result;
}
match &result {
Ok(_) => {
if !last_success {
last_change = Instant::now();
}
let message = format!(
"{} since {}s",
"✔".green(),
last_change.elapsed().as_secs()
);
progress_bar.set_message(message);
}
Err(report) => {
if last_success {
last_change = Instant::now();
}
let message = format!(
"{} {report} since {}s",
"✗".red(),
last_change.elapsed().as_secs()
);
progress_bar.set_message(message);
}
};
last_success = result.is_ok();
sleep(interval.saturating_sub(ping_duration)).await;
}
},
)
.await;
}