This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/infra/module_manager/module_manager.c
2024-09-14 17:41:07 +08:00

251 lines
7.4 KiB
C

#include "module_manager_interna.h"
#include "stellar/module_manager.h"
#include "stellar/utils.h"
#include <dlfcn.h>
#include <stdbool.h>
UT_icd module_specs_icd = {sizeof(struct module_specific), NULL, NULL, NULL};
/*******************************************
* 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;
}
struct module_specific *module_specs_load(toml_table_t* conf, int *mod_num)
{
if(conf==NULL|| mod_num==NULL) return NULL;
*mod_num = 0;
struct module_specific* mod_spec=NULL;
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_specific, *mod_num);
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))
{
goto MODULE_SPEC_LOAD_ERROR;
}
void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
if (!handle) {
fprintf(stderr, "Error loading plugin %s: %s\n", path, dlerror());
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) {
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) {
fprintf(stderr, "Could not load exit function %s: %s\n", exit_func_name, dlerror());
}
FREE(path);
FREE(init_func_name);
FREE(exit_func_name);
}
return mod_spec;
MODULE_SPEC_LOAD_ERROR:
if(mod_spec)FREE(mod_spec);
*mod_num=0;
return NULL;
}
struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t *conf, int max_thread_num, struct mq_schema *mq_schema)
{
int spec_num = 0;
struct module_specific *specs = module_specs_load(conf, &spec_num);
struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1);
if(spec_num > 0)
{
utarray_new(mod_mgr->schema.module_specs_array,&module_specs_icd);
utarray_reserve(mod_mgr->schema.module_specs_array, spec_num);
}
mod_mgr->schema.max_thread_num=max_thread_num;
mod_mgr->schema.mq_schema=mq_schema;
// TODO: store module specific data in hash
for(int i = 0; i < spec_num; i++)
{
if (specs[i].on_init_cb != NULL)
{
//TODO: duplicate check mod_name
specs[i].mod=specs[i].on_init_cb(mod_mgr);
utarray_push_back(mod_mgr->schema.module_specs_array, &specs[i]);
}
}
FREE(specs);
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)
{
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);
return mod_mgr;
}
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return;
struct module_specific *p=NULL;
if (mod_mgr->schema.module_specs_array)
{
while ((p = (struct module_specific *)utarray_next(mod_mgr->schema.module_specs_array, p)))
{
if (p->on_exit_cb)
p->on_exit_cb(mod_mgr, p->mod);
}
utarray_free(mod_mgr->schema.module_specs_array);
}
#if 0
if(plug_mgr->stellar_mq_schema_array)
{
for(unsigned int i = 0; i < utarray_len(plug_mgr->stellar_mq_schema_array); i++)
{
stellar_mq_destroy_topic( plug_mgr->st, i);
}
utarray_free(plug_mgr->stellar_mq_schema_array);
}
//if(plug_mgr->stellar_exdata_schema_array)utarray_free(plug_mgr->stellar_exdata_schema_array);
if(plug_mgr->registered_polling_plugin_array)utarray_free(plug_mgr->registered_polling_plugin_array);
if(plug_mgr->registered_packet_plugin_array)
{
struct registered_plugin_schema *s = NULL;
while ((s = (struct registered_plugin_schema *)utarray_next(plug_mgr->registered_packet_plugin_array, s)))
{
if(s->registed_mq_subscriber_info)utarray_free(s->registed_mq_subscriber_info);
}
utarray_free(plug_mgr->registered_packet_plugin_array);
}
#endif
FREE(mod_mgr);
return;
}
int stellar_module_manager_get_max_thread_num(struct stellar_module_manager*mod_mgr)
{
if(mod_mgr==NULL)return -1;
return mod_mgr->schema.max_thread_num;
}
struct mq_schema *stellar_module_get_mq_schema(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return NULL;
return mod_mgr->schema.mq_schema;
}
__thread int local_thread_id=-1;
__thread struct mq_runtime *local_mq_rt=NULL;
int stellar_module_manager_get_thread_id(struct stellar_module_manager* mod_mgr __unused)
{
return local_thread_id;
}
struct mq_runtime *stellar_module_get_mq_runtime(struct stellar_module_manager *mod_mgr __unused)
{
return local_mq_rt;
}
void stellar_module_manager_register_thread(struct stellar_module_manager* mod_mgr __unused, int thread_id, struct mq_runtime *mq_rt)
{
local_thread_id=thread_id;
local_mq_rt=mq_rt;
return;
}
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;
struct module_specific *p=NULL;
if (mod_mgr->schema.module_specs_array)
{
while ((p = (struct module_specific *)utarray_next(mod_mgr->schema.module_specs_array, p)))
{
if (p->mod)
{
if (strcmp(p->mod->name, module_name) == 0)
{
return p->mod;
}
}
}
}
return NULL;
}
/*******************************************
* stellar module API *
*******************************************/
struct stellar_module *stellar_module_new(const char *name)
{
struct stellar_module *mod = CALLOC(struct stellar_module, 1);
memcpy(mod->name, name, MIN(NAME_MAX, strlen(name)));
return mod;
}
void stellar_module_free(struct stellar_module *mod)
{
if(mod==NULL)return;
FREE(mod);
return;
}
void * stellar_module_get_ctx(struct stellar_module *mod)
{
if(mod==NULL)return NULL;
return mod->module_ctx;
}
void stellar_module_set_ctx(struct stellar_module *mod, void *ctx)
{
if(mod==NULL)return;
mod->module_ctx=ctx;
return;
}
const char *stellar_module_get_name(struct stellar_module* mod)
{
if(mod==NULL)return NULL;
return mod->name;
}
void stellar_module_set_name(struct stellar_module* mod, const char *name)
{
if(mod==NULL)return;
memcpy(mod->name, name, MIN(NAME_MAX, strlen(name)));
return;
}