Client app event handler

This commit is contained in:
arkpar 2016-01-16 13:30:27 +01:00
parent a53bbaef55
commit d07934ffee
3 changed files with 41 additions and 6 deletions

View File

@ -4,13 +4,15 @@ extern crate rustc_serialize;
extern crate log; extern crate log;
extern crate env_logger; extern crate env_logger;
use std::io::*; use std::io::stdin;
use std::env; use std::env;
use log::{LogLevelFilter}; use log::{LogLevelFilter};
use env_logger::LogBuilder; use env_logger::LogBuilder;
use util::hash::*; use util::*;
use ethcore::client::*;
use ethcore::service::ClientService; use ethcore::service::ClientService;
use ethcore::ethereum; use ethcore::ethereum;
use ethcore::sync::*;
fn setup_log() { fn setup_log() {
let mut builder = LogBuilder::new(); let mut builder = LogBuilder::new();
@ -26,7 +28,9 @@ fn setup_log() {
fn main() { fn main() {
setup_log(); setup_log();
let spec = ethereum::new_frontier(); let spec = ethereum::new_frontier();
let mut _service = ClientService::start(spec).unwrap(); let mut service = ClientService::start(spec).unwrap();
let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0 });
service.io().register_handler(io_handler).expect("Error registering IO handler");
loop { loop {
let mut cmd = String::new(); let mut cmd = String::new();
stdin().read_line(&mut cmd).unwrap(); stdin().read_line(&mut cmd).unwrap();
@ -36,3 +40,21 @@ fn main() {
} }
} }
struct ClientIoHandler {
client: Arc<RwLock<Client>>,
timer: TimerToken,
}
impl IoHandler<NetSyncMessage> for ClientIoHandler {
fn initialize<'s>(&'s mut self, io: &mut IoContext<'s, NetSyncMessage>) {
self.timer = io.register_timer(5000).expect("Error registering timer");
}
fn timeout<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, timer: TimerToken) {
if self.timer == timer {
println!("Chain info: {:?}", self.client.read().unwrap().deref().chain_info());
}
}
}

View File

@ -13,6 +13,7 @@ use verification::*;
use block::*; use block::*;
/// General block status /// General block status
#[derive(Debug)]
pub enum BlockStatus { pub enum BlockStatus {
/// Part of the blockchain. /// Part of the blockchain.
InChain, InChain,
@ -25,6 +26,7 @@ pub enum BlockStatus {
} }
/// Information about the blockchain gthered together. /// Information about the blockchain gthered together.
#[derive(Debug)]
pub struct BlockChainInfo { pub struct BlockChainInfo {
/// Blockchain difficulty. /// Blockchain difficulty.
pub total_difficulty: U256, pub total_difficulty: U256,
@ -39,6 +41,7 @@ pub struct BlockChainInfo {
} }
/// Block queue status /// Block queue status
#[derive(Debug)]
pub struct BlockQueueStatus { pub struct BlockQueueStatus {
pub full: bool, pub full: bool,
} }

View File

@ -7,7 +7,8 @@ use client::Client;
/// Client service setup. Creates and registers client and network services with the IO subsystem. /// Client service setup. Creates and registers client and network services with the IO subsystem.
pub struct ClientService { pub struct ClientService {
_net_service: NetworkService<SyncMessage>, net_service: NetworkService<SyncMessage>,
client: Arc<RwLock<Client>>,
} }
impl ClientService { impl ClientService {
@ -22,14 +23,23 @@ impl ClientService {
let client = Arc::new(RwLock::new(try!(Client::new(spec, &dir, net_service.io().channel())))); let client = Arc::new(RwLock::new(try!(Client::new(spec, &dir, net_service.io().channel()))));
EthSync::register(&mut net_service, client.clone()); EthSync::register(&mut net_service, client.clone());
let client_io = Box::new(ClientIoHandler { let client_io = Box::new(ClientIoHandler {
client: client client: client.clone()
}); });
try!(net_service.io().register_handler(client_io)); try!(net_service.io().register_handler(client_io));
Ok(ClientService { Ok(ClientService {
_net_service: net_service, net_service: net_service,
client: client,
}) })
} }
pub fn io(&mut self) -> &mut IoService<NetSyncMessage> {
self.net_service.io()
}
pub fn client(&self) -> Arc<RwLock<Client>> {
self.client.clone()
}
} }
/// IO interface for the Client handler /// IO interface for the Client handler