use std::sync::mpsc::{Receiver, Sender};
use std::time::Duration;
use crate::{Bytes, Event};
pub mod serial;
pub mod serialforwarder;
pub struct Transport {
handle: Option<TransportHandle>,
stopper: Box<dyn FnOnce() -> Result<(), &'static str>>,
}
pub struct TransportHandle {
pub tx: Sender<Event<Bytes>>,
pub rx: Receiver<Event<Bytes>>,
}
pub trait TransportBuilder {
fn start(&self) -> Transport;
fn set_reconnect_timeout(&mut self, timeout: Duration);
}
impl Transport {
pub fn new(tx: Sender<Event<Bytes>>, rx: Receiver<Event<Bytes>>) -> Transport {
Transport::with_stopper(tx, rx, Box::new(|| Ok(())))
}
pub fn with_stopper(
tx: Sender<Event<Bytes>>,
rx: Receiver<Event<Bytes>>,
stopper: Box<dyn FnOnce() -> Result<(), &'static str>>,
) -> Transport {
Transport {
handle: Some(TransportHandle { tx, rx }),
stopper,
}
}
pub fn get_handle(&mut self) -> TransportHandle {
self.handle.take().unwrap()
}
pub fn stop(self) -> Result<(), &'static str> {
(self.stopper)()
}
}