2016-07-15 15:32:29 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-07-15 17:39:59 +02:00
|
|
|
//! Parity sync service
|
2016-07-15 15:32:29 +02:00
|
|
|
|
|
|
|
use std::sync::Arc;
|
2016-08-30 14:05:02 +02:00
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
|
use hypervisor::{SYNC_MODULE_ID, HYPERVISOR_IPC_URL, ControlService};
|
2016-07-16 15:51:06 +02:00
|
|
|
use ethcore::client::{RemoteClient, ChainNotify};
|
2016-07-20 18:13:56 +02:00
|
|
|
use ethsync::{SyncProvider, EthSync, ManageNetwork, ServiceConfiguration};
|
2016-07-26 00:21:08 +02:00
|
|
|
use modules::service_urls;
|
2016-08-24 18:35:38 +02:00
|
|
|
use boot;
|
2016-08-30 14:05:02 +02:00
|
|
|
use nanoipc;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct SyncControlService {
|
|
|
|
pub stop: Arc<AtomicBool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ControlService for SyncControlService {
|
2016-09-01 12:04:19 +02:00
|
|
|
fn shutdown(&self) -> bool {
|
2016-08-30 14:05:02 +02:00
|
|
|
trace!(target: "hypervisor", "Received shutdown from control service");
|
2016-09-01 12:04:19 +02:00
|
|
|
self.stop.store(true, ::std::sync::atomic::Ordering::SeqCst);
|
|
|
|
true
|
2016-08-30 14:05:02 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-15 15:32:29 +02:00
|
|
|
|
2016-07-26 00:21:08 +02:00
|
|
|
pub fn main() {
|
2016-08-24 18:35:38 +02:00
|
|
|
boot::setup_cli_logger("sync");
|
2016-07-20 18:13:56 +02:00
|
|
|
|
2016-08-24 18:35:38 +02:00
|
|
|
let service_config: ServiceConfiguration = boot::payload()
|
|
|
|
.unwrap_or_else(|e| panic!("Fatal: error reading boot arguments ({:?})", e));
|
2016-07-20 18:13:56 +02:00
|
|
|
|
2016-08-24 18:35:38 +02:00
|
|
|
let remote_client = dependency!(RemoteClient, &service_urls::with_base(&service_config.io_path, service_urls::CLIENT));
|
2016-07-20 18:13:56 +02:00
|
|
|
|
|
|
|
let sync = EthSync::new(service_config.sync, remote_client.service().clone(), service_config.net).unwrap();
|
2016-07-15 15:32:29 +02:00
|
|
|
|
2016-08-30 14:05:02 +02:00
|
|
|
let _ = boot::main_thread();
|
|
|
|
let service_stop = Arc::new(AtomicBool::new(false));
|
|
|
|
|
|
|
|
let hypervisor = boot::register(
|
2016-08-24 18:35:38 +02:00
|
|
|
&service_urls::with_base(&service_config.io_path, HYPERVISOR_IPC_URL),
|
2016-08-30 14:05:02 +02:00
|
|
|
&service_urls::with_base(&service_config.io_path, service_urls::SYNC_CONTROL),
|
2016-08-24 18:35:38 +02:00
|
|
|
SYNC_MODULE_ID
|
|
|
|
);
|
|
|
|
|
|
|
|
boot::host_service(
|
2016-08-22 18:41:58 +02:00
|
|
|
&service_urls::with_base(&service_config.io_path, service_urls::SYNC),
|
2016-08-30 14:05:02 +02:00
|
|
|
service_stop.clone(),
|
2016-08-22 18:41:58 +02:00
|
|
|
sync.clone() as Arc<SyncProvider>
|
|
|
|
);
|
2016-08-24 18:35:38 +02:00
|
|
|
boot::host_service(
|
2016-08-22 18:41:58 +02:00
|
|
|
&service_urls::with_base(&service_config.io_path, service_urls::NETWORK_MANAGER),
|
2016-08-30 14:05:02 +02:00
|
|
|
service_stop.clone(),
|
2016-08-22 18:41:58 +02:00
|
|
|
sync.clone() as Arc<ManageNetwork>
|
|
|
|
);
|
2016-08-24 18:35:38 +02:00
|
|
|
boot::host_service(
|
2016-08-22 18:41:58 +02:00
|
|
|
&service_urls::with_base(&service_config.io_path, service_urls::SYNC_NOTIFY),
|
2016-08-30 14:05:02 +02:00
|
|
|
service_stop.clone(),
|
2016-08-22 18:41:58 +02:00
|
|
|
sync.clone() as Arc<ChainNotify>
|
|
|
|
);
|
|
|
|
|
2016-08-30 14:05:02 +02:00
|
|
|
let control_service = Arc::new(SyncControlService::default());
|
|
|
|
let as_control = control_service.clone() as Arc<ControlService>;
|
|
|
|
let mut worker = nanoipc::Worker::<ControlService>::new(&as_control);
|
2016-09-01 12:04:19 +02:00
|
|
|
let thread_stop = control_service.stop.clone();
|
2016-08-30 14:05:02 +02:00
|
|
|
worker.add_reqrep(
|
|
|
|
&service_urls::with_base(&service_config.io_path, service_urls::SYNC_CONTROL)
|
|
|
|
).unwrap();
|
|
|
|
|
2016-09-01 12:04:19 +02:00
|
|
|
while !thread_stop.load(::std::sync::atomic::Ordering::SeqCst) {
|
2016-08-30 14:05:02 +02:00
|
|
|
worker.poll();
|
2016-07-15 15:32:29 +02:00
|
|
|
}
|
2016-09-01 12:04:19 +02:00
|
|
|
service_stop.store(true, ::std::sync::atomic::Ordering::SeqCst);
|
2016-08-30 14:05:02 +02:00
|
|
|
|
|
|
|
hypervisor.module_shutdown(SYNC_MODULE_ID);
|
|
|
|
trace!(target: "hypervisor", "Sync process terminated gracefully");
|
2016-07-15 15:32:29 +02:00
|
|
|
}
|