-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
267 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::env; | ||
use std::process; | ||
use std::sync::mpsc; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
use ::mping::{ping, PingOption}; | ||
|
||
/// A multi-targets ping example, which use mping crate. | ||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() != 2 { | ||
println!("Usage: {} <ip-address>", args[0]); | ||
process::exit(1); | ||
} | ||
|
||
let pid = process::id() as u16; | ||
let target = args[1].clone(); | ||
|
||
let addrs = vec![target.parse().unwrap()]; | ||
let popt = PingOption { | ||
timeout: Duration::from_secs(1), | ||
ttl: 64, | ||
tos: None, | ||
ident: pid, | ||
len: 56, | ||
rate: 100, | ||
delay: 3, | ||
count: None, | ||
}; | ||
let (tx, rx) = mpsc::channel(); | ||
|
||
thread::spawn(move || match ping(addrs, popt, false, Some(tx)) { | ||
Ok(_) => {} | ||
Err(e) => { | ||
println!("error: {:?}", e); | ||
} | ||
}); | ||
|
||
for tr in rx { | ||
let total = tr.received + tr.loss; | ||
let loss_rate = if total == 0 { | ||
0.0 | ||
} else { | ||
(tr.loss as f64) / (total as f64) | ||
}; | ||
|
||
if tr.received == 0 { | ||
println!( | ||
"{}: sent:{}, recv:{}, loss rate: {:.2}%, latency: {}ms", | ||
target, | ||
total, | ||
tr.received, | ||
loss_rate * 100.0, | ||
0 | ||
) | ||
} else { | ||
println!( | ||
"{}: sent:{}, recv:{}, loss rate: {:.2}%, latency: {:.2}ms", | ||
target, | ||
total, | ||
tr.received, | ||
loss_rate * 100.0, | ||
Duration::from_nanos(tr.latency as u64 / (tr.received as u64)).as_secs_f64() | ||
* 1000.0 | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub mod ping; | ||
pub mod stat; | ||
pub mod stat; | ||
|
||
pub use ping::*; | ||
pub use stat::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.