Electroneum
t_daemon Class Reference

Public Member Functions

 t_daemon (boost::program_options::variables_map const &_vm)
 
bool run ()
 
void stop ()
 

Detailed Description

Definition at line 4265 of file wallet_rpc_server.cpp.

Constructor & Destructor Documentation

◆ t_daemon()

t_daemon::t_daemon ( boost::program_options::variables_map const &  _vm)
inline

Definition at line 4273 of file wallet_rpc_server.cpp.

4274  : vm(_vm)
4275  , wrpc(new tools::wallet_rpc_server)
4276  {
4277  }

Member Function Documentation

◆ run()

bool t_daemon::run ( )
inline

Definition at line 4279 of file wallet_rpc_server.cpp.

4280  {
4281  std::unique_ptr<tools::wallet2> wal;
4282  try
4283  {
4284  const bool testnet = tools::wallet2::has_testnet_option(vm);
4285  const bool stagenet = tools::wallet2::has_stagenet_option(vm);
4286  if (testnet && stagenet)
4287  {
4288  MERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --testnet and --stagenet"));
4289  return false;
4290  }
4291 
4293  const auto arg_from_json = wallet_args::arg_generate_from_json();
4294 
4295  const auto wallet_file = command_line::get_arg(vm, arg_wallet_file);
4296  const auto from_json = command_line::get_arg(vm, arg_from_json);
4297  const auto wallet_dir = command_line::get_arg(vm, arg_wallet_dir);
4298  const auto prompt_for_password = command_line::get_arg(vm, arg_prompt_for_password);
4299  const auto password_prompt = prompt_for_password ? password_prompter : nullptr;
4300 
4301  if(!wallet_file.empty() && !from_json.empty())
4302  {
4303  LOG_ERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --wallet-file and --generate-from-json"));
4304  return false;
4305  }
4306 
4307  if (!wallet_dir.empty())
4308  {
4309  wal = NULL;
4310  goto just_dir;
4311  }
4312 
4313  if (wallet_file.empty() && from_json.empty())
4314  {
4315  LOG_ERROR(tools::wallet_rpc_server::tr("Must specify --wallet-file or --generate-from-json or --wallet-dir"));
4316  return false;
4317  }
4318 
4319  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Loading wallet..."));
4320  if(!wallet_file.empty())
4321  {
4322  wal = tools::wallet2::make_from_file(vm, true, wallet_file, password_prompt).first;
4323  }
4324  else
4325  {
4326  try
4327  {
4328  auto rc = tools::wallet2::make_from_json(vm, true, from_json, password_prompt);
4329  wal = std::move(rc.first);
4330  }
4331  catch (const std::exception &e)
4332  {
4333  MERROR("Error creating wallet: " << e.what());
4334  return false;
4335  }
4336  }
4337  if (!wal)
4338  {
4339  return false;
4340  }
4341 
4342  bool quit = false;
4343  tools::signal_handler::install([&wal, &quit](int) {
4344  assert(wal);
4345  quit = true;
4346  wal->stop();
4347  });
4348 
4349  wal->refresh(wal->is_trusted_daemon());
4350  // if we ^C during potentially length load/refresh, there's no server loop yet
4351  if (quit)
4352  {
4353  MINFO(tools::wallet_rpc_server::tr("Saving wallet..."));
4354  wal->store();
4355  MINFO(tools::wallet_rpc_server::tr("Successfully saved"));
4356  return false;
4357  }
4358  MINFO(tools::wallet_rpc_server::tr("Successfully loaded"));
4359  }
4360  catch (const std::exception& e)
4361  {
4362  LOG_ERROR(tools::wallet_rpc_server::tr("Wallet initialization failed: ") << e.what());
4363  return false;
4364  }
4365  just_dir:
4366  if (wal) wrpc->set_wallet(wal.release());
4367  bool r = wrpc->init(&vm);
4368  CHECK_AND_ASSERT_MES(r, false, tools::wallet_rpc_server::tr("Failed to initialize wallet RPC server"));
4369  tools::signal_handler::install([this](int) {
4370  wrpc->send_stop_signal();
4371  });
4372  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Starting wallet RPC server"));
4373  try
4374  {
4375  wrpc->run();
4376  }
4377  catch (const std::exception &e)
4378  {
4379  LOG_ERROR(tools::wallet_rpc_server::tr("Failed to run wallet: ") << e.what());
4380  return false;
4381  }
4382  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Stopped wallet RPC server"));
4383  try
4384  {
4385  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Saving wallet..."));
4386  wrpc->stop();
4387  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Successfully saved"));
4388  }
4389  catch (const std::exception& e)
4390  {
4391  LOG_ERROR(tools::wallet_rpc_server::tr("Failed to save wallet: ") << e.what());
4392  return false;
4393  }
4394  return true;
4395  }
static bool install(T t)
installs a signal handler
Definition: util.h:164
static bool has_testnet_option(const boost::program_options::variables_map &vm)
Definition: wallet2.cpp:1172
static std::pair< std::unique_ptr< wallet2 >, password_container > make_from_json(const boost::program_options::variables_map &vm, bool unattended, const std::string &json_file, const std::function< boost::optional< password_container >(const char *, bool)> &password_prompter)
Uses stdin and stdout. Returns a wallet2 if no errors.
Definition: wallet2.cpp:1226
static std::pair< std::unique_ptr< wallet2 >, password_container > make_from_file(const boost::program_options::variables_map &vm, bool unattended, const std::string &wallet_file, const std::function< boost::optional< password_container >(const char *, bool)> &password_prompter)
Uses stdin and stdout. Returns a wallet2 and password for wallet_file if no errors.
Definition: wallet2.cpp:1232
static bool has_stagenet_option(const boost::program_options::variables_map &vm)
Definition: wallet2.cpp:1177
static const char * tr(const char *str)
#define MERROR(x)
Definition: misc_log_ex.h:73
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
#define MINFO(x)
Definition: misc_log_ex.h:75
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
const T & move(const T &t)
Definition: gtest-port.h:1317
command_line::arg_descriptor< std::string > arg_generate_from_json()
Definition: wallet_args.cpp:72
command_line::arg_descriptor< std::string > arg_wallet_file()
Definition: wallet_args.cpp:76
Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void t_daemon::stop ( )
inline

Definition at line 4397 of file wallet_rpc_server.cpp.

4398  {
4399  wrpc->send_stop_signal();
4400  }

The documentation for this class was generated from the following file: