-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.rs
77 lines (65 loc) · 1.98 KB
/
serialize.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::process::exit;
use std::time::{Duration, Instant};
use tyra::prelude::*;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
struct TestMsg {
content: String,
}
impl ActorMessage for TestMsg {}
#[derive(Clone)]
struct RemoteActor {}
impl Actor for RemoteActor {
fn handle_serialized_message(
&mut self,
msg: SerializedMessage,
context: &ActorContext<Self>,
) -> Result<ActorResult, Box<dyn Error>> {
let result = bincode::deserialize(&msg.content);
if result.is_err() {
return Ok(ActorResult::Ok);
}
let decoded: TestMsg = result.unwrap();
context
.actor_ref
.send_after(decoded, Duration::from_millis(50))?;
Ok(ActorResult::Ok)
}
}
impl Handler<TestMsg> for RemoteActor {
fn handle(
&mut self,
msg: TestMsg,
_context: &ActorContext<Self>,
) -> Result<ActorResult, Box<dyn Error>> {
println!("{}", msg.content);
Ok(ActorResult::Ok)
}
}
struct RemoteActorFactory {}
impl ActorFactory<RemoteActor> for RemoteActorFactory {
fn new_actor(
&mut self,
_context: ActorContext<RemoteActor>,
) -> Result<RemoteActor, Box<dyn Error>> {
Ok(RemoteActor {})
}
}
fn main() {
let actor_config = TyraConfig::new().unwrap();
let actor_system = ActorSystem::new(actor_config);
let hw = RemoteActorFactory {};
let x = actor_system.builder().spawn("hello-world", hw).unwrap();
let msg = TestMsg {
content: String::from("Hello World!"),
};
let serialized = bincode::serialize(&msg).unwrap();
actor_system.send_to_address(x.get_address(), SerializedMessage::new(serialized));
let start = Instant::now();
actor_system.stop(Duration::from_secs(10));
let result = actor_system.await_shutdown();
let duration = start.elapsed();
println!("It took {:?} to send stop", duration);
exit(result);
}