diff options
Diffstat (limited to 'src/simplewallet')
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 54 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.h | 3 |
2 files changed, 50 insertions, 7 deletions
diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index f44eb33d0..1e8d02cec 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -468,6 +468,7 @@ simple_wallet::simple_wallet() , m_refresh_progress_reporter(*this) , m_auto_refresh_run(false) , m_auto_refresh_refreshing(false) + , m_in_manual_refresh(false) { m_cmd_binder.set_handler("start_mining", boost::bind(&simple_wallet::start_mining, this, _1), tr("start_mining [<number_of_threads>] - Start mining in daemon")); m_cmd_binder.set_handler("stop_mining", boost::bind(&simple_wallet::stop_mining, this, _1), tr("Stop mining in daemon")); @@ -1071,7 +1072,6 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa m_wallet->init(m_daemon_address); - refresh(std::vector<std::string>()); success_msg_writer() << "**********************************************************************\n" << tr("Use \"help\" command to see the list of available commands.\n") << @@ -1084,6 +1084,11 @@ bool simple_wallet::close_wallet() if (m_auto_refresh_run.load(std::memory_order_relaxed)) { m_auto_refresh_run.store(false, std::memory_order_relaxed); + m_wallet->stop(); + { + std::unique_lock<std::mutex> lock(m_auto_refresh_mutex); + m_auto_refresh_cond.notify_one(); + } m_auto_refresh_thread.join(); } @@ -1276,11 +1281,16 @@ void simple_wallet::on_skip_transaction(uint64_t height, const cryptonote::trans //---------------------------------------------------------------------------------------------------- bool simple_wallet::refresh(const std::vector<std::string>& args) { - std::unique_lock<std::mutex> lock(m_auto_refresh_mutex); - if (!try_connect_to_daemon()) return true; + bool auto_refresh_run = m_auto_refresh_run.load(std::memory_order_relaxed); + m_auto_refresh_run.store(false, std::memory_order_relaxed); + // stop any background refresh, and take over + m_wallet->stop(); + std::unique_lock<std::mutex> lock(m_auto_refresh_mutex); + m_auto_refresh_cond.notify_one(); + message_writer() << tr("Starting refresh..."); uint64_t fetched_blocks = 0; @@ -1300,7 +1310,9 @@ bool simple_wallet::refresh(const std::vector<std::string>& args) std::ostringstream ss; try { + m_in_manual_refresh.store(true, std::memory_order_relaxed); m_wallet->refresh(start_height, fetched_blocks); + m_in_manual_refresh.store(false, std::memory_order_relaxed); ok = true; // Clear line "Height xxx of xxx" std::cout << "\r \r"; @@ -1346,6 +1358,8 @@ bool simple_wallet::refresh(const std::vector<std::string>& args) fail_msg_writer() << tr("refresh failed: ") << ss.str() << ". " << tr("Blocks received: ") << fetched_blocks; } + m_in_manual_refresh.store(false, std::memory_order_relaxed); + m_auto_refresh_run.store(auto_refresh_run, std::memory_order_relaxed); return true; } //---------------------------------------------------------------------------------------------------- @@ -2222,6 +2236,8 @@ void simple_wallet::wallet_refresh_thread() while (true) { std::unique_lock<std::mutex> lock(m_auto_refresh_mutex); + if (!m_auto_refresh_run.load(std::memory_order_relaxed)) + break; m_auto_refresh_refreshing = true; try { @@ -2230,9 +2246,9 @@ void simple_wallet::wallet_refresh_thread() } catch(...) {} m_auto_refresh_refreshing = false; - m_auto_refresh_cond.wait_for(lock, chrono::seconds(90)); if (!m_auto_refresh_run.load(std::memory_order_relaxed)) break; + m_auto_refresh_cond.wait_for(lock, chrono::seconds(90)); } } //---------------------------------------------------------------------------------------------------- @@ -2244,6 +2260,10 @@ bool simple_wallet::run() { m_auto_refresh_thread = std::thread([&]{wallet_refresh_thread();}); } + else + { + refresh(std::vector<std::string>()); + } return m_cmd_binder.run_handling(std::string("[") + tr("wallet") + " " + addr_start + "]: ", ""); } //---------------------------------------------------------------------------------------------------- @@ -2306,6 +2326,18 @@ bool simple_wallet::process_command(const std::vector<std::string> &args) return m_cmd_binder.process_command_vec(args); } //---------------------------------------------------------------------------------------------------- +void simple_wallet::interrupt() +{ + if (m_in_manual_refresh.load(std::memory_order_relaxed)) + { + m_wallet->stop(); + } + else + { + stop(); + } +} +//---------------------------------------------------------------------------------------------------- int main(int argc, char* argv[]) { #ifdef WIN32 @@ -2480,7 +2512,7 @@ int main(int argc, char* argv[]) bool r = wrpc.init(vm); CHECK_AND_ASSERT_MES(r, 1, sw::tr("Failed to initialize wallet rpc server")); - tools::signal_handler::install([&wrpc, &wal] { + tools::signal_handler::install([&wrpc, &wal](int) { wrpc.send_stop_signal(); wal.store(); }); @@ -2513,8 +2545,16 @@ int main(int argc, char* argv[]) } else { - tools::signal_handler::install([&w] { - w.stop(); + tools::signal_handler::install([&w](int type) { + if (type == SIGINT) + { + // if we're pressing ^C when refreshing, just stop refreshing + w.interrupt(); + } + else + { + w.stop(); + } }); w.run(); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index be510ffb2..dd1e59d6c 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -68,6 +68,7 @@ namespace cryptonote bool deinit(); bool run(); void stop(); + void interrupt(); //wallet *create_wallet(); bool process_command(const std::vector<std::string> &args); @@ -134,6 +135,7 @@ namespace cryptonote uint64_t get_daemon_blockchain_height(std::string& err); bool try_connect_to_daemon(); bool ask_wallet_create_if_needed(); + /*! * \brief Prints the seed with a nice message * \param seed seed to print @@ -238,5 +240,6 @@ namespace cryptonote std::thread m_auto_refresh_thread; std::mutex m_auto_refresh_mutex; std::condition_variable m_auto_refresh_cond; + std::atomic<bool> m_in_manual_refresh; }; } |