✨ feat(module manager internal API): remove new_with_file
This commit is contained in:
@@ -9,44 +9,33 @@
|
||||
|
||||
|
||||
/*******************************************
|
||||
* module manager internal API *
|
||||
* module manager API *
|
||||
*******************************************/
|
||||
|
||||
#include "toml/toml.h"
|
||||
#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 *stellar_module_manager_new(const char *module_spec_toml_path, 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;
|
||||
if(module_spec_toml_path==NULL)return mod_mgr;
|
||||
FILE *fp = fopen(module_spec_toml_path, "r");
|
||||
if (fp == NULL)return mod_mgr;
|
||||
mod_mgr->module_spec_toml_path = strdup(module_spec_toml_path);
|
||||
|
||||
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_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;
|
||||
if(mod_array==NULL)goto MODULE_SPEC_LOAD_END;
|
||||
|
||||
mod_num = toml_array_nelem(mod_array);
|
||||
mod_mgr->module_specs = CALLOC(struct module_spec_load, mod_num);
|
||||
@@ -61,12 +50,12 @@ struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, in
|
||||
if (toml_rtos(path_raw, &path) || toml_rtos(init_func_name_raw, &init_func_name) ||
|
||||
toml_rtos(exit_func_name_raw, &exit_func_name))
|
||||
{
|
||||
goto MODULE_SPEC_LOAD_ERROR;
|
||||
goto MODULE_SPEC_LOAD_END;
|
||||
}
|
||||
void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
|
||||
if (!handle) {
|
||||
fprintf(stderr, "Error loading module %s: %s\n", path, dlerror());
|
||||
goto MODULE_SPEC_LOAD_ERROR;
|
||||
goto MODULE_SPEC_LOAD_END;
|
||||
}
|
||||
|
||||
mod_mgr->module_specs[i].on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
|
||||
@@ -82,7 +71,6 @@ struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, in
|
||||
fprintf(stderr, "Module %s already exists\n", mod_mgr->module_specs[i].mod->name);
|
||||
if (mod_mgr->module_specs[i].on_exit_cb)
|
||||
mod_mgr->module_specs[i].on_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
|
||||
assert(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -97,27 +85,19 @@ struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, in
|
||||
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;
|
||||
mod_mgr->load_module_num+=1;
|
||||
}
|
||||
mod_mgr->load_module_num=mod_num;
|
||||
|
||||
MODULE_SPEC_LOAD_ERROR:
|
||||
MODULE_SPEC_LOAD_END:
|
||||
|
||||
assert(mod_mgr->load_module_num==mod_num);
|
||||
|
||||
if(conf )toml_free(conf);
|
||||
|
||||
return mod_mgr;
|
||||
}
|
||||
|
||||
/*******************************************
|
||||
* stellar module manager API *
|
||||
*******************************************/
|
||||
|
||||
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema)
|
||||
{
|
||||
FILE *fp=fopen(module_spec_toml_path, "r");
|
||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, max_thread_num, mq_schema);
|
||||
if(fp)fclose(fp);
|
||||
return mod_mgr;
|
||||
}
|
||||
|
||||
|
||||
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
|
||||
{
|
||||
if(mod_mgr==NULL)return;
|
||||
|
||||
Reference in New Issue
Block a user