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
//! delayed Execution Routine and Events
//!
//! Once combined with repeation, many
//! useful variants obtained:
//! - Delayed Deliver (original)
//! - Instant Deliver
//! - Repeat Deliver
//! - Loop Deliver

#[cfg(not(feature = "log"))]
use crate::log;
#[cfg(feature = "time")]
use crate::time::Timing;
use crate::{
    actor::{Actor, Future, Handle},
    context::Context,
    message::Message,
    reactor::{inc_poll_budget, pending_polled},
};
use alloc::boxed::Box;
#[cfg(feature = "time")]
use core::time::Duration;
use core::{
    any::Any,
    pin::Pin,
    ptr,
    task::{Context as CoreContext, Poll},
};

/// send a delayed message with condition
/// and get executed once the condition
/// satisfied.
///
pub struct Delayer<A: Actor> {
    inner: Option<A::Message>,
    /// `delay` serve as a indicator to show
    /// whether the message should be delivered or not
    /// it return a optional boolean to notify the `Delayer`
    /// that to repeat deliver the message or not
    /// - Some(1) : Deliver the message only ONCE
    /// - None : Repeatedly deliver the message without end
    /// - Some(number) : Deliver the message with `number` times
    //delay: Pin<Box<dyn Future<A, Output = Option<usize>>>>,
    delay: DelayIndicator<A>,
    handle: Handle,
    state: DelayerState,
}

/// Indicator to direct the delayer
/// at runtime,
/// - `Continue`: just continue the execution, no
///   intercept happens
/// - `Abort`: intercept the signal and
///   stop blocking immediately
/// - `Emit`: emit the signal notify actor
///   and execute immediately
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum DelayingState {
    Abort,
    Continue,
    Emit,
}

/// Delayer State
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum DelayerState {
    Created,
    Started,
    Running,
    Aborted,
    Emitted,
    Finished,
}

/// the return type
/// when some condition satisfied
pub enum Indicator<M> {
    /// the return type
    /// is message
    Message(M),
    /// the return type
    /// is the indicator of
    /// message Repeation
    Repeation(Option<usize>),
}

pub(crate) struct DelayIndicator<A: Actor> {
    // make sure cx must be woken
    // if some progress is made
    inner: Pin<Box<dyn Future<A, Output = Indicator<A::Message>>>>,
    repeated: usize,
}

impl<A: Actor> DelayIndicator<A> {
    pub(crate) fn inc(&mut self) {
        self.repeated += 1;
    }

    pub(crate) fn new<F>(f: F) -> Self
    where
        F: FnMut(&mut A, &mut Context<A>, &mut CoreContext<'_>) -> Poll<Indicator<A::Message>>
            + 'static
            + Unpin,
    {
        Self {
            inner: Box::pin(f),
            repeated: 0,
        }
    }

    pub(crate) fn repeated(&self) -> usize {
        self.repeated
    }
}

/// An abstraction for Actor's delaying routine
///
pub trait Delaying<A: Actor> {
    /// called before the Delayer
    fn started(&mut self, _: &mut Context<A>) {}

    /// change the state of the Delayer to
    /// abort/emit/continue the Delayer when
    /// some condition satisfied
    ///
    /// Real-Time control or more elaborated
    /// execution could be achieved right here
    fn state(&mut self, _: &mut Context<A>) -> DelayingState {
        DelayingState::Continue
    }

    /// called before the Delayer get aborted
    fn aborted(&mut self, _: &mut Context<A>) {}

    /// called after the Delayer emitted in advance
    fn emitted(&mut self, _: &mut Context<A>) {}

    /// called after the Delayer finished
    fn finished(&mut self, _: &mut Context<A>) {}
}

impl<A: Actor> Delayer<A> {
    /// get the handle of the Delayer
    pub fn handle(&self) -> Handle {
        self.handle
    }

    /// get the state
    #[allow(dead_code)]
    pub fn state(&self) -> DelayerState {
        self.state
    }

    /// set the state
    #[allow(dead_code)]
    pub fn set_state(&mut self, state: DelayerState) {
        self.state = state;
    }

