openethereum/src/service.rs

78 lines
2.0 KiB
Rust
Raw Normal View History

2016-01-13 23:15:53 +01:00
use util::*;
use sync::*;
use spec::Spec;
use error::*;
use std::env;
use client::Client;
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>,
client: Arc<RwLock<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 23:53:20 +01:00
pub fn start(spec: Spec, init_nodes: &Vec<String>) -> Result<ClientService, Error> {
let mut net_service = try!(NetworkService::start(init_nodes));
2016-01-14 19:03:48 +01:00
info!("Starting {}", net_service.host_info());
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 {
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-23 23:53:20 +01:00
/// Get the network service.
pub fn add_node(&mut self, _enode: &str) {
unimplemented!();
}
2016-01-19 17:02:01 +01:00
/// TODO [arkpar] Please document me
2016-01-16 13:30:27 +01:00
pub fn io(&mut self) -> &mut IoService<NetSyncMessage> {
self.net_service.io()
}
2016-01-19 17:02:01 +01:00
/// TODO [arkpar] Please document me
2016-01-16 13:30:27 +01:00
pub fn client(&self) -> Arc<RwLock<Client>> {
self.client.clone()
}
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-14 19:03:48 +01:00
client: Arc<RwLock<Client>>
2016-01-13 23:15:53 +01:00
}
impl IoHandler<NetSyncMessage> for ClientIoHandler {
2016-01-16 18:30:27 +01:00
fn initialize<'s>(&'s mut self, _io: &mut IoContext<'s, NetSyncMessage>) {
}
2016-01-13 23:15:53 +01:00
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 {
2016-01-17 23:07:58 +01:00
&mut SyncMessage::BlockVerified => {
self.client.write().unwrap().import_verified_blocks();
2016-01-13 23:15:53 +01:00
},
2016-01-15 01:03:29 +01:00
_ => {}, // ignore other messages
2016-01-13 23:15:53 +01:00
}
}
2016-01-15 01:03:29 +01:00
_ => {}, // ignore other messages
2016-01-13 23:15:53 +01:00
}
}
}