🦄 refactor(module manager): remove internal api load_spec
This commit is contained in:
@@ -13,29 +13,51 @@
|
|||||||
*******************************************/
|
*******************************************/
|
||||||
|
|
||||||
#include "toml/toml.h"
|
#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|| mod_spec==NULL) return 0;
|
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;
|
||||||
|
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;
|
int mod_num = 0;
|
||||||
toml_table_t *conf = toml_parse_file(fp, NULL, 0);
|
toml_table_t *conf = toml_parse_file(fp, NULL, 0);
|
||||||
toml_array_t* mod_array = toml_array_in(conf, "module");
|
toml_array_t* mod_array = toml_array_in(conf, "module");
|
||||||
if(mod_array==NULL)goto MODULE_SPEC_LOAD_ERROR;
|
char *path = NULL;
|
||||||
mod_num = toml_array_nelem(mod_array);
|
char *init_func_name = NULL;
|
||||||
*mod_spec = CALLOC(struct module_spec_load, mod_num);
|
char *exit_func_name = NULL;
|
||||||
|
|
||||||
struct stellar_module_manager *mod_mgr = (struct stellar_module_manager *)container_of(mod_spec, struct stellar_module_manager, module_specs);
|
if(mod_array==NULL)goto MODULE_SPEC_LOAD_ERROR;
|
||||||
|
|
||||||
// TODO: store module specific in hash
|
mod_num = toml_array_nelem(mod_array);
|
||||||
|
mod_mgr->module_specs = CALLOC(struct module_spec_load, mod_num);
|
||||||
|
|
||||||
|
// TODO: store module specific in hash
|
||||||
for (int i = 0; i < mod_num; i++) {
|
for (int i = 0; i < mod_num; i++) {
|
||||||
toml_table_t* toml_mod = toml_table_at(mod_array, i);
|
toml_table_t* toml_mod = toml_table_at(mod_array, i);
|
||||||
|
|
||||||
const char *path_raw = toml_raw_in(toml_mod, "path");
|
const char *path_raw = toml_raw_in(toml_mod, "path");
|
||||||
const char *init_func_name_raw = toml_raw_in(toml_mod, "init");
|
const char *init_func_name_raw = toml_raw_in(toml_mod, "init");
|
||||||
const char *exit_func_name_raw = toml_raw_in(toml_mod, "exit");
|
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) ||
|
if (toml_rtos(path_raw, &path) || toml_rtos(init_func_name_raw, &init_func_name) ||
|
||||||
toml_rtos(exit_func_name_raw, &exit_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;
|
goto MODULE_SPEC_LOAD_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
mod_spec[i]->on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
|
mod_mgr->module_specs[i].on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
|
||||||
if (mod_spec[i]->on_init_cb)
|
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);
|
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)
|
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());
|
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);
|
mod_mgr->module_specs[i].on_exit_cb = (module_on_exit_func *) dlsym(handle, exit_func_name);
|
||||||
if (!mod_spec[i]->on_exit_cb) {
|
if (!mod_mgr->module_specs[i].on_exit_cb) {
|
||||||
fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror());
|
fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror());
|
||||||
}
|
}
|
||||||
mod_spec[i]->path=path;
|
mod_mgr->module_specs[i].path=path;
|
||||||
mod_spec[i]->init_cb_name=init_func_name;
|
mod_mgr->module_specs[i].init_cb_name=init_func_name;
|
||||||
mod_spec[i]->exit_cb_name=exit_func_name;
|
mod_mgr->module_specs[i].exit_cb_name=exit_func_name;
|
||||||
}
|
}
|
||||||
if(conf )toml_free(conf);
|
mod_mgr->load_module_num=mod_num;
|
||||||
return mod_num;
|
|
||||||
MODULE_SPEC_LOAD_ERROR:
|
MODULE_SPEC_LOAD_ERROR:
|
||||||
if(conf )toml_free(conf);
|
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;
|
return mod_mgr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,9 +46,8 @@ struct stellar_module_manager
|
|||||||
}__attribute__((aligned(sizeof(void*))));
|
}__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")));
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -17,27 +17,6 @@ const char *gtest_mock_spec_toml =
|
|||||||
"init = \"gtest_mock_init\"\n"
|
"init = \"gtest_mock_init\"\n"
|
||||||
"exit = \"gtest_mock_exit\"\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) {
|
TEST(module_manager_internal, stellar_module_manager_new_with_toml) {
|
||||||
|
|
||||||
struct mq_schema *mq_schema=NULL;
|
struct mq_schema *mq_schema=NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user