2018-03-19 10:12:20 -07:00
|
|
|
//! Comms task structure. A comms task communicates with a remote node through a
|
|
|
|
//! socket. Local communication with coordinating threads is made via
|
2018-04-04 04:49:11 -07:00
|
|
|
//! `crossbeam_channel::unbounded()`.
|
2018-04-30 08:55:51 -07:00
|
|
|
use crossbeam;
|
|
|
|
use crossbeam_channel::{Receiver, Sender};
|
2018-03-19 10:12:20 -07:00
|
|
|
use std::fmt::Debug;
|
2018-04-30 08:55:51 -07:00
|
|
|
use std::io;
|
2018-04-06 08:04:28 -07:00
|
|
|
use std::net::TcpStream;
|
2018-04-30 08:55:51 -07:00
|
|
|
use std::sync::Arc;
|
2018-03-20 09:32:19 -07:00
|
|
|
|
2018-05-05 06:39:32 -07:00
|
|
|
use hbbft::messaging::SourcedMessage;
|
|
|
|
use hbbft::proto::Message;
|
|
|
|
use hbbft::proto_io::{self, ProtoIo};
|
2018-03-19 10:12:20 -07:00
|
|
|
|
2018-04-06 09:01:14 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
IoError(io::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
2018-04-30 08:55:51 -07:00
|
|
|
fn from(err: io::Error) -> Error {
|
|
|
|
Error::IoError(err)
|
|
|
|
}
|
2018-04-06 09:01:14 -07:00
|
|
|
}
|
|
|
|
|
2018-03-19 10:12:20 -07:00
|
|
|
/// A communication task connects a remote node to the thread that manages the
|
|
|
|
/// consensus algorithm.
|
2018-05-08 07:20:32 -07:00
|
|
|
pub struct CommsTask<'a, T: 'a + Clone + Debug + Send + Sync + From<Vec<u8>> + AsRef<[u8]>> {
|
2018-03-19 10:12:20 -07:00
|
|
|
/// The transmit side of the multiple producer channel from comms threads.
|
2018-04-06 08:04:28 -07:00
|
|
|
tx: &'a Sender<SourcedMessage<T>>,
|
2018-04-05 05:09:46 -07:00
|
|
|
/// The receive side of the channel to the comms thread.
|
2018-04-06 08:04:28 -07:00
|
|
|
rx: &'a Receiver<Message<T>>,
|
2018-03-19 10:12:20 -07:00
|
|
|
/// The socket IO task.
|
2018-05-05 06:39:32 -07:00
|
|
|
io: ProtoIo<TcpStream>,
|
2018-04-03 15:08:26 -07:00
|
|
|
/// The index of this comms task for identification against its remote node.
|
2018-04-30 08:55:51 -07:00
|
|
|
pub node_index: usize,
|
2018-03-19 10:12:20 -07:00
|
|
|
}
|
|
|
|
|
2018-05-08 07:20:32 -07:00
|
|
|
impl<'a, T: 'a + Clone + Debug + Send + Sync + From<Vec<u8>> + AsRef<[u8]>> CommsTask<'a, T> {
|
2018-04-30 08:55:51 -07:00
|
|
|
pub fn new(
|
|
|
|
tx: &'a Sender<SourcedMessage<T>>,
|
|
|
|
rx: &'a Receiver<Message<T>>,
|
|
|
|
stream: TcpStream,
|
|
|
|
node_index: usize,
|
|
|
|
) -> Self {
|
|
|
|
debug!(
|
|
|
|
"Creating comms task #{} for {:?}",
|
|
|
|
node_index,
|
|
|
|
stream.peer_addr().unwrap()
|
|
|
|
);
|
2018-04-04 04:18:57 -07:00
|
|
|
|
2018-03-19 10:12:20 -07:00
|
|
|
CommsTask {
|
2018-04-29 06:27:40 -07:00
|
|
|
tx,
|
|
|
|
rx,
|
2018-04-12 09:17:33 -07:00
|
|
|
io: ProtoIo::from_stream(stream),
|
2018-04-30 08:55:51 -07:00
|
|
|
node_index,
|
2018-03-19 10:12:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The main socket IO loop and an asynchronous thread responding to manager
|
|
|
|
/// thread requests.
|
2018-04-06 09:01:14 -07:00
|
|
|
pub fn run(&mut self) -> Result<(), Error> {
|
2018-03-20 09:32:19 -07:00
|
|
|
// Borrow parts of `self` before entering the thread binding scope.
|
2018-04-02 13:26:40 -07:00
|
|
|
let tx = Arc::new(self.tx);
|
|
|
|
let rx = Arc::new(self.rx);
|
2018-04-06 09:01:14 -07:00
|
|
|
let mut io1 = self.io.try_clone()?;
|
2018-04-03 15:08:26 -07:00
|
|
|
let node_index = self.node_index;
|
2018-03-20 09:32:19 -07:00
|
|
|
|
|
|
|
crossbeam::scope(|scope| {
|
|
|
|
// Local comms receive loop thread.
|
|
|
|
scope.spawn(move || {
|
2018-04-04 05:49:37 -07:00
|
|
|
loop {
|
2018-04-05 05:09:46 -07:00
|
|
|
// Receive a multicast message from the manager thread.
|
|
|
|
let message = rx.recv().unwrap();
|
|
|
|
// Forward the message to the remote node.
|
2018-04-11 05:21:50 -07:00
|
|
|
io1.send(message).unwrap();
|
2018-04-04 04:49:11 -07:00
|
|
|
}
|
2018-03-20 09:32:19 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Remote comms receive loop.
|
2018-04-05 09:10:08 -07:00
|
|
|
debug!("Starting remote RX loop for node {}", node_index);
|
2018-03-20 09:32:19 -07:00
|
|
|
loop {
|
2018-04-11 05:21:50 -07:00
|
|
|
match self.io.recv() {
|
2018-04-04 04:18:57 -07:00
|
|
|
Ok(message) => {
|
2018-04-30 08:55:51 -07:00
|
|
|
tx.send(SourcedMessage {
|
|
|
|
source: node_index,
|
|
|
|
message,
|
|
|
|
}).unwrap();
|
|
|
|
}
|
|
|
|
Err(proto_io::Error::ProtobufError(e)) => {
|
|
|
|
warn!("Node {} - Protobuf error {}", node_index, e)
|
|
|
|
}
|
2018-03-20 09:32:19 -07:00
|
|
|
Err(e) => {
|
2018-04-05 09:10:08 -07:00
|
|
|
warn!("Node {} - Critical error {:?}", node_index, e);
|
2018-03-20 09:32:19 -07:00
|
|
|
break;
|
|
|
|
}
|
2018-03-19 10:12:20 -07:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 09:32:19 -07:00
|
|
|
});
|
2018-04-06 09:01:14 -07:00
|
|
|
Ok(())
|
2018-03-19 10:12:20 -07:00
|
|
|
}
|
|
|
|
}
|