2016-01-13 23:15:53 +01:00
|
|
|
use util::*;
|
|
|
|
use spec::Spec;
|
|
|
|
use error::*;
|
|
|
|
use std::env;
|
|
|
|
use client::Client;
|
|
|
|
|
2016-01-21 23:33:52 +01:00
|
|
|
/// Message type for external and internal events
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum SyncMessage {
|
|
|
|
/// New block has been imported into the blockchain
|
|
|
|
NewChainBlock(Bytes), //TODO: use Cow
|
|
|
|
/// A block is ready
|
|
|
|
BlockVerified,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// TODO [arkpar] Please document me
|
|
|
|
pub type NetSyncMessage = NetworkIoMessage<SyncMessage>;
|
|
|
|
|
2016-01-15 01:03:29 +01:00
|
|
|
/// Client service setup. Creates and registers client and network services with the IO subsystem.
|
2016-01-13 23:15:53 +01:00
|
|
|
pub struct ClientService {
|
2016-01-16 13:30:27 +01:00
|
|
|
net_service: NetworkService<SyncMessage>,
|
2016-01-21 23:33:52 +01:00
|
|
|
client: Arc<Client>,
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ClientService {
|
2016-01-15 01:03:29 +01:00
|
|
|
/// Start the service in a separate thread.
|
2016-01-23 02:36:58 +01:00
|
|
|
pub fn start(spec: Spec, net_config: NetworkConfiguration) -> Result<ClientService, Error> {
|
|
|
|
let mut net_service = try!(NetworkService::start(net_config));
|
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());
|
2016-01-21 23:33:52 +01:00
|
|
|
let client = try!(Client::new(spec, &dir, net_service.io().channel()));
|
2016-01-21 16:48:37 +01:00
|
|
|
let client_io = Arc::new(ClientIoHandler {
|
2016-01-16 13:30:27 +01:00
|
|
|
client: client.clone()
|
2016-01-13 23:15:53 +01:00
|
|
|
});
|
|
|
|
try!(net_service.io().register_handler(client_io));
|
|
|
|
|
|
|
|
Ok(ClientService {
|
2016-01-16 13:30:27 +01:00
|
|
|
net_service: net_service,
|
|
|
|
client: client,
|
2016-01-13 23:15:53 +01:00
|
|
|
})
|
|
|
|
}
|
2016-01-16 13:30:27 +01:00
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Add a node to network
|
2016-01-23 23:53:20 +01:00
|
|
|
pub fn add_node(&mut self, _enode: &str) {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Get general IO interface
|
2016-01-16 13:30:27 +01:00
|
|
|
pub fn io(&mut self) -> &mut IoService<NetSyncMessage> {
|
|
|
|
self.net_service.io()
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:56:06 +01:00
|
|
|
/// Get client interface
|
2016-01-21 23:33:52 +01:00
|
|
|
pub fn client(&self) -> Arc<Client> {
|
2016-01-16 13:30:27 +01:00
|
|
|
self.client.clone()
|
2016-01-22 04:54:38 +01:00
|
|
|
|
|
|
|
}
|
2016-01-29 15:56:06 +01:00
|
|
|
|
|
|
|
/// Get network service component
|
|
|
|
pub fn network(&mut self) -> &mut NetworkService<SyncMessage> {
|
|
|
|
&mut self.net_service
|
2016-01-16 13:30:27 +01:00
|
|
|
}
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
2016-01-15 01:03:29 +01:00
|
|
|
/// IO interface for the Client handler
|
2016-01-13 23:15:53 +01:00
|
|
|
struct ClientIoHandler {
|
2016-01-21 23:33:52 +01:00
|
|
|
client: Arc<Client>
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 17:21:51 +01:00
|
|
|
const CLIENT_TICK_TIMER: TimerToken = 0;
|
|
|
|
const CLIENT_TICK_MS: u64 = 5000;
|
|
|
|
|
2016-01-13 23:15:53 +01:00
|
|
|
impl IoHandler<NetSyncMessage> for ClientIoHandler {
|
2016-01-21 17:21:51 +01:00
|
|
|
fn initialize(&self, io: &IoContext<NetSyncMessage>) {
|
|
|
|
io.register_timer(CLIENT_TICK_TIMER, CLIENT_TICK_MS).expect("Error registering client timer");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn timeout(&self, _io: &IoContext<NetSyncMessage>, timer: TimerToken) {
|
|
|
|
if timer == CLIENT_TICK_TIMER {
|
2016-01-21 23:33:52 +01:00
|
|
|
self.client.tick();
|
2016-01-21 17:21:51 +01:00
|
|
|
}
|
2016-01-16 18:30:27 +01:00
|
|
|
}
|
2016-01-13 23:15:53 +01:00
|
|
|
|
2016-01-17 15:56:09 +01:00
|
|
|
#[allow(match_ref_pats)]
|
2016-01-22 14:44:17 +01:00
|
|
|
#[allow(single_match)]
|
2016-01-21 23:33:52 +01:00
|
|
|
fn message(&self, io: &IoContext<NetSyncMessage>, net_message: &NetSyncMessage) {
|
2016-01-22 14:44:17 +01:00
|
|
|
if let &UserMessage(ref message) = net_message {
|
|
|
|
match message {
|
|
|
|
&SyncMessage::BlockVerified => {
|
|
|
|
self.client.import_verified_blocks(&io.channel());
|
|
|
|
},
|
|
|
|
_ => {}, // ignore other messages
|
2016-01-13 23:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|