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
use std::any::Any;
use std::sync::Arc;
use actors::{InnerMessage, Message, SystemMessage};
use actors::actor_cell::ActorCell;
use actors::cthulhu::Cthulhu;
#[derive(Debug, Eq, Hash, PartialEq)]
pub enum ActorPath {
Local(String),
Distant(ConnectionInfo),
}
impl ActorPath {
pub fn new_local(path: String) -> Arc<ActorPath> {
Arc::new(ActorPath::Local(path))
}
pub fn new_distant(distant_logical_path: String, addr_port: String) -> Arc<ActorPath> {
Arc::new(ActorPath::Distant(
ConnectionInfo {
distant_logical_path: distant_logical_path,
addr_port: addr_port,
}))
}
pub fn logical_path(&self) -> &String {
match *self {
ActorPath::Local(ref s) => s,
ActorPath::Distant(ref c) => &(c.distant_logical_path),
}
}
pub fn child(&self, name: String) -> Arc<ActorPath> {
match *self {
ActorPath::Local(ref s) => {
let path = format!("{}/{}", s, name);
ActorPath::new_local(path)
},
ActorPath::Distant(_) => panic!("Cannot create a child for a distant actor."),
}
}
}
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct ConnectionInfo {
distant_logical_path: String,
addr_port: String,
}
impl ConnectionInfo {
pub fn distant_logical_path(&self) -> &String {
&self.distant_logical_path
}
pub fn addr_port(&self) -> &String {
&self.addr_port
}
}
#[derive(Clone)]
enum InnerActor {
Cthulhu(Cthulhu),
Actor(ActorCell),
}
pub struct ActorRef {
inner_actor: Option<InnerActor>,
path: Arc<ActorPath>,
}
impl ActorRef {
pub fn new_distant(path: Arc<ActorPath>) -> ActorRef {
ActorRef {
inner_actor: None,
path: path,
}
}
pub fn with_cthulhu(cthulhu: Cthulhu) -> ActorRef {
let path = ActorPath::new_local("/".to_owned());
ActorRef {
inner_actor: Some(InnerActor::Cthulhu(cthulhu)),
path: path,
}
}
pub fn with_cell(cell: ActorCell, path: Arc<ActorPath>) -> ActorRef {
ActorRef {
inner_actor: Some(InnerActor::Actor(cell)),
path: path,
}
}
pub fn receive_system_message(&self, system_message: SystemMessage) {
info!("{} receiving a system message", self.path().logical_path());
let inner = self.inner_actor.as_ref().expect("Tried to put a system message in the mailbox of a distant actor.");
match *inner {
InnerActor::Actor(ref actor) => actor.receive_system_message(system_message),
InnerActor::Cthulhu(ref cthulhu) => cthulhu.receive_system_message(),
};
}
pub fn receive(&self, message: InnerMessage, sender: ActorRef) {
info!("{} receiving a message", self.path().logical_path());
let inner = self.inner_actor.as_ref().expect("Tried to put a message in the mailbox of a distant actor.");
match *inner {
InnerActor::Actor(ref actor) => actor.receive_message(message, sender),
InnerActor::Cthulhu(ref cthulhu) => cthulhu.receive(),
};
}
pub fn handle(&self) {
info!("{} handling a message", self.path().logical_path());
let inner = self.inner_actor.as_ref().expect("");
match *inner {
InnerActor::Actor(ref actor) => actor.handle_envelope(),
InnerActor::Cthulhu(ref cthulhu) => cthulhu.handle(),
};
}
pub fn path(&self) -> Arc<ActorPath> {
self.path.clone()
}
pub fn tell_to<MessageTo: Message>(&self, to: ActorRef, message: MessageTo) {
info!("{} is sending a message to {}", self.path().logical_path(), to.path().logical_path());
let message: Box<Any + Send> = Box::new(message);
to.receive(InnerMessage::Message(message), self.clone())
}
}
impl Clone for ActorRef {
fn clone(&self) -> ActorRef {
ActorRef {
inner_actor: self.inner_actor.clone(),
path: self.path.clone(),
}
}
}