    /// deliver the message with specified `delay` duration
    ///
    /// feature **`time`** must be enabled
    /// and the [Timing](`Timing`) implementation
    /// is required with which the actor can know
    /// the time
    ///
    /// NOTE that the delayed duration is NO LESS than
    /// `delay` you specified, and is within minor deviation
    #[cfg(feature = "time")]
    #[cfg_attr(docsrs, doc(cfg(feature = "time")))]
    pub fn from_duration<T: Timing + 'static>(message: A::Message, delay: Duration) -> Self {
        let deadline = T::now() + delay;
        let delay = move |_: &mut A, _: &mut Context<A>, _: &mut CoreContext<'_>| {
            if T::now() > deadline {
                Poll::Ready(Indicator::Repeation(Some(1)))
            } else {
                Poll::Pending
            }
        };
        let indicator = DelayIndicator::new(delay);
        Self {
            inner: Some(message),
            delay: indicator,
            handle: Handle::new(),
            state: DelayerState::Created,
        }
    }

    /// instantly deliver the message to its destination,
    /// and it nearly takes no time
    /// and it can be used as `Messager`
    pub fn instant(message: A::Message) -> Self {
        let delay = DelayIndicator::new(
            |_: &mut A,
             _: &mut Context<A>,
             _: &mut CoreContext<'_>|
             -> Poll<Indicator<A::Message>> {
                Poll::Ready(Indicator::Repeation(Some(1)))
            },
        );
        Self {
            inner: Some(message),
            delay,
            handle: Handle::new(),
            state: DelayerState::Created,
        }
    }

    /// periodically deliver the message to its destination,
    ///
    /// feature **`time`** must be enabled
    /// and the [Timing](`Timing`) implementation
    /// is required with which the actor can know
    /// the time
    #[cfg(feature = "time")]
    #[cfg_attr(docsrs, doc(cfg(feature = "time")))]
    pub unsafe fn repeat<T: Timing + 'static>(
        message: A::Message,
        duration: Option<Duration>,
        repeats: Option<usize>,
    ) -> Self {
        let mut deadline = duration.map(|d| T::now() + d);
        let delay = DelayIndicator::new(
            move |_: &mut A,
                  _: &mut Context<A>,
                  _: &mut CoreContext<'_>|
                  -> Poll<Indicator<A::Message>> {
                match duration {
                    // if duration is none, then emit
                    // the message at once
                    None => Poll::Ready(Indicator::Repeation(repeats)),
                    // duration is specified,
                    // wait for the delay being completed
                    Some(dur) => {
                        if T::now() >= *deadline.as_ref().unwrap() {
                            *deadline.as_mut().unwrap() += dur;
                            Poll::Ready(Indicator::Repeation(repeats))
                        } else {
                            Poll::Pending
                        }
                    }
                }
            },
        );
        Self {
            inner: Some(message),
            delay,
            handle: Handle::new(),
            state: DelayerState::Created,
        }
    }

    /// use a function/Clousre as condition
    /// to deliver the message
    pub fn from_fn<F>(message: Option<A::Message>, delay: F) -> Self
    where
        F: FnMut(&mut A, &mut Context<A>, &mut CoreContext<'_>) -> Poll<Indicator<A::Message>>
            + 'static
            + Unpin,
    {
        Self {
            inner: message,
            delay: DelayIndicator::new(delay),
            handle: Handle::new(),
            state: DelayerState::Created,
        }
    }
}

