extern crate rs_jsonrpc_core; #[macro_use] extern crate rs_jsonrpc_macros; extern crate rs_jsonrpc_tcp_server; use std::collections::BTreeMap; use rs_jsonrpc_core::{futures, MetaIoHandler, Metadata, Error, Value, Result}; use rs_jsonrpc_core::futures::future::FutureResult; #[derive(Clone, Default)] struct Meta(String); impl Metadata for Meta {} build_rpc_trait! { pub trait Rpc { type Metadata; /// 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, rs_jsonrpc_macros::Trailing) -> 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 add(&self, a: u64, b: u64) -> Result { Ok(a + b) } fn mul(&self, a: u64, b: rs_jsonrpc_macros::Trailing) -> 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 = rs_jsonrpc_tcp_server::ServerBuilder::new(io) .session_meta_extractor(|context: &rs_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() }