2016-01-13 23:15:53 +01:00
|
|
|
//! Client service.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
|
|
|
|
use util::*;
|
|
|
|
use sync::*;
|
|
|
|
use spec::Spec;
|
|
|
|
use error::*;
|
|
|
|
use std::env;
|
|
|
|
use client::Client;
|
|
|
|
|
|
|
|
pub struct ClientService {
|
|
|
|
_net_service: NetworkService<SyncMessage>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientService {
|
|
|
|
pub fn start(spec: Spec) -> Result<ClientService, Error> {
|
|
|
|
let mut net_service = try!(NetworkService::start());
|
2016-01-14 19:03:48 +01:00
|
|
|
info!("Starting {}", net_service.host_info());
|
2016-01-14 22:38:49 +01:00
|
|
|
info!("Configured for {} using {} engine", spec.name, spec.engine_name);
|
2016-01-14 19:03:48 +01:00
|
|
|
let mut dir = env::home_dir().unwrap();
|
|
|
|
dir.push(".parity");
|
|
|
|
dir.push(H64::from(spec.genesis_header().hash()).hex());
|
|
|
|
let client = Arc::new(RwLock::new(try!(Client::new(spec, &dir, net_service.io().channel()))));
|
2016-01-13 23:15:53 +01:00
|
|
|
EthSync::register(&mut net_service, client.clone());
|
|
|
|
let client_io = Box::new(ClientIoHandler {
|
|
|
|
client: client
|
|
|
|
});
|
|
|
|
try!(net_service.io().register_handler(client_io));
|
|
|
|
|
|
|
|
Ok(ClientService {
|
|
|
|
_net_service: net_service,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ClientIoHandler {
|
2016-01-14 19:03:48 +01:00
|
|
|
client: Arc<RwLock<Client>>
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl IoHandler<NetSyncMessage> for ClientIoHandler {
|
|
|
|
fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>) {
|
|
|
|
}
|
|
|
|
|
|
|
|
fn message<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>, net_message: &'s mut NetSyncMessage) {
|
|
|
|
match net_message {
|
|
|
|
&mut UserMessage(ref mut message) => {
|
|
|
|
match message {
|
|
|
|
&mut SyncMessage::BlockVerified(ref mut bytes) => {
|
2016-01-14 19:03:48 +01:00
|
|
|
self.client.write().unwrap().import_verified_block(mem::replace(bytes, Bytes::new()));
|
2016-01-13 23:15:53 +01:00
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|