Skip to content

Commit 046564f

Browse files
committed
Add examples to the pinger library. Closes #428
1 parent 8ae998b commit 046564f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

pinger/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# pinger
2+
3+
> A small cross-platform library to execute the ping command and parse the output.
4+
5+
This crate is primarily built for use with `gping`, but it can also be used as a
6+
standalone library.
7+
8+
This allows you to reliably ping hosts without having to worry about process permissions,
9+
in a cross-platform manner on Windows, Linux and macOS.
10+
11+
## Usage
12+
13+
A full example of using the library can be found in the `examples/` directory, but the
14+
interface is quite simple:
15+
16+
```rust
17+
use pinger::ping;
18+
19+
fn ping_google() {
20+
let stream = ping("google.com", None).expect("Error pinging");
21+
for message in stream {
22+
match message {
23+
pinger::PingResult::Pong(duration, _) => {
24+
println!("Duration: {:?}", duration)
25+
}
26+
_ => {} // Handle errors, log ping timeouts, etc.
27+
}
28+
}
29+
}
30+
```
31+
32+
## Adding pinger to your project.
33+
34+
`cargo add pinger`
35+

pinger/examples/simple-ping.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use pinger::ping_with_interval;
2+
3+
pub fn main() {
4+
let target = "tomforb.es".to_string();
5+
let interval = std::time::Duration::from_secs(1);
6+
let stream = ping_with_interval(target, interval, None).expect("Error pinging");
7+
for message in stream {
8+
match message {
9+
pinger::PingResult::Pong(duration, line) => {
10+
println!("Duration: {:?}\t\t(raw: {:?})", duration, line)
11+
}
12+
pinger::PingResult::Timeout(line) => println!("Timeout! (raw: {line:?})"),
13+
pinger::PingResult::Unknown(line) => println!("Unknown line: {:?}", line),
14+
pinger::PingResult::PingExited(code, stderr) => {
15+
println!("Ping exited! Code: {:?}. Stderr: {:?}", code, stderr)
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)