aorura_emu/
server.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use aorura::*;
use failure::*;

use std::convert::TryFrom;
use std::io::prelude::*;
use std::sync::Mutex;

pub struct Server {
    state: Mutex<State>,
}

impl Server {
    pub fn new() -> Self {
        Self {
            state: Mutex::new(Default::default()),
        }
    }

    pub fn get(&self) -> State {
        *self.state.lock().unwrap()
    }

    pub fn run<RW: Read + Write>(&self, master: &mut RW, is_writable: bool) -> Fallible<()> {
        let mut cmd: Command = Default::default();

        loop {
            master.read_exact(&mut cmd)?;

            let mut state = self.state.lock().unwrap();

            if cmd == STATUS_COMMAND {
                cmd = (*state).into();
                master.write(&cmd)?;
            } else {
                match State::try_from(&cmd) {
                    Ok(new_state) => {
                        if is_writable {
                            *state = new_state;
                        }
                        master.write(b"Y")?;
                    }
                    Err(err) => {
                        log::warn!("{}", err);
                        master.write(b"N")?;
                    }
                }
            }
        }
    }

    pub fn set(&self, state: State) -> () {
        *self.state.lock().unwrap() = state
    }
}