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
98 lines
2.4 KiB
Rust
98 lines
2.4 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 env_logger;
|
|
#[macro_use]
|
|
extern crate tarpc;
|
|
extern crate futures;
|
|
extern crate tokio_core;
|
|
|
|
use add::{SyncService as AddSyncService, SyncServiceExt as AddExt};
|
|
use double::{SyncService as DoubleSyncService, SyncServiceExt as DoubleExt};
|
|
use std::sync::mpsc;
|
|
use std::thread;
|
|
use tarpc::sync::{client, server};
|
|
use tarpc::sync::client::ClientExt as Fc;
|
|
use tarpc::util::{FirstSocketAddr, Message, Never};
|
|
|
|
pub mod add {
|
|
service! {
|
|
/// Add two ints together.
|
|
rpc add(x: i32, y: i32) -> i32;
|
|
}
|
|
}
|
|
|
|
pub mod double {
|
|
use tarpc::util::Message;
|
|
|
|
service! {
|
|
/// 2 * x
|
|
rpc double(x: i32) -> i32 | Message;
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct AddServer;
|
|
|
|
impl AddSyncService for AddServer {
|
|
fn add(&self, x: i32, y: i32) -> Result<i32, Never> {
|
|
Ok(x + y)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
struct DoubleServer {
|
|
client: add::SyncClient,
|
|
}
|
|
|
|
impl DoubleServer {
|
|
fn new(client: add::SyncClient) -> Self {
|
|
DoubleServer { client: client }
|
|
}
|
|
}
|
|
|
|
impl DoubleSyncService for DoubleServer {
|
|
fn double(&self, x: i32) -> Result<i32, Message> {
|
|
self.client
|
|
.add(x, x)
|
|
.map_err(|e| e.to_string().into())
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let _ = env_logger::init();
|
|
let (tx, rx) = mpsc::channel();
|
|
thread::spawn(move || {
|
|
let handle = AddServer.listen("localhost:0".first_socket_addr(),
|
|
server::Options::default())
|
|
.unwrap();
|
|
tx.send(handle.addr()).unwrap();
|
|
handle.run();
|
|
});
|
|
|
|
|
|
let add = rx.recv().unwrap();
|
|
let (tx, rx) = mpsc::channel();
|
|
thread::spawn(move || {
|
|
let add_client = add::SyncClient::connect(add, client::Options::default()).unwrap();
|
|
let handle = DoubleServer::new(add_client)
|
|
.listen("localhost:0".first_socket_addr(),
|
|
server::Options::default())
|
|
.unwrap();
|
|
tx.send(handle.addr()).unwrap();
|
|
handle.run();
|
|
});
|
|
let double = rx.recv().unwrap();
|
|
|
|
let double_client = double::SyncClient::connect(double, client::Options::default()).unwrap();
|
|
for i in 0..5 {
|
|
let doubled = double_client.double(i).unwrap();
|
|
println!("{:?}", doubled);
|
|
}
|
|
}
|