From 405e3e2e7da7014517fb09e79efc69714cf8b6ed Mon Sep 17 00:00:00 2001 From: NikVolf Date: Wed, 13 Apr 2016 19:03:57 +0300 Subject: [PATCH] initial setup --- Cargo.lock | 71 +++++++++++++++++++++++++++++++++ Cargo.toml | 8 ++++ build.rs | 25 ++++++++++++ ipc/nano/src/lib.rs | 20 ++++++++++ parity/hypervisor/mod.rs | 44 ++++++++++++++++++++ parity/hypervisor/service.rs | 19 +++++++++ parity/hypervisor/service.rs.in | 50 +++++++++++++++++++++++ parity/main.rs | 5 +++ 8 files changed, 242 insertions(+) create mode 100644 parity/hypervisor/mod.rs create mode 100644 parity/hypervisor/service.rs create mode 100644 parity/hypervisor/service.rs.in diff --git a/Cargo.lock b/Cargo.lock index ca0f20449..69e0211d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,7 @@ name = "parity" version = "1.1.0" dependencies = [ + "bincode 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "clippy 0.0.61 (registry+https://github.com/rust-lang/crates.io-index)", "ctrlc 1.1.1 (git+https://github.com/tomusdrw/rust-ctrlc.git)", "daemonize 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -9,6 +10,9 @@ dependencies = [ "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "ethcore 1.1.0", "ethcore-devtools 1.1.0", + "ethcore-ipc 1.1.0", + "ethcore-ipc-codegen 1.1.0", + "ethcore-ipc-nano 1.1.0", "ethcore-rpc 1.1.0", "ethcore-util 1.1.0", "ethcore-webapp 1.1.0", @@ -17,12 +21,16 @@ dependencies = [ "fdlimit 0.1.0", "hyper 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nanomsg 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "rpassword 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_codegen 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -63,6 +71,17 @@ dependencies = [ "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bincode" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bitflags" version = "0.3.3" @@ -78,6 +97,11 @@ name = "blastfig" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "byteorder" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "bytes" version = "0.3.0" @@ -254,6 +278,36 @@ dependencies = [ "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ethcore-ipc" +version = "1.1.0" +dependencies = [ + "ethcore-devtools 1.1.0", + "nanomsg 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethcore-ipc-codegen" +version = "1.1.0" +dependencies = [ + "aster 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethcore-ipc 1.1.0", + "quasi 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "quasi_codegen 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.31.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ethcore-ipc-nano" +version = "1.1.0" +dependencies = [ + "ethcore-ipc 1.1.0", + "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "nanomsg 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ethcore-rpc" version = "1.1.0" @@ -659,6 +713,23 @@ name = "modifier" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "nanomsg" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "nanomsg-sys 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nanomsg-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "net2" version = "0.2.23" diff --git a/Cargo.toml b/Cargo.toml index 2f0d0789c..e8c4dbe46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,9 @@ build = "build.rs" [build-dependencies] rustc_version = "0.1" +syntex = "*" +serde_codegen = "0.7.0" +"ethcore-ipc-codegen" = { path = "ipc/codegen" } [dependencies] log = "0.3" @@ -30,6 +33,11 @@ ethcore-devtools = { path = "devtools" } ethcore-rpc = { path = "rpc", optional = true } ethcore-webapp = { path = "webapp", optional = true } semver = "0.2" +nanomsg = "0.5.0" +ethcore-ipc-nano = { path = "ipc/nano" } +"ethcore-ipc" = { path = "ipc/rpc" } +bincode = "*" +serde = "0.7.0" [dependencies.hyper] version = "0.8" diff --git a/build.rs b/build.rs index 41b9a1b3e..9bb9ec249 100644 --- a/build.rs +++ b/build.rs @@ -15,11 +15,36 @@ // along with Parity. If not, see . extern crate rustc_version; +extern crate syntex; +extern crate ethcore_ipc_codegen as codegen; +extern crate serde_codegen; +use std::env; +use std::path::Path; use rustc_version::{version_meta, Channel}; fn main() { if let Channel::Nightly = version_meta().channel { println!("cargo:rustc-cfg=nightly"); } + + let out_dir = env::var_os("OUT_DIR").unwrap(); + + // ipc pass + { + let src = Path::new("parity/hypervisor/service.rs.in"); + let dst = Path::new(&out_dir).join("hypervisor_service_ipc.rs"); + let mut registry = syntex::Registry::new(); + codegen::register(&mut registry); + registry.expand("", &src, &dst).unwrap(); + } + + // serde pass + { + let src = Path::new(&out_dir).join("hypervisor_service_ipc.rs"); + let dst = Path::new(&out_dir).join("hypervisor_service_cg.rs"); + let mut registry = syntex::Registry::new(); + serde_codegen::register(&mut registry); + registry.expand("", &src, &dst).unwrap(); + } } diff --git a/ipc/nano/src/lib.rs b/ipc/nano/src/lib.rs index 18a5b0ffe..b7d271c47 100644 --- a/ipc/nano/src/lib.rs +++ b/ipc/nano/src/lib.rs @@ -155,6 +155,26 @@ impl Worker where S: IpcInterface { Ok(()) } + + /// Add generic socket for request-reply style communications + /// with multiple clients + pub fn add_reqrep(&mut self, addr: &str) -> Result<(), SocketError> { + let mut socket = try!(Socket::new(Protocol::Rep).map_err(|e| { + warn!(target: "ipc", "Failed to create ipc socket: {:?}", e); + SocketError::DuplexLink + })); + + let endpoint = try!(socket.bind(addr).map_err(|e| { + warn!(target: "ipc", "Failed to bind socket to address '{}': {:?}", addr, e); + SocketError::DuplexLink + })); + + self.sockets.push((socket, endpoint)); + + self.rebuild_poll_request(); + + Ok(()) + } } #[cfg(test)] diff --git a/parity/hypervisor/mod.rs b/parity/hypervisor/mod.rs new file mode 100644 index 000000000..18cbd4f34 --- /dev/null +++ b/parity/hypervisor/mod.rs @@ -0,0 +1,44 @@ +// 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 . + +//! Parity interprocess hypervisor module + +pub mod service; + +pub const HYPERVISOR_IPC_URL: &'static str = "ipc:///tmp/parity-internal-hyper-status.ipc"; + +use nanoipc; + +pub struct Hypervisor { + ipc_worker: RwLock>, +} + +impl Hypervisor { + /// initializes the Hypervisor service with the open ipc socket for incoming clients + pub fn init() -> Arc{ + let mut worker = nanoipc::Worker::::new(Arc::new(Service::new())); + worker.add_reqrep(HYPERVISOR_IPC_URL); + + Arc::new(Hypervisor{ + ipc_worker: worker + }) + } + + /// Waits for every required module to check in + pub fn wait_for_startup() { + + } +} diff --git a/parity/hypervisor/service.rs b/parity/hypervisor/service.rs new file mode 100644 index 000000000..3171fa11c --- /dev/null +++ b/parity/hypervisor/service.rs @@ -0,0 +1,19 @@ +// 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 . + +//! Parity interprocess hypervisor IPC service + +include!(concat!(env!("OUT_DIR"), "/hypervisor_service_cg.rs")); diff --git a/parity/hypervisor/service.rs.in b/parity/hypervisor/service.rs.in new file mode 100644 index 000000000..d0ae5fdb1 --- /dev/null +++ b/parity/hypervisor/service.rs.in @@ -0,0 +1,50 @@ +// 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 . + +use std::sync::{RwLock,Arc}; +use std::ops::*; +use ipc::IpcConfig; +use std::collections::HashMap; + +type IpcModuleId = u64; + +const DATABASE_MODULE_ID: IpcModuleId = 1000; +const BLOCKCHAIN_MODULE_ID: IpcModuleId = 2000; + +pub struct HypervisorService { + check_list: RwLock>, +} + +#[derive(Ipc)] +impl HypervisorService { + fn module_ready(&self, module_id: u64) -> bool { + let mut check_list = self.check_list.write().unwrap(); + check_list.get_mut(&module_id).map(|mut status| *status = true); + check_list.iter().any(|(_, status)| !status) + } +} + +impl HypervisorService { + pub fn new() -> Arc { + let mut check_list = HashMap::new(); + check_list.insert(DATABASE_MODULE_ID, false); + Arc::new(HypervisorService { + check_list: RwLock::new(check_list), + }) + } +} + +impl ::ipc::IpcConfig for HypervisorService {} diff --git a/parity/main.rs b/parity/main.rs index 87e92cde3..7b4fb5fbe 100644 --- a/parity/main.rs +++ b/parity/main.rs @@ -37,6 +37,10 @@ extern crate time; extern crate number_prefix; extern crate rpassword; extern crate semver; +extern crate ethcore_ipc as ipc; +extern crate ethcore_ipc_nano as nanoipc; +extern crate serde; +extern crate bincode; // for price_info.rs #[macro_use] extern crate hyper; @@ -73,6 +77,7 @@ use webapp::Listening as WebappServer; mod price_info; mod upgrade; +mod hypervisor; fn die_with_message(msg: &str) -> ! { println!("ERROR: {}", msg);