summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2018-04-10 13:28:06 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2018-04-10 13:28:06 +0100
commitf61501398da652e7aa13ccfffd1f3d37dfa5f2ca (patch)
tree2314d1f34d9cdeac3c668b8177729b05e63110ba
parent071efc336ef41fa82506baffa467d5bac7be3ebe (diff)
downloadcanopied-f61501398da652e7aa13ccfffd1f3d37dfa5f2ca.tar.bz2
Send a query every 100ms
-rw-r--r--src/main.rs35
1 files changed, 12 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs
index 2b3b7db..915ded6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,7 +22,7 @@ mod definitions;
use tokio::prelude::*;
use std::env::args;
use futures::sync::mpsc;
-use tokio::timer::Delay;
+use tokio::timer::Interval;
use std::time::{Duration, Instant};
fn packet_print(id: u16, bytes: &[u8]) -> String {
@@ -72,31 +72,20 @@ fn main() {
.map(|_| ()),
);
- let mut counter = 1;
- for def in raw_defs {
- break;
+ trace!("Preparing an Interval to run the requests");
+ let always_defs = futures::stream::iter_ok(raw_defs.iter().cycle());
+ let every_100ms = Interval::new(Instant::now(), Duration::from_millis(100));
+ let zipped = every_100ms.zip(always_defs);
+
+ tokio::spawn(zipped.map_err(|_| ()).for_each(move |(_, def)| {
let req = def.request();
let dest = req.sendto();
let packet = req.packet().to_vec();
- let mut targ = sender.clone();
- let when = Instant::now() + Duration::from_millis(counter * 5000);
- info!(
- "Spawning read for {} ({}) at {:?}",
- def.name(),
- def.description(),
- when
- );
- tokio::spawn(Delay::new(when).map_err(|_| ()).and_then(move |_| {
- targ.send((dest, packet))
- .wait()
- .and_then(|mut s| {
- s.close().unwrap();
- Ok(())
- })
- .map_err(|_| ())
- }));
- counter += 1;
- }
+ let targ = sender.clone();
+ tokio::spawn(futures::future::lazy(move || {
+ targ.send((dest, packet)).wait().map(|_| ()).map_err(|_| ())
+ }))
+ }));
tokio::spawn(
stream