-
-
Notifications
You must be signed in to change notification settings - Fork 807
/
Copy pathmain.rs
97 lines (81 loc) · 2.39 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
mod echo_service;
mod echo_rpc;
mod grpc_api;
use actix::prelude::*;
use log::{error, info};
use crate::echo_service::{EchoService, RunEcho};
use crate::echo_rpc::EchoRpc;
#[derive(Message)]
#[rtype(result = "()")]
pub struct SendEcho {
pub payload: String,
}
#[derive(Message)]
#[rtype(result = "()")]
pub struct EchoReceived {
pub payload: String,
}
struct EchoSender {
service: Addr<EchoService>,
echo_rpc: Option<Addr<EchoRpc>>,
}
impl EchoSender {
fn new(service: Addr<EchoService>) -> EchoSender {
EchoSender {
service,
echo_rpc: None,
}
}
}
impl Actor for EchoSender {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Context<Self>) {
self.service.send(RunEcho { addr: ctx.address().recipient() })
.into_actor(self)
.map(|res, act, ctx| {
match res {
Ok(Ok(echo_rpc)) => {
act.echo_rpc = Some(echo_rpc);
}
_ => {
error!("Unable to start echo RPC");
ctx.stop();
}
}
})
.wait(ctx)
}
}
impl Handler<SendEcho> for EchoSender {
type Result = ();
fn handle(&mut self, msg: SendEcho, ctx: &mut Context<Self>) {
info!("Sending echo: {}", msg.payload);
match &self.echo_rpc {
Some(echo_rpc) => {
echo_rpc.do_send(grpc_api::EchoRequest { payload: msg.payload });
}
None => {
// Maybe we could do something smart like trying to (re)connect here.
error!("Not connected!");
ctx.stop();
}
}
}
}
impl Handler<EchoReceived> for EchoSender {
type Result = ();
fn handle(&mut self, msg: EchoReceived, _: &mut Context<Self>) {
info!("EchoSender has just received: {}", msg.payload)
}
}
const ENDPOINT: &str = "http://127.0.0.1:50051";
#[actix_rt::main]
async fn main() {
env_logger::init();
let service = EchoService::new(ENDPOINT.to_string()).start();
let sender = EchoSender::new(service).start();
sender.do_send(SendEcho { payload: "Alpha".to_string() });
sender.do_send(SendEcho { payload: "Beta".to_string() });
sender.do_send(SendEcho { payload: "Gamma".to_string() });
actix_rt::Arbiter::local_join().await;
}