From 12d70e1792a5b1ff08d4b58fb49fb9e58d6551a8 Mon Sep 17 00:00:00 2001 From: saturneric Date: Thu, 29 Feb 2024 00:32:43 +0800 Subject: feat: upgrade module system 1. load module and resolve symbols at runtime 2. restrict sdk functions and structures to c style 3. add some core api to support it --- src/core/module/GlobalModuleContext.cpp | 42 +++++++++++++++++++++++++++++++-- 1 file changed, 40 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 9bc4f06b..cf3e134b 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include "core/module/Event.h" #include "core/module/Module.h" @@ -49,6 +50,18 @@ class GlobalModuleContext::Impl { acquired_channel_.insert(kGpgFrontendNonAsciiChannel); } + auto SearchModule(ModuleIdentifier module_id) -> ModulePtr { + // Search for the module in the register table. + auto module_info_opt = search_module_register_table(module_id); + if (!module_info_opt.has_value()) { + GF_CORE_LOG_ERROR("cannot find module id {} at register table", + module_id); + return nullptr; + } + + return module_info_opt.value()->module; + } + auto GetChannel(ModuleRawPtr module) -> int { // Search for the module in the register table. auto module_info_opt = @@ -97,7 +110,7 @@ class GlobalModuleContext::Impl { return false; } - if (!module->Register()) { + if (module->Register() != 0) { GF_CORE_LOG_ERROR("register module {} failed", module->GetModuleIdentifier()); return false; @@ -222,6 +235,9 @@ class GlobalModuleContext::Impl { GF_CORE_LOG_DEBUG("event {}'s current listeners size: {}", event->GetIdentifier(), listeners_set.size()); + // register trigger id index table + module_on_triggering_events_table_[event->GetTriggerIdentifier()] = event; + // Iterate through each listener and execute the corresponding module for (const auto& listener_module_id : listeners_set) { // Search for the module's information in the registration table @@ -272,7 +288,17 @@ class GlobalModuleContext::Impl { return true; } - auto IsModuleActivated(const ModuleIdentifier& m_id) const -> bool { + auto SearchEvent(const EventTriggerIdentifier& trigger_id) + -> std::optional { + if (module_on_triggering_events_table_.find(trigger_id) != + module_on_triggering_events_table_.end()) { + return module_on_triggering_events_table_[trigger_id]; + } + return {}; + } + + [[nodiscard]] auto IsModuleActivated(const ModuleIdentifier& m_id) const + -> bool { auto m = search_module_register_table(m_id); return m.has_value() && m->get()->activate; } @@ -290,6 +316,8 @@ class GlobalModuleContext::Impl { module_register_table_; std::map> module_events_table_; + std::map + module_on_triggering_events_table_; std::set acquired_channel_; TaskRunnerPtr default_task_runner_; @@ -323,6 +351,11 @@ GlobalModuleContext::GlobalModuleContext() GlobalModuleContext::~GlobalModuleContext() = default; +auto GlobalModuleContext::SearchModule(ModuleIdentifier module_id) + -> ModulePtr { + return p_->SearchModule(std::move(module_id)); +} + // Function to get the task runner associated with a module. auto GlobalModuleContext::GetTaskRunner(ModuleRawPtr module) -> std::optional { @@ -362,6 +395,11 @@ auto GlobalModuleContext::TriggerEvent(EventRefrernce event) -> bool { return p_->TriggerEvent(std::move(event)); } +auto GlobalModuleContext::SearchEvent(EventTriggerIdentifier trigger_id) + -> std::optional { + return p_->SearchEvent(trigger_id); +} + auto GlobalModuleContext::GetChannel(ModuleRawPtr module) -> int { return p_->GetChannel(module); } -- cgit v1.2.3 From c1f5b3336836e15d193582e9b8f3e044f7d8bc1b Mon Sep 17 00:00:00 2001 From: saturneric Date: Thu, 29 Feb 2024 18:15:57 +0800 Subject: feat: add module controller and continue to work on module system 1. speed up building by reducing build info sheader including 2. add module controller 3. continue to work on module system --- src/core/module/GlobalModuleContext.cpp | 45 +++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index cf3e134b..cd970df4 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -179,6 +179,15 @@ class GlobalModuleContext::Impl { GF_CORE_LOG_DEBUG("new event {} of module system created", event); } + // module -> event + auto module_info_opt = search_module_register_table(module_id); + if (!module_info_opt.has_value()) { + GF_CORE_LOG_ERROR("cannot find module id {} at register table", + module_id); + return false; + } + module_info_opt.value()->listening_event_ids.push_back(event); + auto& listeners_set = met_it->second; // Add the listener (module) to the event. auto listener_it = listeners_set.find(module_id); @@ -199,7 +208,7 @@ class GlobalModuleContext::Impl { auto module_info = module_info_opt.value(); // Activate the module if it is not already deactive. - if (!module_info->activate && module_info->module->Deactive()) { + if (!module_info->activate && (module_info->module->Deactive() == 0)) { module_info->activate = false; } @@ -303,11 +312,27 @@ class GlobalModuleContext::Impl { return m.has_value() && m->get()->activate; } + auto ListAllRegisteredModuleID() -> QList { + QList module_ids; + for (const auto& module : module_register_table_) { + module_ids.append(module.first); + } + return module_ids; + } + + auto GetModuleListening(const ModuleIdentifier& module_id) + -> QList { + auto module_info = search_module_register_table(module_id); + if (!module_info.has_value()) return {}; + return module_info->get()->listening_event_ids; + } + private: struct ModuleRegisterInfo { int channel; ModulePtr module; bool activate; + QList listening_event_ids; }; using ModuleRegisterInfoPtr = std::shared_ptr; @@ -335,7 +360,8 @@ class GlobalModuleContext::Impl { } // Function to search for a module in the register table. - auto search_module_register_table(const ModuleIdentifier& identifier) const + [[nodiscard]] 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()) { @@ -375,7 +401,7 @@ auto GlobalModuleContext::GetGlobalTaskRunner() } auto GlobalModuleContext::RegisterModule(ModulePtr module) -> bool { - return p_->RegisterModule(std::move(module)); + return p_->RegisterModule(module); } auto GlobalModuleContext::ActiveModule(ModuleIdentifier module_id) -> bool { @@ -392,7 +418,7 @@ auto GlobalModuleContext::DeactivateModule(ModuleIdentifier module_id) -> bool { } auto GlobalModuleContext::TriggerEvent(EventRefrernce event) -> bool { - return p_->TriggerEvent(std::move(event)); + return p_->TriggerEvent(event); } auto GlobalModuleContext::SearchEvent(EventTriggerIdentifier trigger_id) @@ -409,7 +435,16 @@ auto GlobalModuleContext::GetDefaultChannel(ModuleRawPtr channel) -> int { } auto GlobalModuleContext::IsModuleActivated(ModuleIdentifier m_id) -> bool { - return p_->IsModuleActivated(std::move(m_id)); + return p_->IsModuleActivated(m_id); } +auto GlobalModuleContext::ListAllRegisteredModuleID() + -> QList { + return p_->ListAllRegisteredModuleID(); +} + +auto GlobalModuleContext::GetModuleListening(ModuleIdentifier module_id) + -> QList { + return p_->GetModuleListening(module_id); +} } // namespace GpgFrontend::Module -- cgit v1.2.3 From 84b04d88723ef05f7873082c235ecfd56facf934 Mon Sep 17 00:00:00 2001 From: saturneric Date: Thu, 29 Feb 2024 22:36:25 +0800 Subject: feat: add prefix GF to all sdk and module symbols --- src/core/module/GlobalModuleContext.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 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 cd970df4..6032e090 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -171,13 +171,6 @@ class GlobalModuleContext::Impl { auto ListenEvent(ModuleIdentifier module_id, EventIdentifier event) -> bool { 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); - GF_CORE_LOG_DEBUG("new event {} of module system created", event); - } // module -> event auto module_info_opt = search_module_register_table(module_id); @@ -186,6 +179,15 @@ class GlobalModuleContext::Impl { module_id); return false; } + + // 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); + GF_CORE_LOG_DEBUG("new event {} of module system created", event); + } + module_info_opt.value()->listening_event_ids.push_back(event); auto& listeners_set = met_it->second; @@ -198,7 +200,7 @@ class GlobalModuleContext::Impl { } auto DeactivateModule(ModuleIdentifier module_id) -> bool { - // Search for the module in the register table. + // search for the module in the register table. auto module_info_opt = search_module_register_table(module_id); if (!module_info_opt.has_value()) { GF_CORE_LOG_ERROR("cannot find module id {} at register table", @@ -207,8 +209,16 @@ class GlobalModuleContext::Impl { } auto module_info = module_info_opt.value(); - // Activate the module if it is not already deactive. - if (!module_info->activate && (module_info->module->Deactive() == 0)) { + // activate the module if it is not already deactive. + if (module_info->activate && (module_info->module->Deactive() == 0)) { + for (const auto& event_ids : module_info->listening_event_ids) { + auto& modules = module_events_table_[event_ids]; + if (auto it = modules.find(module_id); it != modules.end()) { + modules.erase(it); + } + } + + module_info->listening_event_ids.clear(); module_info->activate = false; } -- cgit v1.2.3 From 26e24222e434036e5ec13f8ec99b958faf325154 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 12 Apr 2024 11:14:37 +0200 Subject: feat: add auto activate function and optimums for some structures --- 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 6032e090..1744ba72 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -327,6 +327,7 @@ class GlobalModuleContext::Impl { for (const auto& module : module_register_table_) { module_ids.append(module.first); } + module_ids.sort(); return module_ids; } -- cgit v1.2.3 From 3d2ab7c349b02740511f0f2113fd80f7f44c8333 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 30 Apr 2024 16:07:33 +0200 Subject: feat: improve functions and ui on module --- src/core/module/GlobalModuleContext.cpp | 18 +++++++++++++++--- 1 file changed, 15 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 1744ba72..b5a04d64 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -98,7 +98,7 @@ class GlobalModuleContext::Impl { return default_task_runner_; } - auto RegisterModule(const ModulePtr& module) -> bool { + auto RegisterModule(const ModulePtr& module, bool integrated_module) -> bool { GF_CORE_LOG_DEBUG("attempting to register module: {}", module->GetModuleIdentifier()); // Check if the module is null or already registered. @@ -120,6 +120,7 @@ class GlobalModuleContext::Impl { GpgFrontend::SecureCreateSharedObject(); register_info->module = module; register_info->channel = acquire_new_unique_channel(); + register_info->integrated = integrated_module; // move module to its task runner' thread register_info->module->setParent(nullptr); @@ -322,6 +323,11 @@ class GlobalModuleContext::Impl { return m.has_value() && m->get()->activate; } + auto IsIntegratedModule(ModuleIdentifier m_id) -> bool { + auto m = search_module_register_table(m_id); + return m.has_value() && m->get()->integrated; + } + auto ListAllRegisteredModuleID() -> QList { QList module_ids; for (const auto& module : module_register_table_) { @@ -343,6 +349,7 @@ class GlobalModuleContext::Impl { int channel; ModulePtr module; bool activate; + bool integrated; QList listening_event_ids; }; @@ -411,8 +418,9 @@ auto GlobalModuleContext::GetGlobalTaskRunner() return p_->GetGlobalTaskRunner(); } -auto GlobalModuleContext::RegisterModule(ModulePtr module) -> bool { - return p_->RegisterModule(module); +auto GlobalModuleContext::RegisterModule(ModulePtr module, + bool integrated_module) -> bool { + return p_->RegisterModule(module, integrated_module); } auto GlobalModuleContext::ActiveModule(ModuleIdentifier module_id) -> bool { @@ -449,6 +457,10 @@ auto GlobalModuleContext::IsModuleActivated(ModuleIdentifier m_id) -> bool { return p_->IsModuleActivated(m_id); } +auto GlobalModuleContext::IsIntegratedModule(ModuleIdentifier m_id) -> bool { + return p_->IsIntegratedModule(m_id); +} + auto GlobalModuleContext::ListAllRegisteredModuleID() -> QList { return p_->ListAllRegisteredModuleID(); -- cgit v1.2.3 From 46870f10fdbc18d695242c945c0c39e4d832ec39 Mon Sep 17 00:00:00 2001 From: saturneric Date: Tue, 30 Apr 2024 19:58:36 +0200 Subject: fix: reset module settings object in a proper way --- src/core/module/GlobalModuleContext.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/core/module/GlobalModuleContext.cpp') diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index b5a04d64..e67e48f9 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -226,7 +226,7 @@ class GlobalModuleContext::Impl { return !module_info->activate; } - auto TriggerEvent(const EventRefrernce& event) -> bool { + auto TriggerEvent(const EventReference& event) -> bool { auto event_id = event->GetIdentifier(); GF_CORE_LOG_DEBUG("attempting to trigger event: {}", event_id); @@ -309,7 +309,7 @@ class GlobalModuleContext::Impl { } auto SearchEvent(const EventTriggerIdentifier& trigger_id) - -> std::optional { + -> std::optional { if (module_on_triggering_events_table_.find(trigger_id) != module_on_triggering_events_table_.end()) { return module_on_triggering_events_table_[trigger_id]; @@ -359,7 +359,7 @@ class GlobalModuleContext::Impl { module_register_table_; std::map> module_events_table_; - std::map + std::map module_on_triggering_events_table_; std::set acquired_channel_; @@ -436,12 +436,12 @@ auto GlobalModuleContext::DeactivateModule(ModuleIdentifier module_id) -> bool { return p_->DeactivateModule(std::move(module_id)); } -auto GlobalModuleContext::TriggerEvent(EventRefrernce event) -> bool { +auto GlobalModuleContext::TriggerEvent(EventReference event) -> bool { return p_->TriggerEvent(event); } auto GlobalModuleContext::SearchEvent(EventTriggerIdentifier trigger_id) - -> std::optional { + -> std::optional { return p_->SearchEvent(trigger_id); } -- cgit v1.2.3