feat(module manager): API new with file

This commit is contained in:
yangwei
2024-09-27 19:09:50 +08:00
parent 7aeb5949ee
commit 849df6b9cc
4 changed files with 84 additions and 85 deletions

View File

@@ -12,27 +12,21 @@
* module manager internal API *
*******************************************/
toml_table_t *toml_parse_file_path(const char *toml_conf_path)
{
if(toml_conf_path==NULL)return NULL;
FILE* fp = fopen(toml_conf_path, "r");
if(fp==NULL)return NULL;
char errbuf[256];
toml_table_t* conf = toml_parse_file(fp, errbuf, sizeof(errbuf));
fclose(fp);
if(conf==NULL) fprintf(stderr, "Error parsing toml: %s\n", errbuf);
return conf;
}
#include "toml/toml.h"
int module_specs_load(toml_table_t* conf, struct module_spec_load **mod_spec)
int module_specs_load(FILE *fp, struct module_spec_load **mod_spec)
{
if(conf==NULL|| mod_spec==NULL) return 0;
if(fp==NULL|| mod_spec==NULL) return 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");
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);
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
for (int i = 0; i < mod_num; i++) {
toml_table_t* toml_mod = toml_table_at(mod_array, i);
@@ -54,7 +48,23 @@ int module_specs_load(toml_table_t* conf, struct module_spec_load **mod_spec)
}
mod_spec[i]->on_init_cb = (module_on_init_func *) dlsym(handle, init_func_name);
if (!mod_spec[i]->on_init_cb) {
if (mod_spec[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)
{
mod_mgr->module_specs[i].is_init_succ = true;
}
else
{
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
{
fprintf(stderr, "Could not load init function %s: %s\n", init_func_name, dlerror());
}
@@ -66,37 +76,37 @@ int module_specs_load(toml_table_t* conf, struct module_spec_load **mod_spec)
mod_spec[i]->init_cb_name=init_func_name;
mod_spec[i]->exit_cb_name=exit_func_name;
}
if(conf )toml_free(conf);
return mod_num;
MODULE_SPEC_LOAD_ERROR:
if(conf )toml_free(conf);
if(*mod_spec)FREE(*mod_spec);
return 0;
}
struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t *conf, int max_thread_num, struct mq_schema *mq_schema)
#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->schema.load_module_num = module_specs_load(conf, &mod_mgr->schema.module_specs);
// TODO: store module specific in hash
for(int i = 0; i < mod_mgr->schema.load_module_num; i++)
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)
{
if (mod_mgr->schema.module_specs[i].on_init_cb != NULL)
{
mod_mgr->schema.module_specs[i].mod=mod_mgr->schema.module_specs[i].on_init_cb(mod_mgr);
if(stellar_module_manager_get_module(mod_mgr, mod_mgr->schema.module_specs[i].mod->name)==NULL)
{
mod_mgr->schema.module_specs[i].is_init_succ=true;
}
else
{
fprintf(stderr, "Module %s already exists\n", mod_mgr->schema.module_specs[i].mod->name);
if(mod_mgr->schema.module_specs[i].on_exit_cb)mod_mgr->schema.module_specs[i].on_exit_cb(mod_mgr, mod_mgr->schema.module_specs[i].mod);
assert(0);
}
}
mod_mgr->module_spec_toml_path = strdup(pathname);
}
return mod_mgr;
}
@@ -107,10 +117,9 @@ struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t
struct stellar_module_manager *stellar_module_manager_new(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema)
{
toml_table_t *conf = toml_parse_file_path(module_spec_toml_path);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, max_thread_num, mq_schema);
if(conf)toml_free(conf);
if(module_spec_toml_path)mod_mgr->module_spec_toml_path=strdup(module_spec_toml_path);
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;
}
@@ -118,20 +127,20 @@ void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return;
if(mod_mgr->module_spec_toml_path)FREE(mod_mgr->module_spec_toml_path);
if (mod_mgr->schema.module_specs)
if (mod_mgr->module_specs)
{
for (int i = 0; i < mod_mgr->schema.load_module_num; i++)
for (int i = 0; i < mod_mgr->load_module_num; i++)
{
if (mod_mgr->schema.module_specs[i].on_exit_cb != NULL &&
mod_mgr->schema.module_specs[i].is_init_succ)
if (mod_mgr->module_specs[i].on_exit_cb != NULL &&
mod_mgr->module_specs[i].is_init_succ)
{
mod_mgr->schema.module_specs[i].on_exit_cb(mod_mgr, mod_mgr->schema.module_specs[i].mod);
mod_mgr->module_specs[i].on_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
}
if(mod_mgr->schema.module_specs[i].path)FREE(mod_mgr->schema.module_specs[i].path);
if(mod_mgr->schema.module_specs[i].init_cb_name)FREE(mod_mgr->schema.module_specs[i].init_cb_name);
if(mod_mgr->schema.module_specs[i].exit_cb_name)FREE(mod_mgr->schema.module_specs[i].exit_cb_name);
if(mod_mgr->module_specs[i].path)FREE(mod_mgr->module_specs[i].path);
if(mod_mgr->module_specs[i].init_cb_name)FREE(mod_mgr->module_specs[i].init_cb_name);
if(mod_mgr->module_specs[i].exit_cb_name)FREE(mod_mgr->module_specs[i].exit_cb_name);
}
FREE(mod_mgr->schema.module_specs);
FREE(mod_mgr->module_specs);
}
FREE(mod_mgr);
return;
@@ -178,14 +187,14 @@ void stellar_module_manager_register_thread(struct stellar_module_manager* mod_m
struct stellar_module *stellar_module_manager_get_module(struct stellar_module_manager *mod_mgr, const char *module_name)
{
if(mod_mgr==NULL || module_name == NULL)return NULL;
if (mod_mgr->schema.module_specs)
if (mod_mgr->module_specs)
{
for(int i=0; i<mod_mgr->schema.load_module_num; i++)
for(int i=0; i<mod_mgr->load_module_num; i++)
{
if(mod_mgr->schema.module_specs[i].mod == NULL)break;
if(strcmp(mod_mgr->schema.module_specs[i].mod->name, module_name)==0 && mod_mgr->schema.module_specs[i].is_init_succ)
if(mod_mgr->module_specs[i].mod == NULL)break;
if(strcmp(mod_mgr->module_specs[i].mod->name, module_name)==0 && mod_mgr->module_specs[i].is_init_succ)
{
return mod_mgr->schema.module_specs[i].mod;
return mod_mgr->module_specs[i].mod;
}
}
}