From 6785aa4cb56833131b69f4d2b44301908b1a1b4c Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Wed, 28 Aug 2019 16:27:26 +0200 Subject: Join with worker threads on device drop --- src/main.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 65 insertions(+), 13 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 5c58b24..fc1a26a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,11 +7,60 @@ mod types; use hjul::*; +use std::error::Error; +use std::fmt; +use std::net::SocketAddr; use std::sync::Arc; use std::time::Duration; use sodiumoxide; -use types::KeyPair; +use types::{Bind, KeyPair}; + +struct Test {} + +impl Bind for Test { + type Error = BindError; + type Endpoint = SocketAddr; + + fn new() -> Test { + Test {} + } + + fn set_port(&self, port: u16) -> Result<(), Self::Error> { + Ok(()) + } + + fn get_port(&self) -> Option { + None + } + + fn recv(&self, buf: &mut [u8]) -> Result<(usize, Self::Endpoint), Self::Error> { + Ok((0, "127.0.0.1:8080".parse().unwrap())) + } + + fn send(&self, buf: &[u8], dst: &Self::Endpoint) -> Result<(), Self::Error> { + Ok(()) + } +} + +#[derive(Debug)] +enum BindError {} + +impl Error for BindError { + fn description(&self) -> &str { + "Generic Bind Error" + } + + fn source(&self) -> Option<&(dyn Error + 'static)> { + None + } +} + +impl fmt::Display for BindError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Not Possible") + } +} #[derive(Debug, Clone)] struct PeerTimer { @@ -24,20 +73,23 @@ fn main() { // choose optimal crypto implementations for platform sodiumoxide::init().unwrap(); + { + let router = router::Device::new( + 4, + |t: &PeerTimer, data: bool, sent: bool| t.a.reset(Duration::from_millis(1000)), + |t: &PeerTimer, data: bool, sent: bool| t.b.reset(Duration::from_millis(1000)), + |t: &PeerTimer| println!("new key requested"), + ); - let router = router::Device::new( - 4, - |t: &PeerTimer, data: bool, sent: bool| t.a.reset(Duration::from_millis(1000)), - |t: &PeerTimer, data: bool, sent: bool| t.b.reset(Duration::from_millis(1000)), - |t: &PeerTimer| println!("new key requested"), - ); + let pt = PeerTimer { + a: runner.timer(|| println!("timer-a fired for peer")), + b: runner.timer(|| println!("timer-b fired for peer")), + }; - let pt = PeerTimer { - a: runner.timer(|| println!("timer-a fired for peer")), - b: runner.timer(|| println!("timer-b fired for peer")), - }; + let peer = router.new_peer(pt.clone()); - let peer = router.new_peer(pt.clone()); + println!("{:?}", pt); + } - println!("{:?}", pt); + println!("joined"); } -- cgit v1.2.3-59-g8ed1b