// TODO Modify the poll routine
// after the actor is not alive
impl<A> Future<A> for Delayer<A>
where
    A: Actor + Delaying<A>,
    A::Message: Message,
    Self: Unpin,
{
    type Output = ();

    fn poll(
        mut self: Pin<&mut Self>,
        act: &mut A,
        ctx: &mut Context<A>,
        cx: &mut CoreContext<'_>,
    ) -> Poll<Self::Output> {
        match self.state {
            DelayerState::Created => {
                let state = <A as Delaying<A>>::state(act, ctx);
                // the delayer state is changed
                // re-poll it
                if self.state != DelayerState::Created {
                    log::debug!("Delayer state changed");
                    cx.waker().wake_by_ref();
                    inc_poll_budget(2);
                    return Poll::Pending;
                }
                match state {
                    DelayingState::Abort => {
                        // abort the Delayer
                        self.state = DelayerState::Aborted;
                        log::debug!("Delayer Aborted when Created");
                        cx.waker().wake_by_ref();
                        inc_poll_budget(2);
                        return Poll::Pending;
                    }
                    DelayingState::Emit => {
                        // emit the delayer in advance
                        self.state = DelayerState::Emitted;
                        log::debug!("Delayer Emitted when Created");
                        cx.waker().wake_by_ref();
                        inc_poll_budget(2);
                        return Poll::Pending;
                    }
                    _ => {}
                }
                self.state = DelayerState::Started;
                log::debug!("Delayer has successfully started");
                cx.waker().wake_by_ref();
                inc_poll_budget(2);
                Poll::Pending
            }

            DelayerState::Started => {
                let state = <A as Delaying<A>>::state(act, ctx);
                // the delayer state is changed
                // re-poll it
                if self.state != DelayerState::Started {
                    log::debug!("Delayer state changed");
                    cx.waker().wake_by_ref();
                    inc_poll_budget(2);
                    return Poll::Pending;
                }
                match state {
                    DelayingState::Abort => {
                        // abort the Delayer
                        self.state = DelayerState::Aborted;
                        log::debug!("Delayer Aborted when Created");
                        cx.waker().wake_by_ref();
                        inc_poll_budget(2);
                        return Poll::Pending;
                    }
                    DelayingState::Emit => {
                        // emit the delayer in advance
                        self.state = DelayerState::Emitted;
                        log::debug!("Delayer Emitted when Created");
                        cx.waker().wake_by_ref();
                        inc_poll_budget(2);
                        return Poll::Pending;
                    }
                    _ => {}
                }
                <A as Delaying<A>>::started(act, ctx);
                // the delayer state is changed
                // re-poll it
                if self.state != DelayerState::Started {
                    log::debug!("Delayer state changed");
                    cx.waker().wake_by_ref();
                    inc_poll_budget(2);
                    return Poll::Pending;
                }
                log::debug!("Delayer has successfully started");
                self.state = DelayerState::Running;
                cx.waker().wake_by_ref();
                inc_poll_budget(2);
                Poll::Pending
            }

            DelayerState::Running => {
                let state = <A as Delaying<A>>::state(act, ctx);
                // the delayer state is changed
                // re-poll it
                if self.state != DelayerState::Running {
                    log::debug!("Delayer state changed");
                    cx.waker().wake_by_ref();
                    inc_poll_budget(2);
                    return Poll::Pending;
                }
                match state {
                    DelayingState::Continue => match self.delay.inner.as_mut().poll(act, ctx, cx) {
                        Poll::Pending => {
                            // TODO avoid too much waking
                            pending_polled();
                            Poll::Pending
                        }
                        Poll::Ready(indicator) => {
                            match indicator {
                                Indicator::Repeation(repeat) => {
                                    if self.inner.is_none() {
                                        log::error!("Message is None for execution");
                                        return Poll::Ready(());
                                    }
                                    match repeat {
                                        // run at least num times
                                        Some(num) => {
                                            // **Safety**: Just use the bitwise copy
                                            // of A::Message to avoid
                                            // memory safety violation
                                            let mut msg = unsafe {
                                                ptr::read_unaligned(
                                                    &self.inner as *const Option<A::Message>,
                                                )
                                            };
                                            A::action(act, msg.take().unwrap(), ctx);
                                            self.delay.inc();
                                            // the delayer state is changed
                                            // re-poll it
                                            if self.state != DelayerState::Running {
                                                log::debug!("Delayer state changed");
                                                cx.waker().wake_by_ref();
                                                inc_poll_budget(2);
                                                return Poll::Pending;
                                            }
                                            if self.delay.repeated() >= num {
                                                // finished
                                                self.state = DelayerState::Finished;
                                            }
                                        }
                                        // run repeatedly
                                        None => {
                                            // **Safety**: Just use the bitwise copy
                                            // of A::Message to avoid
                                            // memory safety violation
                                            let mut msg = unsafe {
                                                ptr::read_unaligned(
                                                    &self.inner as *const Option<A::Message>,
                                                )
                                            };
                                            A::action(act, msg.take().unwrap(), ctx);
                                            // the delayer state is changed
                                            // re-poll it
                                            if self.state != DelayerState::Running {
                                                log::debug!("Delayer state changed");
                                                cx.waker().wake_by_ref();
                                                inc_poll_budget(2);
                                                return Poll::Pending;
                                            }
                                        }
                                    }
                                }
                                // TODO the message is only
                                // available when the future is executed
                                // it can serve as a delayed future
                                // which sends a message
                                Indicator::Message(msg) => {
                                    // inner message will be ignored
                                    // though it is present
                                    A::action(act, msg, ctx);
                                    // the delayer state is changed
                                    // re-poll it
                                    if self.state != DelayerState::Running {
                                        log::debug!("Delayer state changed");
                                        cx.waker().wake_by_ref();
                                        inc_poll_budget(2);
                                        return Poll::Pending;
                                    }
                                    self.state = DelayerState::Finished;
                                }
                            }
                            // although it is probably finished,
                            // but `A::finished` is not called
                            cx.waker().wake_by_ref();
                            inc_poll_budget(2);
                            Poll::Pending
                        }
                    },
                    DelayingState::Abort => {
                        self.state = DelayerState::Aborted;
                        //log::debug!("Delayer is successfully Aborted");
                        cx.waker().wake_by_ref();
                        inc_poll_budget(2);
                        Poll::Pending
                    }
                    DelayingState::Emit => {
                        self.state = DelayerState::Emitted;
                        if self.inner.is_none() {
                            log::error!("Message is Empty to emit");
                            return Poll::Ready(());
                        }
                        A::action(act, self.inner.take().unwrap(), ctx);
                        // the delayer state is changed
                        // re-poll it
                        if self.state != DelayerState::Running {
                            log::debug!("Delayer state changed");
                            cx.waker().wake_by_ref();
                            inc_poll_budget(2);
                            return Poll::Pending;
                        }
                        //<A as Delaying<A>>::emitted(act, ctx);
                        //log::debug!("Delayer is successfully Emitted");
                        cx.waker().wake_by_ref();
                        inc_poll_budget(2);
                        Poll::Pending
                    }
                }
            }

            DelayerState::Aborted => {
                ctx.abort_future(self.handle);
                <A as Delaying<A>>::aborted(act, ctx);
                // the delayer state is changed
                // re-poll it
                if self.state != DelayerState::Aborted {
                    log::debug!("Delayer state changed");
                    cx.waker().wake_by_ref();
                    inc_poll_budget(2);
                    return Poll::Pending;
                }
                log::debug!("Delayer is successfully Aborted");
                Poll::Ready(())
            }

            DelayerState::Emitted => {
                A::emitted(act, ctx);
                // the delayer state is changed
                // re-poll it
                if self.state != DelayerState::Emitted {
                    log::debug!("Delayer state changed");
                    cx.waker().wake_by_ref();
                    inc_poll_budget(2);
                    return Poll::Pending;
                }
                self.state = DelayerState::Finished;
                log::debug!("Delayer is successfully emitted in advance");
                Poll::Ready(())
            }

            DelayerState::Finished => {
                <A as Delaying<A>>::finished(act, ctx);
                // the delayer state is changed
                // re-poll it
                if self.state != DelayerState::Finished {
                    log::debug!("Delayer state changed");
                    cx.waker().wake_by_ref();
                    inc_poll_budget(2);
                    return Poll::Pending;
                }
                log::debug!("Delayer is successfully Finished");
                Poll::Ready(())
            }
        }
    }

    fn downcast_ref(&self) -> Option<&dyn Any> {
        Some(self)
    }

    fn downcast_mut(self: Pin<&mut Self>) -> Option<Pin<&mut dyn Any>> {
        Some(self)
    }
}