2018-10-06 01:00:44 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2018-12-10 06:51:35 +01:00
|
|
|
use actix_rt::spawn;
|
2018-12-09 19:15:49 +01:00
|
|
|
use actix_service::{IntoService, NewService, Service};
|
2018-09-01 19:29:56 +02:00
|
|
|
use futures::unsync::mpsc;
|
2018-10-06 01:00:44 +02:00
|
|
|
use futures::{future, Async, Future, Poll, Stream};
|
2018-09-01 19:29:56 +02:00
|
|
|
|
2019-01-05 22:19:06 +01:00
|
|
|
pub struct StreamDispatcher<S, T>
|
|
|
|
where
|
|
|
|
S: Stream,
|
|
|
|
T: Service<Result<S::Item, S::Error>>,
|
|
|
|
{
|
2018-09-01 19:29:56 +02:00
|
|
|
stream: S,
|
|
|
|
service: T,
|
|
|
|
item: Option<Result<S::Item, S::Error>>,
|
2019-01-05 22:19:06 +01:00
|
|
|
stop_rx: mpsc::UnboundedReceiver<T::Error>,
|
|
|
|
stop_tx: mpsc::UnboundedSender<T::Error>,
|
2018-09-01 19:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, T> StreamDispatcher<S, T>
|
|
|
|
where
|
|
|
|
S: Stream,
|
2019-01-05 22:19:06 +01:00
|
|
|
T: Service<Result<S::Item, S::Error>, Response = ()>,
|
2018-09-01 19:29:56 +02:00
|
|
|
T::Future: 'static,
|
|
|
|
{
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<F>(stream: S, service: F) -> Self
|
|
|
|
where
|
|
|
|
F: IntoService<T, Result<S::Item, S::Error>>,
|
|
|
|
{
|
2018-09-01 19:29:56 +02:00
|
|
|
let (stop_tx, stop_rx) = mpsc::unbounded();
|
|
|
|
StreamDispatcher {
|
|
|
|
stream,
|
|
|
|
item: None,
|
2018-09-04 18:49:21 +02:00
|
|
|
service: service.into_service(),
|
2018-09-01 19:29:56 +02:00
|
|
|
stop_rx,
|
|
|
|
stop_tx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, T> Future for StreamDispatcher<S, T>
|
|
|
|
where
|
|
|
|
S: Stream,
|
2019-01-05 22:19:06 +01:00
|
|
|
T: Service<Result<S::Item, S::Error>, Response = ()>,
|
2018-09-01 19:29:56 +02:00
|
|
|
T::Future: 'static,
|
|
|
|
{
|
|
|
|
type Item = ();
|
2019-01-05 22:19:06 +01:00
|
|
|
type Error = T::Error;
|
2018-09-01 19:29:56 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-01-05 22:19:06 +01:00
|
|
|
if let Ok(Async::Ready(Some(e))) = self.stop_rx.poll() {
|
|
|
|
return Err(e);
|
2018-09-01 19:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut item = self.item.take();
|
|
|
|
loop {
|
|
|
|
if item.is_some() {
|
|
|
|
match self.service.poll_ready()? {
|
|
|
|
Async::Ready(_) => spawn(StreamDispatcherService {
|
|
|
|
fut: self.service.call(item.take().unwrap()),
|
|
|
|
stop: self.stop_tx.clone(),
|
|
|
|
}),
|
|
|
|
Async::NotReady => {
|
|
|
|
self.item = item;
|
|
|
|
return Ok(Async::NotReady);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match self.stream.poll() {
|
|
|
|
Ok(Async::Ready(Some(el))) => item = Some(Ok(el)),
|
|
|
|
Err(err) => item = Some(Err(err)),
|
|
|
|
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(None)) => return Ok(Async::Ready(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamDispatcherService<F: Future> {
|
|
|
|
fut: F,
|
2019-01-05 22:19:06 +01:00
|
|
|
stop: mpsc::UnboundedSender<F::Error>,
|
2018-09-01 19:29:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Future> Future for StreamDispatcherService<F> {
|
|
|
|
type Item = ();
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll() {
|
|
|
|
Ok(Async::Ready(_)) => Ok(Async::Ready(())),
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
2019-01-05 22:19:06 +01:00
|
|
|
Err(e) => {
|
|
|
|
let _ = self.stop.unbounded_send(e);
|
2018-09-01 19:29:56 +02:00
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-06 01:00:44 +02:00
|
|
|
|
|
|
|
/// `NewService` that implements, read one item from the stream.
|
|
|
|
pub struct TakeItem<T> {
|
|
|
|
_t: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> TakeItem<T> {
|
|
|
|
/// Create new `TakeRequest` instance.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
TakeItem { _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 23:04:42 +01:00
|
|
|
impl<T> Default for TakeItem<T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
TakeItem { _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 19:03:03 +01:00
|
|
|
impl<T> Clone for TakeItem<T> {
|
|
|
|
fn clone(&self) -> TakeItem<T> {
|
|
|
|
TakeItem { _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T: Stream> NewService<T> for TakeItem<T> {
|
2018-10-06 01:00:44 +02:00
|
|
|
type Response = (Option<T::Item>, T);
|
|
|
|
type Error = T::Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Service = TakeItemService<T>;
|
|
|
|
type Future = future::FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
future::ok(TakeItemService { _t: PhantomData })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `NewService` that implements, read one request from framed object feature.
|
|
|
|
pub struct TakeItemService<T> {
|
|
|
|
_t: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
2018-11-01 19:03:03 +01:00
|
|
|
impl<T> Clone for TakeItemService<T> {
|
|
|
|
fn clone(&self) -> TakeItemService<T> {
|
|
|
|
TakeItemService { _t: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T: Stream> Service<T> for TakeItemService<T> {
|
2018-10-06 01:00:44 +02:00
|
|
|
type Response = (Option<T::Item>, T);
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = TakeItemServiceResponse<T>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, req: T) -> Self::Future {
|
2018-10-06 01:00:44 +02:00
|
|
|
TakeItemServiceResponse { stream: Some(req) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct TakeItemServiceResponse<T: Stream> {
|
|
|
|
stream: Option<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Stream> Future for TakeItemServiceResponse<T> {
|
|
|
|
type Item = (Option<T::Item>, T);
|
|
|
|
type Error = T::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.stream.as_mut().expect("Use after finish").poll()? {
|
|
|
|
Async::Ready(item) => Ok(Async::Ready((item, self.stream.take().unwrap()))),
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|