Files
tarpc/examples/pubsub.rs
Tim be5f55c5f6 Extend snake_to_camel plugin to replace {} in the doc string with the original snake-cased ident. (#50)
* Extend snake_to_camel plugin to replace {} in the doc string with the origin snake-cased ident.

Also, track tokio-rs master.

This is really ad-hoc, undiscoverable, and unintuitive, but there's no way to programmatically create doc strings
in regular code, and I want to produce better doc strings for the associated types.

Given `fn foo_bar`:

Before: `/// The type of future returned by the function of the same name.`
After: ``/// The type of future returned by `{}`.``
    => `/// The type of future returned by foo_bar.`

* Fix some docs

* Use a helper fn on pipeline::Frame instead of handrolled match.

* Don't hide docs for ClientFuture.

It's exposed in the Connect impl of FutureService -- the tradeoff for not generating *another* item -- and hiding it breaks doc links.

* Formatting

* Rename snake_to_camel plugin => tarpc-plugins

* Update README

* Mangle a lot of names in macro expansion.

To lower the chance of any issues, prefix idents in service expansion with __tarpc_service.
In future_enum, prefix with __future_enum. The pattern is basically __macro_name_ident.

Any imported enum variant will conflict with a let binding or a function arg, so we basically
can't use any generic idents at all. Example:

    enum Req { request(..) }
    use self::Req::request;

    fn make_request(request: Request) { ... }

                    ^^^^^^^ conflict here

Additionally, suffix generated associated types with Fut to avoid conflicts with camelcased rpcs.
Why someone would do that, I don't know, but we shouldn't allow that wart.
2016-09-14 01:19:24 -07:00

135 lines
3.9 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 env_logger;
extern crate futures;
#[macro_use]
extern crate tarpc;
extern crate tokio_proto as tokio;
use futures::{BoxFuture, Future};
use publisher::FutureServiceExt as PublisherExt;
use subscriber::FutureServiceExt as SubscriberExt;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use tarpc::util::{Never, Message};
use tarpc::future::Connect as Fc;
use tarpc::sync::Connect as Sc;
pub mod subscriber {
service! {
rpc receive(message: String);
}
}
pub mod publisher {
use std::net::SocketAddr;
use tarpc::util::Message;
service! {
rpc broadcast(message: String);
rpc subscribe(id: u32, address: SocketAddr) | Message;
rpc unsubscribe(id: u32);
}
}
#[derive(Clone, Debug)]
struct Subscriber {
id: u32,
publisher: publisher::SyncClient,
}
impl subscriber::FutureService for Subscriber {
type ReceiveFut = futures::Finished<(), Never>;
fn receive(&self, message: String) -> Self::ReceiveFut {
println!("{} received message: {}", self.id, message);
futures::finished(())
}
}
impl Subscriber {
fn new(id: u32, publisher: publisher::SyncClient) -> tokio::server::ServerHandle {
let subscriber = Subscriber {
id: id,
publisher: publisher.clone(),
}
.listen("localhost:0")
.wait()
.unwrap();
publisher.subscribe(&id, &subscriber.local_addr()).unwrap();
subscriber
}
}
#[derive(Clone, Debug)]
struct Publisher {
clients: Arc<Mutex<HashMap<u32, subscriber::FutureClient>>>,
}
impl Publisher {
fn new() -> Publisher {
Publisher { clients: Arc::new(Mutex::new(HashMap::new())) }
}
}
impl publisher::FutureService for Publisher {
type BroadcastFut = BoxFuture<(), Never>;
fn broadcast(&self, message: String) -> Self::BroadcastFut {
futures::collect(self.clients
.lock()
.unwrap()
.values()
// Ignore failing subscribers.
.map(move |client| client.receive(&message).then(|_| Ok(())))
.collect::<Vec<_>>())
.map(|_| ())
.boxed()
}
type SubscribeFut = BoxFuture<(), Message>;
fn subscribe(&self, id: u32, address: SocketAddr) -> Self::SubscribeFut {
let clients = self.clients.clone();
subscriber::FutureClient::connect(&address)
.map(move |subscriber| {
println!("Subscribing {}.", id);
clients.lock().unwrap().insert(id, subscriber);
()
})
.map_err(|e| e.to_string().into())
.boxed()
}
type UnsubscribeFut = BoxFuture<(), Never>;
fn unsubscribe(&self, id: u32) -> Self::UnsubscribeFut {
println!("Unsubscribing {}", id);
self.clients.lock().unwrap().remove(&id).unwrap();
futures::finished(()).boxed()
}
}
fn main() {
let _ = env_logger::init();
let publisher = Publisher::new().listen("localhost:0").wait().unwrap();
let publisher = publisher::SyncClient::connect(publisher.local_addr()).unwrap();
let _subscriber1 = Subscriber::new(0, publisher.clone());
let _subscriber2 = Subscriber::new(1, publisher.clone());
println!("Broadcasting...");
publisher.broadcast(&"hello to all".to_string()).unwrap();
publisher.unsubscribe(&1).unwrap();
publisher.broadcast(&"hello again".to_string()).unwrap();
thread::sleep(Duration::from_millis(300));
}