Skip to content

Commit

Permalink
Pass command line arguments (network/data_path) to nano_rpc child pro…
Browse files Browse the repository at this point in the history
…cess (#1957)

* Pass command line args to nano_rpc

* Remove unused network_params

* Formatting

* Simplify get_current_network_as_string stealing from #1953
  • Loading branch information
wezrule authored and Russel Waters committed May 6, 2019
1 parent ab3eefd commit f656ac2
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions nano/lib/config.hpp
Expand Up @@ -116,6 +116,11 @@ class network_constants
return err;
}

const char * get_current_network_as_string () const
{
return is_live_network () ? "live" : is_beta_network () ? "beta" : "test";
}

bool is_live_network () const
{
return current_network == nano_networks::nano_live_network;
Expand Down
10 changes: 8 additions & 2 deletions nano/nano_node/daemon.cpp
Expand Up @@ -71,10 +71,16 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano::
}
else
{
if (!boost::filesystem::exists (config.rpc.rpc_path))
{
throw std::runtime_error (std::string ("RPC is configured to spawn a new process however the file cannot be found at: ") + config.rpc.rpc_path);
}

auto network = node->network_params.network.get_current_network_as_string ();
#if BOOST_PROCESS_SUPPORTED
rpc_process = std::make_unique<boost::process::child> (config.rpc.rpc_path, "--daemon");
rpc_process = std::make_unique<boost::process::child> (config.rpc.rpc_path, "--daemon", "--data_path", data_path, "--network", network);
#else
auto rpc_exe_command = boost::str (boost::format ("%1% %2%") % config.rpc.rpc_path % "--daemon");
auto rpc_exe_command = boost::str (boost::format ("%1% --daemon --data_path=%2% --network=%3%") % config.rpc.rpc_path % data_path % network);
// clang-format off
rpc_process_thread = std::make_unique<std::thread> ([rpc_exe_command, &logger = node->logger]() {
nano::thread_role::set (nano::thread_role::name::rpc_process_container);
Expand Down
5 changes: 3 additions & 2 deletions nano/nano_rpc/entry.cpp
Expand Up @@ -72,9 +72,10 @@ int main (int argc, char * const * argv)
// clang-format off
description.add_options ()
("help", "Print out options")
("version", "Prints out version")
("daemon", "Start RPC daemon")
("data_path", boost::program_options::value<std::string> (), "Use the supplied path as the data directory");
("data_path", boost::program_options::value<std::string> (), "Use the supplied path as the data directory")
("network", boost::program_options::value<std::string> (), "Use the supplied network (live, beta or test)")
("version", "Prints out version");
// clang-format on

boost::program_options::variables_map vm;
Expand Down
8 changes: 7 additions & 1 deletion nano/nano_wallet/entry.cpp
Expand Up @@ -315,8 +315,14 @@ int run_wallet (QApplication & application, int argc, char * const * argv, boost
}
else
{
if (!boost::filesystem::exists (config.rpc.rpc_path))
{
throw std::runtime_error (std::string ("RPC is configured to spawn a new process however the file cannot be found at: ") + config.rpc.rpc_path);
}

auto network = node->network_params.network.get_current_network_as_string ();
#if BOOST_PROCESS_SUPPORTED
rpc_process = std::make_unique<boost::process::child> (config.rpc.rpc_path, "--daemon");
rpc_process = std::make_unique<boost::process::child> (config.rpc.rpc_path, "--daemon", "--data_path", data_path, "--network", network);
#else
show_error ("rpc_enable is set to true in the config. Set it to false and start the RPC server manually.");
#endif
Expand Down
2 changes: 1 addition & 1 deletion nano/rpc/rpc.cpp
Expand Up @@ -44,7 +44,7 @@ void nano::rpc::start ()

void nano::rpc::accept ()
{
auto connection (std::make_shared<nano::rpc_connection> (config, network_constants, io_ctx, logger, rpc_handler_interface));
auto connection (std::make_shared<nano::rpc_connection> (config, io_ctx, logger, rpc_handler_interface));
acceptor.async_accept (connection->socket, [this, connection](boost::system::error_code const & ec) {
if (ec != boost::asio::error::operation_aborted && acceptor.is_open ())
{
Expand Down
1 change: 0 additions & 1 deletion nano/rpc/rpc.hpp
Expand Up @@ -22,7 +22,6 @@ class rpc
boost::asio::ip::tcp::acceptor acceptor;
nano::logger_mt logger;
boost::asio::io_context & io_ctx;
nano::network_constants network_constants;
nano::rpc_handler_interface & rpc_handler_interface;
bool stopped{ false };
};
Expand Down
3 changes: 1 addition & 2 deletions nano/rpc/rpc_connection.cpp
Expand Up @@ -8,12 +8,11 @@
#include <nano/rpc/rpc_connection.hpp>
#include <nano/rpc/rpc_handler.hpp>

nano::rpc_connection::rpc_connection (nano::rpc_config const & rpc_config, nano::network_constants const & network_constants, boost::asio::io_context & io_ctx, nano::logger_mt & logger, nano::rpc_handler_interface & rpc_handler_interface) :
nano::rpc_connection::rpc_connection (nano::rpc_config const & rpc_config, boost::asio::io_context & io_ctx, nano::logger_mt & logger, nano::rpc_handler_interface & rpc_handler_interface) :
socket (io_ctx),
io_ctx (io_ctx),
logger (logger),
rpc_config (rpc_config),
network_constants (network_constants),
rpc_handler_interface (rpc_handler_interface)
{
responded.clear ();
Expand Down
4 changes: 1 addition & 3 deletions nano/rpc/rpc_connection.hpp
Expand Up @@ -9,12 +9,11 @@ namespace nano
class logger_mt;
class rpc_config;
class rpc_handler_interface;
class network_constants;

class rpc_connection : public std::enable_shared_from_this<nano::rpc_connection>
{
public:
rpc_connection (nano::rpc_config const & rpc_config, nano::network_constants const & network_constants, boost::asio::io_context & io_ctx, nano::logger_mt & logger, nano::rpc_handler_interface & rpc_handler_interface_a);
rpc_connection (nano::rpc_config const & rpc_config, boost::asio::io_context & io_ctx, nano::logger_mt & logger, nano::rpc_handler_interface & rpc_handler_interface_a);
virtual ~rpc_connection () = default;
virtual void parse_connection ();
virtual void write_completion_handler (std::shared_ptr<nano::rpc_connection> rpc_connection);
Expand All @@ -31,7 +30,6 @@ class rpc_connection : public std::enable_shared_from_this<nano::rpc_connection>
boost::asio::io_context & io_ctx;
nano::logger_mt & logger;
nano::rpc_config const & rpc_config;
nano::network_constants const & network_constants;
nano::rpc_handler_interface & rpc_handler_interface;
};
}

0 comments on commit f656ac2

Please sign in to comment.