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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use std::collections::VecDeque;
use std::future::Future;
use std::pin::Pin;
use std::task;
use std::task::Poll;

use aktoro_channel::error::TrySendError;
use aktoro_channel::Notify;
use aktoro_raw as raw;
use aktoro_raw::Updater as RawUpdater;
use aktoro_raw::Wait as RawWait;
use futures_core::Stream;
use futures_io as io;
use futures_io::AsyncRead;
use futures_io::AsyncWrite;

use crate::channel;
use crate::channel::Receiver;
use crate::channel::Sender;
use crate::control;
use crate::control::Controlled;
use crate::control::Controller;
use crate::event::Event;
use crate::message::AsyncMessageFut;
use crate::message::AsyncMessageStream;
use crate::message::AsyncReadStream;
use crate::message::AsyncWriteFut;
use crate::update;
use crate::update::Update;
use crate::update::Updated;
use crate::update::Updater;

/// The configuration that is used by [`Context`].
///
/// ## Note
///
/// This is only used when spawning sub-actors and
/// shoudln't be used elsewhere.
pub struct ContextConfig {
    /// Whether the context should wait to get
    /// notified before starting to handle
    /// messages, events, etc.
    ready: Option<Notify>,
}

/// An actor context using the [`aktoro-channel`] crate.
///
/// [`aktoro-channel`]: https://docs.rs/aktoro-channel
pub struct Context<A: raw::Actor, R: raw::Runtime> {
    /// The identifier of the actor, as used by
    /// the runtime.
    actor_id: u64,
    /// Whether the context should wait to get
    /// notified before starting to handle
    /// messages, events, etc.
    ready: Option<Notify>,
    /// The actor's current status.
    status: A::Status,
    /// An actor's control channel sender.
    ctrler: Controller<A>,
    /// An actor's control channel receiver.
    ctrled: Controlled<A>,
    /// Whether the status has been recently updated
    /// and the runtime should be notified.
    update: bool,
    /// A list of futures that should be fully
    /// executed before handling messages, events,
    /// non-blocking futures, etc.
    b_futs: Vec<Pin<Box<dyn raw::AsyncMessageFut<Actor = A>>>>,
    /// A list of futures that the context should
    /// give the output to the actor as a message.
    futs: Vec<Pin<Box<dyn raw::AsyncMessageFut<Actor = A>>>>,
    /// A list of streams that the context should
    /// give the yielded items to the actor as
    /// messages.
    streams: Vec<Pin<Box<dyn raw::AsyncMessageStream<Actor = A>>>>,
    /// A list of asynchronous readers that the
    /// context should forward the data to the
    /// actor as messages.
    reads: Vec<Pin<Box<dyn raw::AsyncReadStream<Actor = A>>>>,
    /// An eventual inner runtime that the context
    /// can use to run/spawn sub-actors.
    rt: Option<R>,
    /// A list of contexts that should be notified
    /// when all blocking futures have been handled.
    to_notify: Vec<Notify>,
    /// A list of the actor's unhandled events.
    events: VecDeque<Box<dyn raw::Event<Actor = A>>>,
    /// An actor's message channel sender.
    sender: Sender<A>,
    /// An actor's message channel receiver.
    recver: Receiver<A>,
    /// An actor's update channel sender.
    updter: Updater<A>,
    /// An actor's update channel receiver.
    updted: Option<Updated<A>>,
}

