Add initial driver implementation
This commit is contained in:
parent
267a99d6b9
commit
6bc88ef6b0
|
@ -1,4 +1,4 @@
|
|||
use crate::{TallyState,Channel};
|
||||
use crate::{TallyState, Channel};
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::{Sender, Receiver};
|
||||
use std::thread;
|
||||
|
@ -40,10 +40,9 @@ impl Adaptor for FakeAdaptor {
|
|||
fn run(&mut self) -> Receiver<TallyState> {
|
||||
println!("arse");
|
||||
let (tx, rx): (Sender<TallyState>, Receiver<TallyState>) = mpsc::channel();
|
||||
let thread_tx = tx.clone();
|
||||
let thread_numchannels = self.numchannels.clone();
|
||||
self.thread = Some(thread::spawn(move || {
|
||||
FakeAdaptor::_run(thread_numchannels, thread_tx);
|
||||
FakeAdaptor::_run(thread_numchannels, tx);
|
||||
}));
|
||||
return rx;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
use crate::TallyState;
|
||||
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::{Sender, Receiver};
|
||||
use std::thread;
|
||||
|
||||
/// A Driver displays the Tallies on a physical tally system.
|
||||
pub trait Driver {
|
||||
/// Run the driver, returning a channel sender which takes TallyStates
|
||||
fn run(&mut self) -> Sender<TallyState>;
|
||||
}
|
||||
|
||||
pub struct FakeDriver {
|
||||
thread: Option<thread::JoinHandle<()>>
|
||||
}
|
||||
|
||||
impl Driver for FakeDriver {
|
||||
fn run(&mut self) -> Sender<TallyState> {
|
||||
let (tx, rx): (Sender<TallyState>, Receiver<TallyState>) = mpsc::channel();
|
||||
self.thread = Some(thread::spawn(move || {
|
||||
FakeDriver::_run(rx);
|
||||
}));
|
||||
return tx;
|
||||
}
|
||||
}
|
||||
|
||||
impl FakeDriver {
|
||||
pub fn new() -> FakeDriver {
|
||||
FakeDriver{
|
||||
thread: None
|
||||
}
|
||||
}
|
||||
|
||||
fn _run(rx: Receiver<TallyState>) {
|
||||
loop {
|
||||
let state = rx.recv().unwrap();
|
||||
println!("{}", state);
|
||||
}
|
||||
}
|
||||
}
|
14
src/main.rs
14
src/main.rs
|
@ -1,9 +1,11 @@
|
|||
mod adaptors;
|
||||
mod drivers;
|
||||
use crate::adaptors::{Adaptor,FakeAdaptor};
|
||||
use crate::drivers::{Driver, FakeDriver};
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Channel(Option<u8>);
|
||||
pub struct Channel (Option<u8>);
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TallyState {
|
||||
|
@ -26,16 +28,17 @@ impl fmt::Display for TallyState {
|
|||
}
|
||||
}
|
||||
|
||||
struct Server<T:Adaptor> {
|
||||
struct Server<T:Adaptor,U:Driver> {
|
||||
adaptor: T,
|
||||
driver: U,
|
||||
}
|
||||
|
||||
impl<T:Adaptor> Server<T> {
|
||||
impl<T:Adaptor,U:Driver> Server<T,U> {
|
||||
fn run(&mut self) {
|
||||
let rx = self.adaptor.run();
|
||||
let tx = self.driver.run();
|
||||
loop{
|
||||
let newstate = rx.recv().unwrap();
|
||||
println!("{}", newstate);
|
||||
tx.send(rx.recv().unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +46,7 @@ impl<T:Adaptor> Server<T> {
|
|||
fn main() {
|
||||
let mut s = Server{
|
||||
adaptor: FakeAdaptor::new(8),
|
||||
driver: FakeDriver::new(),
|
||||
};
|
||||
|
||||
s.run();
|
||||
|
|
Loading…
Reference in New Issue