Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update stdio.
  • Loading branch information
tomusdrw committed Jul 13, 2020
commit 84fcfbd2edca2c37f35e642e4d9fa79f84b7643e
2 changes: 1 addition & 1 deletion stdio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/paritytech/jsonrpc"
version = "14.2.0"

[dependencies]
futures = "0.1.23"
futures = { version = "0.3", features = [ "compat" ] }
jsonrpc-core = { version = "14.2", path = "../core" }
log = "0.4"
tokio = "0.1.7"
Expand Down
2 changes: 1 addition & 1 deletion stdio/examples/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use jsonrpc_stdio_server::ServerBuilder;

fn main() {
let mut io = IoHandler::default();
io.add_method("say_hello", |_params| Ok(Value::String("hello".to_owned())));
io.add_sync_method("say_hello", |_params| Ok(Value::String("hello".to_owned())));

ServerBuilder::new(io).build();
}
44 changes: 24 additions & 20 deletions stdio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//!
//! fn main() {
//! let mut io = IoHandler::default();
//! io.add_method("say_hello", |_params| {
//! io.add_sync_method("say_hello", |_params| {
//! Ok(Value::String("hello".to_owned()))
//! });
//!
Expand All @@ -24,22 +24,23 @@ extern crate log;

pub use jsonrpc_core;

use jsonrpc_core::IoHandler;
use jsonrpc_core::{MetaIoHandler, Middleware, Metadata};
use std::sync::Arc;
use tokio::prelude::{Future, Stream};
use tokio_codec::{FramedRead, FramedWrite, LinesCodec};

/// Stdio server builder
pub struct ServerBuilder {
handler: Arc<IoHandler>,
pub struct ServerBuilder<M: Metadata = (), T: Middleware<M> = jsonrpc_core::NoopMiddleware> {
handler: Arc<MetaIoHandler<M, T>>,
}

impl ServerBuilder {
impl<M: Metadata, T: Middleware<M>> ServerBuilder<M, T> where
M: Default,
T::Future: Unpin,
T::CallFuture: Unpin,
{
/// Returns a new server instance
pub fn new<T>(handler: T) -> Self
where
T: Into<IoHandler>,
{
pub fn new(handler: impl Into<MetaIoHandler<M, T>>) -> Self {
ServerBuilder {
handler: Arc::new(handler.into()),
}
Expand All @@ -57,22 +58,25 @@ impl ServerBuilder {

let handler = self.handler.clone();
let future = framed_stdin
.and_then(move |line| process(&handler, line).map_err(|_| unreachable!()))
.and_then(move |line| Self::process(&handler, line).map_err(|_| unreachable!()))
.forward(framed_stdout)
.map(|_| ())
.map_err(|e| panic!("{:?}", e));

tokio::run(future);
}
}

/// Process a request asynchronously
fn process(io: &Arc<IoHandler>, input: String) -> impl Future<Item = String, Error = ()> + Send {
io.handle_request(&input).map(move |result| match result {
Some(res) => res,
None => {
info!("JSON RPC request produced no response: {:?}", input);
String::from("")
}
})
/// Process a request asynchronously
fn process(io: &Arc<MetaIoHandler<M, T>>, input: String) -> impl Future<Item = String, Error = ()> + Send {
use jsonrpc_core::futures::FutureExt;
let f = io.handle_request(&input, Default::default());
futures::compat::Compat::new(f.map(Ok))
.map(move |result| match result {
Some(res) => res,
None => {
info!("JSON RPC request produced no response: {:?}", input);
String::from("")
}
})
}
}