impl<A, RT> raw::Context<A> for Context<A, RT>
where
    A: raw::Actor + 'static,
    RT: raw::Runtime,
{
    type Config = ContextConfig;

    type Controller = Controller<A>;
    type Sender = Sender<A>;
    type Updater = Updater<A>;

    fn new(actor_id: u64, config: ContextConfig) -> Context<A, RT> {
        // We create the actor's control, message and
        // update channels.
        let (ctrler, ctrled) = control::new();
        let (sender, recver) = channel::new();
        let (updter, updted) = update::new();

        Context {
            actor_id,
            ready: config.ready,
            status: A::Status::default(),
            ctrler,
            ctrled,
            update: false,
            b_futs: vec![],
            futs: vec![],
            streams: vec![],
            reads: vec![],
            rt: None,
            to_notify: vec![],
            events: VecDeque::new(),
            sender,
            recver,
            updter,
            updted: Some(updted),
        }
    }

    fn actor_id(&self) -> u64 {
        self.actor_id
    }

    fn emit<E>(&mut self, event: E)
    where
        A: raw::EventHandler<E>,
        E: Send + 'static,
    {
        self.events.push_back(Box::new(Event::new(event)));
    }

    fn status(&self) -> &A::Status {
        &self.status
    }

    fn set_status(&mut self, status: A::Status) {
        if self.status != status {
            self.status = status;
            self.update = true;
        }
    }

    fn update(&mut self) -> Result<(), TrySendError<Update<A>>> {
        self.update = false;
        self.updter
            .try_send(Update::new(self.actor_id, self.status.clone()))
    }

    fn controller(&self) -> &Controller<A> {
        &self.ctrler
    }

    fn sender(&self) -> &Sender<A> {
        &self.sender
    }

    fn updated_ref(&mut self) -> Option<&mut Updated<A>> {
        self.updted.as_mut()
    }

    fn updated(&mut self) -> Option<Updated<A>> {
        self.updted.take()
    }

    fn controlled(&mut self) -> &mut Controlled<A> {
        &mut self.ctrled
    }

    fn receiver(&mut self) -> &mut Receiver<A> {
        &mut self.recver
    }

    fn updater(&mut self) -> &mut Updater<A> {
        &mut self.updter
    }

    fn actors(&self) -> Vec<u64> {
        if let Some(rt) = &self.rt {
            rt.actors()
        } else {
            vec![]
        }
    }

    fn spawn<S, C>(&mut self, actor: S) -> Option<raw::Spawned<S>>
    where
        S: raw::Actor<Context = C> + 'static,
        C: raw::Context<S, Config = ContextConfig>,
    {
        let rt = if let Some(rt) = &mut self.rt {
            rt
        } else {
            self.rt = Some(RT::default());
            self.rt.as_mut().unwrap()
        };

        let mut config = ContextConfig::default();

        let (notify, ready) = Notify::new();
        config.ready = Some(ready);

        if let Some(spawned) = rt.spawn_with(actor, config) {
            self.to_notify.push(notify);
            Some(spawned)
        } else {
            None
        }
    }

    fn wait<F, M, O, T>(&mut self, fut: Pin<Box<F>>, map: M) -> raw::Cancellable<F>
    where
        F: Future<Output = O> + Unpin + Send + 'static,
        M: Fn(O) -> T + Unpin + Send + Sync + 'static,
        A: raw::Handler<T, Output = ()>,
        O: Send + 'static,
        T: Send + 'static,
    {
        let (cancellable, inner) = raw::Cancellable::new(fut);

        self.futs.push(Box::pin(AsyncMessageFut::new(inner, map)));

        cancellable
    }

    fn blocking_wait<F, M, O, T>(&mut self, fut: Pin<Box<F>>, map: M) -> raw::Cancellable<F>
    where
        F: Future<Output = O> + Unpin + Send + 'static,
        M: Fn(O) -> T + Unpin + Send + Sync + 'static,
        A: raw::Handler<T, Output = ()>,
        O: Send + 'static,
        T: Send + 'static,
    {
        let (cancellable, inner) = raw::Cancellable::new(fut);
        self.b_futs.push(Box::pin(AsyncMessageFut::new(inner, map)));

        cancellable
    }

    fn subscribe<S, M, I, T>(&mut self, stream: Pin<Box<S>>, map: M) -> raw::Cancellable<S>
    where
        S: Stream<Item = I> + Unpin + Send + 'static,
        M: Fn(I) -> T + Unpin + Send + Sync + 'static,
        A: raw::Handler<T, Output = ()>,
        I: Send + 'static,
        T: Send + 'static,
    {
        let (cancellable, inner) = raw::Cancellable::new(stream);

        self.streams
            .push(Box::pin(AsyncMessageStream::new(inner, map)));

        cancellable
    }

    fn read<R, M, N, T, E>(
        &mut self,
        read: Pin<Box<R>>,
        cap: usize,
        map: M,
        map_err: N,
    ) -> raw::Cancellable<R>
    where
        R: AsyncRead + Unpin + Send + 'static,
        M: Fn(Vec<u8>) -> T + Unpin + Send + Sync + 'static,
        N: Fn(io::Error) -> E + Unpin + Send + Sync + 'static,
        A: raw::Handler<T, Output = ()> + raw::Handler<E, Output = ()>,
        T: Send + 'static,
        E: Send + 'static,
    {
        let (cancellable, inner) = raw::Cancellable::new(read);

        self.reads
            .push(Box::pin(AsyncReadStream::new(inner, cap, map, map_err)));

        cancellable
    }

    fn write<W, M, N, T, E>(
        &mut self,
        write: Pin<Box<W>>,
        data: Vec<u8>,
        map: M,
        map_err: N,
    ) -> raw::Cancellable<W>
    where
        W: AsyncWrite + Unpin + Send + 'static,
        M: Fn((Vec<u8>, usize), Pin<Box<W>>) -> T + Unpin + Send + Sync + 'static,
        N: Fn(io::Error) -> E + Unpin + Send + Sync + 'static,
        A: raw::Handler<T, Output = ()> + raw::Handler<E, Output = ()>,
        T: Send + 'static,
        E: Send + 'static,
    {
        let (cancellable, inner) = raw::Cancellable::new(write);

        self.futs
            .push(Box::pin(AsyncWriteFut::new(inner, data, map, map_err)));

        cancellable
    }

    fn blocking_write<W, M, N, T, E>(
        &mut self,
        write: Pin<Box<W>>,
        data: Vec<u8>,
        map: M,
        map_err: N,
    ) -> raw::Cancellable<W>
    where
        W: AsyncWrite + Unpin + Send + 'static,
        M: Fn((Vec<u8>, usize), Pin<Box<W>>) -> T + Unpin + Send + Sync + 'static,
        N: Fn(io::Error) -> E + Unpin + Send + Sync + 'static,
        A: raw::Handler<T, Output = ()> + raw::Handler<E, Output = ()>,
        T: Send + 'static,
        E: Send + 'static,
    {
        let (cancellable, inner) = raw::Cancellable::new(write);

        self.b_futs
            .push(Box::pin(AsyncWriteFut::new(inner, data, map, map_err)));

        cancellable
    }
}

