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
//! A framework to implement the reactive pattern on real-time systems.
//!
//! # Example of reactive actor executed by a dispatcher in its thread
//!
//! ```
//! use rtactor::dispatcher;
//! use rtactor::{spawn_dispatcher, ActiveMailbox, Addr, Behavior, Message, ProcessContext, send_notification};
//! use std::time::Duration;
//!
//! // A very simple reactive actor that allows incrementing and querying an integer.
//! struct TestReactive {
//! pub val: i32,
//! }
//!
//! enum Notification {
//! Increment(i32),
//! }
//!
//! enum Request {
//! GetValue,
//! ToString(String /*label*/),
//! }
//!
//! enum Response {
//! GetValue(i32),
//! ToString(String),
//! }
//!
//! impl Behavior for TestReactive {
//! fn process_message<'a>(&mut self, context: &'a mut ProcessContext, msg: &Message) {
//! match msg {
//! Message::Notification(notif) => {
//! if let Some(notif) = notif.data.downcast_ref::<Notification>() {
//! match notif {
//! Notification::Increment(increment) => self.val += increment,
//! }
//! }
//! }
//! Message::Request(request) => {
//! if let Some(data) = request.data.downcast_ref::<Request>() {
//! match data {
//! Request::GetValue => {
//! context.send_response(request, Response::GetValue(self.val));
//! }
//!
//! Request::ToString(label) => context.send_response(
//! &request,
//! Response::ToString(format!("{label}: {}", self.val)),
//! ),
//! }
//! }
//! }
//! _ => panic!(),
//! }
//! }
//! }
//!
//! let initial_value = 0;
//!
//! // Start a dispatcher inside its own thread.
//! // The active object is created with the closure called inside the dispatcher thread.
//! // This allows to have reactive object that are not movable between threads.
//! let (dispatcher_addr, join_handle, test_reactive_addr) = spawn_dispatcher(10, move |disp| {
//! // Create a reactive object on the heap.
//! let test_reactive = Box::new(TestReactive { val: initial_value });
//! // Move it inside the dispatcher and return the reactive address as the return of `setup_func`
//! disp.register_reactive(test_reactive)
//! });
//!
//! send_notification(&test_reactive_addr, Notification::Increment(10))
//! .unwrap();
//!
//! // Create an active object to interact with the reactive under test.
//! let mut prober = ActiveMailbox::new(1);
//!
//! // Request the value.
//! let result = prober.request_for::<_, Response>(
//! &test_reactive_addr,
//! Request::GetValue,
//! Duration::from_secs(10),
//! );
//!
//! if let Ok(Response::GetValue(val)) = result {
//! assert_eq!(val, 10);
//! } else {
//! panic!();
//! }
//!
//! // An other notification.
//! send_notification(&test_reactive_addr, Notification::Increment(-3))
//! .unwrap();
//!
//! // An other different request.
//! let result = prober.request_for::<_, Response>(
//! &test_reactive_addr,
//! Request::ToString("the value".to_string()),
//! Duration::from_secs(10),
//! );
//! if let Ok(Response::ToString(s)) = result {
//! assert_eq!(s, "the value: 7");
//! } else {
//! panic!();
//! }
//!
//! // Request to stop the dispatcher using its own address.
//! let result = prober.request_for::<_, dispatcher::Response>(
//! &dispatcher_addr,
//! dispatcher::Request::StopDispatcher{},
//! Duration::from_secs(10),
//! );
//! if let Ok(dispatcher::Response::StopDispatcher()) = result {
//! } else {
//! panic!();
//! }
//!
//! // Wait that the dispatcher thread finishes.
//! join_handle.join().unwrap();
//! ```
//!
//! # Example of simulation of a reactive actor in a single threaded test
//!
//! ```
//! use rtactor::simulation::SimulationDispatcher;
//! use rtactor::{ActiveMailbox, Behavior, Message, ProcessContext, send_notification};
//! use std::time::Duration;
//!
//! // A very simple reactive actor that allows incrementing and querying an integer.
//! struct TestReactive {
//! pub val: i32,
//! }
//!
//! enum Notification {
//! Increment(i32),
//! }
//!
//! enum Request {
//! GetValue,
//! ToString(String /*label*/),
//! }
//!
//! enum Response {
//! GetValue(i32),
//! ToString(String),
//! }
//!
//! impl Behavior for TestReactive {
//! fn process_message<'a>(&mut self, context: &'a mut ProcessContext, msg: &Message) {
//! match msg {
//! Message::Notification(notif) => {
//! if let Some(notif) = notif.data.downcast_ref::<Notification>() {
//! match notif {
//! Notification::Increment(increment) => self.val += increment,
//! }
//! }
//! }
//! Message::Request(request) => {
//! if let Some(data) = request.data.downcast_ref::<Request>() {
//! match data {
//! Request::GetValue => {
//! context.send_response(request, Response::GetValue(self.val))
//! }
//!
//! Request::ToString(label) => context.send_response(
//! &request,
//! Response::ToString(format!("{label}: {}", self.val)),
//! ),
//! }
//! }
//! }
//! _ => panic!(),
//! }
//! }
//! }
//!
//! // Create a simulation dispatcher.
//! let mut disp = SimulationDispatcher::new(10);
//!
//! // Create a reactive object on the heap.
//! let test_reactive = Box::new(TestReactive { val: 0 });
//!
//! // Move it inside the dispatcher. It starts the dispatch of messages for it.
//! let test_reactive_addr = disp.register_reactive(test_reactive);
//!
//! // Send a notification to the reactive.
//! send_notification(&test_reactive_addr, Notification::Increment(10))
//! .unwrap();
//!
//! // Create an active object to interact with the reactive under test.
//! let mut prober = ActiveMailbox::new(1);
//!
//! // Ask the simulation dispatcher to simulate a request by the active actor.
//! let result = disp.active_request_for::<_, Response>(
//! &mut prober,
//! &test_reactive_addr,
//! Request::GetValue,
//! Duration::from_secs(10),
//! );
//! if let Ok(Response::GetValue(val)) = result {
//! assert_eq!(val, 10);
//! } else {
//! panic!();
//! }
//!
//! // An other notification.
//! send_notification(&test_reactive_addr, Notification::Increment(-3))
//! .unwrap();
//!
//! // An other different request.
//! let result = disp.active_request_for::<_, Response>(
//! &mut prober,
//! &test_reactive_addr,
//! Request::ToString("the value".to_string()),
//! Duration::from_secs(10),
//! );
//! if let Ok(Response::ToString(s)) = result {
//! assert_eq!(s, "the value: 7");
//! } else {
//! panic!();
//! }
//!
//! // No need to stop the dispatcher, there is no thread, everything is single threaded.
//! // The reactive actor will be dropped by the drop of the simulation dispatcher.
//! ```
//!
//! # Doc about actors
//! A good explanation why the actor pattern is good for multitask:
//! <https://getakka.net/articles/intro/what-problems-does-actor-model-solve.html>
//!
//! A well designed rust actor framework (but not suitable for real-time):
//! <https://docs.rs/axiom/latest/axiom/>
pub extern crate rtactor_macros;
extern crate self as rtactor;
pub use send_notification;
pub use Addr;
pub use Error;
pub use Message;
pub use Notification;
pub use Request;
pub use RequestId;
pub use Response;
pub use ActiveActor;
pub use ActiveMailbox;
pub use SyncAccessor;
pub use spawn_dispatcher;
pub use MpscDispatcher;
pub use Behavior;
pub use DummyBehavior;
pub use Instant;
pub use ProcessContext;
pub use Timeout;
pub use Timer;
pub use ;
pub use MockBehavior;
// This crate used in the interface is reexported to allow user to build with the same version.
// see https://doc.rust-lang.org/rustdoc/write-documentation/re-exports.html
pub use mockall;