2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2017-05-06 13:24:18 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Parity-specific PUB-SUB rpc implementation.
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::RwLock;
|
2017-05-06 13:24:18 +02:00
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
use jsonrpc_core::{self as core, Result, MetaIoHandler};
|
2018-11-09 12:51:49 +01:00
|
|
|
use jsonrpc_core::futures::{future, Future, Stream, Sink};
|
2017-06-13 16:29:35 +02:00
|
|
|
use jsonrpc_macros::Trailing;
|
2017-05-06 13:24:18 +02:00
|
|
|
use jsonrpc_macros::pubsub::Subscriber;
|
|
|
|
use jsonrpc_pubsub::SubscriptionId;
|
|
|
|
use tokio_timer;
|
|
|
|
|
2018-10-22 09:40:50 +02:00
|
|
|
use parity_runtime::Executor;
|
2017-05-06 13:24:18 +02:00
|
|
|
use v1::helpers::GenericPollManager;
|
|
|
|
use v1::metadata::Metadata;
|
|
|
|
use v1::traits::PubSub;
|
|
|
|
|
|
|
|
/// Parity PubSub implementation.
|
|
|
|
pub struct PubSubClient<S: core::Middleware<Metadata>> {
|
|
|
|
poll_manager: Arc<RwLock<GenericPollManager<S>>>,
|
2018-10-22 09:40:50 +02:00
|
|
|
executor: Executor,
|
2017-05-06 13:24:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: core::Middleware<Metadata>> PubSubClient<S> {
|
|
|
|
/// Creates new `PubSubClient`.
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn new(rpc: MetaIoHandler<Metadata, S>, executor: Executor) -> Self {
|
2017-05-06 13:24:18 +02:00
|
|
|
let poll_manager = Arc::new(RwLock::new(GenericPollManager::new(rpc)));
|
2018-11-09 12:51:49 +01:00
|
|
|
let pm2 = Arc::downgrade(&poll_manager);
|
2017-05-06 13:24:18 +02:00
|
|
|
|
|
|
|
let timer = tokio_timer::wheel()
|
|
|
|
.tick_duration(Duration::from_millis(500))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Start ticking
|
|
|
|
let interval = timer.interval(Duration::from_millis(1000));
|
2018-10-22 09:40:50 +02:00
|
|
|
executor.spawn(interval
|
2017-05-06 13:24:18 +02:00
|
|
|
.map_err(|e| warn!("Polling timer error: {:?}", e))
|
2018-11-09 12:51:49 +01:00
|
|
|
.for_each(move |_| {
|
|
|
|
if let Some(pm2) = pm2.upgrade() {
|
|
|
|
pm2.read().tick()
|
|
|
|
} else {
|
|
|
|
Box::new(future::err(()))
|
|
|
|
}
|
|
|
|
})
|
2017-05-06 13:24:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
PubSubClient {
|
2017-06-13 17:36:39 +02:00
|
|
|
poll_manager,
|
2018-10-22 09:40:50 +02:00
|
|
|
executor,
|
2017-05-06 13:24:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-13 17:36:39 +02:00
|
|
|
impl PubSubClient<core::NoopMiddleware> {
|
|
|
|
/// Creates new `PubSubClient` with deterministic ids.
|
|
|
|
#[cfg(test)]
|
2018-10-22 09:40:50 +02:00
|
|
|
pub fn new_test(rpc: MetaIoHandler<Metadata, core::NoopMiddleware>, executor: Executor) -> Self {
|
|
|
|
let client = Self::new(MetaIoHandler::with_middleware(Default::default()), executor);
|
2017-06-13 17:36:39 +02:00
|
|
|
*client.poll_manager.write() = GenericPollManager::new_test(rpc);
|
|
|
|
client
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 13:24:18 +02:00
|
|
|
impl<S: core::Middleware<Metadata>> PubSub for PubSubClient<S> {
|
|
|
|
type Metadata = Metadata;
|
|
|
|
|
2017-06-13 16:29:35 +02:00
|
|
|
fn parity_subscribe(&self, mut meta: Metadata, subscriber: Subscriber<core::Value>, method: String, params: Trailing<core::Params>) {
|
|
|
|
let params = params.unwrap_or(core::Params::Array(vec![]));
|
2017-05-06 13:24:18 +02:00
|
|
|
// Make sure to get rid of PubSub session otherwise it will never be dropped.
|
|
|
|
meta.session = None;
|
|
|
|
|
|
|
|
let mut poll_manager = self.poll_manager.write();
|
|
|
|
let (id, receiver) = poll_manager.subscribe(meta, method, params);
|
2017-05-17 16:20:41 +02:00
|
|
|
match subscriber.assign_id(id.clone()) {
|
2017-05-06 13:24:18 +02:00
|
|
|
Ok(sink) => {
|
2018-10-22 09:40:50 +02:00
|
|
|
self.executor.spawn(receiver.forward(sink.sink_map_err(|e| {
|
2017-05-06 13:24:18 +02:00
|
|
|
warn!("Cannot send notification: {:?}", e);
|
|
|
|
})).map(|_| ()));
|
|
|
|
},
|
|
|
|
Err(_) => {
|
2017-05-17 16:20:41 +02:00
|
|
|
poll_manager.unsubscribe(&id);
|
2017-05-06 13:24:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 11:38:17 +01:00
|
|
|
fn parity_unsubscribe(&self, id: SubscriptionId) -> Result<bool> {
|
2017-05-17 16:20:41 +02:00
|
|
|
let res = self.poll_manager.write().unsubscribe(&id);
|
2017-10-05 12:35:01 +02:00
|
|
|
Ok(res)
|
2017-05-06 13:24:18 +02:00
|
|
|
}
|
|
|
|
}
|