From d07934ffeec437f3c201215620106b039162c1c3 Mon Sep 17 00:00:00 2001 From: arkpar Date: Sat, 16 Jan 2016 13:30:27 +0100 Subject: [PATCH] Client app event handler --- src/bin/client.rs | 28 +++++++++++++++++++++++++--- src/client.rs | 3 +++ src/service.rs | 16 +++++++++++++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/bin/client.rs b/src/bin/client.rs index 3dda6fc22..a862737be 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -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>, + timer: TimerToken, +} + +impl IoHandler 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()); + } + } +} + diff --git a/src/client.rs b/src/client.rs index ba0fe1a6e..dd036a858 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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, } diff --git a/src/service.rs b/src/service.rs index da1f65f88..363cd455e 100644 --- a/src/service.rs +++ b/src/service.rs @@ -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, + net_service: NetworkService, + client: Arc>, } 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 { + self.net_service.io() + } + + pub fn client(&self) -> Arc> { + self.client.clone() + } } /// IO interface for the Client handler