diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/core/module/Event.cpp (renamed from src/module/system/Event.cpp) | 0 | ||||
-rw-r--r-- | src/core/module/Event.h (renamed from src/module/system/Event.h) | 15 | ||||
-rw-r--r-- | src/core/module/GlobalModuleContext.cpp | 341 | ||||
-rw-r--r-- | src/core/module/GlobalModuleContext.h (renamed from src/module/system/GlobalModuleContext.h) | 5 | ||||
-rw-r--r-- | src/core/module/GpgFrontendModuleSystem.h (renamed from src/module/system/GpgFrontendModuleSystem.h) | 6 | ||||
-rw-r--r-- | src/core/module/Module.cpp (renamed from src/module/system/Module.cpp) | 2 | ||||
-rw-r--r-- | src/core/module/Module.h (renamed from src/module/system/Module.h) | 5 | ||||
-rw-r--r-- | src/core/module/ModuleManager.cpp (renamed from src/module/system/ModuleManager.cpp) | 18 | ||||
-rw-r--r-- | src/core/module/ModuleManager.h (renamed from src/module/system/ModuleManager.h) | 23 | ||||
-rw-r--r-- | src/module/CMakeLists.txt | 26 | ||||
-rw-r--r-- | src/module/GpgFrontendModuleInit.cpp | 11 | ||||
-rw-r--r-- | src/module/integrated/version_checking_module/VersionCheckingModule.cpp | 6 | ||||
-rw-r--r-- | src/module/sdk/GpgFrontendModuleSDK.h | 4 | ||||
-rw-r--r-- | src/module/system/GlobalModuleContext.cpp | 341 | ||||
-rw-r--r-- | src/module/system/GpgFrontendModuleSystemExport.h | 42 |
16 files changed, 404 insertions, 444 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 96dd7ffe..29b3e468 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -31,6 +31,7 @@ aux_source_directory(./function CORE_SOURCE) aux_source_directory(./thread CORE_SOURCE) aux_source_directory(./model CORE_SOURCE) aux_source_directory(./common CORE_SOURCE) +aux_source_directory(./module CORE_SOURCE) aux_source_directory(. CORE_SOURCE) # define libgpgfrontend_core @@ -77,7 +78,7 @@ if (MINGW) endif () # spdlog -target_link_libraries(gpgfrontend_core PRIVATE spdlog) +target_link_libraries(gpgfrontend_core PUBLIC spdlog) # link libarchive target_link_libraries(gpgfrontend_core PRIVATE archive) diff --git a/src/module/system/Event.cpp b/src/core/module/Event.cpp index 3e27e26d..3e27e26d 100644 --- a/src/module/system/Event.cpp +++ b/src/core/module/Event.cpp diff --git a/src/module/system/Event.h b/src/core/module/Event.h index 4ffe4f0f..8703b159 100644 --- a/src/module/system/Event.h +++ b/src/core/module/Event.h @@ -31,9 +31,7 @@ #include <memory> -#include "GpgFrontendModuleSystemExport.h" -#include "core/GpgFrontendCore.h" -#include "nlohmann/json_fwd.hpp" +#include "core/GpgFrontendCoreExport.h" namespace GpgFrontend::Module { @@ -43,9 +41,9 @@ using EventRefrernce = std::shared_ptr<Event>; using EventIdentifier = std::string; using Evnets = std::vector<Event>; -class Event { +class GPGFRONTEND_CORE_EXPORT Event { public: - using ParameterValue = std::variant<int, float, std::string, nlohmann::json>; + using ParameterValue = std::any; using EventIdentifier = std::string; struct ParameterInitializer { std::string key; @@ -78,6 +76,13 @@ class Event { std::unique_ptr<Impl> p_; }; +template <typename... Args> +Event MakeEvent(const std::string& eventIdentifier, Args&&... args) { + std::initializer_list<Event::ParameterInitializer> params = { + Event::ParameterInitializer{std::forward<Args>(args)}...}; + return Event(eventIdentifier, params); +} + } // namespace GpgFrontend::Module #endif // GPGFRONTEND_EVENT_H
\ No newline at end of file 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 <[email protected]> + * + * 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 <https://www.gnu.org/licenses/>. + * + * 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 <[email protected]> starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "GlobalModuleContext.h" + +#include <boost/random/mersenne_twister.hpp> +#include <boost/random/uniform_int_distribution.hpp> +#include <memory> +#include <optional> +#include <string> +#include <unordered_map> +#include <unordered_set> + +#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<TaskRunnerPtr> 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<TaskRunnerPtr> 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<TaskRunnerPtr> 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<Thread::TaskRunner>(); + + // Register the module with its identifier. + module_register_table_[module->GetModuleIdentifier()] = + std::make_shared<ModuleRegisterInfo>(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<ModuleIdentifier>(); + 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<ModuleRegisterInfo>; + + std::unordered_map<ModuleIdentifier, ModuleRegisterInfoPtr> + module_register_table_; + std::map<EventIdentifier, std::unordered_set<ModuleIdentifier>> + module_events_table_; + + std::set<int> 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<ModuleRegisterInfoPtr> 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<ModuleIdentifier>& search_module_events_table(ModuleIdentifier); +}; + +// Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument. +GlobalModuleContext::GlobalModuleContext(TaskRunnerPtr task_runner) + : p_(std::make_unique<Impl>(task_runner)) {} + +GlobalModuleContext::~GlobalModuleContext() = default; + +// Function to get the task runner associated with a module. +std::optional<TaskRunnerPtr> GlobalModuleContext::GetTaskRunner( + ModulePtr module) { + return p_->GetTaskRunner(module); +} + +// Function to get the task runner associated with a module. +std::optional<TaskRunnerPtr> GlobalModuleContext::GetTaskRunner( + ModuleIdentifier module_id) { + return p_->GetTaskRunner(module_id); +} + +// Function to get the global task runner. +std::optional<TaskRunnerPtr> 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 diff --git a/src/module/system/GlobalModuleContext.h b/src/core/module/GlobalModuleContext.h index a86a7c0f..e4dcac2f 100644 --- a/src/module/system/GlobalModuleContext.h +++ b/src/core/module/GlobalModuleContext.h @@ -29,10 +29,9 @@ #ifndef GPGFRONTEND_GLOBALMODULECONTEXT_H #define GPGFRONTEND_GLOBALMODULECONTEXT_H -#include "GpgFrontendModuleSystemExport.h" #include "core/GpgFrontendCore.h" +#include "core/module/Event.h" #include "core/thread/TaskRunner.h" -#include "module/system/Event.h" namespace GpgFrontend::Module { @@ -47,7 +46,7 @@ using GlobalModuleContextPtr = std::shared_ptr<GlobalModuleContext>; using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; -class GPGFRONTEND_MODULE_SYSTEM_EXPORT GlobalModuleContext : public QObject { +class GPGFRONTEND_CORE_EXPORT GlobalModuleContext : public QObject { Q_OBJECT public: GlobalModuleContext(TaskRunnerPtr); diff --git a/src/module/system/GpgFrontendModuleSystem.h b/src/core/module/GpgFrontendModuleSystem.h index 100c73e2..903aec69 100644 --- a/src/module/system/GpgFrontendModuleSystem.h +++ b/src/core/module/GpgFrontendModuleSystem.h @@ -29,6 +29,6 @@ #pragma once #include <core/GpgFrontendCore.h> -#include <module/system/Event.h> -#include <module/system/Module.h> -#include <module/system/ModuleManager.h>
\ No newline at end of file +#include <core/module/Event.h> +#include <core/module/Module.h> +#include <core/module/ModuleManager.h>
\ No newline at end of file diff --git a/src/module/system/Module.cpp b/src/core/module/Module.cpp index 6aafa681..d84b74af 100644 --- a/src/module/system/Module.cpp +++ b/src/core/module/Module.cpp @@ -30,7 +30,7 @@ #include <memory> -#include "module/system/GlobalModuleContext.h" +#include "core/module/GlobalModuleContext.h" namespace GpgFrontend::Module { diff --git a/src/module/system/Module.h b/src/core/module/Module.h index 9a2b7ade..bb7678ae 100644 --- a/src/module/system/Module.h +++ b/src/core/module/Module.h @@ -31,10 +31,9 @@ #include <memory> -#include "GpgFrontendModuleSystemExport.h" +#include "core/module/Event.h" #include "core/thread/Task.h" #include "core/thread/TaskRunner.h" -#include "module/system/Event.h" namespace GpgFrontend::Module { @@ -50,7 +49,7 @@ using GlobalModuleContextPtr = std::shared_ptr<GlobalModuleContext>; using TaskRunnerPtr = std::shared_ptr<Thread::TaskRunner>; -class GPGFRONTEND_MODULE_SYSTEM_EXPORT Module : public QObject { +class GPGFRONTEND_CORE_EXPORT Module : public QObject { Q_OBJECT public: Module(ModuleIdentifier, ModuleVersion, ModuleMetaData); diff --git a/src/module/system/ModuleManager.cpp b/src/core/module/ModuleManager.cpp index 13e2b445..dc31072b 100644 --- a/src/module/system/ModuleManager.cpp +++ b/src/core/module/ModuleManager.cpp @@ -28,9 +28,9 @@ #include "ModuleManager.h" +#include "core/module/GlobalModuleContext.h" +#include "core/module/Module.h" #include "core/thread/TaskRunner.h" -#include "module/system/GlobalModuleContext.h" -#include "module/system/Module.h" namespace GpgFrontend::Module { @@ -40,15 +40,15 @@ class ModuleManager::Impl { public: Impl() : task_runner_(std::make_shared<Thread::TaskRunner>()), - global_module_context_( - std::make_shared<GlobalModuleContext>(task_runner_)) { + gpc_(std::make_shared<GlobalModuleContext>(task_runner_)) { task_runner_->start(); } void RegisterModule(ModulePtr module) { task_runner_->PostTask(new Thread::Task( std::move([=](GpgFrontend::Thread::DataObjectPtr) -> int { - global_module_context_->RegisterModule(module); + module->SetGPC(gpc_); + gpc_->RegisterModule(module); return 0; }), __func__, nullptr, true)); @@ -57,7 +57,7 @@ class ModuleManager::Impl { void TriggerEvent(EventRefrernce event) { task_runner_->PostTask(new Thread::Task( std::move([=](GpgFrontend::Thread::DataObjectPtr) -> int { - global_module_context_->TriggerEvent(event); + gpc_->TriggerEvent(event); return 0; }), __func__, nullptr, true)); @@ -66,20 +66,20 @@ class ModuleManager::Impl { void ActiveModule(ModuleIdentifier identifier) { task_runner_->PostTask(new Thread::Task( std::move([=](GpgFrontend::Thread::DataObjectPtr) -> int { - global_module_context_->ActiveModule(identifier); + gpc_->ActiveModule(identifier); return 0; }), __func__, nullptr, true)); } std::optional<TaskRunnerPtr> GetTaskRunner(ModuleIdentifier module_id) { - return global_module_context_->GetTaskRunner(module_id); + return gpc_->GetTaskRunner(module_id); } private: static ModuleMangerPtr global_module_manager_; TaskRunnerPtr task_runner_; - GlobalModuleContextPtr global_module_context_; + GlobalModuleContextPtr gpc_; }; ModuleManager::ModuleManager() : p_(std::make_unique<Impl>()) {} diff --git a/src/module/system/ModuleManager.h b/src/core/module/ModuleManager.h index fc046d1a..bb201ebe 100644 --- a/src/module/system/ModuleManager.h +++ b/src/core/module/ModuleManager.h @@ -32,7 +32,6 @@ #include <memory> #include <string> -#include "GpgFrontendModuleSystemExport.h" #include "core/GpgFrontendCore.h" #include "core/thread/Task.h" @@ -55,7 +54,7 @@ using ModulePtr = std::shared_ptr<Module>; using ModuleMangerPtr = std::shared_ptr<ModuleManager>; using GlobalModuleContextPtr = std::shared_ptr<GlobalModuleContext>; -class GPGFRONTEND_MODULE_SYSTEM_EXPORT ModuleManager : public QObject { +class GPGFRONTEND_CORE_EXPORT ModuleManager : public QObject { Q_OBJECT public: ~ModuleManager(); @@ -80,6 +79,26 @@ class GPGFRONTEND_MODULE_SYSTEM_EXPORT ModuleManager : public QObject { ModuleManager(); }; +template <typename T, typename... Args> +void RegisterModule(Args&&... args) { + ModuleManager::GetInstance()->RegisterModule( + std::make_shared<T>(std::forward<Args>(args)...)); +} + +template <typename T, typename... Args> +void RegisterAndActivateModule(Args&&... args) { + auto manager = ModuleManager::GetInstance(); + auto module = std::make_shared<T>(std::forward<Args>(args)...); + manager->RegisterModule(module); + manager->ActiveModule(module->GetModuleIdentifier()); +} + +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)...)))); +} + } // namespace GpgFrontend::Module #endif // GPGFRONTEND_MODULEMANAGER_H
\ No newline at end of file diff --git a/src/module/CMakeLists.txt b/src/module/CMakeLists.txt index 1a618fc3..683386b2 100644 --- a/src/module/CMakeLists.txt +++ b/src/module/CMakeLists.txt @@ -26,30 +26,6 @@ set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_VISIBILITY_INLINES_HIDDEN 1) -# define libgpgfrontend_module_system -aux_source_directory(system MODULE_SYSTEM_SOURCE) -add_library(gpgfrontend_module_system SHARED ${MODULE_SYSTEM_SOURCE}) -set(_export_file_system "${CMAKE_CURRENT_SOURCE_DIR}/system/GpgFrontendModuleSystemExport.h") -generate_export_header(gpgfrontend_module_system EXPORT_FILE_NAME "${_export_file_system}") - -# link json -target_link_libraries(gpgfrontend_module_system - PUBLIC nlohmann_json::nlohmann_json) - -# spdlog -target_link_libraries(gpgfrontend_module_system PUBLIC spdlog) - -# link Qt core -if(Qt6_DIR) - target_link_libraries(gpgfrontend_module_system PUBLIC Qt6::Core) -else() - target_link_libraries(gpgfrontend_module_system PUBLIC Qt5::Core) -endif() - -# link core and ui module -target_link_libraries(gpgfrontend_module_system PUBLIC - gpgfrontend_core gpgfrontend_ui) - # define libgpgfrontend_module_sdk aux_source_directory(sdk MODULE_SDK_SOURCE) @@ -60,7 +36,7 @@ target_include_directories(gpgfrontend_module_sdk PUBLIC sdk) # link module system target_link_libraries(gpgfrontend_module_sdk - PUBLIC gpgfrontend_module_system) + PUBLIC gpgfrontend_core) # tracking integrated modules set(all_integrated_module_libraries "") diff --git a/src/module/GpgFrontendModuleInit.cpp b/src/module/GpgFrontendModuleInit.cpp index 234d0c18..7188bdd3 100644 --- a/src/module/GpgFrontendModuleInit.cpp +++ b/src/module/GpgFrontendModuleInit.cpp @@ -36,7 +36,7 @@ #include <memory> #include "core/function/GlobalSettingStation.h" -#include "module/system/ModuleManager.h" +#include "core/module/ModuleManager.h" // integrated modules #include "integrated/version_checking_module/VersionCheckingModule.h" @@ -45,9 +45,12 @@ namespace GpgFrontend::Module { void LoadGpgFrontendIntegratedModules() { SPDLOG_INFO("loading integrated module..."); - ModuleManager::GetInstance()->RegisterModule( - std::make_shared< - Integrated::VersionCheckingModule::VersionCheckingModule>()); + + // VersionCheckingModule + RegisterAndActivateModule< + Integrated::VersionCheckingModule::VersionCheckingModule>(); + + SPDLOG_INFO("load integrated module done."); } void InitModuleLoggingSystem() { diff --git a/src/module/integrated/version_checking_module/VersionCheckingModule.cpp b/src/module/integrated/version_checking_module/VersionCheckingModule.cpp index 1524d876..64fee5dc 100644 --- a/src/module/integrated/version_checking_module/VersionCheckingModule.cpp +++ b/src/module/integrated/version_checking_module/VersionCheckingModule.cpp @@ -39,18 +39,18 @@ VersionCheckingModule::VersionCheckingModule() bool VersionCheckingModule::Register() { SPDLOG_INFO("version checking module registering"); + listenEvent("APPLICATION_STARTED"); return true; } bool VersionCheckingModule::Active() { SPDLOG_INFO("version checking module activating"); - - listenEvent("APPLICATION_STARTED"); return true; } int VersionCheckingModule::Exec(EventRefrernce event) { - SPDLOG_INFO("version checking module ececuting"); + SPDLOG_INFO("version checking module executing, event id: {}", + event->GetIdentifier()); getTaskRunner()->PostTask(new VersionCheckTask()); return 0; diff --git a/src/module/sdk/GpgFrontendModuleSDK.h b/src/module/sdk/GpgFrontendModuleSDK.h index 47b017bf..bb38d7bb 100644 --- a/src/module/sdk/GpgFrontendModuleSDK.h +++ b/src/module/sdk/GpgFrontendModuleSDK.h @@ -28,5 +28,5 @@ #pragma once -#include <module/sdk/GpgFrontendModuleSDKExport.h> -#include <module/system/GpgFrontendModuleSystem.h>
\ No newline at end of file +#include <core/module/GpgFrontendModuleSystem.h> +#include <module/sdk/GpgFrontendModuleSDKExport.h>
\ No newline at end of file diff --git a/src/module/system/GlobalModuleContext.cpp b/src/module/system/GlobalModuleContext.cpp index c1312af3..e69de29b 100644 --- a/src/module/system/GlobalModuleContext.cpp +++ b/src/module/system/GlobalModuleContext.cpp @@ -1,341 +0,0 @@ -/** - * Copyright (C) 2021 Saturneric <[email protected]> - * - * 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 <https://www.gnu.org/licenses/>. - * - * 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 <[email protected]> starting on May 12, 2021. - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#include "GlobalModuleContext.h" - -#include <boost/random/mersenne_twister.hpp> -#include <boost/random/uniform_int_distribution.hpp> -#include <memory> -#include <optional> -#include <string> -#include <unordered_map> -#include <unordered_set> - -#include "core/thread/Task.h" -#include "module/system/Event.h" -#include "module/system/Module.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<TaskRunnerPtr> 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<TaskRunnerPtr> 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<TaskRunnerPtr> 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<Thread::TaskRunner>(); - - // Register the module with its identifier. - module_register_table_[module->GetModuleIdentifier()] = - std::make_shared<ModuleRegisterInfo>(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<ModuleIdentifier>(); - 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<ModuleRegisterInfo>; - - std::unordered_map<ModuleIdentifier, ModuleRegisterInfoPtr> - module_register_table_; - std::map<EventIdentifier, std::unordered_set<ModuleIdentifier>> - module_events_table_; - - std::set<int> 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<ModuleRegisterInfoPtr> 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<ModuleIdentifier>& search_module_events_table(ModuleIdentifier); -}; - -// Constructor for GlobalModuleContext, takes a TaskRunnerPtr as an argument. -GlobalModuleContext::GlobalModuleContext(TaskRunnerPtr task_runner) - : p_(std::make_unique<Impl>(task_runner)) {} - -GlobalModuleContext::~GlobalModuleContext() = default; - -// Function to get the task runner associated with a module. -std::optional<TaskRunnerPtr> GlobalModuleContext::GetTaskRunner( - ModulePtr module) { - return p_->GetTaskRunner(module); -} - -// Function to get the task runner associated with a module. -std::optional<TaskRunnerPtr> GlobalModuleContext::GetTaskRunner( - ModuleIdentifier module_id) { - return p_->GetTaskRunner(module_id); -} - -// Function to get the global task runner. -std::optional<TaskRunnerPtr> 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 diff --git a/src/module/system/GpgFrontendModuleSystemExport.h b/src/module/system/GpgFrontendModuleSystemExport.h deleted file mode 100644 index dc8d6c3a..00000000 --- a/src/module/system/GpgFrontendModuleSystemExport.h +++ /dev/null @@ -1,42 +0,0 @@ - -#ifndef GPGFRONTEND_MODULE_SYSTEM_EXPORT_H -#define GPGFRONTEND_MODULE_SYSTEM_EXPORT_H - -#ifdef GPGFRONTEND_MODULE_SYSTEM_STATIC_DEFINE -# define GPGFRONTEND_MODULE_SYSTEM_EXPORT -# define GPGFRONTEND_MODULE_SYSTEM_NO_EXPORT -#else -# ifndef GPGFRONTEND_MODULE_SYSTEM_EXPORT -# ifdef gpgfrontend_module_system_EXPORTS - /* We are building this library */ -# define GPGFRONTEND_MODULE_SYSTEM_EXPORT __attribute__((visibility("default"))) -# else - /* We are using this library */ -# define GPGFRONTEND_MODULE_SYSTEM_EXPORT __attribute__((visibility("default"))) -# endif -# endif - -# ifndef GPGFRONTEND_MODULE_SYSTEM_NO_EXPORT -# define GPGFRONTEND_MODULE_SYSTEM_NO_EXPORT __attribute__((visibility("hidden"))) -# endif -#endif - -#ifndef GPGFRONTEND_MODULE_SYSTEM_DEPRECATED -# define GPGFRONTEND_MODULE_SYSTEM_DEPRECATED __attribute__ ((__deprecated__)) -#endif - -#ifndef GPGFRONTEND_MODULE_SYSTEM_DEPRECATED_EXPORT -# define GPGFRONTEND_MODULE_SYSTEM_DEPRECATED_EXPORT GPGFRONTEND_MODULE_SYSTEM_EXPORT GPGFRONTEND_MODULE_SYSTEM_DEPRECATED -#endif - -#ifndef GPGFRONTEND_MODULE_SYSTEM_DEPRECATED_NO_EXPORT -# define GPGFRONTEND_MODULE_SYSTEM_DEPRECATED_NO_EXPORT GPGFRONTEND_MODULE_SYSTEM_NO_EXPORT GPGFRONTEND_MODULE_SYSTEM_DEPRECATED -#endif - -#if 0 /* DEFINE_NO_DEPRECATED */ -# ifndef GPGFRONTEND_MODULE_SYSTEM_NO_DEPRECATED -# define GPGFRONTEND_MODULE_SYSTEM_NO_DEPRECATED -# endif -#endif - -#endif /* GPGFRONTEND_MODULE_SYSTEM_EXPORT_H */ |