rtactor 0.6.0

An Actor framework specially designed for Real-Time constrained use cases.
Documentation
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
//! Various tools to allow time simulation of actors.

#[cfg(feature = "mockall")]
pub use crate::reactive_mocker::ReactiveMocker;

use super::actor::{self, Message, RequestId};
use super::reactive::{self, ProcessContext};
use crate::dispatcher::Dispatcher;
use crate::mpsc_dispatcher::{self, MpscDispatcher};
use crate::{ActiveMailbox, Addr, Error};

use std::ops::ControlFlow;
use std::time;
use std::time::Duration;

////////////////////////////// public types /////////////////////////////////////

/// A dispatcher used to simulate time execution of reactive actors.
pub struct SimulationDispatcher {
    disp: MpscDispatcher,
    timeout_scheduler: reactive::TimeoutScheduler,
    instant_source: SimulatedInstantSource,
    request_id_counter_backup: RequestId,
}

////////////////////////////// internal types /////////////////////////////////////

// Implement reactive::InstantSource for the simulation.
struct SimulatedInstantSource {
    elapsed_duration: Duration,
    simulated_now: time::Instant,
}

////////////////////////////// public macros /////////////////////////////////////

/// Create a struct that allows a simulation access to an address and implement some or more SyncRequester or SyncNotifier
///
/// Example:
/// ```rs
/// define_syn_accessor(MySyncAccessorStructName, FirstSyncRequesterName, SecondSyncRequesterName, ...);
/// ```
///
/// It will have a function `new(&'a RefCell<SimulationDispatcher>, &Addr) -> MySyncAccessorStructName<'a>`
///
#[macro_export]
macro_rules! define_sim_sync_accessor{
    ($sync_accessor_name:ident, $($sync_trait:ty),+)
    =>
    {
        pub struct $sync_accessor_name<'a> {
            mailbox: ::rtactor::ActiveMailbox,
            target_addr: ::rtactor::Addr,
            disp: &'a std::cell::RefCell<::rtactor::simulation::SimulationDispatcher>,
        }

        impl<'a> $sync_accessor_name<'a> {
            pub fn new<'b>(disp: &'a std::cell::RefCell<::rtactor::simulation::SimulationDispatcher>, target_addr: &'b::rtactor::Addr) -> $sync_accessor_name<'a> {
                $sync_accessor_name {
                    mailbox: ::rtactor::ActiveMailbox::new(1),
                    target_addr: target_addr.clone(),
                    disp,
                }
            }
            pub fn target_addr(&self) -> &::rtactor::Addr {&self.target_addr}
        }

        impl<'a> ::rtactor::SyncAccessor for $sync_accessor_name<'a> {

            fn send_notification<T>(&mut self, data: T) -> Result<(), ::rtactor::Error>
            where
            T: 'static + Send,
            {
                let addr = self.target_addr.clone();
                self.mailbox.send_notification(&addr, data)
            }
            fn request_for<TRequest, TResponse>(
                &mut self,
                request_data: TRequest,
                timeout: std::time::Duration,
            ) -> Result<TResponse, ::rtactor::Error>
            where
                TRequest: 'static + Send + Sized,
                TResponse: 'static + Send + Sized
            {
                let addr = self.target_addr.clone();
                self.disp.borrow_mut().active_request_for(&mut self.mailbox, &addr, request_data, timeout)
            }

        }

        $(
            impl<'a> $sync_trait for $sync_accessor_name<'a> {}
        )*
    }
}

////////////////////////////// public impl's /////////////////////////////////////

impl SimulationDispatcher {
    pub fn new(queue_size: usize) -> SimulationDispatcher {
        let disp = mpsc_dispatcher::Builder::new(queue_size).build();
        SimulationDispatcher {
            disp,
            request_id_counter_backup: 0,
            instant_source: SimulatedInstantSource::new(),
            timeout_scheduler: reactive::TimeoutScheduler::new(),
        }
    }

    pub fn addr(&self) -> Addr {
        self.disp.addr()
    }

    #[deprecated = "use the better named addr()"]
    pub fn get_addr(&self) -> Addr {
        self.addr()
    }

    /// Move reactive into the dispatcher.
    pub fn register_reactive(&mut self, behaviour: Box<dyn reactive::Behavior>) -> actor::Addr {
        self.disp.register_reactive(behaviour)
    }

