WebSockets server for signer

This commit is contained in:
Tomasz Drwięga
2016-05-27 13:03:00 +02:00
parent 945ebcbf9b
commit b77fdcdd68
12 changed files with 275 additions and 10 deletions

View File

@@ -30,7 +30,15 @@
//! and their responsibility is to confirm (or confirm and sign)
//! the transaction for you.
//!
//! ```
//! extern crate ethcore_signer;
//!
//! use ethcore_signer::Server;
//!
//! fn main() {
//! let _server = Server::start("127.0.0.1:8084".parse().unwrap());
//! }
//! ```
#[macro_use]
extern crate log;
@@ -41,10 +49,14 @@ extern crate serde_json;
extern crate rustc_serialize;
extern crate ethcore_util as util;
extern crate ws;
mod signing_queue;
mod ws_server;
pub mod types;
pub use ws_server::*;
#[cfg(test)]
mod tests {
#[test]

View File

@@ -14,13 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Serializable wrapper around vector of bytes
use rustc_serialize::hex::ToHex;
use serde::{Serialize, Serializer, Deserialize, Deserializer, Error};
use serde::de::Visitor;
use util::common::FromHex;
///! Serializable wrapper around vector of bytes
/// Wrapper structure around vector of bytes.
#[derive(Debug, PartialEq, Eq, Default, Hash, Clone)]
pub struct Bytes(pub Vec<u8>);

View File

@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Reusable types with JSON Serialization.
#[cfg(feature = "serde_macros")]
include!("mod.rs.in");

View File

@@ -14,7 +14,5 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
///! Reusable types with JSON Serialization.
pub mod transaction_request;
pub mod bytes;

128
signer/src/ws_server.rs Normal file
View File

@@ -0,0 +1,128 @@
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! `WebSockets` server.
use ws;
use std;
use std::thread;
use std::ops::Drop;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::net::SocketAddr;
use util::panics::{PanicHandler, OnPanicListener, MayPanic};
/// Signer startup error
#[derive(Debug)]
pub enum ServerError {
/// Wrapped `std::io::Error`
IoError(std::io::Error),
/// Other `ws-rs` error
Other(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::Other(err),
}
}
}
/// `WebSockets` server implementation.
pub struct Server {
handle: Option<thread::JoinHandle<ws::WebSocket<Factory>>>,
broadcaster: ws::Sender,
panic_handler: Arc<PanicHandler>,
}
impl Server {
/// Starts a new `WebSocket` server in separate thread.
/// Returns a `Server` handle which closes the server when droped.
pub fn start(addr: SocketAddr) -> Result<Server, ServerError> {
let config = {
let mut config = ws::Settings::default();
config.max_connections = 5;
config.method_strict = true;
config
};
// Create WebSocket
let session_id = Arc::new(AtomicUsize::new(1));
let ws = try!(ws::Builder::new().with_settings(config).build(Factory {
session_id: session_id,
}));
let panic_handler = PanicHandler::new_in_arc();
let ph = panic_handler.clone();
let broadcaster = ws.broadcaster();
// Spawn a thread with event loop
let handle = thread::spawn(move || {
ph.catch_panic(move || {
ws.listen(addr).unwrap()
}).unwrap()
});
// Return a handle
Ok(Server {
handle: Some(handle),
broadcaster: broadcaster,
panic_handler: panic_handler,
})
}
}
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.broadcaster.shutdown().expect("WsServer should close nicely.");
self.handle.take().unwrap().join().unwrap();
}
}
struct Session {
id: usize,
out: ws::Sender,
}
impl ws::Handler for Session {
fn on_open(&mut self, _shake: ws::Handshake) -> ws::Result<()> {
try!(self.out.send(format!("Hello client no: {}. We are not implemented yet.", self.id)));
try!(self.out.close(ws::CloseCode::Normal));
Ok(())
}
}
struct Factory {
session_id: Arc<AtomicUsize>,
}
impl ws::Factory for Factory {
type Handler = Session;
fn connection_made(&mut self, sender: ws::Sender) -> Self::Handler {
Session {
id: self.session_id.fetch_add(1, Ordering::SeqCst),
out: sender,
}
}
}