From fccf9933afbb4616daa9a48721f455ff6bff7dc0 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Mon, 2 Mar 2020 05:05:15 +0100 Subject: [PATCH] implemented online/offline status detecting --- contrib/epee/include/net/http_client.h | 1 + src/wallet/core_default_rpc_proxy.h | 13 ++++- src/wallet/core_fast_rpc_proxy.h | 5 ++ src/wallet/core_rpc_proxy.h | 1 + src/wallet/view_iface.h | 2 + src/wallet/wallets_manager.cpp | 20 ++++++- src/wallet/wallets_manager.h | 1 + utils/build_android_libs.sh | 78 -------------------------- 8 files changed, 38 insertions(+), 83 deletions(-) delete mode 100755 utils/build_android_libs.sh diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h index 11a95f02..28e6a30b 100644 --- a/contrib/epee/include/net/http_client.h +++ b/contrib/epee/include/net/http_client.h @@ -873,6 +873,7 @@ using namespace std; if (!tr.connect(u_c.host, static_cast(u_c.port), timeout)) { LOG_PRINT_L2("invoke_request: cannot connect to " << u_c.host << ":" << u_c.port); + return false; } } diff --git a/src/wallet/core_default_rpc_proxy.h b/src/wallet/core_default_rpc_proxy.h index b514adbc..4a50d565 100644 --- a/src/wallet/core_default_rpc_proxy.h +++ b/src/wallet/core_default_rpc_proxy.h @@ -11,7 +11,7 @@ #include "core_rpc_proxy.h" #include "storages/http_abstract_invoke.h" -#define WALLET_RCP_CONNECTION_TIMEOUT 200000 +#define WALLET_RCP_CONNECTION_TIMEOUT 10000 #define WALLET_RCP_COUNT_ATTEMNTS 3 namespace tools @@ -63,7 +63,10 @@ namespace tools ret = request(); } - m_plast_daemon_is_disconnected->store(!ret); + if (ret) + m_last_success_interract_time = time(nullptr); + else + m_plast_daemon_is_disconnected = true; return ret; } @@ -90,10 +93,16 @@ namespace tools return epee::net_utils::invoke_http_json_remote_command2(m_daemon_address + url, req, res, m_http_client, WALLET_RCP_CONNECTION_TIMEOUT); }); } + //------------------------------------------------------------------------------------------------------------------------------ + virtual time_t get_last_success_interract_time() + { + return m_last_success_interract_time; + } epee::critical_section m_lock; epee::net_utils::http::http_simple_client m_http_client; std::string m_daemon_address; + std::atomic m_last_success_interract_time; std::atomic *m_plast_daemon_is_disconnected; std::atomic m_last_daemon_is_disconnected_stub; diff --git a/src/wallet/core_fast_rpc_proxy.h b/src/wallet/core_fast_rpc_proxy.h index c26c1fb2..48b2500d 100644 --- a/src/wallet/core_fast_rpc_proxy.h +++ b/src/wallet/core_fast_rpc_proxy.h @@ -138,6 +138,11 @@ namespace tools { return tools::get_transfer_address(adr_str, addr, payment_id, this); } + //------------------------------------------------------------------------------------------------------------------------------ + virtual time_t get_last_success_interract_time() + { + return time(nullptr); + } private: currency::core_rpc_server& m_rpc; diff --git a/src/wallet/core_rpc_proxy.h b/src/wallet/core_rpc_proxy.h index 828bd509..0b6e0c92 100644 --- a/src/wallet/core_rpc_proxy.h +++ b/src/wallet/core_rpc_proxy.h @@ -45,6 +45,7 @@ namespace tools virtual bool check_connection(){ return false; } + virtual time_t get_last_success_interract_time() { return 0; } virtual bool get_transfer_address(const std::string& adr_str, currency::account_public_address& addr, std::string& payment_id){ return false; } }; } diff --git a/src/wallet/view_iface.h b/src/wallet/view_iface.h index 76159bcf..cbfe04a1 100644 --- a/src/wallet/view_iface.h +++ b/src/wallet/view_iface.h @@ -524,11 +524,13 @@ public: struct wallet_sync_status_info { + bool is_daemon_connected; uint64_t wallet_state; bool is_in_long_refresh; uint64_t progress; BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(is_daemon_connected) KV_SERIALIZE(wallet_state) KV_SERIALIZE(is_in_long_refresh) KV_SERIALIZE(progress) diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index b6c01a50..94f776df 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -29,7 +29,7 @@ if (it == m_wallets.end()) \ return API_RETURN_CODE_WALLET_WRONG_ID; \ auto& name = it->second.w; - +#define DAEMON_IDLE_UPDATE_TIME_MS 1000 wallets_manager::wallets_manager():m_pview(&m_view_stub), m_stop_singal_sent(false), @@ -232,7 +232,9 @@ bool wallets_manager::init(int argc, char* argv[], view::i_view* pview_handler) if (command_line::has_arg(m_vm, arg_remote_node)) { m_remote_node_mode = true; - m_rpc_proxy.reset(new tools::default_http_core_proxy()); + auto proxy_ptr = new tools::default_http_core_proxy(); + proxy_ptr->set_plast_daemon_is_disconnected(&m_last_daemon_is_disconnected); + m_rpc_proxy.reset(proxy_ptr); m_rpc_proxy->set_connection_addr(command_line::get_arg(m_vm, arg_remote_node)); } @@ -567,7 +569,7 @@ void wallets_manager::loop() while(!m_stop_singal_sent) { update_state_info(); - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::this_thread::sleep_for(std::chrono::milliseconds(DAEMON_IDLE_UPDATE_TIME_MS)); } } @@ -1185,11 +1187,23 @@ std::string wallets_manager::transfer(size_t wallet_id, const view::transfer_par return API_RETURN_CODE_OK; } +bool wallets_manager::get_is_remote_daemon_connected() +{ + if (!m_remote_node_mode) + return true; + if (m_last_daemon_is_disconnected) + return false; + if (time(nullptr) - m_rpc_proxy->get_last_success_interract_time() > DAEMON_IDLE_UPDATE_TIME_MS * 2) + return false; + return true; +} + std::string wallets_manager::get_wallet_status(uint64_t wallet_id) { GET_WALLET_OPT_BY_ID(wallet_id, wo); view::wallet_sync_status_info wsi = AUTO_VAL_INIT(wsi); wsi.is_in_long_refresh = wo.long_refresh_in_progress; + wsi.is_daemon_connected = get_is_remote_daemon_connected(); wsi.progress = wo.w.unlocked_get().get()->get_sync_progress(); wsi.wallet_state = wo.wallet_state; return epee::serialization::store_t_to_json(wsi); diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index 8b69def2..e6f1b472 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -162,6 +162,7 @@ private: void update_wallets_info(); void init_wallet_entry(wallet_vs_options& wo, uint64_t id); static void prepare_wallet_status_info(wallet_vs_options& wo, view::wallet_status_info& wsi); + bool get_is_remote_daemon_connected(); //----- i_backend_wallet_callback ------ virtual void on_new_block(size_t wallet_id, uint64_t height, const currency::block& block); virtual void on_transfer2(size_t wallet_id, const tools::wallet_public::wallet_transfer_info& wti, uint64_t balance, uint64_t unlocked_balance, uint64_t total_mined); diff --git a/utils/build_android_libs.sh b/utils/build_android_libs.sh deleted file mode 100755 index 8d67fbb5..00000000 --- a/utils/build_android_libs.sh +++ /dev/null @@ -1,78 +0,0 @@ -set -x #echo on -cd .. -rm -r _builds_android -cmake -S. -B_builds_android -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a -DCMAKE_ANDROID_NDK=/Users/roky/Library/Android/sdk/ndk/18.1.5063045 -DCMAKE_ANDROID_STL_TYPE=c++_static -DCMAKE_INSTALL_PREFIX=`pwd`/_install_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -cmake --build _builds_android --config Debug --target install -- -j 4 -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -rm -r _builds_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -cmake -S. -B_builds_android -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 -DCMAKE_ANDROID_ARCH_ABI=x86 -DCMAKE_ANDROID_NDK=/Users/roky/Library/Android/sdk/ndk/18.1.5063045 -DCMAKE_ANDROID_STL_TYPE=c++_static -DCMAKE_INSTALL_PREFIX=`pwd`/_install_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -cmake --build _builds_android --config Debug --target install -- -j 4 -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -rm -r _builds_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - - -cmake -S. -B_builds_android -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a -DCMAKE_ANDROID_NDK=/Users/roky/Library/Android/sdk/ndk/18.1.5063045 -DCMAKE_ANDROID_STL_TYPE=c++_static -DCMAKE_INSTALL_PREFIX=`pwd`/_install_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -cmake --build _builds_android --config Debug --target install -- -j 4 -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -rm -r _builds_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - - -cmake -S. -B_builds_android -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 -DCMAKE_ANDROID_ARCH_ABI=x86_64 -DCMAKE_ANDROID_NDK=/Users/roky/Library/Android/sdk/ndk/18.1.5063045 -DCMAKE_ANDROID_STL_TYPE=c++_static -DCMAKE_INSTALL_PREFIX=`pwd`/_install_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -cmake --build _builds_android --config Debug --target install -- -j 4 -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -rm -r _builds_android -if [ $? -ne 0 ]; then - echo "Failed to perform command" - exit 1 -fi - -