From 25163db2bf64d33ba9a4eb277dcef7455f23a5da Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Mon, 18 Apr 2022 19:03:14 +0200 Subject: [PATCH] implemented non-blocking api for javascript calls --- src/common/threads_pool.h | 4 +-- src/gui/qt-daemon/application/mainwindow.cpp | 38 +++++++++++++++++++- src/gui/qt-daemon/application/mainwindow.h | 9 ++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/common/threads_pool.h b/src/common/threads_pool.h index 4e4e3905..5870ea31 100644 --- a/src/common/threads_pool.h +++ b/src/common/threads_pool.h @@ -51,11 +51,11 @@ namespace utils int num_threads = std::thread::hardware_concurrency(); this->init(num_threads); } - void init(unsigned int num_threads) + void init(size_t num_threads) { m_is_stop = false; - for (int i = 0; i < num_threads; i++) + for (size_t i = 0; i < num_threads; i++) { m_threads.push_back(std::thread([this]() {this->worker_func(); })); } diff --git a/src/gui/qt-daemon/application/mainwindow.cpp b/src/gui/qt-daemon/application/mainwindow.cpp index 030a8d20..77e558cb 100644 --- a/src/gui/qt-daemon/application/mainwindow.cpp +++ b/src/gui/qt-daemon/application/mainwindow.cpp @@ -100,6 +100,7 @@ MainWindow::MainWindow() , m_system_shutdown(false) , m_view(nullptr) , m_channel(nullptr) + , m_ui_dispatch_id_counter(0) { #ifndef _MSC_VER //workaround for macos broken tolower from std, very dirty hack @@ -415,7 +416,7 @@ bool MainWindow::init(const std::string& html_path) //QtWebEngine::initialize(); init_tray_icon(html_path); set_html_path(html_path); - + m_threads_pool.init(2); m_backend.subscribe_to_core_events(this); bool r = QSslSocket::supportsSsl(); @@ -920,6 +921,41 @@ QString MainWindow::start_backend(const QString& params) CATCH_ENTRY_FAIL_API_RESPONCE(); } +QString MainWindow::sync_call(const QString& func_name, const QString& params) +{ + if (func_name == "transfer") + { + return this->transfer(params); + } + else if (func_name == "test_call") + { + return params; + } + else + { + return QString(QString() + "{ \"status\": \"Method '" + func_name + "' not found\"}"); + } +} + +QString MainWindow::async_call(const QString& func_name, const QString& params) +{ + + uint64_t job_id = m_ui_dispatch_id_counter++; + QString method_name = func_name; + QString argements = params; + + auto async_callback = [this, method_name, argements, job_id]() + { + QString res_str = this->sync_call(method_name, argements); + this->dispatch_async_call_result(std::to_string(job_id).c_str(), res_str); //general function + }; + + m_threads_pool.add_job(async_callback); + LOG_PRINT_L2("[UI_ASYNC_CALL]: started " << method_name.toStdString() << ", job id: " << job_id); + return QString::fromStdString(std::string("{ \"job_id\": ") + std::to_string(job_id) + "}"); +} + + bool MainWindow::update_wallet_status(const view::wallet_status_info& wsi) { TRY_ENTRY(); diff --git a/src/gui/qt-daemon/application/mainwindow.h b/src/gui/qt-daemon/application/mainwindow.h index c7e4c70b..f01dbe93 100644 --- a/src/gui/qt-daemon/application/mainwindow.h +++ b/src/gui/qt-daemon/application/mainwindow.h @@ -18,6 +18,8 @@ #include "currency_core/offers_services_helpers.h" #endif +#include "common/threads_pool.h" + QT_BEGIN_NAMESPACE class QWebEngineView; class QLineEdit; @@ -180,6 +182,9 @@ public: QString is_remnotenode_mode_preconfigured(); QString start_backend(const QString& params); + QString async_call(const QString& func_name, const QString& params); + QString sync_call(const QString& func_name, const QString& params); + //for test purposes onlys QString request_dummy(); @@ -193,11 +198,11 @@ signals: void wallet_sync_progress(const QString str); void handle_internal_callback(const QString str, const QString callback_name); void update_pos_mining_text(const QString str); - void do_dispatch(const QString status, const QString params); //general function void on_core_event(const QString method_name); //general function void set_options(const QString str); //general function void handle_deeplink_click(const QString str); void handle_current_action_state(const QString str); + void dispatch_async_call_result(const QString id, const QString resp); //general function private: //-------------------- i_core_event_handler -------------------- @@ -259,6 +264,8 @@ private: std::atomic m_gui_deinitialize_done_1; std::atomic m_backend_stopped_2; std::atomic m_system_shutdown; + std::atomic m_ui_dispatch_id_counter; + utils::threads_pool m_threads_pool; std::string m_master_password;