2018-09-07 14:34:31 -07:00
|
|
|
use std::net;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
2018-09-07 13:06:51 -07:00
|
|
|
use futures::future::{err, ok};
|
2018-09-08 09:36:38 -07:00
|
|
|
use futures::{Future, Poll};
|
2018-08-19 10:47:04 -07:00
|
|
|
use tokio_reactor::Handle;
|
|
|
|
use tokio_tcp::TcpStream;
|
2018-08-21 17:08:23 -07:00
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
use super::{NewService, Service};
|
2018-08-19 10:47:04 -07:00
|
|
|
|
2018-09-07 13:06:51 -07:00
|
|
|
pub enum ServerMessage {
|
|
|
|
Connect(net::TcpStream),
|
|
|
|
Shutdown,
|
|
|
|
ForceShutdown,
|
|
|
|
}
|
|
|
|
|
2018-08-19 10:47:04 -07:00
|
|
|
pub(crate) type BoxedServerService = Box<
|
|
|
|
Service<
|
2018-09-07 13:06:51 -07:00
|
|
|
Request = ServerMessage,
|
2018-08-19 10:47:04 -07:00
|
|
|
Response = (),
|
|
|
|
Error = (),
|
|
|
|
Future = Box<Future<Item = (), Error = ()>>,
|
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
pub(crate) struct ServerService<T> {
|
2018-09-07 11:35:25 -07:00
|
|
|
service: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ServerService<T> {
|
|
|
|
fn new(service: T) -> Self {
|
2018-09-08 09:36:38 -07:00
|
|
|
ServerService { service }
|
2018-09-07 11:35:25 -07:00
|
|
|
}
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Service for ServerService<T>
|
|
|
|
where
|
2018-09-07 14:49:17 -07:00
|
|
|
T: Service<Request = TcpStream, Response = (), Error = ()>,
|
2018-08-19 10:47:04 -07:00
|
|
|
T::Future: 'static,
|
2018-09-07 14:34:31 -07:00
|
|
|
T::Error: 'static,
|
2018-08-19 10:47:04 -07:00
|
|
|
{
|
2018-09-07 13:06:51 -07:00
|
|
|
type Request = ServerMessage;
|
2018-08-19 10:47:04 -07:00
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
|
|
|
type Future = Box<Future<Item = (), Error = ()>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-09-08 09:36:38 -07:00
|
|
|
self.service.poll_ready().map_err(|_| ())
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
|
2018-09-07 13:06:51 -07:00
|
|
|
fn call(&mut self, req: ServerMessage) -> Self::Future {
|
|
|
|
match req {
|
|
|
|
ServerMessage::Connect(stream) => {
|
|
|
|
let stream = TcpStream::from_std(stream, &Handle::default()).map_err(|e| {
|
|
|
|
error!("Can not convert to an async tcp stream: {}", e);
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Ok(stream) = stream {
|
2018-09-08 09:36:38 -07:00
|
|
|
Box::new(self.service.call(stream))
|
2018-09-07 13:06:51 -07:00
|
|
|
} else {
|
|
|
|
Box::new(err(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Box::new(ok(())),
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
pub(crate) struct ServerNewService<F, T>
|
|
|
|
where
|
|
|
|
F: Fn() -> T + Send + Clone,
|
|
|
|
{
|
2018-08-23 13:16:14 -07:00
|
|
|
inner: F,
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
impl<F, T> ServerNewService<F, T>
|
2018-08-19 10:47:04 -07:00
|
|
|
where
|
2018-08-23 13:16:14 -07:00
|
|
|
F: Fn() -> T + Send + Clone + 'static,
|
2018-09-07 14:49:17 -07:00
|
|
|
T: NewService<Request = TcpStream, Response = (), Error = (), InitError = ()> + 'static,
|
2018-08-19 10:47:04 -07:00
|
|
|
T::Service: 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
{
|
2018-08-23 15:42:34 -07:00
|
|
|
pub(crate) fn create(inner: F) -> Box<ServerServiceFactory + Send> {
|
2018-09-07 11:35:25 -07:00
|
|
|
Box::new(Self { inner })
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
pub trait ServerServiceFactory {
|
|
|
|
fn clone_factory(&self) -> Box<ServerServiceFactory + Send>;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
|
|
|
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>>;
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
impl<F, T> ServerServiceFactory for ServerNewService<F, T>
|
2018-08-19 10:47:04 -07:00
|
|
|
where
|
2018-08-23 13:16:14 -07:00
|
|
|
F: Fn() -> T + Send + Clone + 'static,
|
2018-09-07 14:49:17 -07:00
|
|
|
T: NewService<Request = TcpStream, Response = (), Error = (), InitError = ()> + 'static,
|
2018-08-19 10:47:04 -07:00
|
|
|
T::Service: 'static,
|
|
|
|
T::Future: 'static,
|
|
|
|
{
|
2018-08-23 15:42:34 -07:00
|
|
|
fn clone_factory(&self) -> Box<ServerServiceFactory + Send> {
|
2018-08-19 10:47:04 -07:00
|
|
|
Box::new(Self {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
|
2018-09-07 13:06:51 -07:00
|
|
|
Box::new((self.inner)().new_service().map(move |inner| {
|
|
|
|
let service: BoxedServerService = Box::new(ServerService::new(inner));
|
|
|
|
service
|
|
|
|
}))
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:42:34 -07:00
|
|
|
impl ServerServiceFactory for Box<ServerServiceFactory> {
|
|
|
|
fn clone_factory(&self) -> Box<ServerServiceFactory + Send> {
|
2018-08-19 10:47:04 -07:00
|
|
|
self.as_ref().clone_factory()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create(&self) -> Box<Future<Item = BoxedServerService, Error = ()>> {
|
|
|
|
self.as_ref().create()
|
|
|
|
}
|
|
|
|
}
|