Skip to content

Commit

Permalink
Add examples to the pinger library. Closes #428
Browse files Browse the repository at this point in the history
  • Loading branch information
orf committed Feb 17, 2024
1 parent 8ae998b commit 046564f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pinger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# pinger

> A small cross-platform library to execute the ping command and parse the output.
This crate is primarily built for use with `gping`, but it can also be used as a
standalone library.

This allows you to reliably ping hosts without having to worry about process permissions,
in a cross-platform manner on Windows, Linux and macOS.

## Usage

A full example of using the library can be found in the `examples/` directory, but the
interface is quite simple:

```rust
use pinger::ping;

fn ping_google() {
let stream = ping("google.com", None).expect("Error pinging");
for message in stream {
match message {
pinger::PingResult::Pong(duration, _) => {
println!("Duration: {:?}", duration)
}
_ => {} // Handle errors, log ping timeouts, etc.
}
}
}
```

## Adding pinger to your project.

`cargo add pinger`

19 changes: 19 additions & 0 deletions pinger/examples/simple-ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use pinger::ping_with_interval;

pub fn main() {
let target = "tomforb.es".to_string();
let interval = std::time::Duration::from_secs(1);
let stream = ping_with_interval(target, interval, None).expect("Error pinging");
for message in stream {
match message {
pinger::PingResult::Pong(duration, line) => {
println!("Duration: {:?}\t\t(raw: {:?})", duration, line)
}
pinger::PingResult::Timeout(line) => println!("Timeout! (raw: {line:?})"),
pinger::PingResult::Unknown(line) => println!("Unknown line: {:?}", line),
pinger::PingResult::PingExited(code, stderr) => {
println!("Ping exited! Code: {:?}. Stderr: {:?}", code, stderr)
}
}
}
}

0 comments on commit 046564f

Please sign in to comment.