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
use crate::common::tsafe::TSafe;
use crate::actors::actor_cell::ActorCell;
use crate::actors::abstract_actor_ref::{AbstractActorRef, ActorRef, AskTimeoutError};
use crate::actors::actor_path::ActorPath;
use crate::actors::message::Message;
use crate::actors::abstract_actor_system::AbstractActorSystem;
use crate::actors::props::Props;
use crate::actors::ask_actor::AskActor;
use crate::futures::future::WrappedFuture;
use crate::futures::promise::Promise;
use crate::futures::completable_promise::CompletablePromise;
use std::hash::{Hash, Hasher};
use std::fmt;
use std::any::Any;
use std::time::Duration;
use std::sync::{Arc, Mutex};
pub struct LocalActorRef {
pub cell: TSafe<ActorCell>,
pub path: TSafe<ActorPath>
}
impl LocalActorRef {
pub fn new(cell: TSafe<ActorCell>, path: TSafe<ActorPath>) -> LocalActorRef {
LocalActorRef {
cell,
path
}
}
fn inner_clone(self: &Self) -> Box<LocalActorRef> {
Box::new(LocalActorRef {
cell: self.cell.clone(),
path: self.path.clone(),
})
}
}
impl AbstractActorRef for LocalActorRef {
fn tell(self: &mut Self, msg: Message, rself: Option<&ActorRef>) {
let cell_cloned = self.cell.clone();
let path_cloned = self.path.clone();
let toref = Box::new(LocalActorRef::new(cell_cloned, path_cloned));
let mut cell = self.cell.lock().unwrap();
cell.send(&self.cell, msg, rself.map_or(None, |v| Some((*v).clone())), toref)
}
fn ask(&mut self, factory: &mut AbstractActorSystem, msg: Message) -> WrappedFuture<Message, AskTimeoutError> {
self.ask_timeout(factory, Duration::from_secs(3), msg)
}
fn ask_timeout(&mut self, factory: &mut AbstractActorSystem, timeout: Duration, msg: Message) -> WrappedFuture<Message, AskTimeoutError> {
let p: CompletablePromise<Message, AskTimeoutError>
= CompletablePromise::new();
let f = p.future();
let ask_actor = factory.actor_of(Props::new(tsafe!(AskActor::new(p, timeout))), None);
self.tell(msg, Some(&ask_actor));
f
}
fn path(&self) -> ActorPath {
self.path.lock().unwrap().clone()
}
fn cell(self: &mut Self) -> TSafe<ActorCell> {
self.cell.clone()
}
fn clone(self: &Self) -> ActorRef {
self.inner_clone()
}
fn as_any(self: &Self) -> Box<Any> {
Box::new(self.inner_clone())
}
}
impl fmt::Display for LocalActorRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ActorRef ({})", self.path.lock().unwrap())
}
}
impl PartialEq for LocalActorRef {
fn eq(&self, other: &Self) -> bool {
*self.path.lock().unwrap() == *other.path.lock().unwrap()
}
}
impl Eq for LocalActorRef {}
impl Hash for LocalActorRef {
fn hash<H: Hasher>(&self, state: &mut H) {
self.path.lock().unwrap().hash(state);
}
}