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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::io::prelude::*;
use std::io::{self, BufReader, BufWriter};
use std::net::{TcpStream, ToSocketAddrs};
use std::time::{Duration};
use std::marker::PhantomData;


/// Hook for LXI response parsing
pub trait LxiHook {
    type Output;
    fn read(stream: &mut BufReader<TcpStream>) -> io::Result<Self::Output>;
}

/// Defaut hook that reads only text ending with a newline
pub struct LxiTextHook {}

impl LxiHook for LxiTextHook {
    type Output = Vec<u8>;
    fn read(stream: &mut BufReader<TcpStream>) -> io::Result<Self::Output> {
        let mut buf = Vec::new();
        stream.read_until(b'\n', &mut buf)
        .and_then(|_num| {
            remove_newline(&mut buf);
            Ok(buf)
        })
    }
}

/// Abstract LXI device we can connect and read/write data
pub struct LxiDevice<H: LxiHook = LxiTextHook> {
    addr: (String, u16),
    stream: Option<LxiStream>,
    timeout: Option<Duration>,
    _ph: PhantomData<H>,
}

pub type LxiTextDevice = LxiDevice<LxiTextHook>;

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

impl<H: LxiHook> LxiDevice<H> {
    pub fn new(addr: (String, u16), timeout: Option<Duration>) -> Self {
        Self { addr, stream: None, timeout, _ph: PhantomData }
    }

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

    pub fn set_timeout(&mut self, timeout: Option<Duration>) -> io::Result<()> {
        self.timeout = timeout;
        match self.stream {
            Some(ref mut stream) => {
                stream.inp.get_mut().set_read_timeout(timeout)?;
                stream.out.get_mut().set_write_timeout(timeout)?;
            },
            None => (),
        }
        Ok(())
    }

    pub fn timeout(&self) -> Option<Duration> {
        self.timeout
    }

    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 = match self.timeout {
            Some(to) => {
                self.address().to_socket_addrs().and_then(|mut addrs| {
                    addrs.next().ok_or(io::ErrorKind::NotFound.into())
                }).and_then(|addr| {
                    TcpStream::connect_timeout(&addr, to)
                })
            },
            None => TcpStream::connect(self.address()),
        }?;

        let inp = BufReader::new(stream.try_clone()?);
        let out = BufWriter::new(stream);
        let mut stream = LxiStream { inp, out };
        stream.set_timeout(self.timeout)?;
        self.stream = Some(stream);

        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())
    }

    fn with_stream<R, F>(&mut self, mut f: F) -> io::Result<R>
    where F: FnMut(&mut LxiStream) -> io::Result<R> {
        self.stream.as_mut().ok_or(io::ErrorKind::NotConnected.into())
        .and_then(|stream| f(stream))
    }

    pub fn send(&mut self, data: &[u8]) -> io::Result<()> {
        self.with_stream(|stream| stream.send(data))
    }

    pub fn receive(&mut self) -> io::Result<H::Output> {
        self.with_stream(|stream| stream.receive::<H>())
    }

    pub fn send_timeout(&mut self, data: &[u8], timeout: Option<Duration>) -> io::Result<()> {
        self.with_stream(|stream| stream.send_timeout(data, timeout))
    }

    pub fn receive_timeout(&mut self, timeout: Option<Duration>) -> io::Result<H::Output> {
        self.with_stream(|stream| stream.receive_timeout::<H>(timeout))
    }
}

impl LxiStream {
    fn set_timeout(&mut self, timeout: Option<Duration>) -> io::Result<()> {
        self.inp.get_mut().set_read_timeout(timeout)?;
        self.out.get_mut().set_write_timeout(timeout)?;
        Ok(())
    }

    fn send(&mut self, data: &[u8]) -> io::Result<()> {
        self.out.write_all(data)
        .and_then(|()| self.out.write_all(b"\r\n"))
        .and_then(|()| self.out.flush())
    }

    fn receive<H: LxiHook>(&mut self) -> io::Result<H::Output> {
        H::read(&mut self.inp)
    }

    fn send_timeout(&mut self, data: &[u8], to: Option<Duration>) -> io::Result<()> {
        let dto = self.out.get_ref().write_timeout()?;
        self.out.get_mut().set_write_timeout(to)?;
        let res = self.send(data);
        self.out.get_mut().set_write_timeout(dto)?;
        res
    }

    fn receive_timeout<H: LxiHook>(&mut self, to: Option<Duration>) -> io::Result<H::Output> {
        let dto = self.inp.get_ref().read_timeout()?;
        self.inp.get_mut().set_read_timeout(to)?;
        let res = self.receive::<H>();
        self.inp.get_mut().set_read_timeout(dto)?;
        res
    }
}

fn remove_newline(text: &mut Vec<u8>) {
    match text.pop() {
        Some(b'\n') => match text.pop() {
            Some(b'\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 emulate() {
        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 = LxiTextDevice::new((String::from("localhost"), p), None);
            d.connect().unwrap();

            d.send(b"*IDN?").unwrap();
            assert_eq!(d.receive().unwrap(), Vec::from("Emulator"));
        }

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