2019-03-04 07:31:01 +00:00
|
|
|
use crate::error;
|
|
|
|
use crate::messages::NodeMessage;
|
2019-03-17 10:49:56 +00:00
|
|
|
use crossbeam_channel::{unbounded as channel, Sender, TryRecvError};
|
|
|
|
use futures::future;
|
|
|
|
use futures::prelude::*;
|
|
|
|
use libp2p::rpc;
|
2019-03-17 12:14:28 +00:00
|
|
|
use libp2p::{PeerId, RPCEvent};
|
2019-03-04 07:31:01 +00:00
|
|
|
use slog::debug;
|
2019-03-17 12:14:28 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::time::{Duration, Instant};
|
2019-03-04 07:31:01 +00:00
|
|
|
use sync::SimpleSync;
|
|
|
|
use types::Hash256;
|
2019-03-04 05:39:37 +00:00
|
|
|
|
2019-03-17 12:14:28 +00:00
|
|
|
/// Timeout for establishing a HELLO handshake.
|
|
|
|
const HELLO_TIMEOUT: Duration = Duration::from_secs(30);
|
|
|
|
|
2019-03-04 05:39:37 +00:00
|
|
|
/// Handles messages received from the network and client and organises syncing.
|
|
|
|
pub struct MessageHandler {
|
2019-03-04 07:31:01 +00:00
|
|
|
sync: SimpleSync,
|
2019-03-04 05:39:37 +00:00
|
|
|
//TODO: Implement beacon chain
|
|
|
|
//chain: BeaconChain
|
2019-03-17 12:14:28 +00:00
|
|
|
/// A mapping of peers we have sent a HELLO rpc request to
|
|
|
|
hello_requests: HashMap<PeerId, Instant>,
|
2019-03-17 10:49:56 +00:00
|
|
|
log: slog::Logger,
|
2019-03-04 05:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Types of messages the handler can receive.
|
2019-03-17 10:49:56 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2019-03-04 05:39:37 +00:00
|
|
|
pub enum HandlerMessage {
|
2019-03-17 12:14:28 +00:00
|
|
|
/// We have initiated a connection to a new peer.
|
|
|
|
PeerDialed(PeerId),
|
2019-03-04 05:39:37 +00:00
|
|
|
/// Peer has disconnected,
|
|
|
|
PeerDisconnected(PeerId),
|
|
|
|
/// A Node message has been received.
|
2019-03-04 07:31:01 +00:00
|
|
|
Message(PeerId, NodeMessage),
|
2019-03-17 10:49:56 +00:00
|
|
|
/// An RPC response/request has been received.
|
2019-03-17 12:14:28 +00:00
|
|
|
RPC(RPCEvent),
|
2019-03-04 07:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageHandler {
|
|
|
|
/// Initializes and runs the MessageHandler.
|
2019-03-17 10:49:56 +00:00
|
|
|
pub fn new(
|
|
|
|
executor: &tokio::runtime::TaskExecutor,
|
|
|
|
log: slog::Logger,
|
|
|
|
) -> error::Result<Sender<HandlerMessage>> {
|
2019-03-04 07:31:01 +00:00
|
|
|
debug!(log, "Service starting");
|
|
|
|
|
|
|
|
let (handler_send, handler_recv) = channel();
|
|
|
|
|
|
|
|
// Initialise sync and begin processing in thread
|
|
|
|
//TODO: Load genesis from BeaconChain
|
|
|
|
let temp_genesis = Hash256::zero();
|
|
|
|
|
2019-03-17 10:49:56 +00:00
|
|
|
// generate the Message handler
|
|
|
|
let sync = SimpleSync::new(temp_genesis);
|
|
|
|
//TODO: Initialise beacon chain
|
|
|
|
let mut handler = MessageHandler {
|
|
|
|
sync,
|
2019-03-17 12:14:28 +00:00
|
|
|
hello_requests: HashMap::new(),
|
2019-03-17 10:49:56 +00:00
|
|
|
log: log.clone(),
|
|
|
|
};
|
2019-03-04 07:31:01 +00:00
|
|
|
|
2019-03-17 10:49:56 +00:00
|
|
|
// spawn handler task
|
|
|
|
// TODO: Handle manual termination of thread
|
|
|
|
executor.spawn(future::poll_fn(move || -> Result<_, _> {
|
|
|
|
loop {
|
|
|
|
handler.handle_message(handler_recv.recv().map_err(|_| {
|
|
|
|
debug!(log, "Handler channel closed. Handler terminating");
|
|
|
|
})?);
|
|
|
|
}
|
|
|
|
}));
|
2019-03-04 07:31:01 +00:00
|
|
|
|
|
|
|
Ok(handler_send)
|
|
|
|
}
|
2019-03-17 10:49:56 +00:00
|
|
|
|
|
|
|
fn handle_message(&mut self, message: HandlerMessage) {
|
|
|
|
debug!(self.log, "Message received {:?}", message);
|
|
|
|
}
|
2019-03-04 05:39:37 +00:00
|
|
|
}
|