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
//! Application service API.

use futures::{self, Future};
use futures::sync::mpsc::{self, UnboundedReceiver};

use {Error, Request, Service};
use dispatch::StreamingDispatch;

/// Application service stream sender.
#[derive(Debug)]
pub struct Sender {
    inner: super::super::Sender,
}

impl Sender {
    /// Writes a chunk of data to the stream.
    pub fn write(&self, data: &str) {
        self.inner.send(Request::new(0, &[data]).unwrap());
    }

    /// Sends an error to the stream, closing it.
    pub fn error(self, id: u64, description: &str) {
        self.inner.send(Request::new(1, &((0, id), description)).unwrap());
    }

    /// Closes the stream.
    pub fn close(self) {}
}

impl Drop for Sender {
    fn drop(&mut self) {
        self.inner.send(Request::new(2, &[0; 0]).unwrap());
    }
}

/// Application service wrapper.
#[derive(Debug)]
pub struct App {
    service: Service,
}

impl App {
    /// Constructs an application service wrapper using the specified service.
    pub fn new(service: Service) -> Self {
        Self { service }
    }

    /// Enqueues an event, opening a bidirectional stream on success.
    pub fn enqueue(&self, event: &str) ->
        impl Future<Item = (Sender, UnboundedReceiver<Result<String, Error>>), Error = Error>
    {
        let (tx, rx) = mpsc::unbounded();

        let dispatch = StreamingDispatch::new(tx);

        self.service.call(Request::new(0, &[event]).unwrap(), dispatch)
            .and_then(|sender| {
                let sender = Sender { inner: sender };
                futures::future::ok((sender, rx))
            })
    }
}