    /// Replace reactive in the dispatcher.
    pub fn replace_reactive(
        &mut self,
        addr: &Addr,
        behaviour: Box<dyn reactive::Behavior>,
    ) -> Result<Box<dyn reactive::Behavior>, Box<dyn reactive::Behavior>> {
        self.disp.replace_reactive(addr, behaviour)
    }

    /// Get the current simulated time.
    pub fn now(&self) -> reactive::Instant {
        self.instant_source.now()
    }

    /// Process messages until either the `duration` is elapsed or `break_func` return `ControlFlow::Break`.
    pub fn process_for_cond<F>(&mut self, duration: Duration, break_func: F)
    where
        F: FnMut() -> ControlFlow<()>,
    {
        self.process_until_cond(self.now() + duration, break_func);
    }
    /*
        fn wait_active_message(&mut self, mailbox: &mut ActiveMailbox, timeout: Duration) -> Result<Message, actor::Error>
        {

        }
    */

    /// Process messages until either the `instant` is reached or `break_func` return `ControlFlow::Break`.
    pub fn process_until_cond<F>(&mut self, instant: reactive::Instant, mut break_func: F)
    where
        F: FnMut() -> ControlFlow<()>,
    {
        loop {
            let mut message_processed: bool;
            let mut stop: bool;
            loop {
                let mut context = ProcessContext::new(
                    &self.disp,
                    self.request_id_counter_backup,
                    &self.instant_source,
                    &mut self.timeout_scheduler,
                );

                // process all queued messages
                loop {
                    (message_processed, stop) = self.disp.try_process_message(&mut context);

                    match (break_func)() {
                        ControlFlow::Break(_) => {
                            stop = true;
                        }
                        ControlFlow::Continue(_) => {
                            let now = context.now();
                            if now > instant || (!message_processed && now == instant) {
                                stop = true;
                            }
                        }
                    }

                    if stop || !message_processed {
                        break;
                    }
                }
                self.request_id_counter_backup = context.request_id_counter();

                if stop {
                    break;
                } else {
                    // try to queue mature timeouts and continue to process them
                    match context.try_send_next_pending_timeout() {
                        ControlFlow::Continue(()) => (),
                        ControlFlow::Break(duration) => {
                            if duration != Duration::MAX && ((self.now() + duration) < instant) {
                                // If there is a timeout before instant, advance to it deadline.
                                self.instant_source.advance_for(duration);
                            } else if instant.at_inf() {
                                // If the instant is infinity, continue the processing, break_func can have some side effect.
                            } else {
                                // else advance the time at instant.
                                self.instant_source.advance_until(instant);
                            }
                        }
                    }
                }
            }

            if stop {
                break;
            }

            // advance until new timeout or time point reached, break if duration reached
            // min(instant, first_timeout_instant)
        }
    }

    /// Process messages until the `duration` is elapsed.
    pub fn process_for(&mut self, duration: Duration) {
        self.process_until(self.now() + duration);
    }

    /// Process messages until the `instant` is reached.
    pub fn process_until(&mut self, instant: reactive::Instant) {
        self.process_until_cond(instant, || ControlFlow::Continue(()))
    }

    /// Process messages until the dispatcher is stopped.
    pub fn process(&mut self) {
        self.process_until(reactive::Instant::INFINITY);
    }

    /// Wait indefinitely that an ActiveMailbox receives a message.
    pub fn active_wait_message(&mut self, active: &mut ActiveMailbox) -> Result<Message, Error> {
        self.active_wait_message_until(active, reactive::Instant::INFINITY)
    }

    /// Wait for a given duration that an ActiveMailbox receives a message.
    pub fn active_wait_message_for(
        &mut self,
        active: &mut ActiveMailbox,
        duration: Duration,
    ) -> Result<Message, Error> {
        self.active_wait_message_until(active, self.now() + duration)
    }

    /// Wait until a point in time that an ActiveMailbox receives a message.
    pub fn active_wait_message_until(
        &mut self,
        active: &mut ActiveMailbox,
        instant: reactive::Instant,
    ) -> Result<Message, Error> {
        self.active_wait_message_until_cond(active, instant, || ControlFlow::Continue(()))
    }

