New network IO API

This commit is contained in:
arkpar 2016-01-13 15:10:48 +01:00
parent fc25330804
commit 0de2a031d1
2 changed files with 29 additions and 19 deletions

View File

@ -1,6 +1,7 @@
use client::BlockChainClient;
use util::network::{HandlerIo, PeerId, PacketId,};
use util::{NetworkContext, PeerId, PacketId,};
use util::error::UtilError;
use sync::SyncMessage;
/// IO interface for the syning handler.
/// Provides peer connection management and an interface to the blockchain client.
@ -16,15 +17,15 @@ pub trait SyncIo {
fn chain<'s>(&'s mut self) -> &'s mut BlockChainClient;
}
/// Wraps `HandlerIo` and the blockchain client
pub struct NetSyncIo<'s, 'h> where 'h:'s {
network: &'s mut HandlerIo<'h>,
/// Wraps `NetworkContext` and the blockchain client
pub struct NetSyncIo<'s, 'h, 'io> where 'h: 's, 'io: 'h {
network: &'s mut NetworkContext<'h, 'io, SyncMessage>,
chain: &'s mut BlockChainClient
}
impl<'s, 'h> NetSyncIo<'s, 'h> {
/// Creates a new instance from the `HandlerIo` and the blockchain client reference.
pub fn new(network: &'s mut HandlerIo<'h>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h> {
impl<'s, 'h, 'io> NetSyncIo<'s, 'h, 'io> {
/// Creates a new instance from the `NetworkContext` and the blockchain client reference.
pub fn new(network: &'s mut NetworkContext<'h, 'io, SyncMessage>, chain: &'s mut BlockChainClient) -> NetSyncIo<'s,'h,'io> {
NetSyncIo {
network: network,
chain: chain,
@ -32,7 +33,7 @@ impl<'s, 'h> NetSyncIo<'s, 'h> {
}
}
impl<'s, 'h> SyncIo for NetSyncIo<'s, 'h> {
impl<'s, 'h, 'op> SyncIo for NetSyncIo<'s, 'h, 'op> {
fn disable_peer(&mut self, peer_id: &PeerId) {
self.network.disable_peer(*peer_id);
}

View File

@ -24,7 +24,9 @@
use std::sync::Arc;
use client::BlockChainClient;
use util::network::{ProtocolHandler, NetworkService, HandlerIo, TimerToken, PeerId, Message};
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId};
use util::TimerToken;
use util::Bytes;
use sync::chain::ChainSync;
use sync::io::NetSyncIo;
@ -35,6 +37,13 @@ mod range_collection;
#[cfg(test)]
mod tests;
/// Message type for external events
pub enum SyncMessage {
/// New block has been imported into the blockchain
NewBlock(Bytes)
}
/// Ethereum network protocol handler
pub struct EthSync {
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint
@ -47,7 +56,7 @@ pub use self::chain::SyncStatus;
impl EthSync {
/// Creates and register protocol with the network service
pub fn register(service: &mut NetworkService, chain: Arc<BlockChainClient + Send + Sized>) {
pub fn register(service: &mut NetworkService<SyncMessage>, chain: Arc<BlockChainClient + Send + Sized>) {
let sync = Box::new(EthSync {
chain: chain,
sync: ChainSync::new(),
@ -61,39 +70,39 @@ impl EthSync {
}
/// Stop sync
pub fn stop(&mut self, io: &mut HandlerIo) {
pub fn stop(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.abort(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
}
/// Restart sync
pub fn restart(&mut self, io: &mut HandlerIo) {
pub fn restart(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
}
}
impl ProtocolHandler for EthSync {
fn initialize(&mut self, io: &mut HandlerIo) {
impl NetworkProtocolHandler<SyncMessage> for EthSync {
fn initialize(&mut self, io: &mut NetworkContext<SyncMessage>) {
self.sync.restart(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
io.register_timer(1000).unwrap();
}
fn read(&mut self, io: &mut HandlerIo, peer: &PeerId, packet_id: u8, data: &[u8]) {
fn read(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId, packet_id: u8, data: &[u8]) {
self.sync.on_packet(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer, packet_id, data);
}
fn connected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
fn connected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) {
self.sync.on_peer_connected(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
}
fn disconnected(&mut self, io: &mut HandlerIo, peer: &PeerId) {
fn disconnected(&mut self, io: &mut NetworkContext<SyncMessage>, peer: &PeerId) {
self.sync.on_peer_aborting(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()), peer);
}
fn timeout(&mut self, io: &mut HandlerIo, _timer: TimerToken) {
fn timeout(&mut self, io: &mut NetworkContext<SyncMessage>, _timer: TimerToken) {
self.sync.maintain_sync(&mut NetSyncIo::new(io, Arc::get_mut(&mut self.chain).unwrap()));
}
fn message(&mut self, _io: &mut HandlerIo, _message: &Message) {
fn message(&mut self, _io: &mut NetworkContext<SyncMessage>, _message: &SyncMessage) {
}
}