use std::collections::BTreeMap; use jsonrpc_core::{futures, MetaIoHandler, Metadata, Error, Value, Result}; use jsonrpc_core::futures::future::FutureResult; use jsonrpc_derive::rpc; #[derive(Clone)] struct Meta(String); impl Metadata for Meta {} #[rpc] pub trait Rpc { type Metadata; /// Get One type. #[rpc(name = "getOne")] fn one(&self) -> Result; /// Adds two numbers and returns a result #[rpc(name = "add")] fn add(&self, _: u64, _: u64) -> Result; /// Multiplies two numbers. Second number is optional. #[rpc(name = "mul")] fn mul(&self, _: u64, _: Option) -> Result; /// Performs asynchronous operation #[rpc(name = "callAsync")] fn call(&self, _: u64) -> FutureResult; /// Performs asynchronous operation with meta #[rpc(meta, name = "callAsyncMeta", alias("callAsyncMetaAlias"))] fn call_meta(&self, _: Self::Metadata, _: BTreeMap) -> FutureResult; } struct RpcImpl; impl Rpc for RpcImpl { type Metadata = Meta; fn one(&self) -> Result { Ok(100) } fn add(&self, a: u64, b: u64) -> Result { Ok(a + b) } fn mul(&self, a: u64, b: Option) -> Result { Ok(a * b.unwrap_or(1)) } fn call(&self, x: u64) -> FutureResult { futures::finished(format!("OK: {}", x)) } fn call_meta(&self, meta: Self::Metadata, map: BTreeMap) -> FutureResult { futures::finished(format!("From: {}, got: {:?}", meta.0, map)) } } fn main() { let mut io = MetaIoHandler::default(); let rpc = RpcImpl; io.extend_with(rpc.to_delegate()); let server = jsonrpc_tcp_server::ServerBuilder ::with_meta_extractor(io, |context: &jsonrpc_tcp_server::RequestContext| { Meta(format!("{}", context.peer_addr)) }) .start(&"0.0.0.0:3030".parse().unwrap()) .expect("Server must start with no issues"); server.wait() }