-
Notifications
You must be signed in to change notification settings - Fork 317
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
12 changed files
with
318 additions
and
283 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,49 +1,44 @@ | ||
use crate::{Parser, PingResult, Pinger}; | ||
use crate::{extract_regex, PingDetectionError, PingOptions, PingResult, Pinger}; | ||
use lazy_regex::*; | ||
use std::time::Duration; | ||
|
||
pub static RE: Lazy<Regex> = lazy_regex!(r"time=(?:(?P<ms>[0-9]+).(?P<ns>[0-9]+)\s+ms)"); | ||
|
||
pub struct BSDPinger { | ||
interval: Duration, | ||
interface: Option<String>, | ||
options: PingOptions, | ||
} | ||
|
||
pub(crate) fn parse_bsd(line: String) -> Option<PingResult> { | ||
if line.starts_with("PING ") { | ||
return None; | ||
} | ||
if line.starts_with("Request timeout") { | ||
return Some(PingResult::Timeout(line)); | ||
} | ||
extract_regex(&RE, line) | ||
} | ||
|
||
impl Pinger for BSDPinger { | ||
type Parser = BSDParser; | ||
fn from_options(options: PingOptions) -> Result<Self, PingDetectionError> | ||
where | ||
Self: Sized, | ||
{ | ||
Ok(Self { options }) | ||
} | ||
|
||
fn new(interval: Duration, interface: Option<String>) -> Self { | ||
Self { | ||
interface, | ||
interval, | ||
} | ||
fn parse_fn(&self) -> fn(String) -> Option<PingResult> { | ||
parse_bsd | ||
} | ||
|
||
fn ping_args(&self, target: String) -> (&str, Vec<String>) { | ||
fn ping_args(&self) -> (&str, Vec<String>) { | ||
let mut args = vec![format!( | ||
"-i{:.1}", | ||
self.interval.as_millis() as f32 / 1_000_f32 | ||
self.options.interval.as_millis() as f32 / 1_000_f32 | ||
)]; | ||
if let Some(interface) = &self.interface { | ||
if let Some(interface) = &self.options.interface { | ||
args.push("-I".into()); | ||
args.push(interface.clone()); | ||
} | ||
args.push(target); | ||
args.push(self.options.target.clone()); | ||
("ping", args) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct BSDParser {} | ||
|
||
impl Parser for BSDParser { | ||
fn parse(&self, line: String) -> Option<PingResult> { | ||
if line.starts_with("PING ") { | ||
return None; | ||
} | ||
if line.starts_with("Request timeout") { | ||
return Some(PingResult::Timeout(line)); | ||
} | ||
self.extract_regex(&RE, line) | ||
} | ||
} |
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.