2018-09-07 14:34:31 -07:00
|
|
|
use std::net;
|
2018-09-26 20:40:45 -07:00
|
|
|
use std::time::Duration;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
2018-12-09 21:51:35 -08:00
|
|
|
use actix_rt::spawn;
|
2018-12-09 10:15:49 -08:00
|
|
|
use actix_service::{NewService, Service};
|
2018-09-26 20:40:45 -07:00
|
|
|
use futures::future::{err, ok, FutureResult};
|
2018-09-08 09:36:38 -07:00
|
|
|
use futures::{Future, Poll};
|
2018-12-06 14:04:42 -08:00
|
|
|
use log::error;
|
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-11-03 09:09:14 -07:00
|
|
|
use super::Token;
|
2018-12-06 14:04:42 -08:00
|
|
|
use crate::counter::CounterGuard;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
/// Server message
|
2019-02-22 12:44:37 -08:00
|
|
|
pub(crate) enum ServerMessage {
|
2018-09-27 17:05:48 -07:00
|
|
|
/// New stream
|
2018-09-07 13:06:51 -07:00
|
|
|
Connect(net::TcpStream),
|
2018-09-27 17:05:48 -07:00
|
|
|
/// Gracefull shutdown
|
2018-09-26 20:40:45 -07:00
|
|
|
Shutdown(Duration),
|
2018-09-27 17:05:48 -07:00
|
|
|
/// Force shutdown
|
2018-09-07 13:06:51 -07:00
|
|
|
ForceShutdown,
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
pub trait StreamServiceFactory: Send + Clone + 'static {
|
2019-02-09 21:39:17 -08:00
|
|
|
type NewService: NewService<Request = TcpStream>;
|
2018-09-26 20:40:45 -07:00
|
|
|
|
|
|
|
fn create(&self) -> Self::NewService;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait InternalServiceFactory: Send {
|
2018-11-03 09:09:14 -07:00
|
|
|
fn name(&self, token: Token) -> &str;
|
2018-09-26 20:40:45 -07:00
|
|
|
|
|
|
|
fn clone_factory(&self) -> Box<InternalServiceFactory>;
|
|
|
|
|
2018-11-03 09:09:14 -07:00
|
|
|
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>>;
|
2018-09-26 20:40:45 -07:00
|
|
|
}
|
|
|
|
|
2018-08-19 10:47:04 -07:00
|
|
|
pub(crate) type BoxedServerService = Box<
|
|
|
|
Service<
|
2019-02-01 19:53:13 -08:00
|
|
|
Request = (Option<CounterGuard>, ServerMessage),
|
2018-08-19 10:47:04 -07:00
|
|
|
Response = (),
|
|
|
|
Error = (),
|
2018-09-26 20:40:45 -07:00
|
|
|
Future = FutureResult<(), ()>,
|
2018-08-19 10:47:04 -07:00
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
pub(crate) struct StreamService<T> {
|
2018-09-07 11:35:25 -07:00
|
|
|
service: T,
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
impl<T> StreamService<T> {
|
2018-11-03 09:09:14 -07:00
|
|
|
pub(crate) fn new(service: T) -> Self {
|
2018-09-26 20:40:45 -07:00
|
|
|
StreamService { service }
|
2018-09-07 11:35:25 -07:00
|
|
|
}
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
|
2019-02-01 19:53:13 -08:00
|
|
|
impl<T> Service for StreamService<T>
|
2018-08-19 10:47:04 -07:00
|
|
|
where
|
2019-02-09 21:39:17 -08:00
|
|
|
T: Service<Request = TcpStream>,
|
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
|
|
|
{
|
2019-02-01 19:53:13 -08:00
|
|
|
type Request = (Option<CounterGuard>, ServerMessage);
|
2018-08-19 10:47:04 -07:00
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
2018-09-26 20:40:45 -07:00
|
|
|
type Future = FutureResult<(), ()>;
|
2018-08-19 10:47:04 -07:00
|
|
|
|
|
|
|
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-26 20:40:45 -07:00
|
|
|
fn call(&mut self, (guard, req): (Option<CounterGuard>, ServerMessage)) -> Self::Future {
|
2018-09-07 13:06:51 -07:00
|
|
|
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-12-21 10:38:08 -08:00
|
|
|
spawn(self.service.call(stream).then(move |res| {
|
2018-09-26 20:40:45 -07:00
|
|
|
drop(guard);
|
2019-02-09 21:39:17 -08:00
|
|
|
res.map_err(|_| ()).map(|_| ())
|
2018-09-26 20:40:45 -07:00
|
|
|
}));
|
|
|
|
ok(())
|
2018-09-07 13:06:51 -07:00
|
|
|
} else {
|
2018-09-26 20:40:45 -07:00
|
|
|
err(())
|
2018-09-07 13:06:51 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-26 20:40:45 -07:00
|
|
|
_ => ok(()),
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
pub(crate) struct StreamNewService<F: StreamServiceFactory> {
|
|
|
|
name: String,
|
|
|
|
inner: F,
|
2018-11-03 09:09:14 -07:00
|
|
|
token: Token,
|
2018-09-26 20:40:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F> StreamNewService<F>
|
|
|
|
where
|
|
|
|
F: StreamServiceFactory,
|
|
|
|
{
|
2018-11-03 09:09:14 -07:00
|
|
|
pub(crate) fn create(name: String, token: Token, inner: F) -> Box<InternalServiceFactory> {
|
|
|
|
Box::new(Self { name, token, inner })
|
2018-09-26 20:40:45 -07:00
|
|
|
}
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
impl<F> InternalServiceFactory for StreamNewService<F>
|
2018-08-19 10:47:04 -07:00
|
|
|
where
|
2018-09-26 20:40:45 -07:00
|
|
|
F: StreamServiceFactory,
|
2018-08-19 10:47:04 -07:00
|
|
|
{
|
2018-11-03 09:09:14 -07:00
|
|
|
fn name(&self, _: Token) -> &str {
|
2018-09-17 20:19:48 -07:00
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
fn clone_factory(&self) -> Box<InternalServiceFactory> {
|
2018-08-19 10:47:04 -07:00
|
|
|
Box::new(Self {
|
2018-09-17 20:19:48 -07:00
|
|
|
name: self.name.clone(),
|
2018-08-19 10:47:04 -07:00
|
|
|
inner: self.inner.clone(),
|
2018-11-03 09:09:14 -07:00
|
|
|
token: self.token,
|
2018-08-19 10:47:04 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-03 09:09:14 -07:00
|
|
|
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
|
|
|
|
let token = self.token;
|
2018-09-28 12:22:49 -07:00
|
|
|
Box::new(
|
|
|
|
self.inner
|
|
|
|
.create()
|
2019-02-22 12:44:37 -08:00
|
|
|
.new_service(&())
|
2018-09-28 12:22:49 -07:00
|
|
|
.map_err(|_| ())
|
|
|
|
.map(move |inner| {
|
|
|
|
let service: BoxedServerService = Box::new(StreamService::new(inner));
|
2018-11-03 09:09:14 -07:00
|
|
|
vec![(token, service)]
|
2018-09-28 12:22:49 -07:00
|
|
|
}),
|
|
|
|
)
|
2018-08-19 10:47:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
impl InternalServiceFactory for Box<InternalServiceFactory> {
|
2018-11-03 09:09:14 -07:00
|
|
|
fn name(&self, token: Token) -> &str {
|
|
|
|
self.as_ref().name(token)
|
2018-09-17 20:19:48 -07:00
|
|
|
}
|
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
fn clone_factory(&self) -> Box<InternalServiceFactory> {
|
2018-08-19 10:47:04 -07:00
|
|
|
self.as_ref().clone_factory()
|
|
|
|
}
|
|
|
|
|
2018-11-03 09:09:14 -07:00
|
|
|
fn create(&self) -> Box<Future<Item = Vec<(Token, BoxedServerService)>, Error = ()>> {
|
2018-08-19 10:47:04 -07:00
|
|
|
self.as_ref().create()
|
|
|
|
}
|
|
|
|
}
|
2018-09-08 14:50:16 -07:00
|
|
|
|
2018-09-26 20:40:45 -07:00
|
|
|
impl<F, T> StreamServiceFactory for F
|
2018-09-08 14:50:16 -07:00
|
|
|
where
|
|
|
|
F: Fn() -> T + Send + Clone + 'static,
|
2019-02-09 21:39:17 -08:00
|
|
|
T: NewService<Request = TcpStream>,
|
2018-09-08 14:50:16 -07:00
|
|
|
{
|
|
|
|
type NewService = T;
|
|
|
|
|
|
|
|
fn create(&self) -> T {
|
|
|
|
(self)()
|
|
|
|
}
|
|
|
|
}
|