1
0
Fork 0
forked from lthn/blockchain

implemented non-blocking api for javascript calls

This commit is contained in:
cryptozoidberg 2022-04-18 19:03:14 +02:00
parent 5a458b6dbe
commit 25163db2bf
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
3 changed files with 47 additions and 4 deletions

View file

@ -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(); }));
}

View file

@ -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();

View file

@ -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<bool> m_gui_deinitialize_done_1;
std::atomic<bool> m_backend_stopped_2;
std::atomic<bool> m_system_shutdown;
std::atomic<uint64_t> m_ui_dispatch_id_counter;
utils::threads_pool m_threads_pool;
std::string m_master_password;