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
178
179
180
181
182
use ws;
use std;
use std::thread;
use std::path::PathBuf;
use std::default::Default;
use std::ops::Drop;
use std::sync::Arc;
use std::net::SocketAddr;
use io::{PanicHandler, OnPanicListener, MayPanic};
use jsonrpc_core::Metadata;
use jsonrpc_core::reactor::RpcHandler;
use rpc::ConfirmationsQueue;
mod session;
#[derive(Debug)]
pub enum ServerError {
IoError(std::io::Error),
WebSocket(ws::Error)
}
impl From<ws::Error> for ServerError {
fn from(err: ws::Error) -> Self {
match err.kind {
ws::ErrorKind::Io(e) => ServerError::IoError(e),
_ => ServerError::WebSocket(err),
}
}
}
pub struct ServerBuilder {
queue: Arc<ConfirmationsQueue>,
authcodes_path: PathBuf,
skip_origin_validation: bool,
}
impl ServerBuilder {
pub fn new(queue: Arc<ConfirmationsQueue>, authcodes_path: PathBuf) -> Self {
ServerBuilder {
queue: queue,
authcodes_path: authcodes_path,
skip_origin_validation: false,
}
}
pub fn skip_origin_validation(mut self, skip: bool) -> Self {
self.skip_origin_validation = skip;
self
}
pub fn start<M: Metadata>(self, addr: SocketAddr, handler: RpcHandler<M>) -> Result<Server, ServerError> {
Server::start(addr, handler, self.queue, self.authcodes_path, self.skip_origin_validation)
}
}
pub struct Server {
handle: Option<thread::JoinHandle<()>>,
broadcaster_handle: Option<thread::JoinHandle<()>>,
queue: Arc<ConfirmationsQueue>,
panic_handler: Arc<PanicHandler>,
addr: SocketAddr,
}
impl Server {
pub fn addr(&self) -> &SocketAddr {
&self.addr
}
fn start<M: Metadata>(addr: SocketAddr, handler: RpcHandler<M>, queue: Arc<ConfirmationsQueue>, authcodes_path: PathBuf, skip_origin_validation: bool) -> Result<Server, ServerError> {
let config = {
let mut config = ws::Settings::default();
config.method_strict = true;
config.shutdown_on_interrupt = false;
config
};
let origin = format!("{}", addr);
let ws = ws::Builder::new().with_settings(config).build(
session::Factory::new(handler, origin, authcodes_path, skip_origin_validation)
)?;
let panic_handler = PanicHandler::new_in_arc();
let ph = panic_handler.clone();
let broadcaster = ws.broadcaster();
let handle = thread::spawn(move || {
ph.catch_panic(move || {
match ws.listen(addr).map_err(ServerError::from) {
Err(ServerError::IoError(io)) => die(format!(
"Signer: Could not start listening on specified address. Make sure that no other instance is running on Signer's port. Details: {:?}",
io
)),
Err(any_error) => die(format!(
"Signer: Unknown error occurred when starting Signer. Details: {:?}",
any_error
)),
Ok(server) => server,
}
}).unwrap();
});
let ph = panic_handler.clone();
let q = queue.clone();
let broadcaster_handle = thread::spawn(move || {
ph.catch_panic(move || {
q.start_listening(|_message| {
broadcaster.send("new_message").unwrap();
}).expect("It's the only place we are running start_listening. It shouldn't fail.");
let res = broadcaster.shutdown();
if let Err(e) = res {
warn!("Signer: Broadcaster was not closed cleanly. Details: {:?}", e);
}
}).unwrap()
});
Ok(Server {
handle: Some(handle),
broadcaster_handle: Some(broadcaster_handle),
queue: queue,
panic_handler: panic_handler,
addr: addr,
})
}
}
impl MayPanic for Server {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}
impl Drop for Server {
fn drop(&mut self) {
self.queue.finish();
self.broadcaster_handle.take().unwrap().join().unwrap();
self.handle.take().unwrap().join().unwrap();
}
}
fn die(msg: String) -> ! {
println!("ERROR: {}", msg);
std::process::exit(1);
}