Files
tarpc/examples/two_clients.rs
Tim 7aabfb3c14 Rewrite using tokio (#44)
* Rewrite tarpc on top of tokio.

* Add examples

* Move error types to their own module.

Also, cull unused error variants.

* Remove unused fn

* Remove CanonicalRpcError* types. They're 100% useless.

* Track tokio master (WIP)

* The great error revamp.

Removed the canonical rpc error type. Instead, the user declares
the error type for each rpc:

In the above example, the error type is Baz. Declaring an error is
optional; if none is specified, it defaults to Never, a convenience
struct that wraps the never type (exclamation mark) to impl Serialize, Deserialize,
Error, etc. Also adds the convenience type StringError for easily
using a String as an error type.

* Add missing license header

* Minor cleanup

* Rename StringError => Message

* Create a sync::Connect trait.

Along with this, the existing Connect trait moves to future::Connect. The future
and sync modules are reexported from the crate root.

Additionally, the utility errors Never and Message are no longer reexported from
the crate root.

* Update readme

* Track tokio/futures master. Add a Spawn utility trait to replace the removed forget.

* Fix pre-push hook

* Add doc comment to SyncServiceExt.

* Fix up some documentation

* Track tokio-proto master

* Don't set tcp nodelay

* Make future::Connect take an associated type for the future.

* Unbox FutureClient::connect return type

* Use type alias instead of newtype struct for ClientFuture

* Fix benches/latency.rs

* Write a plugin to convert lower_snake_case idents/types to UpperCamelCase.

Use it to add associated types to FutureService instead of boxing the return futures.

* Specify plugin = true in snake_to_camel/Cargo.toml. Weird things happen otherwise.

* Add clippy.toml
2016-09-04 16:09:50 -07:00

79 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(conservative_impl_trait, plugin)]
#![plugin(snake_to_camel)]
#[macro_use]
extern crate log;
#[macro_use]
extern crate tarpc;
extern crate serde;
extern crate bincode;
extern crate env_logger;
extern crate futures;
use bar::FutureServiceExt as BarExt;
use baz::FutureServiceExt as BazExt;
use tarpc::util::Never;
use tarpc::sync::Connect;
mod bar {
service! {
rpc bar(i: i32) -> i32;
}
}
#[derive(Clone)]
struct Bar;
impl bar::FutureService for Bar {
type Bar = futures::Finished<i32, Never>;
fn bar(&self, i: i32) -> Self::Bar {
futures::finished(i)
}
}
mod baz {
service! {
rpc baz(s: String) -> String;
}
}
#[derive(Clone)]
struct Baz;
impl baz::FutureService for Baz {
type Baz = futures::Finished<String, Never>;
fn baz(&self, s: String) -> Self::Baz {
futures::finished(format!("Hello, {}!", s))
}
}
macro_rules! pos {
() => (concat!(file!(), ":", line!()))
}
fn main() {
let _ = env_logger::init();
let bar = Bar.listen("localhost:0").unwrap();
let baz = Baz.listen("localhost:0").unwrap();
let bar_client = bar::SyncClient::connect(bar.local_addr()).unwrap();
let baz_client = baz::SyncClient::connect(baz.local_addr()).unwrap();
info!("Result: {:?}", bar_client.bar(&17));
let total = 20;
for i in 1..(total + 1) {
if i % 2 == 0 {
info!("Result 1: {:?}", bar_client.bar(&i));
} else {
info!("Result 2: {:?}", baz_client.baz(&i.to_string()));
}
}
info!("Done.");
}