openethereum/src/bin/client/main.rs

108 lines
3.0 KiB
Rust
Raw Normal View History

2015-12-22 22:19:50 +01:00
extern crate ethcore_util as util;
2016-01-07 16:08:12 +01:00
extern crate ethcore;
extern crate rustc_serialize;
extern crate log;
2016-01-09 23:21:57 +01:00
extern crate env_logger;
2015-12-22 22:19:50 +01:00
2016-01-16 13:30:27 +01:00
use std::io::stdin;
use std::env;
2016-01-15 01:44:57 +01:00
use log::{LogLevelFilter};
use env_logger::LogBuilder;
2016-01-16 13:30:27 +01:00
use util::*;
use ethcore::client::*;
2016-01-21 23:33:52 +01:00
use ethcore::service::{ClientService, NetSyncMessage};
use ethcore::ethereum;
2016-01-18 23:23:32 +01:00
use ethcore::blockchain::CacheSize;
2015-12-22 22:19:50 +01:00
fn setup_log() {
let mut builder = LogBuilder::new();
2016-01-15 01:44:57 +01:00
builder.filter(None, LogLevelFilter::Info);
if env::var("RUST_LOG").is_ok() {
builder.parse(&env::var("RUST_LOG").unwrap());
}
builder.init().unwrap();
}
2015-12-22 22:19:50 +01:00
fn main() {
setup_log();
let spec = ethereum::new_frontier();
2016-01-16 13:30:27 +01:00
let mut service = ClientService::start(spec).unwrap();
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default() });
2016-01-16 13:30:27 +01:00
service.io().register_handler(io_handler).expect("Error registering IO handler");
2015-12-22 22:19:50 +01:00
loop {
let mut cmd = String::new();
stdin().read_line(&mut cmd).unwrap();
2016-01-07 16:08:12 +01:00
if cmd == "quit\n" || cmd == "exit\n" || cmd == "q\n" {
2015-12-22 22:19:50 +01:00
break;
}
}
}
2016-01-18 23:23:32 +01:00
struct Informant {
chain_info: RwLock<Option<BlockChainInfo>>,
cache_info: RwLock<Option<CacheSize>>,
report: RwLock<Option<ClientReport>>,
}
impl Default for Informant {
fn default() -> Self {
Informant {
chain_info: RwLock::new(None),
cache_info: RwLock::new(None),
report: RwLock::new(None),
}
}
2016-01-18 23:23:32 +01:00
}
impl Informant {
pub fn tick(&self, client: &Client) {
2016-01-18 23:23:32 +01:00
// 5 seconds betwen calls. TODO: calculate this properly.
let dur = 5usize;
let chain_info = client.chain_info();
let cache_info = client.cache_info();
let report = client.report();
if let (_, &Some(ref last_cache_info), &Some(ref last_report)) = (self.chain_info.read().unwrap().deref(), self.cache_info.read().unwrap().deref(), self.report.read().unwrap().deref()) {
2016-01-18 23:23:32 +01:00
println!("[ {} {} ]---[ {} blk/s | {} tx/s | {} gas/s //···{}···// {} ({}) bl {} ({}) ex ]",
chain_info.best_block_number,
chain_info.best_block_hash,
(report.blocks_imported - last_report.blocks_imported) / dur,
(report.transactions_applied - last_report.transactions_applied) / dur,
(report.gas_processed - last_report.gas_processed) / From::from(dur),
0, // TODO: peers
cache_info.blocks,
cache_info.blocks as isize - last_cache_info.blocks as isize,
cache_info.block_details,
cache_info.block_details as isize - last_cache_info.block_details as isize
);
}
*self.chain_info.write().unwrap().deref_mut() = Some(chain_info);
*self.cache_info.write().unwrap().deref_mut() = Some(cache_info);
*self.report.write().unwrap().deref_mut() = Some(report);
2016-01-18 23:23:32 +01:00
}
}
2016-01-16 13:30:27 +01:00
const INFO_TIMER: TimerToken = 0;
2016-01-16 13:30:27 +01:00
struct ClientIoHandler {
2016-01-21 23:33:52 +01:00
client: Arc<Client>,
2016-01-18 23:23:32 +01:00
info: Informant,
2016-01-16 13:30:27 +01:00
}
impl IoHandler<NetSyncMessage> for ClientIoHandler {
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
io.register_timer(INFO_TIMER, 5000).expect("Error registering timer");
2016-01-16 13:30:27 +01:00
}
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
if INFO_TIMER == timer {
2016-01-21 23:33:52 +01:00
self.info.tick(&self.client);
2016-01-16 13:30:27 +01:00
}
}
}