diff --git a/server/modules/filter/dbfwfilter/test/maxscale/module.hh b/server/modules/filter/dbfwfilter/test/maxscale/module.hh new file mode 100644 index 000000000..2b8a3e079 --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/maxscale/module.hh @@ -0,0 +1,103 @@ +#pragma once +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include +#include + +namespace maxscale +{ + +/** + * The template Module is an abstraction for a MaxScale module, to + * be used as the base class of a concrete module. + */ +template +class Module; + +/** + * The template specialization Module provides functionality common + * to all modules. + */ +template <> +class Module +{ +public: + /** + * Load a module with a specific name, assumed to be of a specific type. + * + * @param zFile_name The name of the module. + * @param zType_name The expected type of the module. + * + * @return The module object, if the module could be loaded, otherwise NULL. + */ + static void* load(const char *zFile_name, const char *zType_name); + + /** + * Perform process initialization of all modules. Should be called only + * when all modules intended to be loaded have been loaded. + * + * @return True, if the process initialization succeeded. + */ + static bool process_init(); + + /** + * Perform process finalization of all modules. + */ + static void process_finish(); + + /** + * Perform thread initialization of all modules. Should be called only + * when all modules intended to be loaded have been loaded. + * + * @return True, if the thread initialization could be performed. + */ + static bool thread_init(); + + /** + * Perform thread finalization of all modules. + */ + static void thread_finish(); +}; + +/** + * The template Module is intended to be derived from using the derived + * class as template argument. + * + * class XyzModule : public Module { ... } + * + * @param zFile_name The name of the module. + * + * @return A module instance if the module could be loaded and it was of + * the expected type. + */ +template +class Module +{ +public: + static std::auto_ptr load(const char* zFile_name) + { + std::auto_ptr sT; + + void* pApi = Module::load(zFile_name, T::zName); + + if (pApi) + { + sT.reset(new T(static_cast(pApi))); + } + + return sT; + } +}; + +} diff --git a/server/modules/filter/dbfwfilter/test/module.cc b/server/modules/filter/dbfwfilter/test/module.cc new file mode 100644 index 000000000..587748670 --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/module.cc @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include "maxscale/module.hh" +#include "../../../../core/maxscale/modules.h" + +namespace maxscale +{ + +//static +void* Module::load(const char* zName, const char* zType) +{ + return load_module(zName, zType); +} + +//static +bool Module::process_init() +{ + bool initialized = false; + + MXS_MODULE_ITERATOR i = mxs_module_iterator_get(NULL); + MXS_MODULE* module = NULL; + + while ((module = mxs_module_iterator_get_next(&i)) != NULL) + { + if (module->process_init) + { + int rc = (module->process_init)(); + + if (rc != 0) + { + break; + } + } + } + + if (module) + { + // If module is non-NULL it means that the initialization failed for + // that module. We now need to call finish on all modules that were + // successfully initialized. + MXS_MODULE* failed_module = module; + i = mxs_module_iterator_get(NULL); + + while ((module = mxs_module_iterator_get_next(&i)) != failed_module) + { + if (module->process_finish) + { + (module->process_finish)(); + } + } + } + else + { + initialized = true; + } + + return initialized; +} + +//static +void Module::process_finish() +{ + MXS_MODULE_ITERATOR i = mxs_module_iterator_get(NULL); + MXS_MODULE* module = NULL; + + while ((module = mxs_module_iterator_get_next(&i)) != NULL) + { + if (module->process_finish) + { + (module->process_finish)(); + } + } +} + +//static +bool Module::thread_init() +{ + bool initialized = false; + + MXS_MODULE_ITERATOR i = mxs_module_iterator_get(NULL); + MXS_MODULE* module = NULL; + + while ((module = mxs_module_iterator_get_next(&i)) != NULL) + { + if (module->thread_init) + { + int rc = (module->thread_init)(); + + if (rc != 0) + { + break; + } + } + } + + if (module) + { + // If module is non-NULL it means that the initialization failed for + // that module. We now need to call finish on all modules that were + // successfully initialized. + MXS_MODULE* failed_module = module; + i = mxs_module_iterator_get(NULL); + + while ((module = mxs_module_iterator_get_next(&i)) != failed_module) + { + if (module->thread_finish) + { + (module->thread_finish)(); + } + } + } + else + { + initialized = true; + } + + return initialized; +} + +//static +void Module::thread_finish() +{ + MXS_MODULE_ITERATOR i = mxs_module_iterator_get(NULL); + MXS_MODULE* module = NULL; + + while ((module = mxs_module_iterator_get_next(&i)) != NULL) + { + if (module->thread_finish) + { + (module->thread_finish)(); + } + } +} + +}