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
use super::*;
use std::net;

/// Contains the TCP connection to the FAH client, as well as its address for reconnecting.
#[derive(Debug)]
pub struct Connection {
    pub conn: net::TcpStream,
    pub addr: net::SocketAddr,
    pub connect_timeout: core::time::Duration,
}

impl Connection {
    pub fn connect_timeout(addr: &net::SocketAddr, timeout: core::time::Duration) -> Result<Self> {
        Ok(Self {
            conn: connect_timeout(addr, timeout)?,
            addr: *addr,
            connect_timeout: timeout,
        })
    }

    /// Executes a command on the FAH client. The response is written to the buffer.
    pub fn exec(&mut self, command: &str, buf: &mut Vec<u8>) -> Result<()> {
        use std::io::Write;

        if command == "" {
            // FAH doesn't respond to an empty command
            buf.clear();
            return Ok(());
        }

        if command.contains('\n') {
            return Err("command contains newline".into());
        }

        self.conn.write_all(format!("{}\n", command).as_bytes())?;

        if let Err(e) = read_message(&mut self.conn, buf) {
            // Try to reconnect on disconnection
            if *e.kind().to_string() == ErrorKind::EOF.to_string() {
                self.conn = connect_timeout(&self.addr, self.connect_timeout)?;
            }
            return Err(e);
        }

        Ok(())
    }

    /// Executes commands which do not return a trailing newline. (Some commands don't end their message
    /// and cause infinite blocking.) The response is written to the buffer.
    pub fn exec_eval(&mut self, command: &str, buf: &mut Vec<u8>) -> Result<()> {
        if command == "" {
            // FAH doesn't respond to an empty command
            buf.clear();
            return Ok(());
        }

        self.exec(format!(r#"eval "$({})\n""#, command).as_str(), buf)?;

        // When using eval with a newline, the response contains an extra trailing backslash.
        if let Some(b) = buf.last() {
            if *b == b'\\' {
                buf.pop();
            }
        }
        Ok(())
    }
}

fn connect_timeout(
    addr: &net::SocketAddr,
    timeout: core::time::Duration,
) -> Result<net::TcpStream> {
    let mut conn = net::TcpStream::connect_timeout(addr, timeout)?;

    // Discard welcome message
    read_message(&mut conn, &mut Vec::new())?;
    Ok(conn)
}

pub fn read_message(r: &mut impl std::io::Read, buf: &mut Vec<u8>) -> Result<()> {
    buf.clear();
    loop {
        let mut b: [u8; 1] = [0];
        if r.read(&mut b)? == 0 {
            // If we haven't reached END_OF_MESSAGE and 0 bytes was read, then EOF was returned
            return Err(ErrorKind::EOF.into());
        }

        buf.push(b[0]);

        const END_OF_MESSAGE: &str = "\n> ";
        if buf.len() >= END_OF_MESSAGE.len()
            && buf.as_slice()[buf.len() - END_OF_MESSAGE.len()..] == *END_OF_MESSAGE.as_bytes()
        {
            buf.truncate(buf.len() - END_OF_MESSAGE.len());
            if let Some(b) = buf.get(0) {
                if *b == b'\n' {
                    buf.drain(..1);
                }
            }
            return Ok(());
        }
    }
}

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

    #[test]
    fn test_read_message() {
        struct Test {
            s: &'static [u8],
            expected: &'static [u8],
            expect_error: bool,
        }

        let tests = vec![
            Test {
                s: b"",
                expected: b"",
                expect_error: true,
            },
            Test {
                s: b"\n",
                expected: b"\n",
                expect_error: true,
            },
            Test {
                s: b"\n> ",
                expected: b"",
                expect_error: false,
            },
            Test {
                s: b"a\n> ",
                expected: b"a",
                expect_error: false,
            },
            Test {
                s: b"a\n> \n> ",
                expected: b"a",
                expect_error: false,
            },
            Test {
                s: b"\na\n> ",
                expected: b"a",
                expect_error: false,
            },
        ];

        let mut buf: Vec<u8> = Vec::new();
        for (i, test) in tests.iter().enumerate() {
            use bytes::buf::ext::BufExt;
            let result = read_message(&mut bytes::Bytes::from_static(test.s).reader(), &mut buf);
            assert_eq!(result.is_err(), test.expect_error);
            assert_eq!(buf.as_slice(), test.expected, "{}", i);
        }
    }
}