2020-04-07 14:26:22 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use bytes::IntoBuf;
|
|
|
|
use futures::stream::futures_unordered::FuturesUnordered;
|
|
|
|
use futures::stream::StreamExt;
|
2020-04-10 20:01:48 +00:00
|
|
|
use futures_util::future::FutureExt;
|
|
|
|
use hyper::client::Client;
|
|
|
|
use hyper::{Body, Method, Request, StatusCode};
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
use crate::data::*;
|
|
|
|
use crate::error::Error;
|
|
|
|
use crate::membership::System;
|
2020-04-10 20:01:48 +00:00
|
|
|
use crate::proto::Message;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn rpc_call_many(
|
|
|
|
sys: Arc<System>,
|
|
|
|
to: &[UUID],
|
|
|
|
msg: &Message,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Vec<Result<Message, Error>> {
|
|
|
|
let mut resp_stream = to
|
|
|
|
.iter()
|
2020-04-07 14:26:22 +00:00
|
|
|
.map(|to| rpc_call(sys.clone(), to, msg, timeout))
|
|
|
|
.collect::<FuturesUnordered<_>>();
|
|
|
|
|
|
|
|
let mut results = vec![];
|
|
|
|
while let Some(resp) = resp_stream.next().await {
|
|
|
|
results.push(resp);
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
results
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn rpc_try_call_many(
|
|
|
|
sys: Arc<System>,
|
|
|
|
to: &[UUID],
|
|
|
|
msg: &Message,
|
|
|
|
stop_after: usize,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<Vec<Message>, Error> {
|
|
|
|
let mut resp_stream = to
|
|
|
|
.iter()
|
2020-04-07 22:39:07 +00:00
|
|
|
.map(|to| rpc_call(sys.clone(), to, msg, timeout))
|
|
|
|
.collect::<FuturesUnordered<_>>();
|
|
|
|
|
|
|
|
let mut results = vec![];
|
|
|
|
let mut errors = vec![];
|
|
|
|
|
|
|
|
while let Some(resp) = resp_stream.next().await {
|
|
|
|
match resp {
|
|
|
|
Ok(msg) => {
|
|
|
|
results.push(msg);
|
|
|
|
if results.len() >= stop_after {
|
2020-04-10 20:01:48 +00:00
|
|
|
break;
|
2020-04-07 22:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
errors.push(e);
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 22:39:07 +00:00
|
|
|
|
|
|
|
if results.len() >= stop_after {
|
|
|
|
Ok(results)
|
|
|
|
} else {
|
|
|
|
let mut msg = "Too many failures:".to_string();
|
|
|
|
for e in errors {
|
|
|
|
msg += &format!("\n{}", e);
|
|
|
|
}
|
|
|
|
Err(Error::Message(msg))
|
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn rpc_call(
|
|
|
|
sys: Arc<System>,
|
|
|
|
to: &UUID,
|
|
|
|
msg: &Message,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<Message, Error> {
|
2020-04-07 14:26:22 +00:00
|
|
|
let addr = {
|
2020-04-11 21:53:32 +00:00
|
|
|
let status = sys.status.borrow().clone();
|
|
|
|
match status.nodes.get(to) {
|
2020-04-07 14:26:22 +00:00
|
|
|
Some(status) => status.addr.clone(),
|
|
|
|
None => return Err(Error::Message(format!("Peer ID not found"))),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sys.rpc_client.call(&addr, msg, timeout).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RpcClient {
|
|
|
|
pub client: Client<hyper::client::HttpConnector, hyper::Body>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RpcClient {
|
|
|
|
pub fn new() -> Self {
|
2020-04-10 20:01:48 +00:00
|
|
|
RpcClient {
|
2020-04-07 14:26:22 +00:00
|
|
|
client: Client::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub async fn call(
|
|
|
|
&self,
|
|
|
|
to_addr: &SocketAddr,
|
|
|
|
msg: &Message,
|
|
|
|
timeout: Duration,
|
|
|
|
) -> Result<Message, Error> {
|
2020-04-09 21:45:07 +00:00
|
|
|
let uri = format!("http://{}/rpc", to_addr);
|
2020-04-07 14:26:22 +00:00
|
|
|
let req = Request::builder()
|
|
|
|
.method(Method::POST)
|
|
|
|
.uri(uri)
|
2020-04-09 16:43:53 +00:00
|
|
|
.body(Body::from(rmp_to_vec_all_named(msg)?))?;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
let resp_fut = self.client.request(req).fuse();
|
2020-04-07 14:26:22 +00:00
|
|
|
let resp = tokio::time::timeout(timeout, resp_fut).await??;
|
|
|
|
|
|
|
|
if resp.status() == StatusCode::OK {
|
|
|
|
let body = hyper::body::to_bytes(resp.into_body()).await?;
|
|
|
|
let msg = rmp_serde::decode::from_read::<_, Message>(body.into_buf())?;
|
|
|
|
match msg {
|
|
|
|
Message::Error(e) => Err(Error::RPCError(e)),
|
2020-04-10 20:01:48 +00:00
|
|
|
x => Ok(x),
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::RPCError(format!("Status code {}", resp.status())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|