impl<A, R> Stream for Context<A, R>
where
    A: raw::Actor,
    R: raw::Runtime,
{
    type Item = raw::Work<A>;

    fn poll_next(self: Pin<&mut Self>, ctx: &mut task::Context) -> Poll<Option<raw::Work<A>>> {
        let context = self.get_mut();
        let mut ret = None;

        // If the context hasn't been marked as being
        // ready yet, we try to see if it should be now.
        if let Some(ready) = context.ready.as_mut() {
            match Pin::new(ready).poll(ctx) {
                Poll::Ready(()) => {
                    context.ready.take();
                }
                Poll::Pending => return Poll::Pending,
            }
        }

        // If an action has been received an action has
        // been received from the actor's control channel,
        // we ask the runtime to make the actor handle it.
        match Pin::new(&mut context.ctrled).poll_next(ctx) {
            Poll::Ready(Some(update)) => {
                return Poll::Ready(Some(raw::Work::Action(update)));
            }
            Poll::Ready(None) => return Poll::Ready(None),
            Poll::Pending => (),
        }

        // If the actor's status has been recently updated,
        // we notify the runtime.
        if context.update {
            context.update = false;
            return Poll::Ready(Some(raw::Work::Update));
        }

        // We try to poll all blocking futures until they
        // all finished executing, making the actor handle
        // the returned messages as they are yielded.
        let mut to_remove = vec![];
        for (i, fut) in context.b_futs.iter_mut().enumerate() {
            match fut.as_mut().poll(ctx) {
                Poll::Ready(Some(msg)) => {
                    to_remove.push(i);
                    ret = Some(raw::Work::Message(msg));

                    break;
                }
                Poll::Ready(None) => to_remove.push(i),
                Poll::Pending => (),
            }
        }

        // We remove the fully executed futures.
        for to_remove in to_remove {
            context.b_futs.remove(to_remove);
        }

        // We eventually return the output of the first
        // fully executed future...
        if let Some(ret) = ret {
            return Poll::Ready(Some(ret));
        // ...or we wait if all blocking futures havn't
        // fully executed.
        } else if !context.b_futs.is_empty() {
            return Poll::Pending;
        }

        // If there are no more blocking futures, we
        // notify the context of all the recently
        // spawned actors that they are ready.
        for to_notify in context.to_notify.drain(..) {
            to_notify.done();
        }

        // If the actor has unhandled events, we ask the
        // runtime to make the actor handle the oldest
        // one.
        if let Some(event) = context.events.pop_front() {
            return Poll::Ready(Some(raw::Work::Event(event)));
        }

        // If a message has been received from the actor's
        // message channel, we ask the runtime to make
        // the runtime handle it.
        match Pin::new(&mut context.recver).poll_next(ctx) {
            Poll::Ready(Some(msg)) => {
                return Poll::Ready(Some(raw::Work::Message(msg)));
            }
            Poll::Ready(None) => return Poll::Ready(None),
            Poll::Pending => (),
        }

        // We poll the inner runtime if there is one.
        if let Some(rt) = context.rt.take() {
            let mut wait = rt.wait();

            // We poll until there is either...
            loop {
                // ...no running actors or...
                if wait.runtime().actors().is_empty() {
                    break;
                }

                // ...the runtime is waiting for them
                // to yield.
                if let Poll::Pending = Pin::new(&mut wait).poll_next(ctx) {
                    break;
                }
            }

            context.rt = Some(wait.into_runtime());
        }

        // We poll all the futures that the context
        // was asked to handle.
        let mut to_remove = vec![];
        for (i, fut) in context.futs.iter_mut().enumerate() {
            match fut.as_mut().poll(ctx) {
                Poll::Ready(Some(msg)) => {
                    to_remove.push(i);
                    ret = Some(raw::Work::Message(msg));

                    break;
                }
                Poll::Ready(None) => to_remove.push(i),
                Poll::Pending => (),
            }
        }

        // We remove the fully executed futures...
        for to_remove in to_remove {
            context.futs.remove(to_remove);
        }

        // ...and return the output of the futures
        // that returned one.
        if let Some(ret) = ret {
            return Poll::Ready(Some(ret));
        }

        // We poll all the streams that the context
        // was asked to handle.
        let mut to_remove = vec![];
        for (i, stream) in context.streams.iter_mut().enumerate() {
            match stream.as_mut().poll_next(ctx) {
                Poll::Ready(Some(msg)) => ret = Some(raw::Work::Message(msg)),
                Poll::Ready(None) => to_remove.push(i),
                Poll::Pending => (),
            }
        }

        // We remove all the closed streams...
        for to_remove in to_remove {
            context.streams.remove(to_remove);
        }

        // ...and return the yielded items.
        if let Some(ret) = ret {
            return Poll::Ready(Some(ret));
        }

        // We poll all the asynchronous readers that
        // the context was asked to handle.
        let mut to_remove = vec![];
        for (i, read) in context.reads.iter_mut().enumerate() {
            match read.as_mut().poll_read(ctx) {
                Poll::Ready(Some(msg)) => ret = Some(raw::Work::Message(msg)),
                Poll::Ready(None) => to_remove.push(i),
                Poll::Pending => (),
            }
        }

        // We remove all the closed readers...
        for to_remove in to_remove {
            context.reads.remove(to_remove);
        }

        // ...and transfer the data read to the
        // actor.
        if let Some(ret) = ret {
            return Poll::Ready(Some(ret));
        }

        Poll::Pending
    }
}

impl Default for ContextConfig {
    fn default() -> Self {
        ContextConfig { ready: None }
    }
}

impl<A, R> Drop for Context<A, R>
where
    A: raw::Actor,
    R: raw::Runtime,
{
    fn drop(&mut self) {
        if let Some(rt) = &mut self.rt {
            rt.stop();
        }
    }
}