Generic PubSub implementation (#5456)

* Generic PubSub

* Adding more tests.

* Fix submodules.

* Remove PartialEq

* Actually remove the implementation.

* Update mod.rs

* Update mod.rs
This commit is contained in:
Tomasz Drwięga
2017-05-06 13:24:18 +02:00
committed by Gav Wood
parent 91d6f14e3c
commit 1617264b69
16 changed files with 524 additions and 27 deletions

View File

@@ -22,7 +22,7 @@ use dir::default_data_path;
use parity_rpc::informant::{RpcStats, Middleware};
use parity_rpc::{self as rpc, HttpServerError, Metadata, Origin, DomainsValidation};
use helpers::parity_ipc_path;
use jsonrpc_core::MetaIoHandler;
use jsonrpc_core::{futures, MetaIoHandler};
use parity_reactor::TokioRemote;
use rpc_apis::{self, ApiSet};
@@ -126,11 +126,53 @@ impl rpc::IpcMetaExtractor<Metadata> for RpcExtractor {
}
}
impl rpc::ws::MetaExtractor<Metadata> for RpcExtractor {
struct Sender(rpc::ws::ws::Sender, futures::sync::mpsc::Receiver<String>);
impl futures::Future for Sender {
type Item = ();
type Error = ();
fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
use self::futures::Stream;
let item = self.1.poll()?;
match item {
futures::Async::NotReady => {
Ok(futures::Async::NotReady)
},
futures::Async::Ready(None) => {
Ok(futures::Async::Ready(()))
},
futures::Async::Ready(Some(val)) => {
if let Err(e) = self.0.send(val) {
warn!("Error sending a subscription update: {:?}", e);
}
self.poll()
},
}
}
}
struct WsRpcExtractor {
remote: TokioRemote,
}
impl WsRpcExtractor {
fn wrap_out(&self, out: rpc::ws::ws::Sender) -> futures::sync::mpsc::Sender<String> {
let (sender, receiver) = futures::sync::mpsc::channel(8);
self.remote.spawn(move |_| Sender(out, receiver));
sender
}
}
impl rpc::ws::MetaExtractor<Metadata> for WsRpcExtractor {
fn extract(&self, req: &rpc::ws::RequestContext) -> Metadata {
let mut metadata = Metadata::default();
let id = req.session_id as u64;
metadata.origin = Origin::Ws(id.into());
metadata.session = Some(Arc::new(rpc::PubSubSession::new(
self.wrap_out(req.out.clone())
)));
metadata
}
}
@@ -173,10 +215,12 @@ pub fn new_ws<D: rpc_apis::Dependencies>(
let start_result = rpc::start_ws(
&addr,
handler,
remote,
remote.clone(),
allowed_origins,
allowed_hosts,
RpcExtractor,
WsRpcExtractor {
remote: remote,
},
WsStats {
stats: deps.stats.clone(),
},
@@ -247,7 +291,14 @@ pub fn new_ipc<D: rpc_apis::Dependencies>(
let handler = setup_apis(conf.apis, dependencies);
let remote = dependencies.remote.clone();
match rpc::start_ipc(&conf.socket_addr, handler, remote, RpcExtractor) {
let ipc = rpc::start_ipc(
&conf.socket_addr,
handler,
remote,
RpcExtractor,
);
match ipc {
Ok(server) => Ok(Some(server)),
Err(io_error) => Err(format!("IPC error: {}", io_error)),
}

View File

@@ -31,11 +31,12 @@ use parity_rpc::informant::{ActivityNotifier, Middleware, RpcStats, ClientNotifi
use parity_rpc::dispatch::{FullDispatcher, LightDispatcher};
use ethsync::{ManageNetwork, SyncProvider, LightSync};
use hash_fetch::fetch::Client as FetchClient;
use jsonrpc_core::{MetaIoHandler};
use jsonrpc_core::{self as core, MetaIoHandler};
use light::{TransactionQueue as LightTransactionQueue, Cache as LightDataCache};
use updater::Updater;
use util::{Mutex, RwLock};
use ethcore_logger::RotatingLogger;
use parity_reactor;
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub enum Api {
@@ -195,18 +196,16 @@ pub struct FullDependencies {
pub dapps_interface: Option<String>,
pub dapps_port: Option<u16>,
pub fetch: FetchClient,
pub remote: parity_reactor::Remote,
}
impl Dependencies for FullDependencies {
type Notifier = ClientNotifier;
fn activity_notifier(&self) -> ClientNotifier {
ClientNotifier {
client: self.client.clone(),
}
}
fn extend_with_set(&self, handler: &mut MetaIoHandler<Metadata, Middleware>, apis: &[Api]) {
impl FullDependencies {
fn extend_api<T: core::Middleware<Metadata>>(
&self,
handler: &mut MetaIoHandler<Metadata, T>,
apis: &[Api],
for_generic_pubsub: bool,
) {
use parity_rpc::v1::*;
macro_rules! add_signing_methods {
@@ -248,10 +247,12 @@ impl Dependencies for FullDependencies {
);
handler.extend_with(client.to_delegate());
let filter_client = EthFilterClient::new(self.client.clone(), self.miner.clone());
handler.extend_with(filter_client.to_delegate());
if !for_generic_pubsub {
let filter_client = EthFilterClient::new(self.client.clone(), self.miner.clone());
handler.extend_with(filter_client.to_delegate());
add_signing_methods!(EthSigning, handler, self);
add_signing_methods!(EthSigning, handler, self);
}
},
Api::Personal => {
handler.extend_with(PersonalClient::new(&self.secret_store, dispatcher.clone(), self.geth_compatibility).to_delegate());
@@ -278,8 +279,14 @@ impl Dependencies for FullDependencies {
self.dapps_port,
).to_delegate());
add_signing_methods!(EthSigning, handler, self);
add_signing_methods!(ParitySigning, handler, self);
if !for_generic_pubsub {
let mut rpc = MetaIoHandler::default();
self.extend_api(&mut rpc, apis, true);
handler.extend_with(PubSubClient::new(rpc, self.remote.clone()).to_delegate());
add_signing_methods!(EthSigning, handler, self);
add_signing_methods!(ParitySigning, handler, self);
}
},
Api::ParityAccounts => {
handler.extend_with(ParityAccountsClient::new(&self.secret_store).to_delegate());
@@ -308,6 +315,20 @@ impl Dependencies for FullDependencies {
}
}
impl Dependencies for FullDependencies {
type Notifier = ClientNotifier;
fn activity_notifier(&self) -> ClientNotifier {
ClientNotifier {
client: self.client.clone(),
}
}
fn extend_with_set(&self, handler: &mut MetaIoHandler<Metadata, Middleware<Self::Notifier>>, apis: &[Api]) {
self.extend_api(handler, apis, false)
}
}
/// Light client notifier. Doesn't do anything yet, but might in the future.
pub struct LightClientNotifier;

View File

@@ -631,6 +631,7 @@ pub fn execute(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) -> R
false => None,
},
fetch: fetch.clone(),
remote: event_loop.remote(),
});
let dependencies = rpc::Dependencies {