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
use std::io;
use futures::sync::mpsc;
use futures::{Async, Future, IntoFuture, Poll, Sink, Stream};
use serde_json::Value;
use tokio::io::{AsyncRead, AsyncWrite};
use super::client::Client;
use super::message::Response as ResponseMessage;
use super::message::{Message, Notification, Request};
use super::transport::Transport;
pub trait Service: Send {
type T: Into<Value> + Send + 'static;
type E: Into<Value> + Send + 'static;
type RequestFuture: IntoStaticFuture<Item = Self::T, Error = Self::E>;
type NotificationFuture: IntoStaticFuture<Item = (), Error = ()>;
fn handle_request(&mut self, method: &str, params: Value) -> Self::RequestFuture;
fn handle_notification(&mut self, method: &str, params: Value) -> Self::NotificationFuture;
}
pub struct Server<S: Service + Send> {
service: S,
pending_responses: mpsc::UnboundedReceiver<(u64, Result<S::T, S::E>)>,
response_sender: mpsc::UnboundedSender<(u64, Result<S::T, S::E>)>,
}
unsafe impl<T: Service> Send for Server<T> {}
impl<S: Service> Server<S> {
pub fn new(service: S) -> Self {
let (tx, rx) = mpsc::unbounded();
Server {
service,
pending_responses: rx,
response_sender: tx,
}
}
pub fn send_responses<T: AsyncRead + AsyncWrite>(
&mut self,
sink: &mut Transport<T>,
) -> Poll<(), io::Error> {
trace!("Server: flushing responses");
while let Ok(poll) = self.pending_responses.poll() {
if let Async::Ready(Some((id, result))) = poll {
let msg = Message::Response(ResponseMessage {
id,
result: result.map(Into::into).map_err(Into::into),
});
sink.start_send(msg).unwrap();
} else {
if let Async::Ready(None) = poll {
panic!("we store the sender, it can't be dropped");
}
return sink.poll_complete();
}
}
panic!("an UnboundedReceiver should never give an error");
}
pub fn process_request(&mut self, request: Request) {
let Request { method, params, id } = request;
let response_sender = self.response_sender.clone();
let future = self
.service
.handle_request(method.as_str(), params)
.into_static_future()
.then(move |response| {
response_sender
.unbounded_send((id, response))
.map_err(|_| ())
});
let _ = tokio::spawn(future);
}
pub fn process_notification(&mut self, notification: Notification) {
let Notification { method, params } = notification;
let future = self.service.handle_notification(method.as_str(), params);
let _ = tokio::spawn(future.into_static_future());
}
}
pub trait ServiceBuilder {
type Service: Service;
fn build(self, client: Client) -> Self::Service;
}
pub trait IntoStaticFuture {
type Future: Future<Item = Self::Item, Error = Self::Error> + 'static + Send;
type Item;
type Error;
fn into_static_future(self) -> Self::Future;
}
impl<F: IntoFuture> IntoStaticFuture for F
where
<F as IntoFuture>::Future: 'static + Send,
{
type Future = <F as IntoFuture>::Future;
type Item = <F as IntoFuture>::Item;
type Error = <F as IntoFuture>::Error;
fn into_static_future(self) -> Self::Future {
self.into_future()
}
}