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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::io::prelude::*;
use std::io::{self, BufReader, BufWriter};
use std::net::{TcpStream};

pub struct LxiDevice {
    addr: (String, u16),
    stream: Option<LxiStream>,
}

struct LxiStream {
    inp: BufReader<TcpStream>,
    out: BufWriter<TcpStream>,
}

impl LxiDevice {
    pub fn new(addr: (String, u16)) -> Self {
        Self { addr, stream: None }
    }

    pub fn address(&self) -> (&str, u16) {
        (self.addr.0.as_str(), self.addr.1)
    }

    pub fn is_connected(&self) -> bool {
        self.stream.is_some()
    }

    pub fn connect(&mut self) -> io::Result<()> {
        if self.is_connected() {
            return Err(io::ErrorKind::AlreadyExists.into())
        }
        let stream = TcpStream::connect(self.address())?;
        let inp = BufReader::new(stream.try_clone()?);
        let out = BufWriter::new(stream);
        self.stream = Some(LxiStream { inp, out });
        Ok(())
    }

    pub fn disconnect(&mut self) -> io::Result<()> {
        if !self.is_connected() {
            return Err(io::ErrorKind::NotConnected.into())
        }
        self.stream = None;
        Ok(())
    }

    pub fn reconnect(&mut self) -> io::Result<()> {
        self.disconnect()
        .and_then(|()| self.connect())
    }

    pub fn send(&mut self, text: &str) -> io::Result<()> {
        self.stream.as_mut().ok_or(io::ErrorKind::NotConnected.into())
        .and_then(|stream| stream.send(text))
    }

    pub fn receive(&mut self) -> io::Result<String> {
        self.stream.as_mut().ok_or(io::ErrorKind::NotConnected.into())
        .and_then(|stream| stream.receive())
    }

    pub fn request(&mut self, text: &str) -> io::Result<String> {
        self.stream.as_mut().ok_or(io::ErrorKind::NotConnected.into())
        .and_then(|stream| stream.request(text))
    }
}

impl LxiStream {
    fn send(&mut self, text: &str) -> io::Result<()> {
        self.out.write_all(text.as_bytes())
        .and_then(|()| self.out.write_all(b"\r\n"))
        .and_then(|()| self.out.flush())
    }

    fn receive(&mut self) -> io::Result<String> {
        let mut buf = Vec::new();
        self.inp.read_until(b'\n', &mut buf)
        .and_then(|_num| {
            let mut text = String::from_utf8_lossy(&buf).into_owned();
            remove_newline(&mut text);
            Ok(text)
        })
    }

    fn request(&mut self, text: &str) -> io::Result<String> {
        self.send(text)
        .and_then(|()| self.receive())
    }
}

fn remove_newline(text: &mut String) {
    match text.pop() {
        Some('\n') => match text.pop() {
            Some('\r') => (),
            Some(c) => text.push(c),
            None => (),
        },
        Some(c) => text.push(c),
        None => (),
    }
}

#[cfg(test)]
mod emul;

#[cfg(test)]
mod tests {
    use super::*;

    use std::thread;
    use std::time::{Duration};

    use emul::{Emulator};

    #[test]
    fn idn() {
        let e = Emulator::new(("localhost", 0)).unwrap();
        let p = e.address().unwrap().port();
        let e = e.run();

        thread::sleep(Duration::from_millis(100));

        {
            let mut d = LxiDevice::new((String::from("localhost"), p));
            d.connect().unwrap();
            assert_eq!(d.request(&"*IDN?").unwrap(), "Emulator");
        }

        e.join().unwrap().unwrap();
    }
}