-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathask.rs
62 lines (49 loc) · 1.66 KB
/
ask.rs
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
/// In this example we show how to use the ask pattern.
/// The ask pattern consists of sending a message expecting an answer, the answer is then put in
/// a Future.
/// We chose to use it from the "outside" here, but it works as well inside an actor by calling
/// `context.ask(target, message)`.
extern crate env_logger;
extern crate rand;
extern crate robots;
use rand::Rng;
use std::any::Any;
use std::sync::Arc;
use robots::actors::{Actor, ActorSystem, ActorCell, ActorContext, Props};
#[derive(Copy, Clone, PartialEq)]
enum Exchanges {
Request,
Answer(u32),
}
struct Answerer {
secret: u32,
}
impl Actor for Answerer {
fn receive(&self, message: Box<Any>, context: ActorCell) {
if let Ok(message) = Box::<Any>::downcast::<Exchanges>(message) {
if *message == Exchanges::Request {
context.complete(context.sender(), Exchanges::Answer(self.secret));
}
}
}
}
impl Answerer {
fn new(_dummy: ()) -> Answerer {
Answerer { secret: rand::thread_rng().gen_range(1, 101) }
}
}
fn main() {
env_logger::init().unwrap();
let actor_system = ActorSystem::new("test".to_owned());
println!("system started");
actor_system.spawn_threads(1);
let props = Props::new(Arc::new(Answerer::new), ());
let answerer = actor_system.actor_of(props, "answerer".to_owned());
let future = actor_system.ask(answerer, Exchanges::Request, "request".to_owned());
let x: Exchanges = actor_system.extract_result(future);
match x {
Exchanges::Request => unreachable!(),
Exchanges::Answer(value) => println!("The secret value is {}", value),
}
actor_system.shutdown();
}