Additional logging and error messages

This commit is contained in:
Tomasz Drwięga 2016-04-06 19:07:27 +02:00
parent d5f9cccf5e
commit dc91e57c2f
4 changed files with 76 additions and 36 deletions

6
Cargo.lock generated
View File

@ -245,7 +245,7 @@ dependencies = [
"ethminer 1.1.0",
"ethsync 1.1.0",
"jsonrpc-core 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-http-server 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-http-server 4.0.0 (git+https://github.com/tomusdrw/jsonrpc-http-server.git)",
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -452,8 +452,8 @@ dependencies = [
[[package]]
name = "jsonrpc-http-server"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
version = "4.0.0"
source = "git+https://github.com/tomusdrw/jsonrpc-http-server.git#46bd4e7cf8352e0efc940cf76d3dff99f1a3da15"
dependencies = [
"hyper 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-core 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -269,10 +269,8 @@ fn setup_rpc_server(
sync: Arc<EthSync>,
secret_store: Arc<AccountService>,
miner: Arc<Miner>,
url: &str,
cors_domain: &str,
apis: Vec<&str>
) -> Option<Arc<PanicHandler>> {
) -> Option<rpc::RpcServer> {
use rpc::v1::*;
let server = rpc::RpcServer::new();
@ -290,7 +288,7 @@ fn setup_rpc_server(
}
}
}
Some(server.start_http(url, cors_domain, ::num_cpus::get()))
Some(server)
}
#[cfg(not(feature = "rpc"))]
@ -299,8 +297,6 @@ fn setup_rpc_server(
_sync: Arc<EthSync>,
_secret_store: Arc<AccountService>,
_miner: Arc<Miner>,
_url: &str,
_cors_domain: &str,
_apis: Vec<&str>
) -> Option<Arc<PanicHandler>> {
None
@ -569,7 +565,10 @@ impl Configuration {
let account_service = Arc::new(self.account_service());
// Build client
let mut service = ClientService::start(self.client_config(), spec, net_settings, &Path::new(&self.path())).unwrap();
let mut service = ClientService::start(
self.client_config(), spec, net_settings, &Path::new(&self.path())
).unwrap_or_else(|e| die_with_error(e));
panic_handler.forward_from(&service);
let client = service.client();
@ -584,7 +583,23 @@ impl Configuration {
let sync = EthSync::register(service.network(), sync_config, client.clone(), miner.clone());
// Setup rpc
if self.args.flag_jsonrpc || self.args.flag_rpc {
let rpc_server = if self.args.flag_jsonrpc || self.args.flag_rpc {
// TODO: use this as the API list.
let apis = self.args.flag_rpcapi.as_ref().unwrap_or(&self.args.flag_jsonrpc_apis);
setup_rpc_server(
service.client(),
sync.clone(),
account_service.clone(),
miner.clone(),
apis.split(',').collect()
)
} else {
None
};
let rpc_handle = rpc_server.map(|server| {
panic_handler.forward_from(&server);
server
}).map(|server| {
let url = format!("{}:{}",
match self.args.flag_rpcaddr.as_ref().unwrap_or(&self.args.flag_jsonrpc_interface).as_str() {
"all" => "0.0.0.0",
@ -594,22 +609,14 @@ impl Configuration {
self.args.flag_rpcport.unwrap_or(self.args.flag_jsonrpc_port)
);
SocketAddr::from_str(&url).unwrap_or_else(|_| die!("{}: Invalid JSONRPC listen host/port given.", url));
let cors = self.args.flag_rpccorsdomain.as_ref().unwrap_or(&self.args.flag_jsonrpc_cors);
// TODO: use this as the API list.
let apis = self.args.flag_rpcapi.as_ref().unwrap_or(&self.args.flag_jsonrpc_apis);
let server_handler = setup_rpc_server(
service.client(),
sync.clone(),
account_service.clone(),
miner.clone(),
&url,
cors,
apis.split(',').collect()
);
if let Some(handler) = server_handler {
panic_handler.forward_from(handler.deref());
}
let cors_domain = self.args.flag_rpccorsdomain.as_ref().unwrap_or(&self.args.flag_jsonrpc_cors);
let start_result = server.start_http(&url, cors_domain, ::num_cpus::get());
match start_result {
Ok(handle) => handle,
Err(rpc::RpcServerError::IoError(err)) => die_with_io_error(err),
Err(e) => die!("{:?}", e),
}
});
// Register IO handler
let io_handler = Arc::new(ClientIoHandler {
@ -621,11 +628,11 @@ impl Configuration {
service.io().register_handler(io_handler).expect("Error registering IO handler");
// Handle exit
wait_for_exit(panic_handler);
wait_for_exit(panic_handler, rpc_handle);
}
}
fn wait_for_exit(panic_handler: Arc<PanicHandler>) {
fn wait_for_exit(panic_handler: Arc<PanicHandler>, _rpc_handle: Option<rpc::Listening>) {
let exit = Arc::new(Condvar::new());
// Handle possible exits
@ -639,6 +646,30 @@ fn wait_for_exit(panic_handler: Arc<PanicHandler>) {
// Wait for signal
let mutex = Mutex::new(());
let _ = exit.wait(mutex.lock().unwrap()).unwrap();
info!("Closing....");
}
fn die_with_error(e: ethcore::error::Error) -> ! {
use ethcore::error::Error;
match e {
Error::Util(UtilError::StdIo(e)) => die_with_io_error(e),
_ => die!("{:?}", e),
}
}
fn die_with_io_error(e: std::io::Error) -> ! {
match e.kind() {
std::io::ErrorKind::PermissionDenied => {
die!("We don't have permission to bind to this port.")
},
std::io::ErrorKind::AddrInUse => {
die!("Specified address is already in use. Please make sure that nothing is listening on specified port or use different one")
},
std::io::ErrorKind::AddrNotAvailable => {
die!("Couldn't use specified interface or given address is invalid.")
},
_ => die!("{:?}", e),
}
}
fn main() {

View File

@ -13,7 +13,7 @@ log = "0.3"
serde = "0.7.0"
serde_json = "0.7.0"
jsonrpc-core = "2.0"
jsonrpc-http-server = "3.0"
jsonrpc-http-server = { git = "https://github.com/tomusdrw/jsonrpc-http-server.git" }
ethcore-util = { path = "../util" }
ethcore = { path = "../ethcore" }
ethash = { path = "../ethash" }

View File

@ -34,14 +34,16 @@ extern crate transient_hashmap;
use std::sync::Arc;
use std::thread;
use util::panics::PanicHandler;
use util::panics::{MayPanic, PanicHandler, OnPanicListener};
use self::jsonrpc_core::{IoHandler, IoDelegate};
pub use jsonrpc_http_server::{Listening, RpcServerError};
pub mod v1;
/// Http server.
pub struct RpcServer {
handler: Arc<IoHandler>,
panic_handler: Arc<PanicHandler>
}
impl RpcServer {
@ -49,6 +51,7 @@ impl RpcServer {
pub fn new() -> RpcServer {
RpcServer {
handler: Arc::new(IoHandler::new()),
panic_handler: PanicHandler::new_in_arc(),
}
}
@ -58,17 +61,23 @@ impl RpcServer {
}
/// Start server asynchronously in new thread and returns panic handler.
pub fn start_http(&self, addr: &str, cors_domain: &str, threads: usize) -> Arc<PanicHandler> {
pub fn start_http(&self, addr: &str, cors_domain: &str, threads: usize) -> Result<Listening, RpcServerError> {
let addr = addr.to_owned();
let cors_domain = cors_domain.to_owned();
let panic_handler = PanicHandler::new_in_arc();
let ph = panic_handler.clone();
let ph = self.panic_handler.clone();
let server = jsonrpc_http_server::Server::new(self.handler.clone());
thread::Builder::new().name("jsonrpc_http".to_string()).spawn(move || {
ph.catch_panic(move || {
server.start(addr.as_ref(), jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain), threads);
server.start(addr.as_ref(), jsonrpc_http_server::AccessControlAllowOrigin::Value(cors_domain), threads)
}).unwrap()
}).expect("Error while creating jsonrpc http thread");
panic_handler
}).expect("Error while creating jsonrpc http thread").join().unwrap()
}
}
impl MayPanic for RpcServer {
fn on_panic<F>(&self, closure: F) where F: OnPanicListener {
self.panic_handler.on_panic(closure);
}
}