openethereum/src/bin/client/main.rs

120 lines
3.2 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
#[cfg(feature = "rpc")]
2016-01-21 01:19:29 +01:00
mod rpc;
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-13 23:15:44 +01:00
use ethcore::service::ClientService;
use ethcore::ethereum;
2016-01-18 23:23:32 +01:00
use ethcore::blockchain::CacheSize;
2016-01-16 13:30:27 +01:00
use ethcore::sync::*;
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();
}
#[cfg(feature = "rpc")]
fn setup_rpc_server(client: Arc<RwLock<Client>>) {
2016-01-21 01:19:29 +01:00
use self::rpc::*;
2016-01-21 00:54:19 +01:00
let mut server = HttpServer::new(1);
server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(EthClient::new(client).to_delegate());
server.start_async("127.0.0.1:3030");
}
#[cfg(not(feature = "rpc"))]
fn setup_rpc_server(_client: Arc<RwLock<Client>>) {
}
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();
setup_rpc_server(service.client());
2016-01-18 23:23:32 +01:00
let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0, 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
#[derive(Default, Debug)]
struct Informant {
chain_info: Option<BlockChainInfo>,
cache_info: Option<CacheSize>,
report: Option<ClientReport>,
}
impl Informant {
pub fn tick(&mut self, client: &Client) {
// 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, &self.cache_info, &self.report) {
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 = Some(chain_info);
self.cache_info = Some(cache_info);
self.report = Some(report);
}
}
2016-01-16 13:30:27 +01:00
struct ClientIoHandler {
client: Arc<RwLock<Client>>,
timer: TimerToken,
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<'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 {
2016-01-18 19:34:30 +01:00
let client = self.client.read().unwrap();
client.tick();
2016-01-18 23:23:32 +01:00
self.info.tick(client.deref());
2016-01-16 13:30:27 +01:00
}
}
}