diff --git a/src/controller.cpp b/src/controller.cpp index 2aaa590..b833009 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -275,15 +275,28 @@ void Controller::noConnection() } /// This will refresh all the balance data from hushd -void Controller::refresh(bool force) -{ - qDebug()<< __func__; - if (!zrpc->haveConnection()) - return; +#include + +void Controller::refresh(bool force) { + qDebug() << __func__; - getInfoThenRefresh(force); + int attempts = 0; + const int max_attempts = 10; + + while (!zrpc->haveConnection()) { + if (attempts >= max_attempts) { + qDebug() << "Max retry reached"; + return; + } + qDebug() << "Waiting for connection... try : " << attempts + 1; + std::this_thread::sleep_for(std::chrono::seconds(2 * attempts)); + attempts++; + } + + getInfoThenRefresh(force); } + void Controller::processInfo(const json& info) { // Testnet? @@ -642,8 +655,19 @@ void Controller::setLag(int lag) void Controller::refreshAddresses() { qDebug()<< __func__; - if (!zrpc->haveConnection()) - return noConnection(); + + int attempts = 0; + const int max_attempts = 10; + + while (!zrpc->haveConnection()) { + if (attempts >= max_attempts) { + qDebug() << "Max try reached"; + return noConnection(); + } + qDebug() << "Waiting for connection... try : " << attempts + 1; + std::this_thread::sleep_for(std::chrono::seconds(2 * attempts)); + attempts++; + } auto newzaddresses = new QList(); auto newtaddresses = new QList(); @@ -897,8 +921,19 @@ void Controller::updateUIBalances() void Controller::refreshBalances() { qDebug()<< __func__; - if (!zrpc->haveConnection()) - return noConnection(); + + int attempts = 0; + const int max_attempts = 10; + + while (!zrpc->haveConnection()) { + if (attempts >= max_attempts) { + qDebug() << "Max try reached"; + return noConnection(); + } + qDebug() << "Waiting for connection... try : " << attempts + 1; + std::this_thread::sleep_for(std::chrono::seconds(2 * attempts)); + attempts++; + } // 1. Get the Balances zrpc->fetchBalance([=] (json reply) { @@ -973,8 +1008,19 @@ void printJsonValue(QTextStream& out, const nlohmann::json& j, int depth = 0) { void Controller::refreshTransactions() { qDebug()<< __func__; - if (!zrpc->haveConnection()) - return noConnection(); + + int attempts = 0; + const int max_attempts = 10; + + while (!zrpc->haveConnection()) { + if (attempts >= max_attempts) { + qDebug() << "Max try reached"; + return noConnection(); + } + qDebug() << "Waiting for connection... try : " << attempts + 1; + std::this_thread::sleep_for(std::chrono::seconds(2 * attempts)); + attempts++; + } qDebug() << __func__ << ": fetchTransactions"; zrpc->fetchTransactions([=] (json reply) { @@ -1574,8 +1620,19 @@ void Controller::checkForUpdate(bool silent) qDebug()<< __func__; // No checking for updates, needs testing with Gitea return; - if (!zrpc->haveConnection()) - return noConnection(); + + int attempts = 0; + const int max_attempts = 10; + + while (!zrpc->haveConnection()) { + if (attempts >= max_attempts) { + qDebug() << "Max try reached"; + return noConnection(); + } + qDebug() << "Waiting for connection... try : " << attempts + 1; + std::this_thread::sleep_for(std::chrono::seconds(2 * attempts)); + attempts++; + } QUrl cmcURL("https://git.hush.is/repos/MyHush/SilentDragonLite/releases"); @@ -1663,8 +1720,19 @@ void Controller::checkForUpdate(bool silent) void Controller::refreshHUSHPrice() { qDebug()<< __func__; - if (!zrpc->haveConnection()) - return; + + int attempts = 0; + const int max_attempts = 10; + + while (!zrpc->haveConnection()) { + if (attempts >= max_attempts) { + qDebug() << "Max try reached"; + return noConnection(); + } + qDebug() << "Waiting for connection... try : " << attempts + 1; + std::this_thread::sleep_for(std::chrono::seconds(2 * attempts)); + attempts++; + } // TODO: use/render all this data QUrl cmcURL("https://api.coingecko.com/api/v3/simple/price?ids=hush&vs_currencies=btc%2Cusd%2Ceur%2Ceth%2Cgbp%2Ccny%2Cjpy%2Crub%2Ccad%2Csgd%2Cchf%2Cinr%2Caud%2Cinr&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true");