From: Greg Burri Date: Wed, 4 Oct 2023 20:59:02 +0000 (+0200) Subject: Measure ping time. X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=b02632d6550866301d16069c1c747b801bebb668;p=stakingWatchdogWatchdog.git Measure ping time. --- diff --git a/src/main.rs b/src/main.rs index b51a6a8..967eb5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,9 @@ fn main() -> Result<()> { let mut rng = rand::thread_rng(); + let mut number_of_pings = 0; + let mut total_ping_duration = Duration::default(); + let socket = UdpSocket::bind("0.0.0.0:0").unwrap(); socket.connect("192.168.2.102:8739").unwrap(); socket @@ -51,34 +54,45 @@ fn main() -> Result<()> { loop { let time_beginning_loop = time::Instant::now(); - if let Err(error) = ping(&socket, &mut rng) { - error_state = true; - println!("Error: {:?}", error); - if time::Instant::now() - time_last_email_send >= EMAIL_RESEND_PERIOD { - // Send e-mail. - println!("Sending email..."); - match send_email( - "Watchdog ERROR", - &format!("Error: {:?}", error), - &config.smtp_login, - &config.smtp_password, - ) { - Err(email_error) => println!("Error sending email: {:?}", email_error), - _ => { - println!("Email successfully sent"); - time_last_email_send = time::Instant::now(); - } + match ping(&socket, &mut rng) { + Ok(t) => { + total_ping_duration += t; + number_of_pings += 1; + + if error_state { + error_state = false; + println!("End of erroneous state"); } - } - } else { - if error_state { - error_state = false; - println!("End of erroneous state"); - } - if time::Instant::now() - time_last_state_printed >= STATE_PRINT_PERIOD { - println!("No error detected"); - time_last_state_printed = time::Instant::now(); + if time::Instant::now() - time_last_state_printed >= STATE_PRINT_PERIOD { + println!( + "No error detected. Mean of ping time: {} μs", + total_ping_duration.as_micros() / number_of_pings + ); + total_ping_duration = Duration::default(); + number_of_pings = 0; + time_last_state_printed = time::Instant::now(); + } + } + Err(error) => { + error_state = true; + println!("Error: {:?}", error); + if time::Instant::now() - time_last_email_send >= EMAIL_RESEND_PERIOD { + // Send e-mail. + println!("Sending email..."); + match send_email( + "Watchdog ERROR", + &format!("Error: {:?}", error), + &config.smtp_login, + &config.smtp_password, + ) { + Err(email_error) => println!("Error sending email: {:?}", email_error), + _ => { + println!("Email successfully sent"); + time_last_email_send = time::Instant::now(); + } + } + } } } @@ -97,10 +111,11 @@ enum PingError { WrongMessageReceived(String), } -fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result<(), PingError> { +fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result { let number: u64 = rng.gen(); let mut buffer = number.to_le_bytes(); + let now = time::Instant::now(); match socket.send(&buffer) { Ok(_size_sent) => { buffer.fill(0); @@ -127,7 +142,7 @@ fn ping(socket: &UdpSocket, rng: &mut ThreadRng) -> std::result::Result<(), Ping Err(error) => return Err(PingError::SocketError(error)), } - Ok(()) + Ok(time::Instant::now() - now) } fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> {