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-03-19 10:12:20 -07:00
|
|
|
use std::fmt::Debug;
|
2018-04-05 05:09:46 -07:00
|
|
|
use std::sync::Arc;
|
2018-03-20 09:32:19 -07:00
|
|
|
use crossbeam;
|
2018-04-02 13:26:40 -07:00
|
|
|
use crossbeam_channel as channel;
|
2018-03-20 09:32:19 -07:00
|
|
|
|
2018-03-19 10:12:20 -07:00
|
|
|
use proto::Message;
|
|
|
|
use task;
|
2018-04-05 05:09:46 -07:00
|
|
|
use messaging::{SourcedMessage};
|
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
|
|
|
where Vec<u8>: From<T>
|
|
|
|
{
|
|
|
|
/// The transmit side of the multiple producer channel from comms threads.
|
2018-04-05 05:09:46 -07:00
|
|
|
tx: &'a channel::Sender<SourcedMessage<T>>,
|
|
|
|
/// The receive side of the channel to the comms thread.
|
2018-04-02 13:26:40 -07:00
|
|
|
rx: &'a channel::Receiver<Message<T>>,
|
2018-03-19 10:12:20 -07:00
|
|
|
/// The socket IO task.
|
2018-04-04 05:49:37 -07:00
|
|
|
task: task::Task,
|
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
|
|
|
where Vec<u8>: From<T>
|
|
|
|
{
|
2018-04-05 05:09:46 -07:00
|
|
|
pub fn new(tx: &'a channel::Sender<SourcedMessage<T>>,
|
2018-04-02 13:26:40 -07:00
|
|
|
rx: &'a channel::Receiver<Message<T>>,
|
2018-04-04 05:49:37 -07:00
|
|
|
stream: ::std::net::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-03 15:08:26 -07:00
|
|
|
task: task::Task::new(stream),
|
|
|
|
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.
|
|
|
|
pub fn run(&mut self) {
|
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-04 05:49:37 -07:00
|
|
|
let mut task1 = self.task.try_clone().unwrap(); // FIXME: handle errors
|
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.
|
|
|
|
task1.send_message(message).unwrap();
|
|
|
|
debug!("SENT Node {}", node_index);
|
2018-04-04 04:49:11 -07:00
|
|
|
}
|
2018-03-20 09:32:19 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// Remote comms receive loop.
|
|
|
|
loop {
|
2018-04-04 05:49:37 -07:00
|
|
|
debug!("Starting remote RX loop for node {}", node_index);
|
|
|
|
match self.task.receive_message() {
|
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
|
|
|
|
})
|
|
|
|
.unwrap()
|
2018-04-04 04:18:57 -07:00
|
|
|
},
|
2018-03-20 09:32:19 -07:00
|
|
|
Err(task::Error::ProtobufError(e)) =>
|
|
|
|
warn!("Protobuf error {}", e),
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Critical error {:?}", e);
|
|
|
|
break;
|
|
|
|
}
|
2018-03-19 10:12:20 -07:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 09:32:19 -07:00
|
|
|
});
|
2018-03-19 10:12:20 -07:00
|
|
|
}
|
|
|
|
}
|