Skip to content

Commit

Permalink
make it as a lib too
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Oct 12, 2023
1 parent 435ca0c commit 41a82cd
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mping"
version = "0.1.5"
version = "0.2.0"
authors = ["smallnest@gmail.com"]
license = "Apache-2.0"
readme = "README.md"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ a multi-targets ping tool, which supports 10,000 packets/second, accurate latenc
> 正常的ping一般用来做探测工具,mping还可以用来做压测工具。
> Go版本: [smallnest/mping](https://github.com/smallnest/mping)

And you can use it as a lib to implement your multi-targets and handle the ping results. See the `ping.rs` example in the examples folder.

## Usage

compile
Expand All @@ -20,7 +23,7 @@ options usage.

```sh
> $$ mping -h
mping 0.1.5
mping 0.2.0
A multi-targets ping tool, which supports 10,000 packets/second.

USAGE:
Expand Down
69 changes: 69 additions & 0 deletions examples/ping.rs
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
)
}
}
}
5 changes: 4 additions & 1 deletion src/lib.rs
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::*;
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::net::{IpAddr, ToSocketAddrs};
#[derive(Debug, StructOpt)]
#[structopt(
name = "mping",
version = "0.1.5",
version = "0.2.0",
about = "A multi-targets ping tool, which supports 10,000 packets/second."
)]
struct Opt {
Expand Down Expand Up @@ -104,7 +104,7 @@ fn main() -> Result<(), anyhow::Error> {
delay: opt.delay,
count: opt.count,
};
mping::ping::ping(ip_addrs, popt)?;
mping::ping::ping(ip_addrs, popt, true, None)?;

Ok(())
}
Expand Down
Loading

0 comments on commit 41a82cd

Please sign in to comment.