Merge pull request #132 from gavofyork/client

Client app event handler
This commit is contained in:
Gav Wood 2016-01-16 13:44:16 +01:00
commit 4f049358f2
3 changed files with 41 additions and 6 deletions

View File

@ -4,13 +4,15 @@ extern crate rustc_serialize;
extern crate log;
extern crate env_logger;
use std::io::*;
use std::io::stdin;
use std::env;
use log::{LogLevelFilter};
use env_logger::LogBuilder;
use util::hash::*;
use util::*;
use ethcore::client::*;
use ethcore::service::ClientService;
use ethcore::ethereum;
use ethcore::sync::*;
fn setup_log() {
let mut builder = LogBuilder::new();
@ -26,7 +28,9 @@ fn setup_log() {
fn main() {
setup_log();
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 {
let mut cmd = String::new();
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::*;
/// General block status
#[derive(Debug)]
pub enum BlockStatus {
/// Part of the blockchain.
InChain,
@ -25,6 +26,7 @@ pub enum BlockStatus {
}
/// Information about the blockchain gthered together.
#[derive(Debug)]
pub struct BlockChainInfo {
/// Blockchain difficulty.
pub total_difficulty: U256,
@ -39,6 +41,7 @@ pub struct BlockChainInfo {
}
/// Block queue status
#[derive(Debug)]
pub struct BlockQueueStatus {
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.
pub struct ClientService {
_net_service: NetworkService<SyncMessage>,
net_service: NetworkService<SyncMessage>,
client: Arc<RwLock<Client>>,
}
impl ClientService {
@ -22,14 +23,23 @@ impl ClientService {
let client = Arc::new(RwLock::new(try!(Client::new(spec, &dir, net_service.io().channel()))));
EthSync::register(&mut net_service, client.clone());
let client_io = Box::new(ClientIoHandler {
client: client
client: client.clone()
});
try!(net_service.io().register_handler(client_io));
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