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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
use std::marker::PhantomData; use futures::{Async, Future, Poll}; use futures::sync::mpsc::UnboundedSender; use futures::sync::oneshot::{channel, Canceled, Receiver, Sender}; use fut::ActorFuture; use actor::{Actor, MessageHandler}; use address::{Subscriber, AsyncSubscriber, MessageProxy, BoxedMessageProxy}; use context::Context; use message::MessageFuture; pub struct SyncAddress<A> where A: Actor { tx: UnboundedSender<BoxedMessageProxy<A>> } impl<A> Clone for SyncAddress<A> where A: Actor { fn clone(&self) -> Self { SyncAddress{tx: self.tx.clone()} } } impl<A> SyncAddress<A> where A: Actor { pub(crate) fn new(sender: UnboundedSender<BoxedMessageProxy<A>>) -> SyncAddress<A> { SyncAddress{tx: sender} } pub fn send<M: 'static + Send>(&self, msg: M) where A: MessageHandler<M>, A::Item: Send, A::Error: Send, { let _ = self.tx.unbounded_send( BoxedMessageProxy(Box::new(SyncEnvelope::new(Some(msg), None)))); } pub fn call<B: Actor, M: 'static + Send>(&self, msg: M) -> MessageResult<A, B, M> where A: MessageHandler<M>, A::Item: Send, A::Error: Send, { let (tx, rx) = channel(); let env = SyncEnvelope::new(Some(msg), Some(tx)); let _ = self.tx.unbounded_send(BoxedMessageProxy(Box::new(env))); MessageResult::new(rx) } pub fn subscriber<M: 'static + Send>(&self) -> Box<Subscriber<M>> where A: MessageHandler<M>, A::Item: Send, A::Error: Send, { Box::new(self.clone()) } pub fn async_subscriber<M>(&self) -> Box<AsyncSubscriber<M, Future=CallResult<A::Item, A::Error>>> where A: MessageHandler<M>, A::Item: Send, A::Error: Send, M: 'static + Send, { Box::new(self.clone()) } } impl<A, M> Subscriber<M> for SyncAddress<A> where M: 'static + Send, A::Item: Send, A::Error: Send, A: Actor + MessageHandler<M> { fn send(&self, msg: M) { self.send(msg) } fn unbuffered_send(&self, msg: M) -> Result<(), M> { self.send(msg); Ok(()) } } impl<A, M> AsyncSubscriber<M> for SyncAddress<A> where M: 'static + Send, A: Actor + MessageHandler<M>, A::Item: Send, A::Error: Send, { type Future = CallResult<A::Item, A::Error>; fn call(&self, msg: M) -> Self::Future { let (tx, rx) = channel(); let env = SyncEnvelope::new(Some(msg), Some(tx)); let _ = self.tx.unbounded_send(BoxedMessageProxy(Box::new(env))); CallResult::new(rx) } fn unbuffered_call(&self, msg: M) -> Result<Self::Future, M> { Ok(AsyncSubscriber::call(self, msg)) } } struct SyncEnvelope<A, M> where A: Actor + MessageHandler<M> { msg: Option<M>, act: PhantomData<A>, tx: Option<Sender<Result<A::Item, A::Error>>>, } impl<A, M> SyncEnvelope<A, M> where A: Actor + MessageHandler<M> { fn new(msg: Option<M>, tx: Option<Sender<Result<A::Item, A::Error>>>) -> SyncEnvelope<A, M> { SyncEnvelope{msg: msg, tx: tx, act: PhantomData} } } impl<A, M> MessageProxy for SyncEnvelope<A, M> where M: 'static, A: Actor + MessageHandler<M>, { type Actor = A; fn handle(&mut self, act: &mut Self::Actor, ctx: &mut Context<A>) { if let Some(msg) = self.msg.take() { let fut = <Self::Actor as MessageHandler<M>>::handle(act, msg, ctx); let f: EnvelopFuture<Self::Actor, _> = EnvelopFuture {msg: PhantomData, fut: fut, tx: self.tx.take()}; ctx.spawn(f); } } } struct EnvelopFuture<A, M> where A: Actor + MessageHandler<M> { msg: PhantomData<M>, fut: MessageFuture<A, M>, tx: Option<Sender<Result<A::Item, A::Error>>>, } impl<A, M> ActorFuture for EnvelopFuture<A, M> where A: Actor + MessageHandler<M> { type Item = (); type Error = (); type Actor = A; fn poll(&mut self, act: &mut A, ctx: &mut Context<A>) -> Poll<Self::Item, Self::Error> { match self.fut.poll(act, ctx) { Ok(Async::Ready(val)) => { if let Some(tx) = self.tx.take() { let _ = tx.send(Ok(val)); } Ok(Async::Ready(())) }, Ok(Async::NotReady) => Ok(Async::NotReady), Err(err) => { if let Some(tx) = self.tx.take() { let _ = tx.send(Err(err)); } Err(()) } } } } #[must_use = "future do nothing unless polled"] pub struct MessageResult<A, B, M> where A: MessageHandler<M>, B: Actor, { rx: Receiver<Result<A::Item, A::Error>>, act: PhantomData<B>, } impl<A, B, M> MessageResult<A, B, M> where B: Actor, A: MessageHandler<M> { pub(crate) fn new(rx: Receiver<Result<A::Item, A::Error>>) -> MessageResult<A, B, M> { MessageResult{rx: rx, act: PhantomData} } } impl<A, B, M> ActorFuture for MessageResult<A, B, M> where B: Actor, A: MessageHandler<M> { type Item = Result<A::Item, A::Error>; type Error = Canceled; type Actor = A; fn poll(&mut self, _: &mut A, _: &mut Context<A>) -> Poll<Self::Item, Self::Error> { self.rx.poll() } } #[must_use = "future do nothing unless polled"] pub struct CallResult<I, E> { rx: Receiver<Result<I, E>>, } impl<I, E> CallResult<I, E> { fn new(rx: Receiver<Result<I, E>>) -> CallResult<I, E> { CallResult{rx: rx} } } impl<I, E> Future for CallResult<I, E> { type Item = Result<I, E>; type Error = Canceled; fn poll(&mut self) -> Poll<Self::Item, Self::Error> { self.rx.poll() } }