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 4285 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 4293 of file wallet_rpc_server.cpp.

4294  : vm(_vm)
4295  , wrpc(new tools::wallet_rpc_server)
4296  {
4297  }

Member Function Documentation

◆ run()

bool t_daemon::run ( )
inline

Definition at line 4299 of file wallet_rpc_server.cpp.

4300  {
4301  std::unique_ptr<tools::wallet2> wal;
4302  try
4303  {
4304  const bool testnet = tools::wallet2::has_testnet_option(vm);
4305  const bool stagenet = tools::wallet2::has_stagenet_option(vm);
4306  if (testnet && stagenet)
4307  {
4308  MERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --testnet and --stagenet"));
4309  return false;
4310  }
4311 
4313  const auto arg_from_json = wallet_args::arg_generate_from_json();
4314 
4315  const auto wallet_file = command_line::get_arg(vm, arg_wallet_file);
4316  const auto from_json = command_line::get_arg(vm, arg_from_json);
4317  const auto wallet_dir = command_line::get_arg(vm, arg_wallet_dir);
4318  const auto prompt_for_password = command_line::get_arg(vm, arg_prompt_for_password);
4319  const auto password_prompt = prompt_for_password ? password_prompter : nullptr;
4320 
4321  if(!wallet_file.empty() && !from_json.empty())
4322  {
4323  LOG_ERROR(tools::wallet_rpc_server::tr("Can't specify more than one of --wallet-file and --generate-from-json"));
4324  return false;
4325  }
4326 
4327  if (!wallet_dir.empty())
4328  {
4329  wal = NULL;
4330  goto just_dir;
4331  }
4332 
4333  if (wallet_file.empty() && from_json.empty())
4334  {
4335  LOG_ERROR(tools::wallet_rpc_server::tr("Must specify --wallet-file or --generate-from-json or --wallet-dir"));
4336  return false;
4337  }
4338 
4339  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Loading wallet..."));
4340  if(!wallet_file.empty())
4341  {
4342  wal = tools::wallet2::make_from_file(vm, true, wallet_file, password_prompt).first;
4343  }
4344  else
4345  {
4346  try
4347  {
4348  auto rc = tools::wallet2::make_from_json(vm, true, from_json, password_prompt);
4349  wal = std::move(rc.first);
4350  }
4351  catch (const std::exception &e)
4352  {
4353  MERROR("Error creating wallet: " << e.what());
4354  return false;
4355  }
4356  }
4357  if (!wal)
4358  {
4359  return false;
4360  }
4361 
4362  bool quit = false;
4363  tools::signal_handler::install([&wal, &quit](int) {
4364  assert(wal);
4365  quit = true;
4366  wal->stop();
4367  });
4368 
4369  wal->refresh(wal->is_trusted_daemon());
4370  // if we ^C during potentially length load/refresh, there's no server loop yet
4371  if (quit)
4372  {
4373  MINFO(tools::wallet_rpc_server::tr("Saving wallet..."));
4374  wal->store();
4375  MINFO(tools::wallet_rpc_server::tr("Successfully saved"));
4376  return false;
4377  }
4378  MINFO(tools::wallet_rpc_server::tr("Successfully loaded"));
4379  }
4380  catch (const std::exception& e)
4381  {
4382  LOG_ERROR(tools::wallet_rpc_server::tr("Wallet initialization failed: ") << e.what());
4383  return false;
4384  }
4385  just_dir:
4386  if (wal) wrpc->set_wallet(wal.release());
4387  bool r = wrpc->init(&vm);
4388  CHECK_AND_ASSERT_MES(r, false, tools::wallet_rpc_server::tr("Failed to initialize wallet RPC server"));
4389  tools::signal_handler::install([this](int) {
4390  wrpc->send_stop_signal();
4391  });
4392  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Starting wallet RPC server"));
4393  try
4394  {
4395  wrpc->run();
4396  }
4397  catch (const std::exception &e)
4398  {
4399  LOG_ERROR(tools::wallet_rpc_server::tr("Failed to run wallet: ") << e.what());
4400  return false;
4401  }
4402  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Stopped wallet RPC server"));
4403  try
4404  {
4405  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Saving wallet..."));
4406  wrpc->stop();
4407  LOG_PRINT_L0(tools::wallet_rpc_server::tr("Successfully saved"));
4408  }
4409  catch (const std::exception& e)
4410  {
4411  LOG_ERROR(tools::wallet_rpc_server::tr("Failed to save wallet: ") << e.what());
4412  return false;
4413  }
4414  return true;
4415  }
#define MERROR(x)
Definition: misc_log_ex.h:73
command_line::arg_descriptor< std::string > arg_wallet_file()
Definition: wallet_args.cpp:76
#define MINFO(x)
Definition: misc_log_ex.h:75
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
void stop()
Definition: wallet2.h:760
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:1227
#define LOG_PRINT_L0(x)
Definition: misc_log_ex.h:99
bool is_trusted_daemon() const
Definition: wallet2.h:765
command_line::arg_descriptor< std::string > arg_generate_from_json()
Definition: wallet_args.cpp:72
static bool install(T t)
installs a signal handler
Definition: util.h:164
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:1233
const T & move(const T &t)
Definition: gtest-port.h:1317
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
static const char * tr(const char *str)
static bool has_testnet_option(const boost::program_options::variables_map &vm)
Definition: wallet2.cpp:1173
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
static bool has_stagenet_option(const boost::program_options::variables_map &vm)
Definition: wallet2.cpp:1178
void refresh(bool trusted_daemon)
Definition: wallet2.cpp:3060
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 4417 of file wallet_rpc_server.cpp.

4418  {
4419  wrpc->send_stop_signal();
4420  }
Here is the call graph for this function:

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