moved structs to their own files
added time since last state change
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
use std::net::Ipv4Addr;
|
||||
use std::process::Command;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::thread;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use crate::ping_request::PingRequest;
|
||||
use crate::ping_result::PingResult;
|
||||
use crate::SECONDS_BETWEEN_PING;
|
||||
|
||||
pub struct Manager;
|
||||
|
||||
const WIN_PING_SUCCESS: u8 = 1;
|
||||
const LIN_PING_SUCCESS: u8 = 0;
|
||||
|
||||
impl Manager {
|
||||
pub fn spawn_manager_thread(targets: Vec<Ipv4Addr>, sender: Sender<PingResult>) {
|
||||
let local_targets = targets.clone();
|
||||
thread::spawn(move || {
|
||||
loop {
|
||||
for target in &local_targets {
|
||||
Manager::spawn_single_ping(PingRequest { target: *target }, sender.clone());
|
||||
}
|
||||
thread::sleep(Duration::from_secs(SECONDS_BETWEEN_PING as u64));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Spawns a thread that pings a target and sends the response back to the main thread
|
||||
// via the sender
|
||||
fn spawn_single_ping(request: PingRequest, sender: Sender<PingResult>) {
|
||||
let local_request = request.clone();
|
||||
thread::spawn(move || {
|
||||
|
||||
let mut result = 0;
|
||||
let mut success = true;
|
||||
let start_time = SystemTime::now();
|
||||
#[cfg(any(target_os="windows"))] {
|
||||
result = Command::new("c:/windows/system32/ping.exe").arg(request.target.to_string())
|
||||
.arg("-n 1")
|
||||
.arg("-w 10")
|
||||
.arg("-4")
|
||||
.output().unwrap().status.code().unwrap_or(255);
|
||||
success = result == 1;
|
||||
}
|
||||
#[cfg(any(target_os="linux"))] {
|
||||
result = Command::new("/usr/bin/ping").arg(request.target.to_string())
|
||||
.arg("-c 1")
|
||||
.arg("-4")
|
||||
.arg("-w 10")
|
||||
.arg("-W 1")
|
||||
.output().unwrap().status.code().unwrap_or(255);
|
||||
success = result == 0;
|
||||
}
|
||||
let ping_duration = SystemTime::now()
|
||||
.duration_since(start_time)
|
||||
.unwrap_or(Duration::from_secs(600));
|
||||
let success = ping_duration.as_millis() >= SECONDS_BETWEEN_PING as u128 && success;
|
||||
sender.send(PingResult {
|
||||
target: local_request.target,
|
||||
success,
|
||||
rtt: ping_duration.as_millis() as u32
|
||||
}).expect("Unable to send response");
|
||||
|
||||
// println!("Attempt for for {}", local_request.target);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user