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
use super::ring_byte_buffer::RingByteBuffer;
use super::prelude::*;
use std::io;
use std::time::{ Instant, Duration };

const DEFAULT_CHANNEL_READ_BUFFER_SIZE: usize = 1024;

/// Channel encode and decode message with protocol, and send and receive bytes via stream
/// 
/// # Examples
/// ```ignore
/// let mut channel = Channel::new(
///     RplidarProtocol::new(),
///     serial_port
/// );
/// 
/// channel.write(&Message::new(1)).unwrap();
/// ```
#[derive(Debug)]
pub struct Channel<P, T: ?Sized> {
    protocol: P,
    stream: Box<T>,
    read_buffer: RingByteBuffer,
}

impl<P, T: ?Sized> Channel<P, T>
where
    P: ProtocolDecoder + ProtocolEncoder,
    T: io::Read + io::Write
{
    /// Create a new `Channel` to read and write messages
    /// 
    /// # Example
    /// ```ignore
    /// let channel = Channel::new(
    ///     RplidarProtocol::new(),
    ///     serial_port
    /// );
    /// ```
    pub fn new(protocol: P, stream: Box<T>) -> Channel<P, T> {
        Channel::with_read_buffer_size(protocol, stream, DEFAULT_CHANNEL_READ_BUFFER_SIZE)
    }

    /// Create a new `Channel` with non-default ring buffer capacity
    /// 
    /// # Example
    /// ```ignore
    /// let channel = Channel::with_read_buffer_size(
    ///     RplidarProtocol::new(),
    ///     serial_port,
    ///     100000 as usize
    /// );
    /// ```
    pub fn with_read_buffer_size(
        protocol: P,
        stream: Box<T>,
        read_buffer_size: usize,
    ) -> Channel<P, T> {
        let mut chn = Channel {
            protocol: protocol,
            stream: stream,
            read_buffer: RingByteBuffer::with_capacity(read_buffer_size),
        };

        chn.reset();

        return chn;
    }

    /// Reset the channel status
    /// This function is usually used to reset protocol encoder and decoder when meet communication error
    /// 
    /// # Example
    /// ```ignore
    /// match channel.invoke(&Message::new(1), Duration::from_secs(1)) {
    ///     Ok(_) => {},
    ///     Err(_) => { channel.reset(); }
    /// }
    /// ```
    pub fn reset(&mut self) {
        self.protocol.reset_encoder();
        self.protocol.reset_decoder();
    }

    /// Read message from channel
    /// 
    /// # Example
    /// ```ignore
    /// if let Some(msg) = channel.read().unwrap() {
    ///     println!("{:?}", msg);
    /// }
    /// ```
    pub fn read(&mut self) -> Result<Option<Message>> {
        loop {
            self.read_buffer.read_from(&mut self.stream)?;

            let (decoded, msg) = self
                .protocol
                .decode(self.read_buffer.current_read_slice())?;
            self.read_buffer.skip_bytes(decoded);

            if decoded == 0 {
                return Ok(Option::None);
            }

            if let Some(_) = msg {
                return Ok(msg);
            }
        }
    }

    /// Read message until timeout
    /// 
    /// # Example
    /// ```ignore
    /// channel.read_until(Duration::from_secs(1));
    /// ```
    pub fn read_until(&mut self, timeout: Duration) -> Result<Option<Message>> {
        let start = Instant::now();

        while Instant::now() - start < timeout {
            if let Some(msg) = self.read()? {
                return Ok(Some(msg));
            }
        }

        return Err(Error::OperationFail("operation failed".to_owned()));
    }

    /// Write message to channel
    /// 
    /// # Example
    /// ```ignore
    /// channel.write(&Message::new(1)).unwrap();
    /// ```
    pub fn write(&mut self, msg:&Message) -> Result<usize> {
        let written = self.protocol.write_to(msg, &mut self.stream)?;
        self.stream.flush()?;
        return Ok(written);
    }

    /// Send a request to channel and wait for response
    /// 
    /// # Example
    /// ```ignore
    /// let resp = channel.invoke(&Message::new(1), Duration::from_secs(1));
    /// ```
    pub fn invoke(&mut self, request:&Message, timeout: Duration) -> Result<Option<Message>> {
        self.write(request)?;
        return self.read_until(timeout);
    }
    
}