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
use std::io;
use futures::{Stream, Poll, Async};
use tokio_proto::streaming::Body;

#[derive(Debug)]
pub struct ResponseStream {
    pub inner: Body<Message, io::Error>,
}

impl Stream for ResponseStream {          
    type Item = Message;
    type Error = io::Error;

    fn poll(&mut self) -> Poll<Option<Message>, io::Error> {
        match self.inner.poll().unwrap() {
            Async::Ready(Some(request)) => {
                Ok(Async::Ready(Some(request)))
            }
            Async::Ready(None) => {
                // the stream finished.
                Ok(Async::Ready(None))
            }
            Async::NotReady =>  {
                // no more messages to read
                Ok(Async::NotReady)
            }
        }        
    }
}

/*
impl<T> Sink for ResponseStream<T>
    where T: Sink<SinkItem = String, SinkError = io::Error>,
{
    type SinkItem = String;
    type SinkError = io::Error;

    fn start_send(&mut self, item: String) -> StartSend<String, io::Error> {
        self.upstream.start_send(item)
    }

    fn poll_complete(&mut self) -> Poll<(), io::Error> {
        self.upstream.poll_complete()
    }
}
*/

pub struct Message {
    pub timestamp: i64,
    pub message_id: String,
    pub message_body: String
}