WebSockets RPC server (#5425)

* Basic WS server.

* CLI for WS server.

* Bump jsonrpc

* Fixing test.
This commit is contained in:
Tomasz Drwięga
2017-04-13 16:32:07 +02:00
committed by Marek Kotewicz
parent 1df30ee83e
commit ea09aa584d
28 changed files with 388 additions and 141 deletions

View File

@@ -1,6 +1,6 @@
[package]
description = "Ethcore jsonrpc"
name = "ethcore-rpc"
description = "Parity JSON-RPC servers."
name = "parity-rpc"
version = "1.7.0"
license = "GPL-3.0"
authors = ["Parity Technologies <admin@parity.io>"]
@@ -25,6 +25,7 @@ rust-crypto = "0.2.36"
jsonrpc-core = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
jsonrpc-minihttp-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
jsonrpc-ws-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
jsonrpc-ipc-server = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }
jsonrpc-macros = { git = "https://github.com/paritytech/jsonrpc.git", branch = "parity-1.7" }

View File

@@ -8,10 +8,10 @@ authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
ctrlc = { git = "https://github.com/paritytech/rust-ctrlc.git" }
docopt = "0.7"
rustc-serialize = "0.3"
ethcore = { path = "../../ethcore" }
ethcore-devtools = { path = "../../devtools" }
ethcore-rpc = { path = ".." }
ethcore-util = { path = "../../util" }
ethjson = { path = "../../json" }
parity-rpc = { path = ".." }
rustc-serialize = "0.3"
serde_json = "0.8"

View File

@@ -16,13 +16,13 @@
extern crate ctrlc;
extern crate docopt;
extern crate rustc_serialize;
extern crate serde_json;
extern crate ethjson;
extern crate ethcore_util as util;
extern crate ethcore;
extern crate ethcore_devtools as devtools;
extern crate ethcore_rpc as rpc;
extern crate ethcore_util as util;
extern crate ethjson;
extern crate parity_rpc as rpc;
extern crate rustc_serialize;
extern crate serde_json;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, Condvar};

View File

@@ -66,6 +66,8 @@ extern crate ethjson;
#[cfg(test)]
extern crate ethcore_devtools as devtools;
pub extern crate jsonrpc_ws_server as ws;
mod metadata;
pub mod v1;
@@ -73,7 +75,7 @@ pub use ipc::{Server as IpcServer, MetaExtractor as IpcMetaExtractor, RequestCon
pub use http::{
hyper,
RequestMiddleware, RequestMiddlewareAction,
AccessControlAllowOrigin, Host,
AccessControlAllowOrigin, Host, DomainsValidation
};
pub use v1::{SigningQueue, SignerService, ConfirmationsQueue, NetworkSettings, Metadata, Origin, informant, dispatch};
@@ -193,3 +195,28 @@ pub fn start_ipc<M, S, H, T>(
.session_metadata_extractor(extractor)
.start(addr)
}
/// Start WS server and return `Server` handle.
pub fn start_ws<M, S, H, T, U>(
addr: &SocketAddr,
handler: H,
remote: tokio_core::reactor::Remote,
allowed_origins: ws::DomainsValidation<ws::Origin>,
allowed_hosts: ws::DomainsValidation<ws::Host>,
extractor: T,
stats: U,
) -> Result<ws::Server, ws::Error> where
M: jsonrpc_core::Metadata,
S: jsonrpc_core::Middleware<M>,
H: Into<jsonrpc_core::MetaIoHandler<M, S>>,
T: ws::MetaExtractor<M>,
U: ws::SessionStats,
{
ws::ServerBuilder::new(handler)
.event_loop_remote(remote)
.allowed_origins(allowed_origins)
.allowed_hosts(allowed_hosts)
.session_meta_extractor(extractor)
.session_stats(stats)
.start(addr)
}

View File

@@ -33,6 +33,9 @@ pub enum Origin {
/// IPC server (includes session hash)
#[serde(rename="ipc")]
Ipc(H256),
/// WS server (includes session hash)
#[serde(rename="ws")]
Ws(H256),
/// Signer (includes session hash)
#[serde(rename="signer")]
Signer(H256),
@@ -53,6 +56,7 @@ impl fmt::Display for Origin {
Origin::Rpc(ref origin) => write!(f, "RPC (service: {})", origin),
Origin::Dapps(ref origin) => write!(f, "Dapp {}", origin),
Origin::Ipc(ref session) => write!(f, "IPC (session: {})", session),
Origin::Ws(ref session) => write!(f, "WebSocket (session: {})", session),
Origin::Signer(ref session) => write!(f, "UI (session: {})", session),
Origin::Unknown => write!(f, "unknown origin"),
}
@@ -112,6 +116,7 @@ mod tests {
let o3 = Origin::Ipc(5.into());
let o4 = Origin::Signer(10.into());
let o5 = Origin::Unknown;
let o6 = Origin::Ws(5.into());
// when
let res1 = serde_json::to_string(&o1).unwrap();
@@ -119,6 +124,7 @@ mod tests {
let res3 = serde_json::to_string(&o3).unwrap();
let res4 = serde_json::to_string(&o4).unwrap();
let res5 = serde_json::to_string(&o5).unwrap();
let res6 = serde_json::to_string(&o6).unwrap();
// then
assert_eq!(res1, r#"{"rpc":"test service"}"#);
@@ -126,6 +132,7 @@ mod tests {
assert_eq!(res3, r#"{"ipc":"0x0000000000000000000000000000000000000000000000000000000000000005"}"#);
assert_eq!(res4, r#"{"signer":"0x000000000000000000000000000000000000000000000000000000000000000a"}"#);
assert_eq!(res5, r#""unknown""#);
assert_eq!(res6, r#"{"ws":"0x0000000000000000000000000000000000000000000000000000000000000005"}"#);
}
#[test]