    /// Wait until a point in time that an ActiveMailbox receives a message or a condition is met.
    pub fn active_wait_message_until_cond<F>(
        &mut self,
        active: &mut ActiveMailbox,
        instant: reactive::Instant,
        mut break_func: F,
    ) -> Result<Message, Error>
    where
        F: FnMut() -> ControlFlow<()>,
    {
        let mut msg: actor::Message = actor::Message::new();
        let mut msg_received = false;
        let mut err: Error = Error::NoMessage;
        self.process_until_cond(instant, || match active.try_get_message() {
            Ok(receive_msg) => {
                msg_received = true;
                msg = receive_msg;
                ControlFlow::Break(())
            }
            Err(actor::Error::NoMessage) => (break_func)(),
            Err(e) => {
                msg_received = false;
                err = e;
                ControlFlow::Break(())
            }
        });

        if msg_received {
            Ok(msg)
        } else {
            Err(err)
        }
    }

    /// Execute a request with a `ActiveMailbox` until either the response arrives or `instant` is reached.
    pub fn active_request_until<TRequest, TResponse>(
        &mut self,
        active: &mut ActiveMailbox,
        dst: &actor::Addr,
        request_data: TRequest,
        instant: reactive::Instant,
    ) -> Result<TResponse, actor::Error>
    where
        TRequest: 'static + Send + Sized,
        TResponse: 'static + Send + Sized,
    {
        self.active_request_until_cond(active, dst, request_data, instant, || {
            ControlFlow::Continue(())
        })
    }

    /// Execute a request with a `ActiveMailbox` until either the response arrives, `break_func` breaks
    /// or `instant` is reached.
    pub fn active_request_until_cond<TRequest, TResponse, F>(
        &mut self,
        active: &mut ActiveMailbox,
        dst: &actor::Addr,
        request_data: TRequest,
        instant: reactive::Instant,
        mut break_func: F,
    ) -> Result<TResponse, actor::Error>
    where
        TRequest: 'static + Send + Sized,
        TResponse: 'static + Send + Sized,
        F: FnMut() -> ControlFlow<()>,
    {
        let request_id = active.generate_request_id();
        dst.receive_request(&active.addr(), request_id, request_data);

        let mut msg: actor::Message = actor::Message::new();
        let mut msg_received = false;
        let mut err = actor::Error::NoMessage;
        self.process_until_cond(instant, || match active.try_get_message() {
            Ok(receive_msg) => {
                msg = receive_msg;
                msg_received = true;
                ControlFlow::Break(())
            }
            Err(actor::Error::NoMessage) => (break_func)(),
            Err(e) => {
                err = e;
                ControlFlow::Break(())
            }
        });

        if msg_received {
            match msg {
                actor::Message::Response(response) => {
                    if response.id_eq(request_id) {
                        match response.result {
                            Ok(data) => match data.downcast::<TResponse>() {
                                Ok(out) => Ok(*out),
                                Err(_) => Err(actor::Error::DowncastFailed),
                            },
                            Err(err) => Err(err.error),
                        }
                    } else {
                        Err(actor::Error::NoMessage)
                    }
                }
                _ => Err(actor::Error::NoMessage),
            }
        } else {
            Err(err)
        }
    }

    /// Execute a request with a `ActiveMailbox` until either the response arrives
    /// or `duration` elapses.
    pub fn active_request_for<TRequest, TResponse>(
        &mut self,
        active: &mut ActiveMailbox,
        dst: &actor::Addr,
        request_data: TRequest,
        duration: Duration,
    ) -> Result<TResponse, actor::Error>
    where
        TRequest: 'static + Send + Sized,
        TResponse: 'static + Send + Sized,
    {
        self.active_request_until(active, dst, request_data, self.now() + duration)
    }
}

////////////////////////////// internal impl's /////////////////////////////////////

impl SimulatedInstantSource {
    pub fn new() -> SimulatedInstantSource {
        SimulatedInstantSource {
            elapsed_duration: Duration::from_secs(0),
            simulated_now: time::Instant::now(),
        }
    }

    pub fn advance_for(&mut self, duration: Duration) {
        self.elapsed_duration += duration;
        self.simulated_now += duration;
    }

    pub fn advance_until(&mut self, instant: reactive::Instant) {
        match instant.internal() {
            reactive::InternalInstant::Finite(internal_instant) => {
                self.elapsed_duration += *internal_instant - self.simulated_now;
                self.simulated_now = *internal_instant;
            }
            reactive::InternalInstant::Infinity => panic!(),
        }
    }

    pub fn now(&self) -> reactive::Instant {
        reactive::Instant::new(self.simulated_now)
    }
}

impl reactive::InstantSource for SimulatedInstantSource {
    fn now(&self) -> reactive::Instant {
        self.now()
    }
}