1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 16:02:59 +01:00
actix-extras/src/context.rs

255 lines
7.0 KiB
Rust
Raw Normal View History

2017-10-07 06:48:14 +02:00
use std;
use std::rc::Rc;
use std::collections::VecDeque;
use futures::{Async, Stream, Poll};
2017-10-21 02:16:17 +02:00
use futures::sync::oneshot::Sender;
2017-10-07 06:48:14 +02:00
use bytes::Bytes;
2017-10-21 02:16:17 +02:00
use actix::{Actor, ActorState, ActorContext, AsyncContext,
Handler, Subscriber, ResponseType};
2017-10-07 06:48:14 +02:00
use actix::fut::ActorFuture;
2017-10-21 02:16:17 +02:00
use actix::dev::{AsyncContextApi, ActorAddressCell, ActorItemsCell, ActorWaitCell, SpawnHandle,
Envelope, ToEnvelope, RemoteEnvelope};
2017-10-07 06:48:14 +02:00
use route::{Route, Frame};
2017-10-15 18:33:17 +02:00
use httpresponse::HttpResponse;
2017-10-07 06:48:14 +02:00
/// Actor execution context
pub struct HttpContext<A> where A: Actor<Context=HttpContext<A>> + Route,
{
2017-10-07 08:14:13 +02:00
act: Option<A>,
2017-10-07 06:48:14 +02:00
state: ActorState,
2017-10-22 00:21:16 +02:00
modified: bool,
2017-10-09 19:44:11 +02:00
items: ActorItemsCell<A>,
2017-10-07 06:48:14 +02:00
address: ActorAddressCell<A>,
stream: VecDeque<Frame>,
2017-10-17 23:44:00 +02:00
wait: ActorWaitCell<A>,
2017-10-07 06:48:14 +02:00
app_state: Rc<<A as Route>::State>,
}
impl<A> ActorContext<A> for HttpContext<A> where A: Actor<Context=Self> + Route
{
/// Stop actor execution
fn stop(&mut self) {
self.address.close();
if self.state == ActorState::Running {
self.state = ActorState::Stopping;
}
2017-10-14 01:33:23 +02:00
self.write_eof();
2017-10-07 06:48:14 +02:00
}
/// Terminate actor execution
fn terminate(&mut self) {
self.address.close();
2017-10-09 19:44:11 +02:00
self.items.close();
2017-10-07 06:48:14 +02:00
self.state = ActorState::Stopped;
}
/// Actor execution state
fn state(&self) -> ActorState {
self.state
}
}
2017-10-12 04:20:05 +02:00
impl<A> AsyncContext<A> for HttpContext<A> where A: Actor<Context=Self> + Route
2017-10-07 06:48:14 +02:00
{
2017-10-09 19:44:11 +02:00
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
2017-10-07 06:48:14 +02:00
where F: ActorFuture<Item=(), Error=(), Actor=A> + 'static
{
2017-10-22 00:21:16 +02:00
self.modified = true;
2017-10-09 19:44:11 +02:00
self.items.spawn(fut)
}
2017-10-12 04:20:05 +02:00
fn wait<F>(&mut self, fut: F)
where F: ActorFuture<Item=(), Error=(), Actor=A> + 'static
{
2017-10-22 00:21:16 +02:00
self.modified = true;
2017-10-17 23:44:00 +02:00
self.wait.add(fut);
2017-10-12 04:20:05 +02:00
}
2017-10-09 19:44:11 +02:00
fn cancel_future(&mut self, handle: SpawnHandle) -> bool {
2017-10-22 00:21:16 +02:00
self.modified = true;
2017-10-09 19:44:11 +02:00
self.items.cancel_future(handle)
2017-10-07 06:48:14 +02:00
}
}
2017-10-07 08:14:13 +02:00
#[doc(hidden)]
2017-10-07 06:48:14 +02:00
impl<A> AsyncContextApi<A> for HttpContext<A> where A: Actor<Context=Self> + Route {
fn address_cell(&mut self) -> &mut ActorAddressCell<A> {
&mut self.address
}
}
impl<A> HttpContext<A> where A: Actor<Context=Self> + Route {
2017-10-10 08:07:32 +02:00
pub fn new(state: Rc<<A as Route>::State>) -> HttpContext<A>
2017-10-07 06:48:14 +02:00
{
HttpContext {
2017-10-07 08:14:13 +02:00
act: None,
2017-10-07 06:48:14 +02:00
state: ActorState::Started,
2017-10-22 00:21:16 +02:00
modified: false,
2017-10-09 19:44:11 +02:00
items: ActorItemsCell::default(),
2017-10-07 09:22:09 +02:00
address: ActorAddressCell::default(),
2017-10-17 23:44:00 +02:00
wait: ActorWaitCell::default(),
2017-10-07 06:48:14 +02:00
stream: VecDeque::new(),
app_state: state,
}
}
2017-10-07 08:14:13 +02:00
pub(crate) fn set_actor(&mut self, act: A) {
self.act = Some(act)
2017-10-07 06:48:14 +02:00
}
}
impl<A> HttpContext<A> where A: Actor<Context=Self> + Route {
/// Shared application state
pub fn state(&self) -> &<A as Route>::State {
&self.app_state
}
/// Start response processing
pub fn start<R: Into<HttpResponse>>(&mut self, response: R) {
self.stream.push_back(Frame::Message(response.into()))
2017-10-07 06:48:14 +02:00
}
/// Write payload
2017-10-16 00:52:52 +02:00
pub fn write<B: Into<Bytes>>(&mut self, data: B) {
self.stream.push_back(Frame::Payload(Some(data.into())))
2017-10-07 06:48:14 +02:00
}
2017-10-07 08:14:13 +02:00
/// Indicate end of streamimng payload
2017-10-07 06:48:14 +02:00
pub fn write_eof(&mut self) {
self.stream.push_back(Frame::Payload(None))
}
}
2017-10-21 02:16:17 +02:00
impl<A> HttpContext<A> where A: Actor<Context=Self> + Route {
#[doc(hidden)]
2017-10-22 00:21:16 +02:00
pub fn subscriber<M>(&mut self) -> Box<Subscriber<M>>
where A: Handler<M>,
M: ResponseType + 'static,
2017-10-21 02:16:17 +02:00
{
Box::new(self.address.unsync_address())
}
#[doc(hidden)]
2017-10-22 00:21:16 +02:00
pub fn sync_subscriber<M>(&mut self) -> Box<Subscriber<M> + Send>
2017-10-21 02:16:17 +02:00
where A: Handler<M>,
2017-10-22 00:21:16 +02:00
M: ResponseType + Send + 'static,
M::Item: Send,
M::Error: Send,
2017-10-21 02:16:17 +02:00
{
Box::new(self.address.sync_address())
}
}
2017-10-07 08:14:13 +02:00
#[doc(hidden)]
2017-10-07 06:48:14 +02:00
impl<A> Stream for HttpContext<A> where A: Actor<Context=Self> + Route
{
type Item = Frame;
type Error = std::io::Error;
2017-10-17 23:44:00 +02:00
fn poll(&mut self) -> Poll<Option<Frame>, std::io::Error> {
2017-10-07 08:14:13 +02:00
if self.act.is_none() {
return Ok(Async::NotReady)
}
let act: &mut A = unsafe {
std::mem::transmute(self.act.as_mut().unwrap() as &mut A)
};
2017-10-07 06:48:14 +02:00
let ctx: &mut HttpContext<A> = unsafe {
std::mem::transmute(self as &mut HttpContext<A>)
};
// update state
match self.state {
ActorState::Started => {
2017-10-07 08:14:13 +02:00
Actor::started(act, ctx);
2017-10-07 06:48:14 +02:00
self.state = ActorState::Running;
},
ActorState::Stopping => {
2017-10-07 08:14:13 +02:00
Actor::stopping(act, ctx);
2017-10-07 06:48:14 +02:00
}
_ => ()
}
let mut prep_stop = false;
loop {
2017-10-22 00:21:16 +02:00
self.modified = false;
2017-10-07 06:48:14 +02:00
2017-10-21 02:16:17 +02:00
// check wait futures
if self.wait.poll(act, ctx) {
return Ok(Async::NotReady)
}
2017-10-22 00:21:16 +02:00
// incoming messages
self.address.poll(act, ctx);
// spawned futures and streams
self.items.poll(act, ctx);
2017-10-07 06:48:14 +02:00
// are we done
2017-10-22 00:21:16 +02:00
if self.modified {
2017-10-07 06:48:14 +02:00
continue
}
// get frame
if let Some(frame) = self.stream.pop_front() {
return Ok(Async::Ready(Some(frame)))
}
// check state
match self.state {
ActorState::Stopped => {
self.state = ActorState::Stopped;
2017-10-07 08:14:13 +02:00
Actor::stopped(act, ctx);
2017-10-07 06:48:14 +02:00
return Ok(Async::Ready(None))
},
ActorState::Stopping => {
if prep_stop {
if self.address.connected() || !self.items.is_empty() {
self.state = ActorState::Running;
continue
} else {
self.state = ActorState::Stopped;
2017-10-07 08:14:13 +02:00
Actor::stopped(act, ctx);
2017-10-07 06:48:14 +02:00
return Ok(Async::Ready(None))
}
} else {
2017-10-07 08:14:13 +02:00
Actor::stopping(act, ctx);
2017-10-07 06:48:14 +02:00
prep_stop = true;
continue
}
},
ActorState::Running => {
if !self.address.connected() && self.items.is_empty() {
self.state = ActorState::Stopping;
2017-10-07 08:14:13 +02:00
Actor::stopping(act, ctx);
2017-10-07 06:48:14 +02:00
prep_stop = true;
continue
}
},
_ => (),
}
return Ok(Async::NotReady)
}
}
}
2017-10-21 02:16:17 +02:00
impl<A, M> ToEnvelope<A, M> for HttpContext<A>
2017-10-22 00:21:16 +02:00
where A: Actor<Context=HttpContext<A>> + Route + Handler<M>,
M: ResponseType + Send + 'static,
M::Item: Send,
M::Error: Send,
2017-10-21 02:16:17 +02:00
{
2017-10-22 00:21:16 +02:00
fn pack(msg: M, tx: Option<Sender<Result<M::Item, M::Error>>>) -> Envelope<A>
2017-10-21 02:16:17 +02:00
{
RemoteEnvelope::new(msg, tx).into()
}
}