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
131use std::sync::Arc;
use std::time::Duration;
use actix::prelude::*;
use actix_web::client::WsProtocolError;
use actix_web_actors::ws;
use crate::gateway;
use crate::geo::BoundingBox;
pub struct WSClient {
fast_buffer: String,
slow_buffer: String,
gateway: Arc<Addr<gateway::Gateway>>,
}
impl WSClient {
pub fn new(gateway: Arc<Addr<gateway::Gateway>>) -> WSClient {
WSClient {
fast_buffer: String::new(),
slow_buffer: String::new(),
gateway,
}
}
pub fn handle_message(&mut self, text: &str, ctx: &mut <Self as Actor>::Context) {
if text.starts_with("+id|") {
self.gateway.do_send(gateway::SubscribeToId {
id: text[4..].to_owned(),
addr: ctx.address(),
});
} else if text.starts_with("-id|") {
self.gateway.do_send(gateway::UnsubscribeFromId {
id: text[4..].to_owned(),
addr: ctx.address(),
});
} else if text.starts_with("bbox|") {
if let Some(bbox) = BoundingBox::try_parse(&text[5..]) {
self.gateway.do_send(gateway::SetBoundingBox {
addr: ctx.address(),
bbox,
});
}
}
}
pub fn flush_fast(&mut self, ctx: &mut <Self as Actor>::Context) {
if !self.fast_buffer.is_empty() {
ctx.text(&self.fast_buffer);
self.fast_buffer.clear();
}
}
pub fn flush_slow(&mut self, ctx: &mut <Self as Actor>::Context) {
if !self.slow_buffer.is_empty() {
ctx.text(&self.slow_buffer);
self.slow_buffer.clear();
}
}
}
impl Actor for WSClient {
type Context = ws::WebsocketContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
// register self in gateway.
let addr: Addr<_> = ctx.address();
self.gateway.do_send(gateway::Connect { addr });
ctx.run_interval(Duration::from_millis(100), |act, ctx| {
act.flush_fast(ctx);
});
ctx.run_interval(Duration::from_millis(1000), |act, ctx| {
act.flush_slow(ctx);
});
}
fn stopping(&mut self, ctx: &mut Self::Context) -> Running {
// notify gateway
let addr: Addr<_> = ctx.address();
self.gateway.do_send(gateway::Disconnect { addr });
Running::Stop
}
}
#[derive(Message, Clone)]
#[rtype(result = "()")]
pub struct SendTextFast(pub String);
impl Handler<SendTextFast> for WSClient {
type Result = ();
fn handle(&mut self, message: SendTextFast, _ctx: &mut Self::Context) {
if !self.fast_buffer.is_empty() {
self.fast_buffer += "\n";
}
self.fast_buffer += &message.0;
}
}
#[derive(Message, Clone)]
#[rtype(result = "()")]
pub struct SendTextSlow(pub String);
impl Handler<SendTextSlow> for WSClient {
type Result = ();
fn handle(&mut self, message: SendTextSlow, _ctx: &mut Self::Context) {
if !self.slow_buffer.is_empty() {
self.slow_buffer += "\n";
}
self.slow_buffer += &message.0;
}
}
/// WebSocket message handler
impl StreamHandler<Result<ws::Message, WsProtocolError>> for WSClient {
fn handle(&mut self, msg: Result<ws::Message, WsProtocolError>, ctx: &mut Self::Context) {
match msg {
Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
Ok(ws::Message::Close(_)) => ctx.stop(),
Ok(ws::Message::Text(text)) => self.handle_message(&text, ctx),
_ => {}
}
}
}