diff options
author | saturneric <[email protected]> | 2023-10-17 18:04:05 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-10-17 18:04:05 +0000 |
commit | 4fa7cc872224014f6e5bc731164c74bfa96db06e (patch) | |
tree | 5bb101372e27524b26c7d8c8ce256832463104e8 | |
parent | fix: solve params mismatch issues (diff) | |
download | GpgFrontend-4fa7cc872224014f6e5bc731164c74bfa96db06e.tar.gz GpgFrontend-4fa7cc872224014f6e5bc731164c74bfa96db06e.zip |
feat: imporve module system
Diffstat (limited to '')
-rw-r--r-- | src/core/module/Event.h | 4 | ||||
-rw-r--r-- | src/core/module/GlobalModuleContext.cpp | 58 | ||||
-rw-r--r-- | src/core/module/GlobalModuleContext.h | 7 | ||||
-rw-r--r-- | src/core/module/Module.cpp | 29 | ||||
-rw-r--r-- | src/core/module/ModuleManager.h | 9 |
5 files changed, 66 insertions, 41 deletions
diff --git a/src/core/module/Event.h b/src/core/module/Event.h index 8703b159..7ef5049c 100644 --- a/src/core/module/Event.h +++ b/src/core/module/Event.h @@ -77,10 +77,10 @@ class GPGFRONTEND_CORE_EXPORT Event { }; template <typename... Args> -Event MakeEvent(const std::string& eventIdentifier, Args&&... args) { +EventRefrernce MakeEvent(const EventIdentifier& event_id, Args&&... args) { std::initializer_list<Event::ParameterInitializer> params = { Event::ParameterInitializer{std::forward<Args>(args)}...}; - return Event(eventIdentifier, params); + return std::make_shared<Event>(event_id, params); } } // namespace GpgFrontend::Module 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 <boost/format/format_fwd.hpp> #include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_int_distribution.hpp> #include <memory> @@ -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<TaskRunnerPtr> GetTaskRunner(ModulePtr module) { + std::optional<TaskRunnerPtr> 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<Thread::TaskRunner>(); + 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<TaskRunnerPtr> 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(_); } diff --git a/src/core/module/GlobalModuleContext.h b/src/core/module/GlobalModuleContext.h index e4dcac2f..fd90d303 100644 --- a/src/core/module/GlobalModuleContext.h +++ b/src/core/module/GlobalModuleContext.h @@ -41,6 +41,7 @@ class Module; class ModuleManager; using ModuleIdentifier = std::string; using ModulePtr = std::shared_ptr<Module>; +using ModuleRawPtr = Module*; using GlobalModuleContextPtr = std::shared_ptr<GlobalModuleContext>; @@ -53,11 +54,11 @@ class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject { ~GlobalModuleContext(); - int GetChannel(ModulePtr); + int GetChannel(ModuleRawPtr); - int GetDefaultChannel(ModulePtr); + int GetDefaultChannel(ModuleRawPtr); - std::optional<TaskRunnerPtr> GetTaskRunner(ModulePtr); + std::optional<TaskRunnerPtr> GetTaskRunner(ModuleRawPtr); std::optional<TaskRunnerPtr> GetTaskRunner(ModuleIdentifier); diff --git a/src/core/module/Module.cpp b/src/core/module/Module.cpp index d84b74af..33525072 100644 --- a/src/core/module/Module.cpp +++ b/src/core/module/Module.cpp @@ -38,26 +38,25 @@ class Module::Impl { public: friend class GlobalModuleContext; - Impl(ModuleIdentifier id, ModuleVersion version, ModuleMetaData meta_data) - : identifier_((boost::format("__module_%1%") % id).str()), + using ExecCallback = std::function<void(int)>; + + Impl(ModuleRawPtr m_ptr, ModuleIdentifier id, ModuleVersion version, + ModuleMetaData meta_data) + : m_ptr_(m_ptr), + identifier_((boost::format("__module_%1%") % id).str()), version_(version), meta_data_(meta_data) {} - int GetChannel() { - return get_global_module_context()->GetChannel(self_shared_ptr_); - } + int GetChannel() { return get_gpc()->GetChannel(m_ptr_); } - int GetDefaultChannel() { - return get_global_module_context()->GetDefaultChannel(self_shared_ptr_); - } + int GetDefaultChannel() { return get_gpc()->GetDefaultChannel(m_ptr_); } std::optional<TaskRunnerPtr> GetTaskRunner() { - return get_global_module_context()->GetTaskRunner(self_shared_ptr_); + return get_gpc()->GetTaskRunner(m_ptr_); } bool ListenEvent(EventIdentifier event) { - return get_global_module_context()->ListenEvent(gpc_get_identifier(), - event); + return get_gpc()->ListenEvent(GetModuleIdentifier(), event); } ModuleIdentifier GetModuleIdentifier() const { return identifier_; } @@ -66,14 +65,12 @@ class Module::Impl { private: GlobalModuleContextPtr gpc_; - const std::shared_ptr<Module> self_shared_ptr_; + Module* m_ptr_; const ModuleIdentifier identifier_; const ModuleVersion version_; const ModuleMetaData meta_data_; - ModuleIdentifier gpc_get_identifier() { return identifier_; } - - const GlobalModuleContextPtr get_global_module_context() { + const GlobalModuleContextPtr get_gpc() { if (gpc_ == nullptr) { throw std::runtime_error("module is not registered by module manager"); } @@ -83,7 +80,7 @@ class Module::Impl { Module::Module(ModuleIdentifier id, ModuleVersion version, ModuleMetaData meta_data) - : p_(std::make_unique<Impl>(id, version, meta_data)) {} + : p_(std::make_unique<Impl>(this, id, version, meta_data)) {} Module::~Module() = default; diff --git a/src/core/module/ModuleManager.h b/src/core/module/ModuleManager.h index bb201ebe..c2889f87 100644 --- a/src/core/module/ModuleManager.h +++ b/src/core/module/ModuleManager.h @@ -32,8 +32,7 @@ #include <memory> #include <string> -#include "core/GpgFrontendCore.h" -#include "core/thread/Task.h" +#include "core/module/Event.h" namespace GpgFrontend::Thread { class TaskRunner; @@ -94,9 +93,9 @@ void RegisterAndActivateModule(Args&&... args) { } template <typename... Args> -void TriggerEvent(const std::string& eventIdentifier, Args&&... args) { - ModuleManager::GetInstance()->TriggerEvent(std::make_shared<Event>( - std::move(MakeEvent(eventIdentifier, std::forward<Args>(args)...)))); +void TriggerEvent(const EventIdentifier& event_id, Args&&... args) { + ModuleManager::GetInstance()->TriggerEvent( + std::move(MakeEvent(event_id, std::forward<Args>(args)...))); } } // namespace GpgFrontend::Module |