diff options
| author | yangwei <[email protected]> | 2024-09-27 19:36:14 +0800 |
|---|---|---|
| committer | yangwei <[email protected]> | 2024-09-27 19:36:14 +0800 |
| commit | dc4805fbb880714331876b6c3795037ddd95a99e (patch) | |
| tree | 03fcd806de6b16ac4f7b44ea79499ae9df460279 /infra | |
| parent | 849df6b9cc632cf051d69fd8b6f30c9dacec3714 (diff) | |
🦄 refactor(module manager): remove internal api load_spec
Diffstat (limited to 'infra')
| -rw-r--r-- | infra/module_manager/module_manager.c | 85 | ||||
| -rw-r--r-- | infra/module_manager/module_manager_interna.h | 3 | ||||
| -rw-r--r-- | infra/module_manager/test/gtest_module_manager_main.cpp | 21 |
3 files changed, 41 insertions, 68 deletions
diff --git a/infra/module_manager/module_manager.c b/infra/module_manager/module_manager.c index dab6218..ed0d2fe 100644 --- a/infra/module_manager/module_manager.c +++ b/infra/module_manager/module_manager.c @@ -13,29 +13,51 @@ *******************************************/ #include "toml/toml.h" +#include <fcntl.h> +#include <unistd.h> -int module_specs_load(FILE *fp, struct module_spec_load **mod_spec) +static int stellar_module_manager_get_filepath_from_fp(FILE *fp, char *path, size_t n) +{ + if(fp==NULL)return -1; + int fd = fileno(fp); + char buf[256]; + snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd); + return readlink(buf, path, n - 1); +} + + +struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, int max_thread_num, struct mq_schema *mq_schema) { - if(fp==NULL|| mod_spec==NULL) return 0; + + struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1); + mod_mgr->schema.max_thread_num=max_thread_num; + mod_mgr->schema.mq_schema=mq_schema; + if(fp==NULL)return mod_mgr; + + char pathname[FILENAME_MAX] = {}; + if(stellar_module_manager_get_filepath_from_fp(fp, pathname, sizeof(pathname)) > 0) + { + mod_mgr->module_spec_toml_path = strdup(pathname); + } int mod_num = 0; toml_table_t *conf = toml_parse_file(fp, NULL, 0); toml_array_t* mod_array = toml_array_in(conf, "module"); + char *path = NULL; + char *init_func_name = NULL; + char *exit_func_name = NULL; + if(mod_array==NULL)goto MODULE_SPEC_LOAD_ERROR; + mod_num = toml_array_nelem(mod_array); - *mod_spec = CALLOC(struct module_spec_load, mod_num); + mod_mgr->module_specs = CALLOC(struct module_spec_load, mod_num); - struct stellar_module_manager *mod_mgr = (struct stellar_module_manager *)container_of(mod_spec, struct stellar_module_manager, module_specs); - - // TODO: store module specific in hash + // TODO: store module specific in hash for (int i = 0; i < mod_num; i++) { toml_table_t* toml_mod = toml_table_at(mod_array, i); const char *path_raw = toml_raw_in(toml_mod, "path"); const char *init_func_name_raw = toml_raw_in(toml_mod, "init"); const char *exit_func_name_raw = toml_raw_in(toml_mod, "exit"); - char *path = NULL; - char *init_func_name = NULL; - char *exit_func_name = NULL; if (toml_rtos(path_raw, &path) || toml_rtos(init_func_name_raw, &init_func_name) || toml_rtos(exit_func_name_raw, &exit_func_name)) { @@ -47,8 +69,8 @@ int module_specs_load(FILE *fp, struct module_spec_load **mod_spec) goto MODULE_SPEC_LOAD_ERROR; } - mod_spec[i]->on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name); - if (mod_spec[i]->on_init_cb) + mod_mgr->module_specs[i].on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name); + if (mod_mgr->module_specs[i].on_init_cb) { mod_mgr->module_specs[i].mod = mod_mgr->module_specs[i].on_init_cb(mod_mgr); if (stellar_module_manager_get_module(mod_mgr, mod_mgr->module_specs[i].mod->name) == NULL) @@ -68,46 +90,19 @@ int module_specs_load(FILE *fp, struct module_spec_load **mod_spec) fprintf(stderr, "Could not load init function %s: %s\n", init_func_name, dlerror()); } - mod_spec[i]->on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name); - if (!mod_spec[i]->on_exit_cb) { + mod_mgr->module_specs[i].on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name); + if (!mod_mgr->module_specs[i].on_exit_cb) { fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror()); } - mod_spec[i]->path=path; - mod_spec[i]->init_cb_name=init_func_name; - mod_spec[i]->exit_cb_name=exit_func_name; + mod_mgr->module_specs[i].path=path; + mod_mgr->module_specs[i].init_cb_name=init_func_name; + mod_mgr->module_specs[i].exit_cb_name=exit_func_name; } - if(conf )toml_free(conf); - return mod_num; + mod_mgr->load_module_num=mod_num; + MODULE_SPEC_LOAD_ERROR: if(conf )toml_free(conf); - if(*mod_spec)FREE(*mod_spec); - return 0; -} - -#include <fcntl.h> -#include <unistd.h> -static int stellar_module_manager_get_filepath_from_fp(FILE *fp, char *path, size_t n) -{ - if(fp==NULL)return -1; - int fd = fileno(fp); - char buf[256]; - snprintf(buf, sizeof(buf), "/proc/self/fd/%d", fd); - return readlink(buf, path, n - 1); -} - -struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, int max_thread_num, struct mq_schema *mq_schema) -{ - - struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1); - mod_mgr->schema.max_thread_num=max_thread_num; - mod_mgr->schema.mq_schema=mq_schema; - mod_mgr->load_module_num = module_specs_load(fp, &mod_mgr->module_specs); - char pathname[FILENAME_MAX] = {}; - if(stellar_module_manager_get_filepath_from_fp(fp, pathname, sizeof(pathname)) > 0) - { - mod_mgr->module_spec_toml_path = strdup(pathname); - } return mod_mgr; } diff --git a/infra/module_manager/module_manager_interna.h b/infra/module_manager/module_manager_interna.h index 491b78c..7b07552 100644 --- a/infra/module_manager/module_manager_interna.h +++ b/infra/module_manager/module_manager_interna.h @@ -46,9 +46,8 @@ struct stellar_module_manager }__attribute__((aligned(sizeof(void*)))); - -int module_specs_load(FILE *fp, struct module_spec_load **load_spec) __attribute__((visibility("hidden"))); struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, int max_thread_num, struct mq_schema *mq_schema) __attribute__((visibility("hidden"))); + #ifdef __cplusplus } #endif
\ No newline at end of file diff --git a/infra/module_manager/test/gtest_module_manager_main.cpp b/infra/module_manager/test/gtest_module_manager_main.cpp index f8559f4..9ee7bef 100644 --- a/infra/module_manager/test/gtest_module_manager_main.cpp +++ b/infra/module_manager/test/gtest_module_manager_main.cpp @@ -17,27 +17,6 @@ const char *gtest_mock_spec_toml = "init = \"gtest_mock_init\"\n" "exit = \"gtest_mock_exit\"\n"; -TEST(module_manager_internal, module_specs_load) { - - FILE *fp = fmemopen((void *)gtest_mock_spec_toml, strlen(gtest_mock_spec_toml), "r"); - EXPECT_TRUE(fp!=NULL); - - struct module_spec_load *specs=NULL; - int mod_num=module_specs_load(fp, &specs); - fclose(fp); - EXPECT_EQ(mod_num, 1); - - EXPECT_EQ(specs[0].on_init_cb, gtest_mock_init); - EXPECT_EQ(specs[0].on_exit_cb, gtest_mock_exit); - - - if(specs->path)free(specs->path); - if(specs->init_cb_name)free(specs->init_cb_name); - if(specs->exit_cb_name)free(specs->exit_cb_name); - free(specs); - -} - TEST(module_manager_internal, stellar_module_manager_new_with_toml) { struct mq_schema *mq_schema=NULL; |
