Skip to content

Instantly share code, notes, and snippets.

@rcloran
Created September 7, 2023 23:03
Show Gist options
  • Save rcloran/1b252ddcc1a0ee2c4530b217639635b1 to your computer and use it in GitHub Desktop.
Save rcloran/1b252ddcc1a0ee2c4530b217639635b1 to your computer and use it in GitHub Desktop.
diff --git a/examples/ping.rs b/examples/ping.rs
index 9cbe35e..c46358b 100644
--- a/examples/ping.rs
+++ b/examples/ping.rs
@@ -3,20 +3,23 @@ extern crate pretty_env_logger;
#[macro_use]
extern crate log;
+use std::error::Error;
+use std::net::IpAddr;
+
use fastping_rs::PingResult::{Idle, Receive};
use fastping_rs::Pinger;
-fn main() {
+fn main() -> Result<(), Box<dyn Error>>{
pretty_env_logger::init();
let pinger = match Pinger::new(None, Some(64)) {
Ok(pinger) => pinger,
Err(e) => panic!("Error creating pinger: {}", e),
};
- pinger.add_ipaddr("8.8.8.8");
+ pinger.add_ipaddr("8.8.8.8".parse::<IpAddr>()?);
pinger.add_ipaddr("1.1.1.1");
- pinger.add_ipaddr("7.7.7.7");
- pinger.add_ipaddr("2001:4860:4860::8888");
+ pinger.add_ipaddr("7.7.7.7".parse::<IpAddr>()?);
+ pinger.add_ipaddr("2001:4860:4860::8888".parse::<IpAddr>()?);
pinger.run_pinger();
loop {
diff --git a/src/lib.rs b/src/lib.rs
index 9d455f9..7302728 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -31,6 +31,11 @@ pub enum PingResult {
Receive { addr: IpAddr, rtt: Duration },
}
+pub trait AddrContainer<T> {
+ fn _add_ipaddr(&self, addr: T);
+ fn _remove_ipaddr(&self, addr: T);
+}
+
pub struct Pinger {
// Number of milliseconds of an idle timeout. Once it passed,
// the library calls an idle callback function. Default is 2000
@@ -114,34 +119,16 @@ impl Pinger {
}
// add either an ipv4 or ipv6 target address for pinging
- pub fn add_ipaddr(&self, ipaddr: &str) {
- let addr = ipaddr.parse::<IpAddr>();
- match addr {
- Ok(valid_addr) => {
- debug!("Address added {}", valid_addr);
- let new_ping = Ping::new(valid_addr);
- self.targets.lock().unwrap().insert(valid_addr, new_ping);
- }
- Err(e) => {
- error!("Error adding ip address {}. Error: {}", ipaddr, e);
- }
- };
+ pub fn add_ipaddr<T>(&self, addr: T) where Self: AddrContainer<T> {
+ self._add_ipaddr(addr);
}
// remove a previously added ipv4 or ipv6 target address
- pub fn remove_ipaddr(&self, ipaddr: &str) {
- let addr = ipaddr.parse::<IpAddr>();
- match addr {
- Ok(valid_addr) => {
- debug!("Address removed {}", valid_addr);
- self.targets.lock().unwrap().remove(&valid_addr);
- }
- Err(e) => {
- error!("Error removing ip address {}. Error: {}", ipaddr, e);
- }
- };
+ pub fn remove_ipaddr<T>(&self, addr: T) where Self: AddrContainer<T> {
+ self._remove_ipaddr(addr);
}
+
// stop running the continous pinger
pub fn stop_pinger(&self) {
let mut stop = self.stop.lock().unwrap();
@@ -308,6 +295,42 @@ impl Pinger {
}
}
+
+impl AddrContainer<IpAddr> for Pinger {
+ // add either an ipv4 or ipv6 target address for pinging
+ fn _add_ipaddr(&self, addr: IpAddr) {
+ debug!("Address added {}", addr);
+ let new_ping = Ping::new(addr);
+ self.targets.lock().unwrap().insert(addr, new_ping);
+ }
+
+ // remove a previously added ipv4 or ipv6 target address
+ fn _remove_ipaddr(&self, addr: IpAddr) {
+ debug!("Address removed {}", addr);
+ self.targets.lock().unwrap().remove(&addr);
+ }
+}
+
+impl AddrContainer<&str> for Pinger {
+ fn _add_ipaddr(&self, addr: &str) {
+ match addr.parse::<IpAddr>() {
+ Ok(addr) => self.add_ipaddr(addr),
+ Err(e) => {
+ error!("Error adding ip address {}. Error: {}", addr, e);
+ }
+ }
+ }
+ fn _remove_ipaddr(&self, addr: &str) {
+ match addr.parse::<IpAddr>() {
+ Ok(addr) => self.remove_ipaddr(addr),
+ Err(e) => {
+ error!("Error adding ip address {}. Error: {}", addr, e);
+ }
+ }
+ }
+}
+
+
#[cfg(test)]
mod tests {
use super::*;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment