From 459cd3d0e512a1166b3a09233c22b7357b514760 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 17 Oct 2023 23:45:10 +0800 Subject: refactor: move module system to core and make it work --- src/core/module/GlobalModuleContext.cpp | 341 ++++++++++++++++++++++++++++++++ 1 file changed, 341 insertions(+) create mode 100644 src/core/module/GlobalModuleContext.cpp (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp new file mode 100644 index 00000000..df58c211 --- /dev/null +++ b/src/core/module/GlobalModuleContext.cpp @@ -0,0 +1,341 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see . + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GlobalModuleContext.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "core/module/Event.h" +#include "core/module/Module.h" +#include "core/thread/Task.h" + +namespace GpgFrontend::Module { + +class GlobalModuleContext::Impl { + public: + Impl(TaskRunnerPtr task_runner) + : default_task_runner_(task_runner), + random_gen_( + (boost::posix_time::microsec_clock::universal_time() - + boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) + .total_milliseconds()) { + // Initialize acquired channels with default values. + acquired_channel_.insert(GPGFRONTEND_DEFAULT_CHANNEL); + acquired_channel_.insert(GPGFRONTEND_NON_ASCII_CHANNEL); + } + + int GetChannel(ModulePtr module) { + // Search for the module in the register table. + auto module_info_opt = + search_module_register_table(module->GetModuleIdentifier()); + if (!module_info_opt.has_value()) { + SPDLOG_ERROR( + "cannot find module id {} at register table, fallbacking to " + "default " + "channel", + module->GetModuleIdentifier()); + return GetDefaultChannel(module); + } + + auto module_info = module_info_opt.value(); + return module_info->channel; + } + + int GetDefaultChannel(ModulePtr) { return GPGFRONTEND_DEFAULT_CHANNEL; } + + std::optional GetTaskRunner(ModulePtr module) { + auto opt = search_module_register_table(module->GetModuleIdentifier()); + if (!opt.has_value()) { + return std::nullopt; + } + return opt.value()->task_runner; + } + + std::optional GetTaskRunner(ModuleIdentifier module_id) { + // Search for the module in the register table. + auto module_info_opt = search_module_register_table(module_id); + if (!module_info_opt.has_value()) { + SPDLOG_ERROR("cannot find module id {} at register table", module_id); + return std::nullopt; + } + return module_info_opt.value()->task_runner; + } + + std::optional GetGlobalTaskRunner() { + return default_task_runner_; + } + + bool RegisterModule(ModulePtr module) { + SPDLOG_DEBUG("attempting to register module: {}", + module->GetModuleIdentifier()); + // Check if the module is null or already registered. + if (module == nullptr || + module_register_table_.find(module->GetModuleIdentifier()) != + module_register_table_.end()) { + SPDLOG_ERROR("module is null or have already registered this module"); + return false; + } + + if (!module->Register()) { + SPDLOG_ERROR("register module {} failed", module->GetModuleIdentifier()); + return false; + } + + ModuleRegisterInfo register_info; + register_info.module = module; + register_info.channel = acquire_new_unique_channel(); + register_info.task_runner = std::make_shared(); + + // Register the module with its identifier. + module_register_table_[module->GetModuleIdentifier()] = + std::make_shared(std::move(register_info)); + + SPDLOG_DEBUG("successfully registered module: {}", + module->GetModuleIdentifier()); + return true; + } + + bool ActiveModule(ModuleIdentifier module_id) { + SPDLOG_DEBUG("attempting to activate module: {}", module_id); + + // Search for the module in the register table. + auto module_info_opt = search_module_register_table(module_id); + if (!module_info_opt.has_value()) { + SPDLOG_ERROR("cannot find module id {} at register table", module_id); + return false; + } + + auto module_info = module_info_opt.value(); + // Activate the module if it is not already active. + if (module_info->activate && module_info->module->Active()) { + module_info->activate = true; + } + + SPDLOG_DEBUG("module activation status: {}", module_info->activate); + return module_info->activate; + } + + bool ListenEvent(ModuleIdentifier module_id, EventIdentifier event) { + SPDLOG_DEBUG("module: {} is attempting to listen to event {}", module_id, + event); + // Check if the event exists, if not, create it. + auto it = module_events_table_.find(event); + if (it == module_events_table_.end()) { + module_events_table_[event] = std::unordered_set(); + it = module_events_table_.find(event); + SPDLOG_INFO("new event {} of module system created", event); + } + + auto& listeners_set = it->second; + // Add the listener (module) to the event. + auto listener_it = + std::find(listeners_set.begin(), listeners_set.end(), module_id); + if (listener_it == listeners_set.end()) { + listeners_set.insert(module_id); + } + return true; + } + + bool DeactivateModule(ModuleIdentifier module_id) { + // Search for the module in the register table. + auto module_info_opt = search_module_register_table(module_id); + if (!module_info_opt.has_value()) { + SPDLOG_ERROR("cannot find module id {} at register table", module_id); + return false; + } + + auto module_info = module_info_opt.value(); + // Activate the module if it is not already deactive. + if (!module_info->activate && module_info->module->Deactive()) { + module_info->activate = false; + } + + return !module_info->activate; + } + + bool TriggerEvent(EventRefrernce event) { + SPDLOG_DEBUG("attempting to trigger event: {}", event->GetIdentifier()); + + // Find the set of listeners associated with the given event in the table + auto it = module_events_table_.find(event->GetIdentifier()); + if (it == module_events_table_.end()) { + // Log a warning if the event is not registered and nobody is listening + SPDLOG_WARN( + "event {} is not listening by anyone and not registered as well", + event->GetIdentifier()); + return false; + } + + // Retrieve the set of listeners for this event + auto& listeners_set = it->second; + + // Check if the set of listeners is empty + if (listeners_set.empty()) { + // Log a warning if nobody is listening to this event + SPDLOG_WARN("event {} is not listening by anyone", + event->GetIdentifier()); + return false; + } + + // Log the number of listeners for this event + SPDLOG_DEBUG("event {}'s current listeners size: {}", + event->GetIdentifier(), listeners_set.size()); + + // Iterate through each listener and execute the corresponding module + for (auto& listener_module_id : listeners_set) { + // Search for the module's information in the registration table + auto module_info_opt = search_module_register_table(listener_module_id); + + // Log an error if the module is not found in the registration table + if (!module_info_opt.has_value()) { + SPDLOG_ERROR("cannot find module id {} at register table", + listener_module_id); + } + + // Retrieve the module's information + auto module_info = module_info_opt.value(); + + // Check if the module is activated + if (!module_info->activate) continue; + + // Execute the module and check if it fails + if (module_info->module->Exec(event)) { + // Log an error if the module execution fails + SPDLOG_ERROR("module {} executed failed", listener_module_id); + } + } + + // Return true to indicate successful execution of all modules + return true; + } + + private: + struct ModuleRegisterInfo { + int channel; + TaskRunnerPtr task_runner; + ModulePtr module; + bool activate; + }; + + using ModuleRegisterInfoPtr = std::shared_ptr; + + std::unordered_map + module_register_table_; + std::map> + module_events_table_; + + std::set acquired_channel_; + boost::random::mt19937 random_gen_; + TaskRunnerPtr default_task_runner_; + + int acquire_new_unique_channel() { + boost::random::uniform_int_distribution<> dist(1, 65535); + + int random_channel = dist(random_gen_); + // Ensure the acquired channel is unique. + while (acquired_channel_.find(random_channel) != acquired_channel_.end()) { + random_channel = dist(random_gen_); + } + + // Add the acquired channel to the set. + acquired_channel_.insert(random_channel); + return random_channel; + } + + // Function to search for a module in the register table. + std::optional search_module_register_table( + ModuleIdentifier identifier) { + auto it = module_register_table_.find(identifier); + if (it == module_register_table_.end()) { + return std::nullopt; + } + return it->second; + } + + std::list& search_module_events_table(ModuleIdentifier); +}; + +// Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument. +GlobalModuleContext::GlobalModuleContext(TaskRunnerPtr task_runner) + : p_(std::make_unique(task_runner)) {} + +GlobalModuleContext::~GlobalModuleContext() = default; + +// Function to get the task runner associated with a module. +std::optional GlobalModuleContext::GetTaskRunner( + ModulePtr module) { + return p_->GetTaskRunner(module); +} + +// Function to get the task runner associated with a module. +std::optional GlobalModuleContext::GetTaskRunner( + ModuleIdentifier module_id) { + return p_->GetTaskRunner(module_id); +} + +// Function to get the global task runner. +std::optional GlobalModuleContext::GetGlobalTaskRunner() { + return p_->GetGlobalTaskRunner(); +} + +bool GlobalModuleContext::RegisterModule(ModulePtr module) { + return p_->RegisterModule(module); +} + +bool GlobalModuleContext::ActiveModule(ModuleIdentifier module_id) { + return p_->ActiveModule(module_id); +} + +bool GlobalModuleContext::ListenEvent(ModuleIdentifier module_id, + EventIdentifier event) { + return p_->ListenEvent(module_id, event); +} + +bool GlobalModuleContext::DeactivateModule(ModuleIdentifier module_id) { + return p_->DeactivateModule(module_id); +} + +bool GlobalModuleContext::TriggerEvent(EventRefrernce event) { + return p_->TriggerEvent(event); +} + +int GlobalModuleContext::GetChannel(ModulePtr module) { + return p_->GetChannel(module); +} + +int GlobalModuleContext::GetDefaultChannel(ModulePtr _) { + return p_->GetDefaultChannel(_); +} + +} // namespace GpgFrontend::Module -- cgit v1.2.3 From 4fa7cc872224014f6e5bc731164c74bfa96db06e Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 18 Oct 2023 02:04:05 +0800 Subject: feat: imporve module system --- src/core/module/GlobalModuleContext.cpp | 58 ++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 15 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index df58c211..69743915 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -28,6 +28,7 @@ #include "GlobalModuleContext.h" +#include #include #include #include @@ -39,6 +40,8 @@ #include "core/module/Event.h" #include "core/module/Module.h" #include "core/thread/Task.h" +#include "spdlog/spdlog.h" +#include "thread/DataObject.h" namespace GpgFrontend::Module { @@ -55,7 +58,7 @@ class GlobalModuleContext::Impl { acquired_channel_.insert(GPGFRONTEND_NON_ASCII_CHANNEL); } - int GetChannel(ModulePtr module) { + int GetChannel(ModuleRawPtr module) { // Search for the module in the register table. auto module_info_opt = search_module_register_table(module->GetModuleIdentifier()); @@ -72,9 +75,9 @@ class GlobalModuleContext::Impl { return module_info->channel; } - int GetDefaultChannel(ModulePtr) { return GPGFRONTEND_DEFAULT_CHANNEL; } + int GetDefaultChannel(ModuleRawPtr) { return GPGFRONTEND_DEFAULT_CHANNEL; } - std::optional GetTaskRunner(ModulePtr module) { + std::optional GetTaskRunner(ModuleRawPtr module) { auto opt = search_module_register_table(module->GetModuleIdentifier()); if (!opt.has_value()) { return std::nullopt; @@ -116,6 +119,7 @@ class GlobalModuleContext::Impl { register_info.module = module; register_info.channel = acquire_new_unique_channel(); register_info.task_runner = std::make_shared(); + register_info.task_runner->start(); // Register the module with its identifier. module_register_table_[module->GetModuleIdentifier()] = @@ -138,7 +142,7 @@ class GlobalModuleContext::Impl { auto module_info = module_info_opt.value(); // Activate the module if it is not already active. - if (module_info->activate && module_info->module->Active()) { + if (!module_info->activate && module_info->module->Active()) { module_info->activate = true; } @@ -185,15 +189,16 @@ class GlobalModuleContext::Impl { } bool TriggerEvent(EventRefrernce event) { - SPDLOG_DEBUG("attempting to trigger event: {}", event->GetIdentifier()); + auto event_id = event->GetIdentifier(); + SPDLOG_DEBUG("attempting to trigger event: {}", event_id); // Find the set of listeners associated with the given event in the table - auto it = module_events_table_.find(event->GetIdentifier()); + auto it = module_events_table_.find(event_id); if (it == module_events_table_.end()) { // Log a warning if the event is not registered and nobody is listening SPDLOG_WARN( "event {} is not listening by anyone and not registered as well", - event->GetIdentifier()); + event_id); return false; } @@ -225,15 +230,38 @@ class GlobalModuleContext::Impl { // Retrieve the module's information auto module_info = module_info_opt.value(); + auto module = module_info->module; + + SPDLOG_DEBUG( + "module {} is listening to event {}, activate state: {}, task runner " + "running state: {}", + module_info->module->GetModuleIdentifier(), event->GetIdentifier(), + module_info->activate, module_info->task_runner->isRunning()); // Check if the module is activated if (!module_info->activate) continue; - // Execute the module and check if it fails - if (module_info->module->Exec(event)) { - // Log an error if the module execution fails - SPDLOG_ERROR("module {} executed failed", listener_module_id); - } + Thread::Task::TaskRunnable exec_runnerable = + [module, event](Thread::DataObjectPtr) -> int { + return module->Exec(event); + }; + + Thread::Task::TaskCallback exec_callback = + [listener_module_id, event_id](int code, Thread::DataObjectPtr) { + if (code < 0) { + // Log an error if the module execution fails + SPDLOG_ERROR( + "module {} execution failed of event {}: exec return code {}", + listener_module_id, event_id, code); + } + }; + + module_info->task_runner->PostTask( + new Thread::Task(exec_runnerable, + (boost::format("event/%1%/module/exec/%2%") % + event_id % listener_module_id) + .str(), + nullptr, exec_callback)); } // Return true to indicate successful execution of all modules @@ -294,7 +322,7 @@ GlobalModuleContext::~GlobalModuleContext() = default; // Function to get the task runner associated with a module. std::optional GlobalModuleContext::GetTaskRunner( - ModulePtr module) { + ModuleRawPtr module) { return p_->GetTaskRunner(module); } @@ -330,11 +358,11 @@ bool GlobalModuleContext::TriggerEvent(EventRefrernce event) { return p_->TriggerEvent(event); } -int GlobalModuleContext::GetChannel(ModulePtr module) { +int GlobalModuleContext::GetChannel(ModuleRawPtr module) { return p_->GetChannel(module); } -int GlobalModuleContext::GetDefaultChannel(ModulePtr _) { +int GlobalModuleContext::GetDefaultChannel(ModuleRawPtr _) { return p_->GetDefaultChannel(_); } -- cgit v1.2.3 From 216905e2532a79101e987e936fdf8b353b2a4dab Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 18 Oct 2023 17:49:06 +0800 Subject: fix: improve thread and module relation --- src/core/module/GlobalModuleContext.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 69743915..7e5f8f00 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -121,6 +121,9 @@ class GlobalModuleContext::Impl { register_info.task_runner = std::make_shared(); register_info.task_runner->start(); + // move module to its task runner' thread + register_info.module->moveToThread(register_info.task_runner.get()); + // Register the module with its identifier. module_register_table_[module->GetModuleIdentifier()] = std::make_shared(std::move(register_info)); -- cgit v1.2.3 From 76fda183d4c1067ab1735965e9bde3c7b29d1345 Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 18 Oct 2023 20:54:02 +0800 Subject: feat: simplify the thread system and improve its stability --- src/core/module/GlobalModuleContext.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 7e5f8f00..10c3e549 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -119,10 +119,10 @@ class GlobalModuleContext::Impl { register_info.module = module; register_info.channel = acquire_new_unique_channel(); register_info.task_runner = std::make_shared(); - register_info.task_runner->start(); + register_info.task_runner->Start(); // move module to its task runner' thread - register_info.module->moveToThread(register_info.task_runner.get()); + register_info.module->moveToThread(register_info.task_runner->GetThread()); // Register the module with its identifier. module_register_table_[module->GetModuleIdentifier()] = @@ -239,7 +239,7 @@ class GlobalModuleContext::Impl { "module {} is listening to event {}, activate state: {}, task runner " "running state: {}", module_info->module->GetModuleIdentifier(), event->GetIdentifier(), - module_info->activate, module_info->task_runner->isRunning()); + module_info->activate, module_info->task_runner->IsRunning()); // Check if the module is activated if (!module_info->activate) continue; -- cgit v1.2.3 From 8fdeb3af49999f29e017b7f7a70bd36f020ba721 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 23 Oct 2023 18:27:25 +0800 Subject: perf: reduce header includes and improve build speed --- src/core/module/GlobalModuleContext.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 10c3e549..de1e244b 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -28,12 +28,10 @@ #include "GlobalModuleContext.h" -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -48,11 +46,11 @@ namespace GpgFrontend::Module { class GlobalModuleContext::Impl { public: Impl(TaskRunnerPtr task_runner) - : default_task_runner_(task_runner), - random_gen_( + : random_gen_( (boost::posix_time::microsec_clock::universal_time() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) - .total_milliseconds()) { + .total_milliseconds()), + default_task_runner_(task_runner) { // Initialize acquired channels with default values. acquired_channel_.insert(GPGFRONTEND_DEFAULT_CHANNEL); acquired_channel_.insert(GPGFRONTEND_NON_ASCII_CHANNEL); -- cgit v1.2.3 From 5a3f422335b27c6c19a2d91f525c77435e8f2384 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 23 Oct 2023 21:23:37 +0800 Subject: fix: solve some issues on log system --- src/core/module/GlobalModuleContext.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index de1e244b..919c744a 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -120,6 +120,7 @@ class GlobalModuleContext::Impl { register_info.task_runner->Start(); // move module to its task runner' thread + register_info.module->setParent(nullptr); register_info.module->moveToThread(register_info.task_runner->GetThread()); // Register the module with its identifier. -- cgit v1.2.3 From fa2e87a48acbc32650ca9db073b991729dfba622 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Tue, 24 Oct 2023 21:22:13 +0800 Subject: feat: use module instead of integrated code at version checking task --- src/core/module/GlobalModuleContext.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 919c744a..1e6d87b1 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -270,6 +270,10 @@ class GlobalModuleContext::Impl { return true; } + bool IsModuleExists(ModuleIdentifier id) const { + return search_module_register_table(id).has_value(); + } + private: struct ModuleRegisterInfo { int channel; @@ -305,15 +309,13 @@ class GlobalModuleContext::Impl { // Function to search for a module in the register table. std::optional search_module_register_table( - ModuleIdentifier identifier) { + ModuleIdentifier identifier) const { auto it = module_register_table_.find(identifier); if (it == module_register_table_.end()) { return std::nullopt; } return it->second; } - - std::list& search_module_events_table(ModuleIdentifier); }; // Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument. @@ -368,4 +370,8 @@ int GlobalModuleContext::GetDefaultChannel(ModuleRawPtr _) { return p_->GetDefaultChannel(_); } +bool GlobalModuleContext::IsModuleExists(ModuleIdentifier id) { + return p_->IsModuleExists(id); +} + } // namespace GpgFrontend::Module -- cgit v1.2.3 From 124929609eabff19359caad276a10f1026793c0f Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 25 Oct 2023 18:26:26 +0800 Subject: fix: solve some code tidy issues --- src/core/module/GlobalModuleContext.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 1e6d87b1..edd35000 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -38,7 +38,6 @@ #include "core/module/Event.h" #include "core/module/Module.h" #include "core/thread/Task.h" -#include "spdlog/spdlog.h" #include "thread/DataObject.h" namespace GpgFrontend::Module { -- cgit v1.2.3 From fd46d4667611c0db9cea3f06205727399b6fb5fd Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 29 Oct 2023 02:46:15 +0800 Subject: refactor: start to tidy up code using clang-tidy --- src/core/module/GlobalModuleContext.cpp | 135 ++++++++++++++++---------------- 1 file changed, 69 insertions(+), 66 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index edd35000..caf04aff 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -34,28 +34,29 @@ #include #include #include +#include #include "core/module/Event.h" #include "core/module/Module.h" #include "core/thread/Task.h" -#include "thread/DataObject.h" +#include "model/DataObject.h" namespace GpgFrontend::Module { class GlobalModuleContext::Impl { public: - Impl(TaskRunnerPtr task_runner) + explicit Impl(TaskRunnerPtr task_runner) : random_gen_( (boost::posix_time::microsec_clock::universal_time() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) .total_milliseconds()), - default_task_runner_(task_runner) { + default_task_runner_(std::move(task_runner)) { // Initialize acquired channels with default values. acquired_channel_.insert(GPGFRONTEND_DEFAULT_CHANNEL); acquired_channel_.insert(GPGFRONTEND_NON_ASCII_CHANNEL); } - int GetChannel(ModuleRawPtr module) { + auto GetChannel(ModuleRawPtr module) -> int { // Search for the module in the register table. auto module_info_opt = search_module_register_table(module->GetModuleIdentifier()); @@ -72,9 +73,11 @@ class GlobalModuleContext::Impl { return module_info->channel; } - int GetDefaultChannel(ModuleRawPtr) { return GPGFRONTEND_DEFAULT_CHANNEL; } + static auto GetDefaultChannel(ModuleRawPtr) -> int { + return GPGFRONTEND_DEFAULT_CHANNEL; + } - std::optional GetTaskRunner(ModuleRawPtr module) { + auto GetTaskRunner(ModuleRawPtr module) -> std::optional { auto opt = search_module_register_table(module->GetModuleIdentifier()); if (!opt.has_value()) { return std::nullopt; @@ -82,7 +85,8 @@ class GlobalModuleContext::Impl { return opt.value()->task_runner; } - std::optional GetTaskRunner(ModuleIdentifier module_id) { + auto GetTaskRunner(ModuleIdentifier module_id) + -> std::optional { // Search for the module in the register table. auto module_info_opt = search_module_register_table(module_id); if (!module_info_opt.has_value()) { @@ -92,11 +96,11 @@ class GlobalModuleContext::Impl { return module_info_opt.value()->task_runner; } - std::optional GetGlobalTaskRunner() { + auto GetGlobalTaskRunner() -> std::optional { return default_task_runner_; } - bool RegisterModule(ModulePtr module) { + auto RegisterModule(const ModulePtr& module) -> bool { SPDLOG_DEBUG("attempting to register module: {}", module->GetModuleIdentifier()); // Check if the module is null or already registered. @@ -131,7 +135,7 @@ class GlobalModuleContext::Impl { return true; } - bool ActiveModule(ModuleIdentifier module_id) { + auto ActiveModule(ModuleIdentifier module_id) -> bool { SPDLOG_DEBUG("attempting to activate module: {}", module_id); // Search for the module in the register table. @@ -151,28 +155,27 @@ class GlobalModuleContext::Impl { return module_info->activate; } - bool ListenEvent(ModuleIdentifier module_id, EventIdentifier event) { + auto ListenEvent(ModuleIdentifier module_id, EventIdentifier event) -> bool { SPDLOG_DEBUG("module: {} is attempting to listen to event {}", module_id, event); // Check if the event exists, if not, create it. - auto it = module_events_table_.find(event); - if (it == module_events_table_.end()) { + auto met_it = module_events_table_.find(event); + if (met_it == module_events_table_.end()) { module_events_table_[event] = std::unordered_set(); - it = module_events_table_.find(event); + met_it = module_events_table_.find(event); SPDLOG_INFO("new event {} of module system created", event); } - auto& listeners_set = it->second; + auto& listeners_set = met_it->second; // Add the listener (module) to the event. - auto listener_it = - std::find(listeners_set.begin(), listeners_set.end(), module_id); + auto listener_it = listeners_set.find(module_id); if (listener_it == listeners_set.end()) { listeners_set.insert(module_id); } return true; } - bool DeactivateModule(ModuleIdentifier module_id) { + auto DeactivateModule(ModuleIdentifier module_id) -> bool { // Search for the module in the register table. auto module_info_opt = search_module_register_table(module_id); if (!module_info_opt.has_value()) { @@ -189,13 +192,13 @@ class GlobalModuleContext::Impl { return !module_info->activate; } - bool TriggerEvent(EventRefrernce event) { + auto TriggerEvent(const EventRefrernce& event) -> bool { auto event_id = event->GetIdentifier(); SPDLOG_DEBUG("attempting to trigger event: {}", event_id); // Find the set of listeners associated with the given event in the table - auto it = module_events_table_.find(event_id); - if (it == module_events_table_.end()) { + auto met_it = module_events_table_.find(event_id); + if (met_it == module_events_table_.end()) { // Log a warning if the event is not registered and nobody is listening SPDLOG_WARN( "event {} is not listening by anyone and not registered as well", @@ -204,7 +207,7 @@ class GlobalModuleContext::Impl { } // Retrieve the set of listeners for this event - auto& listeners_set = it->second; + auto& listeners_set = met_it->second; // Check if the set of listeners is empty if (listeners_set.empty()) { @@ -219,7 +222,7 @@ class GlobalModuleContext::Impl { event->GetIdentifier(), listeners_set.size()); // Iterate through each listener and execute the corresponding module - for (auto& listener_module_id : listeners_set) { + for (const auto& listener_module_id : listeners_set) { // Search for the module's information in the registration table auto module_info_opt = search_module_register_table(listener_module_id); @@ -227,6 +230,7 @@ class GlobalModuleContext::Impl { if (!module_info_opt.has_value()) { SPDLOG_ERROR("cannot find module id {} at register table", listener_module_id); + continue; } // Retrieve the module's information @@ -243,20 +247,18 @@ class GlobalModuleContext::Impl { if (!module_info->activate) continue; Thread::Task::TaskRunnable exec_runnerable = - [module, event](Thread::DataObjectPtr) -> int { - return module->Exec(event); + [module, event](DataObjectPtr) -> int { return module->Exec(event); }; + + Thread::Task::TaskCallback exec_callback = [listener_module_id, event_id]( + int code, DataObjectPtr) { + if (code < 0) { + // Log an error if the module execution fails + SPDLOG_ERROR( + "module {} execution failed of event {}: exec return code {}", + listener_module_id, event_id, code); + } }; - Thread::Task::TaskCallback exec_callback = - [listener_module_id, event_id](int code, Thread::DataObjectPtr) { - if (code < 0) { - // Log an error if the module execution fails - SPDLOG_ERROR( - "module {} execution failed of event {}: exec return code {}", - listener_module_id, event_id, code); - } - }; - module_info->task_runner->PostTask( new Thread::Task(exec_runnerable, (boost::format("event/%1%/module/exec/%2%") % @@ -269,8 +271,8 @@ class GlobalModuleContext::Impl { return true; } - bool IsModuleExists(ModuleIdentifier id) const { - return search_module_register_table(id).has_value(); + auto IsModuleExists(const ModuleIdentifier& m_id) const -> bool { + return search_module_register_table(m_id).has_value(); } private: @@ -292,7 +294,7 @@ class GlobalModuleContext::Impl { boost::random::mt19937 random_gen_; TaskRunnerPtr default_task_runner_; - int acquire_new_unique_channel() { + auto acquire_new_unique_channel() -> int { boost::random::uniform_int_distribution<> dist(1, 65535); int random_channel = dist(random_gen_); @@ -307,70 +309,71 @@ class GlobalModuleContext::Impl { } // Function to search for a module in the register table. - std::optional search_module_register_table( - ModuleIdentifier identifier) const { - auto it = module_register_table_.find(identifier); - if (it == module_register_table_.end()) { + auto search_module_register_table(const ModuleIdentifier& identifier) const + -> std::optional { + auto mrt_it = module_register_table_.find(identifier); + if (mrt_it == module_register_table_.end()) { return std::nullopt; } - return it->second; + return mrt_it->second; } }; // Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument. GlobalModuleContext::GlobalModuleContext(TaskRunnerPtr task_runner) - : p_(std::make_unique(task_runner)) {} + : p_(std::make_unique(std::move(task_runner))) {} GlobalModuleContext::~GlobalModuleContext() = default; // Function to get the task runner associated with a module. -std::optional GlobalModuleContext::GetTaskRunner( - ModuleRawPtr module) { +auto GlobalModuleContext::GetTaskRunner(ModuleRawPtr module) + -> std::optional { return p_->GetTaskRunner(module); } // Function to get the task runner associated with a module. -std::optional GlobalModuleContext::GetTaskRunner( - ModuleIdentifier module_id) { - return p_->GetTaskRunner(module_id); +auto GlobalModuleContext::GetTaskRunner(ModuleIdentifier module_id) + -> std::optional { + return p_->GetTaskRunner(std::move(module_id)); } // Function to get the global task runner. -std::optional GlobalModuleContext::GetGlobalTaskRunner() { +auto GlobalModuleContext::GetGlobalTaskRunner() + -> std::optional { return p_->GetGlobalTaskRunner(); } -bool GlobalModuleContext::RegisterModule(ModulePtr module) { - return p_->RegisterModule(module); +auto GlobalModuleContext::RegisterModule(ModulePtr module) -> bool { + return p_->RegisterModule(std::move(module)); } -bool GlobalModuleContext::ActiveModule(ModuleIdentifier module_id) { - return p_->ActiveModule(module_id); +auto GlobalModuleContext::ActiveModule(ModuleIdentifier module_id) -> bool { + return p_->ActiveModule(std::move(module_id)); } -bool GlobalModuleContext::ListenEvent(ModuleIdentifier module_id, - EventIdentifier event) { - return p_->ListenEvent(module_id, event); +auto GlobalModuleContext::ListenEvent(ModuleIdentifier module_id, + EventIdentifier event) -> bool { + return p_->ListenEvent(std::move(module_id), std::move(event)); } -bool GlobalModuleContext::DeactivateModule(ModuleIdentifier module_id) { - return p_->DeactivateModule(module_id); +auto GlobalModuleContext::DeactivateModule(ModuleIdentifier module_id) -> bool { + return p_->DeactivateModule(std::move(module_id)); } -bool GlobalModuleContext::TriggerEvent(EventRefrernce event) { - return p_->TriggerEvent(event); +auto GlobalModuleContext::TriggerEvent(EventRefrernce event) -> bool { + return p_->TriggerEvent(std::move(event)); } -int GlobalModuleContext::GetChannel(ModuleRawPtr module) { +auto GlobalModuleContext::GetChannel(ModuleRawPtr module) -> int { return p_->GetChannel(module); } -int GlobalModuleContext::GetDefaultChannel(ModuleRawPtr _) { - return p_->GetDefaultChannel(_); +auto GlobalModuleContext::GetDefaultChannel(ModuleRawPtr channel) -> int { + return GlobalModuleContext::Impl::GetDefaultChannel(channel); } -bool GlobalModuleContext::IsModuleExists(ModuleIdentifier id) { - return p_->IsModuleExists(id); +auto GlobalModuleContext::IsModuleExists(ModuleIdentifier m_id) -> bool { + return p_->IsModuleExists(std::move(m_id)); } } // namespace GpgFrontend::Module -- cgit v1.2.3 From 5d7b1d5493df8723259eca0613a9ce0af6077289 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 30 Oct 2023 14:52:29 +0800 Subject: style: improve code style of core --- src/core/module/GlobalModuleContext.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index caf04aff..cb459f65 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -52,8 +52,8 @@ class GlobalModuleContext::Impl { .total_milliseconds()), default_task_runner_(std::move(task_runner)) { // Initialize acquired channels with default values. - acquired_channel_.insert(GPGFRONTEND_DEFAULT_CHANNEL); - acquired_channel_.insert(GPGFRONTEND_NON_ASCII_CHANNEL); + acquired_channel_.insert(kGpgfrontendDefaultChannel); + acquired_channel_.insert(kGpgfrontendNonAsciiChannel); } auto GetChannel(ModuleRawPtr module) -> int { @@ -74,7 +74,7 @@ class GlobalModuleContext::Impl { } static auto GetDefaultChannel(ModuleRawPtr) -> int { - return GPGFRONTEND_DEFAULT_CHANNEL; + return kGpgfrontendDefaultChannel; } auto GetTaskRunner(ModuleRawPtr module) -> std::optional { -- cgit v1.2.3 From 883db05d54510e76b6548e107593187e1306117d Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 04:28:46 -0800 Subject: feat: general improvements of aync execution and memory security --- src/core/module/GlobalModuleContext.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index cb459f65..4195d719 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -119,7 +119,8 @@ class GlobalModuleContext::Impl { ModuleRegisterInfo register_info; register_info.module = module; register_info.channel = acquire_new_unique_channel(); - register_info.task_runner = std::make_shared(); + register_info.task_runner = + GpgFrontend::SecureCreateSharedObject(); register_info.task_runner->Start(); // move module to its task runner' thread @@ -128,7 +129,8 @@ class GlobalModuleContext::Impl { // Register the module with its identifier. module_register_table_[module->GetModuleIdentifier()] = - std::make_shared(std::move(register_info)); + GpgFrontend::SecureCreateSharedObject( + std::move(register_info)); SPDLOG_DEBUG("successfully registered module: {}", module->GetModuleIdentifier()); -- cgit v1.2.3 From 054e6e28cca2517dda2319ef683314b3318c39a6 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 12:25:21 -0800 Subject: feat: standarized and speed up app env loading process --- src/core/module/GlobalModuleContext.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 4195d719..88250a1e 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -273,8 +273,9 @@ class GlobalModuleContext::Impl { return true; } - auto IsModuleExists(const ModuleIdentifier& m_id) const -> bool { - return search_module_register_table(m_id).has_value(); + auto IsModuleActivated(const ModuleIdentifier& m_id) const -> bool { + auto m = search_module_register_table(m_id); + return m.has_value() && m->get()->activate; } private: @@ -374,8 +375,8 @@ auto GlobalModuleContext::GetDefaultChannel(ModuleRawPtr channel) -> int { return GlobalModuleContext::Impl::GetDefaultChannel(channel); } -auto GlobalModuleContext::IsModuleExists(ModuleIdentifier m_id) -> bool { - return p_->IsModuleExists(std::move(m_id)); +auto GlobalModuleContext::IsModuleActivated(ModuleIdentifier m_id) -> bool { + return p_->IsModuleActivated(std::move(m_id)); } } // namespace GpgFrontend::Module -- cgit v1.2.3 From a0f0fd98efa12ef33959badcc25ff568e8b242b4 Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 6 Dec 2023 21:10:48 +0800 Subject: feat: move test to src and add submodule googletest --- src/core/module/GlobalModuleContext.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 88250a1e..9eddb57a 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -52,8 +52,8 @@ class GlobalModuleContext::Impl { .total_milliseconds()), default_task_runner_(std::move(task_runner)) { // Initialize acquired channels with default values. - acquired_channel_.insert(kGpgfrontendDefaultChannel); - acquired_channel_.insert(kGpgfrontendNonAsciiChannel); + acquired_channel_.insert(kGpgFrontendDefaultChannel); + acquired_channel_.insert(kGpgFrontendNonAsciiChannel); } auto GetChannel(ModuleRawPtr module) -> int { @@ -74,7 +74,7 @@ class GlobalModuleContext::Impl { } static auto GetDefaultChannel(ModuleRawPtr) -> int { - return kGpgfrontendDefaultChannel; + return kGpgFrontendDefaultChannel; } auto GetTaskRunner(ModuleRawPtr module) -> std::optional { -- cgit v1.2.3 From 6a9ca113beb78f5d7315e2a40025ea5bdcdb8d17 Mon Sep 17 00:00:00 2001 From: saturneric Date: Wed, 13 Dec 2023 20:23:14 +0800 Subject: fix: slove a memory issue found by valgrind --- src/core/module/GlobalModuleContext.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 9eddb57a..e900535c 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -116,21 +116,21 @@ class GlobalModuleContext::Impl { return false; } - ModuleRegisterInfo register_info; - register_info.module = module; - register_info.channel = acquire_new_unique_channel(); - register_info.task_runner = + auto register_info = + GpgFrontend::SecureCreateSharedObject(); + register_info->module = module; + register_info->channel = acquire_new_unique_channel(); + register_info->task_runner = GpgFrontend::SecureCreateSharedObject(); - register_info.task_runner->Start(); + register_info->task_runner->Start(); // move module to its task runner' thread - register_info.module->setParent(nullptr); - register_info.module->moveToThread(register_info.task_runner->GetThread()); + register_info->module->setParent(nullptr); + register_info->module->moveToThread( + register_info->task_runner->GetThread()); // Register the module with its identifier. - module_register_table_[module->GetModuleIdentifier()] = - GpgFrontend::SecureCreateSharedObject( - std::move(register_info)); + module_register_table_[module->GetModuleIdentifier()] = register_info; SPDLOG_DEBUG("successfully registered module: {}", module->GetModuleIdentifier()); @@ -148,8 +148,19 @@ class GlobalModuleContext::Impl { } auto module_info = module_info_opt.value(); + + // try to get module from module info + auto module = module_info->module; + if (module == nullptr) { + SPDLOG_ERROR( + "module id {} at register table is releated to a null module", + module_id); + return false; + } + // Activate the module if it is not already active. - if (!module_info->activate && module_info->module->Active()) { + if (!module_info->activate) { + module->Active(); module_info->activate = true; } -- cgit v1.2.3 From f9a49043c35e73fc2d4ffb3ed9b39c33849c43b3 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 15 Dec 2023 21:14:17 +0800 Subject: fix: slove threading and memory issues --- src/core/module/GlobalModuleContext.cpp | 61 ++++++++++++++------------------- 1 file changed, 26 insertions(+), 35 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index e900535c..e2f58c92 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -40,17 +40,18 @@ #include "core/module/Module.h" #include "core/thread/Task.h" #include "model/DataObject.h" +#include "thread/TaskRunnerGetter.h" +#include "utils/MemoryUtils.h" namespace GpgFrontend::Module { class GlobalModuleContext::Impl { public: - explicit Impl(TaskRunnerPtr task_runner) + explicit Impl() : random_gen_( (boost::posix_time::microsec_clock::universal_time() - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) - .total_milliseconds()), - default_task_runner_(std::move(task_runner)) { + .total_milliseconds()) { // Initialize acquired channels with default values. acquired_channel_.insert(kGpgFrontendDefaultChannel); acquired_channel_.insert(kGpgFrontendNonAsciiChannel); @@ -77,23 +78,15 @@ class GlobalModuleContext::Impl { return kGpgFrontendDefaultChannel; } - auto GetTaskRunner(ModuleRawPtr module) -> std::optional { - auto opt = search_module_register_table(module->GetModuleIdentifier()); - if (!opt.has_value()) { - return std::nullopt; - } - return opt.value()->task_runner; + auto GetTaskRunner(ModuleRawPtr /*module*/) -> std::optional { + return Thread::TaskRunnerGetter::GetInstance().GetTaskRunner( + Thread::TaskRunnerGetter::kTaskRunnerType_Module); } - auto GetTaskRunner(ModuleIdentifier module_id) + auto GetTaskRunner(ModuleIdentifier /*module_id*/) -> std::optional { - // Search for the module in the register table. - auto module_info_opt = search_module_register_table(module_id); - if (!module_info_opt.has_value()) { - SPDLOG_ERROR("cannot find module id {} at register table", module_id); - return std::nullopt; - } - return module_info_opt.value()->task_runner; + return Thread::TaskRunnerGetter::GetInstance().GetTaskRunner( + Thread::TaskRunnerGetter::kTaskRunnerType_Module); } auto GetGlobalTaskRunner() -> std::optional { @@ -120,14 +113,13 @@ class GlobalModuleContext::Impl { GpgFrontend::SecureCreateSharedObject(); register_info->module = module; register_info->channel = acquire_new_unique_channel(); - register_info->task_runner = - GpgFrontend::SecureCreateSharedObject(); - register_info->task_runner->Start(); // move module to its task runner' thread register_info->module->setParent(nullptr); register_info->module->moveToThread( - register_info->task_runner->GetThread()); + Thread::TaskRunnerGetter::GetInstance() + .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Module) + ->GetThread()); // Register the module with its identifier. module_register_table_[module->GetModuleIdentifier()] = register_info; @@ -250,11 +242,9 @@ class GlobalModuleContext::Impl { auto module_info = module_info_opt.value(); auto module = module_info->module; - SPDLOG_DEBUG( - "module {} is listening to event {}, activate state: {}, task runner " - "running state: {}", - module_info->module->GetModuleIdentifier(), event->GetIdentifier(), - module_info->activate, module_info->task_runner->IsRunning()); + SPDLOG_DEBUG("module {} is listening to event {}, activate state: {}", + module_info->module->GetModuleIdentifier(), + event->GetIdentifier(), module_info->activate); // Check if the module is activated if (!module_info->activate) continue; @@ -272,12 +262,14 @@ class GlobalModuleContext::Impl { } }; - module_info->task_runner->PostTask( - new Thread::Task(exec_runnerable, - (boost::format("event/%1%/module/exec/%2%") % - event_id % listener_module_id) - .str(), - nullptr, exec_callback)); + Thread::TaskRunnerGetter::GetInstance() + .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Module) + ->PostTask( + new Thread::Task(exec_runnerable, + (boost::format("event/%1%/module/exec/%2%") % + event_id % listener_module_id) + .str(), + nullptr, exec_callback)); } // Return true to indicate successful execution of all modules @@ -292,7 +284,6 @@ class GlobalModuleContext::Impl { private: struct ModuleRegisterInfo { int channel; - TaskRunnerPtr task_runner; ModulePtr module; bool activate; }; @@ -334,8 +325,8 @@ class GlobalModuleContext::Impl { }; // Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument. -GlobalModuleContext::GlobalModuleContext(TaskRunnerPtr task_runner) - : p_(std::make_unique(std::move(task_runner))) {} +GlobalModuleContext::GlobalModuleContext() + : p_(SecureCreateUniqueObject()) {} GlobalModuleContext::~GlobalModuleContext() = default; -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/core/module/GlobalModuleContext.cpp | 59 ++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index e2f58c92..6222a97d 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -62,7 +62,7 @@ class GlobalModuleContext::Impl { auto module_info_opt = search_module_register_table(module->GetModuleIdentifier()); if (!module_info_opt.has_value()) { - SPDLOG_ERROR( + GF_CORE_LOG_ERROR( "cannot find module id {} at register table, fallbacking to " "default " "channel", @@ -94,18 +94,20 @@ class GlobalModuleContext::Impl { } auto RegisterModule(const ModulePtr& module) -> bool { - SPDLOG_DEBUG("attempting to register module: {}", - module->GetModuleIdentifier()); + GF_CORE_LOG_DEBUG("attempting to register module: {}", + module->GetModuleIdentifier()); // Check if the module is null or already registered. if (module == nullptr || module_register_table_.find(module->GetModuleIdentifier()) != module_register_table_.end()) { - SPDLOG_ERROR("module is null or have already registered this module"); + GF_CORE_LOG_ERROR( + "module is null or have already registered this module"); return false; } if (!module->Register()) { - SPDLOG_ERROR("register module {} failed", module->GetModuleIdentifier()); + GF_CORE_LOG_ERROR("register module {} failed", + module->GetModuleIdentifier()); return false; } @@ -124,18 +126,19 @@ class GlobalModuleContext::Impl { // Register the module with its identifier. module_register_table_[module->GetModuleIdentifier()] = register_info; - SPDLOG_DEBUG("successfully registered module: {}", - module->GetModuleIdentifier()); + GF_CORE_LOG_DEBUG("successfully registered module: {}", + module->GetModuleIdentifier()); return true; } auto ActiveModule(ModuleIdentifier module_id) -> bool { - SPDLOG_DEBUG("attempting to activate module: {}", module_id); + GF_CORE_LOG_DEBUG("attempting to activate module: {}", module_id); // Search for the module in the register table. auto module_info_opt = search_module_register_table(module_id); if (!module_info_opt.has_value()) { - SPDLOG_ERROR("cannot find module id {} at register table", module_id); + GF_CORE_LOG_ERROR("cannot find module id {} at register table", + module_id); return false; } @@ -144,7 +147,7 @@ class GlobalModuleContext::Impl { // try to get module from module info auto module = module_info->module; if (module == nullptr) { - SPDLOG_ERROR( + GF_CORE_LOG_ERROR( "module id {} at register table is releated to a null module", module_id); return false; @@ -156,19 +159,19 @@ class GlobalModuleContext::Impl { module_info->activate = true; } - SPDLOG_DEBUG("module activation status: {}", module_info->activate); + GF_CORE_LOG_DEBUG("module activation status: {}", module_info->activate); return module_info->activate; } auto ListenEvent(ModuleIdentifier module_id, EventIdentifier event) -> bool { - SPDLOG_DEBUG("module: {} is attempting to listen to event {}", module_id, - event); + GF_CORE_LOG_DEBUG("module: {} is attempting to listen to event {}", + module_id, event); // Check if the event exists, if not, create it. auto met_it = module_events_table_.find(event); if (met_it == module_events_table_.end()) { module_events_table_[event] = std::unordered_set(); met_it = module_events_table_.find(event); - SPDLOG_INFO("new event {} of module system created", event); + GF_CORE_LOG_INFO("new event {} of module system created", event); } auto& listeners_set = met_it->second; @@ -184,7 +187,8 @@ class GlobalModuleContext::Impl { // Search for the module in the register table. auto module_info_opt = search_module_register_table(module_id); if (!module_info_opt.has_value()) { - SPDLOG_ERROR("cannot find module id {} at register table", module_id); + GF_CORE_LOG_ERROR("cannot find module id {} at register table", + module_id); return false; } @@ -199,13 +203,13 @@ class GlobalModuleContext::Impl { auto TriggerEvent(const EventRefrernce& event) -> bool { auto event_id = event->GetIdentifier(); - SPDLOG_DEBUG("attempting to trigger event: {}", event_id); + GF_CORE_LOG_DEBUG("attempting to trigger event: {}", event_id); // Find the set of listeners associated with the given event in the table auto met_it = module_events_table_.find(event_id); if (met_it == module_events_table_.end()) { // Log a warning if the event is not registered and nobody is listening - SPDLOG_WARN( + GF_CORE_LOG_WARN( "event {} is not listening by anyone and not registered as well", event_id); return false; @@ -217,14 +221,14 @@ class GlobalModuleContext::Impl { // Check if the set of listeners is empty if (listeners_set.empty()) { // Log a warning if nobody is listening to this event - SPDLOG_WARN("event {} is not listening by anyone", - event->GetIdentifier()); + GF_CORE_LOG_WARN("event {} is not listening by anyone", + event->GetIdentifier()); return false; } // Log the number of listeners for this event - SPDLOG_DEBUG("event {}'s current listeners size: {}", - event->GetIdentifier(), listeners_set.size()); + GF_CORE_LOG_DEBUG("event {}'s current listeners size: {}", + event->GetIdentifier(), listeners_set.size()); // Iterate through each listener and execute the corresponding module for (const auto& listener_module_id : listeners_set) { @@ -233,8 +237,8 @@ class GlobalModuleContext::Impl { // Log an error if the module is not found in the registration table if (!module_info_opt.has_value()) { - SPDLOG_ERROR("cannot find module id {} at register table", - listener_module_id); + GF_CORE_LOG_ERROR("cannot find module id {} at register table", + listener_module_id); continue; } @@ -242,9 +246,10 @@ class GlobalModuleContext::Impl { auto module_info = module_info_opt.value(); auto module = module_info->module; - SPDLOG_DEBUG("module {} is listening to event {}, activate state: {}", - module_info->module->GetModuleIdentifier(), - event->GetIdentifier(), module_info->activate); + GF_CORE_LOG_DEBUG( + "module {} is listening to event {}, activate state: {}", + module_info->module->GetModuleIdentifier(), event->GetIdentifier(), + module_info->activate); // Check if the module is activated if (!module_info->activate) continue; @@ -256,7 +261,7 @@ class GlobalModuleContext::Impl { int code, DataObjectPtr) { if (code < 0) { // Log an error if the module execution fails - SPDLOG_ERROR( + GF_CORE_LOG_ERROR( "module {} execution failed of event {}: exec return code {}", listener_module_id, event_id, code); } -- cgit v1.2.3 From 57438e754cd857ef48c3960eefe2e957716acd80 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 21:28:59 +0800 Subject: fix: slove a multi-threads issue at task model --- src/core/module/GlobalModuleContext.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 6222a97d..a86879ab 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -254,18 +254,18 @@ class GlobalModuleContext::Impl { // Check if the module is activated if (!module_info->activate) continue; - Thread::Task::TaskRunnable exec_runnerable = + Thread::Task::TaskRunnable const exec_runnerable = [module, event](DataObjectPtr) -> int { return module->Exec(event); }; - Thread::Task::TaskCallback exec_callback = [listener_module_id, event_id]( - int code, DataObjectPtr) { - if (code < 0) { - // Log an error if the module execution fails - GF_CORE_LOG_ERROR( - "module {} execution failed of event {}: exec return code {}", - listener_module_id, event_id, code); - } - }; + Thread::Task::TaskCallback const exec_callback = + [listener_module_id, event_id](int code, DataObjectPtr) { + if (code < 0) { + // Log an error if the module execution fails + GF_CORE_LOG_ERROR( + "module {} execution failed of event {}: exec return code {}", + listener_module_id, event_id, code); + } + }; Thread::TaskRunnerGetter::GetInstance() .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Module) -- cgit v1.2.3 From bf538056b24a68b8fd235b1c50991ee8eb46a776 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 14:02:37 +0800 Subject: refactor: use QString instead of std::string and improve threading system --- src/core/module/GlobalModuleContext.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index a86879ab..cac3dfbb 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -29,7 +29,6 @@ #include "GlobalModuleContext.h" #include -#include #include #include #include @@ -269,12 +268,11 @@ class GlobalModuleContext::Impl { Thread::TaskRunnerGetter::GetInstance() .GetTaskRunner(Thread::TaskRunnerGetter::kTaskRunnerType_Module) - ->PostTask( - new Thread::Task(exec_runnerable, - (boost::format("event/%1%/module/exec/%2%") % - event_id % listener_module_id) - .str(), - nullptr, exec_callback)); + ->PostTask(new Thread::Task(exec_runnerable, + QString("event/%1/module/exec/%2") + .arg(event_id) + .arg(listener_module_id), + nullptr, exec_callback)); } // Return true to indicate successful execution of all modules -- cgit v1.2.3 From 6983b5c1dd82d159236ebd06cf17f071cc9c1ee9 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Jan 2024 23:08:38 +0800 Subject: refactor: remove boost and use QString instead of std::filesystem::path --- src/core/module/GlobalModuleContext.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index cac3dfbb..16650c96 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -28,8 +28,6 @@ #include "GlobalModuleContext.h" -#include -#include #include #include #include @@ -46,11 +44,7 @@ namespace GpgFrontend::Module { class GlobalModuleContext::Impl { public: - explicit Impl() - : random_gen_( - (boost::posix_time::microsec_clock::universal_time() - - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) - .total_milliseconds()) { + explicit Impl() { // Initialize acquired channels with default values. acquired_channel_.insert(kGpgFrontendDefaultChannel); acquired_channel_.insert(kGpgFrontendNonAsciiChannel); @@ -299,16 +293,13 @@ class GlobalModuleContext::Impl { module_events_table_; std::set acquired_channel_; - boost::random::mt19937 random_gen_; TaskRunnerPtr default_task_runner_; auto acquire_new_unique_channel() -> int { - boost::random::uniform_int_distribution<> dist(1, 65535); - - int random_channel = dist(random_gen_); + int random_channel = QRandomGenerator::global()->bounded(65535); // Ensure the acquired channel is unique. while (acquired_channel_.find(random_channel) != acquired_channel_.end()) { - random_channel = dist(random_gen_); + random_channel = QRandomGenerator::global()->bounded(65535); } // Add the acquired channel to the set. -- cgit v1.2.3 From 4994f4eaa1211d402b791660ad6221154a4c2405 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 16 Jan 2024 11:49:50 +0800 Subject: fix: make task and threading system safer --- src/core/module/GlobalModuleContext.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 16650c96..50f97334 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include "core/module/Event.h" #include "core/module/Module.h" -- cgit v1.2.3 From f22ceca734868a4cb946c232f661aad72da01ded Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 19 Jan 2024 20:10:17 +0800 Subject: fix: slove discovered faults and bugs --- src/core/module/GlobalModuleContext.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 50f97334..9bc4f06b 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -163,7 +163,7 @@ class GlobalModuleContext::Impl { if (met_it == module_events_table_.end()) { module_events_table_[event] = std::unordered_set(); met_it = module_events_table_.find(event); - GF_CORE_LOG_INFO("new event {} of module system created", event); + GF_CORE_LOG_DEBUG("new event {} of module system created", event); } auto& listeners_set = met_it->second; -- cgit v1.2.3