Skip to content

Commit

Permalink
Merge pull request #8 from bparli/bp-listeners-check-stop
Browse files Browse the repository at this point in the history
listeners check stop signal
  • Loading branch information
bparli authored Jul 31, 2019
2 parents b2e45e6 + 364cfc6 commit d486a99
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
32 changes: 20 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ impl Pinger {

// run pinger either once or continuously
fn run_pings(&self, run_once: bool) {
{
let mut stop = self.stop.lock().unwrap();
if run_once {
info!("TEST run once is true");
*stop = true;
} else {
*stop = false;
}
}

let thread_rx = self.thread_rx.clone();
let tx = self.tx.clone();
let txv6 = self.txv6.clone();
Expand All @@ -179,6 +169,17 @@ impl Pinger {
let addrs = self.addrs.clone();
let timer = self.timer.clone();
let max_rtt = self.max_rtt.clone();

{
let mut stop = self.stop.lock().unwrap();
if run_once {
debug!("Running pinger for one round");
*stop = true;
} else {
*stop = false;
}
}

if run_once {
send_pings(timer, stop, results_sender, thread_rx, tx, txv6, addrs, max_rtt);
} else {
Expand All @@ -195,6 +196,7 @@ impl Pinger {
let thread_tx = self.thread_tx.clone();
let rx = self.rx.clone();
let timer = self.timer.clone();
let stop = self.stop.clone();

thread::spawn(move || {
let mut receiver = rx.lock().unwrap();
Expand All @@ -206,7 +208,9 @@ impl Pinger {
match thread_tx.send(PingResult::Receive{addr: addr, rtt: Instant::now().duration_since(*start_time)}) {
Ok(_) => {},
Err(e) => {
error!("Error sending ping result on channel: {}", e)
if !*stop.lock().unwrap() {
error!("Error sending ping result on channel: {}", e)
}
}
}
},
Expand All @@ -221,6 +225,8 @@ impl Pinger {
let thread_txv6 = self.thread_tx.clone();
let rxv6 = self.rxv6.clone();
let timerv6 = self.timer.clone();
let stopv6 = self.stop.clone();

thread::spawn(move || {
let mut receiver = rxv6.lock().unwrap();
let mut iter = icmpv6_packet_iter(&mut receiver);
Expand All @@ -231,7 +237,9 @@ impl Pinger {
match thread_txv6.send(PingResult::Receive{addr: addr, rtt: Instant::now().duration_since(*start_time)}) {
Ok(_) => {},
Err(e) => {
error!("Error sending ping result on channel: {}", e)
if !*stopv6.lock().unwrap() {
error!("Error sending ping result on channel: {}", e)
}
}
}
},
Expand Down
18 changes: 11 additions & 7 deletions src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ pub fn send_pings(timer: Arc<RwLock<Instant>>,
// Update the address to the ping response being received
if let Some(seen) = addrs.lock().unwrap().get_mut(&addr) {
*seen = true;
}
// Send the ping result over the client channel
match results_sender.send(result) {
Ok(_) => {},
Err(e) => {
error!("Error sending ping result on channel: {}", e)
// Send the ping result over the client channel
match results_sender.send(result) {
Ok(_) => {},
Err(e) => {
if !*stop.lock().unwrap() {
error!("Error sending ping result on channel: {}", e)
}
}
}
}
}
Expand All @@ -110,7 +112,9 @@ pub fn send_pings(timer: Arc<RwLock<Instant>>,
match results_sender.send(PingResult::Idle{addr: *addr}) {
Ok(_) => {},
Err(e) => {
error!("Error sending ping Idle result on channel: {}", e)
if !*stop.lock().unwrap() {
error!("Error sending ping Idle result on channel: {}", e)
}
}
}
}
Expand Down

0 comments on commit d486a99

Please sign in to comment.