use futures::Future;
use async::Executor;
use call::{Call, Method};
use call::client::{CallOption, ClientCStreamReceiver, ClientCStreamSender, ClientDuplexReceiver,
ClientDuplexSender, ClientSStreamReceiver, ClientUnaryReceiver};
use channel::Channel;
use error::Result;
pub struct Client {
channel: Channel,
}
impl Client {
pub fn new(channel: Channel) -> Client {
Client { channel: channel }
}
pub fn unary_call<P, Q>(&self, method: &Method<P, Q>, req: &P, opt: CallOption) -> Result<Q> {
let f = self.unary_call_async(method, req, opt)?;
f.wait()
}
pub fn unary_call_async<P, Q>(
&self,
method: &Method<P, Q>,
req: &P,
opt: CallOption,
) -> Result<ClientUnaryReceiver<Q>> {
Call::unary_async(&self.channel, method, req, opt)
}
pub fn client_streaming<P, Q>(
&self,
method: &Method<P, Q>,
opt: CallOption,
) -> Result<(ClientCStreamSender<P>, ClientCStreamReceiver<Q>)> {
Call::client_streaming(&self.channel, method, opt)
}
pub fn server_streaming<P, Q>(
&self,
method: &Method<P, Q>,
req: &P,
opt: CallOption,
) -> Result<ClientSStreamReceiver<Q>> {
Call::server_streaming(&self.channel, method, req, opt)
}
pub fn duplex_streaming<P, Q>(
&self,
method: &Method<P, Q>,
opt: CallOption,
) -> Result<(ClientDuplexSender<P>, ClientDuplexReceiver<Q>)> {
Call::duplex_streaming(&self.channel, method, opt)
}
pub fn spawn<F>(&self, f: F)
where
F: Future<Item = (), Error = ()> + Send + 'static,
{
Executor::new(self.channel.cq()).spawn(f)
}
}