From 92db0c5d8651853f1d4b2e5ea216601ab64c88b7 Mon Sep 17 00:00:00 2001 From: Dekoze Date: Fri, 22 Dec 2017 23:04:53 -0500 Subject: [PATCH] provide error message when a port is already in use change dtor to use stop fn remove catch all change stderr outputs to logging move seperate fix to PR #298 --- rai/node/bootstrap.cpp | 18 +++++++++++++----- rai/node/node.cpp | 36 ++++++++++++++++++------------------ rai/node/rpc.cpp | 12 ++++++++++-- rai/rai_node/daemon.cpp | 27 +++++++++++++++++---------- 4 files changed, 58 insertions(+), 35 deletions(-) diff --git a/rai/node/bootstrap.cpp b/rai/node/bootstrap.cpp index 836ef34c..b46a9009 100755 --- a/rai/node/bootstrap.cpp +++ b/rai/node/bootstrap.cpp @@ -1104,11 +1104,19 @@ node (node_a) void rai::bootstrap_listener::start () { - acceptor.open (local.protocol ()); - acceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true)); - acceptor.bind (local); - acceptor.listen (); - accept_connection (); + acceptor.open (local.protocol ()); + acceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true)); + + boost::system::error_code ec; + acceptor.bind (local, ec); + if (ec) + { + BOOST_LOG (node.log) << boost::str (boost::format ("Error while binding for bootstrap on port %1%: %2%") % local.port() % ec.message ()); + throw std::runtime_error (ec.message()); + } + + acceptor.listen (); + accept_connection (); } void rai::bootstrap_listener::stop () diff --git a/rai/node/node.cpp b/rai/node/node.cpp index 82fae272..980caae2 100755 --- a/rai/node/node.cpp +++ b/rai/node/node.cpp @@ -1466,30 +1466,30 @@ block_processor_thread ([this] () { this->block_processor.process_blocks (); }) } } }); - BOOST_LOG (log) << "Node starting, version: " << RAIBLOCKS_VERSION_MAJOR << "." << RAIBLOCKS_VERSION_MINOR; + BOOST_LOG (log) << "Node starting, version: " << RAIBLOCKS_VERSION_MAJOR << "." << RAIBLOCKS_VERSION_MINOR; BOOST_LOG (log) << boost::str (boost::format ("Work pool running %1% threads") % work.threads.size ()); - if (!init_a.error ()) - { - if (config.logging.node_lifetime_tracing ()) - { - std::cerr << "Constructing node\n"; - } + if (!init_a.error ()) + { + if (config.logging.node_lifetime_tracing ()) + { + BOOST_LOG (log) << "Constructing node"; + } rai::transaction transaction (store.environment, nullptr, true); - if (store.latest_begin (transaction) == store.latest_end ()) - { - // Store was empty meaning we just created it, add the genesis block - rai::genesis genesis; - genesis.initialize (transaction, store); - } - } + if (store.latest_begin (transaction) == store.latest_end ()) + { + // Store was empty meaning we just created it, add the genesis block + rai::genesis genesis; + genesis.initialize (transaction, store); + } + } } rai::node::~node () { - if (config.logging.node_lifetime_tracing ()) - { - std::cerr << "Destructing node\n"; - } + if (config.logging.node_lifetime_tracing ()) + { + BOOST_LOG (log) << "Destructing node"; + } } void rai::node::send_keepalive (rai::endpoint const & endpoint_a) diff --git a/rai/node/rpc.cpp b/rai/node/rpc.cpp index 6e92878f..380796cd 100755 --- a/rai/node/rpc.cpp +++ b/rai/node/rpc.cpp @@ -76,8 +76,16 @@ node (node_a) { auto endpoint (rai::tcp_endpoint (config_a.address, config_a.port)); acceptor.open (endpoint.protocol ()); - acceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true)); - acceptor.bind (endpoint); + acceptor.set_option (boost::asio::ip::tcp::acceptor::reuse_address (true)); + + boost::system::error_code ec; + acceptor.bind (endpoint, ec); + if (ec) + { + BOOST_LOG (node.log) << boost::str (boost::format ("Error while binding for RPC on port %1%: %2%") % endpoint.port() % ec.message ()); + throw std::runtime_error (ec.message()); + } + acceptor.listen (); node_a.observers.blocks.add ([this] (std::shared_ptr block_a, rai::account const & account_a, rai::amount const &) { diff --git a/rai/rai_node/daemon.cpp b/rai/rai_node/daemon.cpp index 85eeb7b1..0ef2113d 100644 --- a/rai/rai_node/daemon.cpp +++ b/rai/rai_node/daemon.cpp @@ -114,21 +114,28 @@ void rai_daemon::daemon::run (boost::filesystem::path const & data_path) } : std::function (rai::uint256_union const &)> (nullptr)); rai::alarm alarm (service); rai::node_init init; - auto node (std::make_shared (init, service, data_path, alarm, config.node, opencl_work)); - if (!init.error ()) + try { - node->start (); - rai::rpc rpc (service, *node, config.rpc); - if (config.rpc_enable) + auto node (std::make_shared (init, service, data_path, alarm, config.node, opencl_work)); + if (!init.error ()) { - rpc.start (); + node->start (); + rai::rpc rpc (service, *node, config.rpc); + if (config.rpc_enable) + { + rpc.start (); + } + runner.reset (new rai::thread_runner (service, node->config.io_threads)); + runner->join (); + } + else + { + std::cerr << "Error initializing node\n"; } - runner.reset (new rai::thread_runner (service, node->config.io_threads)); - runner->join (); } - else + catch(const std::runtime_error& e) { - std::cerr << "Error initializing node\n"; + std::cerr << "Error while running node (" << e.what() << ")\n"; } } else