mirror of
https://github.com/OMGeeky/tarpc.git
synced 2025-12-26 17:02:32 +01:00
* Change `client::{future, sync}, server::{future, sync}` to `future::{client, server}, sync::{client, server}`
This reflects the most common usage pattern and allows for some types to not need to be fully qualified when used together (e.g. the previously-named `client::future::Options` and `server::future::Options` can now be `client::Options` and `server::Options`).
* sync::client: create a RequestHandler struct to encapsulate logic of processing client requests.
The largest benefit is that unit testing becomes easier, e.g. testing that the request processing stops when all request senders are dropped.
* Rename Serialize error variants to make sense.
* Rename tarpc_service_ConnectFuture__ => Connect (because it's public)
* Use tokio proto's ClientProxy.
Rather than reimplement the same logic. Curiously, this commit
isn't a net loss in LOC. Oh well.
* Factor out os-specific functionality in listener() into their own fns
* Remove service-fn dep
66 lines
1.7 KiB
Rust
66 lines
1.7 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(plugin)]
|
|
#![plugin(tarpc_plugins)]
|
|
|
|
extern crate futures;
|
|
#[macro_use]
|
|
extern crate tarpc;
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate tokio_core;
|
|
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use std::sync::mpsc;
|
|
use std::thread;
|
|
use tarpc::sync::{client, server};
|
|
use tarpc::sync::client::ClientExt;
|
|
|
|
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 (tx, rx) = mpsc::channel();
|
|
thread::spawn(move || {
|
|
let handle = HelloServer.listen("localhost:10000", server::Options::default()).unwrap();
|
|
tx.send(handle.addr()).unwrap();
|
|
handle.run();
|
|
});
|
|
let client = SyncClient::connect(rx.recv().unwrap(), client::Options::default()).unwrap();
|
|
println!("{}", client.hello("Mom".to_string()).unwrap());
|
|
println!("{}", client.hello("".to_string()).unwrap_err());
|
|
}
|