Created
February 4, 2017 19:08
-
-
Save compressed/df4fe83a877f9522386ee30147278262 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(conservative_impl_trait, plugin)] | |
#![plugin(tarpc_plugins)] | |
extern crate futures; | |
#[macro_use] | |
extern crate tarpc; | |
extern crate tokio_core; | |
use app1::FutureServiceExt; | |
use app2::FutureServiceExt as F; | |
use futures::Future; | |
use tarpc::{client, server}; | |
use tarpc::client::future::Connect; | |
use tarpc::util::{FirstSocketAddr, Never}; | |
use tokio_core::reactor; | |
mod stub { | |
service! { | |
rpc hello(name: String) -> String; | |
} | |
} | |
mod app1 { | |
use super::*; | |
service! { | |
rpc hello(name: String) -> String; // order matters | |
rpc goodbye(name: String) -> String; | |
} | |
#[derive(Clone)] | |
pub struct HelloServer; | |
impl FutureService for HelloServer { | |
type HelloFut = futures::Finished<String, Never>; | |
type GoodbyeFut = futures::Finished<String, Never>; | |
fn hello(&self, name: String) -> Self::HelloFut { | |
futures::finished(format!("app1: Hello, {}!", name)) | |
} | |
fn goodbye(&self, name: String) -> Self::GoodbyeFut { | |
futures::finished(format!("Goodbye, {}!", name)) | |
} | |
} | |
} | |
mod app2 { | |
use super::*; | |
service! { | |
rpc hello(name: String) -> String; | |
rpc cya(name: String) -> String; | |
} | |
#[derive(Clone)] | |
pub struct HelloServer; | |
impl FutureService for HelloServer { | |
type HelloFut = futures::Finished<String, Never>; | |
type CyaFut = futures::Finished<String, Never>; | |
fn hello(&self, name: String) -> Self::HelloFut { | |
futures::finished(format!("app2: Hello, {}!", name)) | |
} | |
fn cya(&self, name: String) -> Self::CyaFut { | |
futures::finished(format!("Cya, {}!", name)) | |
} | |
} | |
} | |
fn main() { | |
let addr = "localhost:10000".first_socket_addr(); | |
let addr2 = "localhost:10001".first_socket_addr(); | |
let mut core = reactor::Core::new().unwrap(); | |
app1::HelloServer.listen(addr, server::Options::default().handle(core.handle())) | |
.wait() | |
.unwrap(); | |
app2::HelloServer.listen(addr2, server::Options::default().handle(core.handle())) | |
.wait() | |
.unwrap(); | |
let options = client::Options::default().handle(core.handle()); | |
let options2 = client::Options::default().handle(core.handle()); | |
core.run(stub::FutureClient::connect(addr, options) | |
.map_err(tarpc::Error::from) | |
.and_then(|client| client.hello("Mom".to_string())) | |
.map(|resp| println!("{}", resp)) | |
.join(stub::FutureClient::connect(addr2, options2) | |
.map_err(tarpc::Error::from) | |
.and_then(|client| client.hello("Mom".to_string())) | |
.map(|resp| println!("{}", resp)))) | |
.unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment