mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-26 17:02:32 +01:00
This allows the client to drive its own execution, as one would expect.
Previously, the reactor had to be driven on a separate thread, which was confusing.
This has a couple notable side effects:
1. SyncClient is no longer `Clone`. This is because `reactor::Core`
is not `Clone`, and creating one is not infallible
(`Core::new` returns a `Result`).
2. SyncClient does not use the user-specified `client::Options::handle` or
`client::Options::remote`, because it constructs its own reactor.
58 lines
1.5 KiB
Rust
58 lines
1.5 KiB
Rust
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the MIT License, <LICENSE or http://opensource.org/licenses/MIT>.
|
|
// This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
#![feature(conservative_impl_trait, plugin)]
|
|
#![plugin(tarpc_plugins)]
|
|
|
|
extern crate futures;
|
|
#[macro_use]
|
|
extern crate tarpc;
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use tarpc::{client, server};
|
|
use tarpc::client::sync::Connect;
|
|
|
|
service! {
|
|
rpc hello(name: String) -> String | NoNameGiven;
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
pub struct NoNameGiven;
|
|
|
|
impl fmt::Display for NoNameGiven {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "{}", self.description())
|
|
}
|
|
}
|
|
|
|
impl Error for NoNameGiven {
|
|
fn description(&self) -> &str {
|
|
r#"The empty String, "", is not a valid argument to rpc `hello`."#
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct HelloServer;
|
|
|
|
impl SyncService for HelloServer {
|
|
fn hello(&self, name: String) -> Result<String, NoNameGiven> {
|
|
if name == "" {
|
|
Err(NoNameGiven)
|
|
} else {
|
|
Ok(format!("Hello, {}!", name))
|
|
}
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let addr = HelloServer.listen("localhost:10000", server::Options::default()).unwrap();
|
|
let mut client = SyncClient::connect(addr, client::Options::default()).unwrap();
|
|
println!("{}", client.hello("Mom".to_string()).unwrap());
|
|
println!("{}", client.hello("".to_string()).unwrap_err());
|
|
}
|