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-06 09:01:14 -07:00
|
|
|
use std::io;
|
2018-03-19 10:12:20 -07:00
|
|
|
use std::fmt::Debug;
|
2018-04-05 05:09:46 -07:00
|
|
|
use std::sync::Arc;
|
2018-04-06 08:04:28 -07:00
|
|
|
use std::net::TcpStream;
|
2018-03-20 09:32:19 -07:00
|
|
|
use crossbeam;
|
2018-04-06 08:04:28 -07:00
|
|
|
use crossbeam_channel::{Sender, Receiver};
|
2018-03-20 09:32:19 -07:00
|
|
|
|
2018-03-19 10:12:20 -07:00
|
|
|
use proto::Message;
|
2018-04-06 08:04:28 -07:00
|
|
|
use proto_io;
|
|
|
|
use proto_io::CodecIo;
|
|
|
|
use messaging::SourcedMessage;
|
2018-04-11 05:21:50 -07:00
|
|
|
use stream_io::StreamIo;
|
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 {
|
|
|
|
fn from(err: io::Error) -> Error { Error::IoError(err) }
|
|
|
|
}
|
|
|
|
|
2018-03-19 10:12:20 -07:00
|
|
|
/// A communication task connects a remote node to the thread that manages the
|
|
|
|
/// consensus algorithm.
|
2018-04-05 05:09:46 -07:00
|
|
|
pub struct CommsTask<'a, T: 'a + Clone + Debug + Send + Sync +
|
|
|
|
From<Vec<u8>> + Into<Vec<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-04-11 05:21:50 -07:00
|
|
|
io: CodecIo<T>,
|
2018-04-03 15:08:26 -07:00
|
|
|
/// The index of this comms task for identification against its remote node.
|
|
|
|
pub node_index: usize
|
2018-03-19 10:12:20 -07:00
|
|
|
}
|
|
|
|
|
2018-04-05 05:09:46 -07:00
|
|
|
impl<'a, T: Clone + Debug + Send + Sync + From<Vec<u8>> + Into<Vec<u8>>>
|
2018-04-04 05:49:37 -07:00
|
|
|
CommsTask<'a, T>
|
2018-03-19 10:12:20 -07:00
|
|
|
{
|
2018-04-06 08:04:28 -07:00
|
|
|
pub fn new(tx: &'a Sender<SourcedMessage<T>>,
|
|
|
|
rx: &'a Receiver<Message<T>>,
|
|
|
|
stream: TcpStream,
|
2018-04-03 15:08:26 -07:00
|
|
|
node_index: usize) ->
|
|
|
|
Self
|
|
|
|
{
|
2018-04-04 04:18:57 -07:00
|
|
|
debug!("Creating comms task #{} for {:?}", node_index,
|
|
|
|
stream.peer_addr().unwrap());
|
|
|
|
|
2018-03-19 10:12:20 -07:00
|
|
|
CommsTask {
|
|
|
|
tx: tx,
|
|
|
|
rx: rx,
|
2018-04-11 05:21:50 -07:00
|
|
|
io: StreamIo::from_stream(stream),
|
2018-04-03 15:08:26 -07:00
|
|
|
node_index: 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();
|
|
|
|
debug!("Node {} <- {:?}", node_index, message);
|
|
|
|
// 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) => {
|
|
|
|
debug!("Node {} -> {:?}", node_index, message);
|
2018-04-05 05:09:46 -07:00
|
|
|
tx.send(
|
|
|
|
SourcedMessage {
|
|
|
|
source: node_index,
|
|
|
|
message
|
2018-04-06 09:01:14 -07:00
|
|
|
}).unwrap();
|
2018-04-04 04:18:57 -07:00
|
|
|
},
|
2018-04-06 08:04:28 -07:00
|
|
|
Err(proto_io::Error::ProtobufError(e)) =>
|
2018-04-05 09:10:08 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|