✨ feat(module manager): API new with file
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ extern "C"
|
||||
#include <limits.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct stellar_module
|
||||
{
|
||||
@@ -34,10 +35,10 @@ struct module_spec_load
|
||||
struct stellar_module_manager
|
||||
{
|
||||
char *module_spec_toml_path;
|
||||
struct
|
||||
{
|
||||
struct module_spec_load *module_specs;
|
||||
int load_module_num;
|
||||
struct
|
||||
{
|
||||
int max_thread_num;
|
||||
struct mq_schema *mq_schema;
|
||||
}schema;
|
||||
@@ -46,11 +47,8 @@ struct stellar_module_manager
|
||||
|
||||
|
||||
|
||||
#include "toml/toml.h"
|
||||
|
||||
toml_table_t *toml_parse_file_path(const char *toml_conf_path) __attribute__((visibility("hidden")));
|
||||
int module_specs_load(toml_table_t* conf, struct module_spec_load **load_spec) __attribute__((visibility("hidden")));
|
||||
struct stellar_module_manager *stellar_module_manager_new_with_toml(toml_table_t *conf, int max_thread_num, struct mq_schema *mq_schema) __attribute__((visibility("hidden")));
|
||||
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
|
||||
@@ -8,16 +8,6 @@
|
||||
/***********************************
|
||||
* TEST MODUEL MANAGER INTERNAL API *
|
||||
***********************************/
|
||||
TEST(module_manager_internal, toml_parse_filepath_null) {
|
||||
toml_table_t *conf = toml_parse_file_path("");
|
||||
EXPECT_TRUE(conf==NULL);
|
||||
}
|
||||
|
||||
TEST(module_manager_internal, toml_parse_filepath_dev_null) {
|
||||
toml_table_t *conf = toml_parse_file_path("/dev/null");
|
||||
EXPECT_TRUE(conf!=NULL);
|
||||
toml_free(conf);
|
||||
}
|
||||
|
||||
extern "C" struct stellar_module *gtest_mock_init(struct stellar_module_manager *mod_mgr){return NULL;}
|
||||
extern "C" void gtest_mock_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod){}
|
||||
@@ -29,11 +19,12 @@ const char *gtest_mock_spec_toml =
|
||||
|
||||
TEST(module_manager_internal, module_specs_load) {
|
||||
|
||||
toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0);
|
||||
EXPECT_TRUE(conf!=NULL);
|
||||
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(conf, &specs);
|
||||
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);
|
||||
@@ -44,17 +35,17 @@ TEST(module_manager_internal, module_specs_load) {
|
||||
if(specs->init_cb_name)free(specs->init_cb_name);
|
||||
if(specs->exit_cb_name)free(specs->exit_cb_name);
|
||||
free(specs);
|
||||
toml_free(conf);
|
||||
|
||||
}
|
||||
|
||||
TEST(module_manager_internal, stellar_module_manager_new_with_toml) {
|
||||
|
||||
struct mq_schema *mq_schema=NULL;
|
||||
toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0);
|
||||
EXPECT_TRUE(conf!=NULL);
|
||||
FILE *fp = fmemopen((void *)gtest_mock_spec_toml, strlen(gtest_mock_spec_toml), "r");
|
||||
EXPECT_TRUE(fp!=NULL);
|
||||
|
||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, 10, mq_schema);
|
||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, 10, mq_schema);
|
||||
fclose(fp);
|
||||
|
||||
EXPECT_TRUE(mod_mgr!=NULL);
|
||||
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==NULL);
|
||||
@@ -65,7 +56,6 @@ TEST(module_manager_internal, stellar_module_manager_new_with_toml) {
|
||||
EXPECT_TRUE(stellar_module_manager_get_mq_runtime(mod_mgr)==NULL);
|
||||
|
||||
stellar_module_manager_free(mod_mgr);
|
||||
toml_free(conf);
|
||||
|
||||
}
|
||||
|
||||
@@ -178,10 +168,12 @@ const char *gtest_module_spec_toml =
|
||||
TEST(module_manager, basic_module) {
|
||||
|
||||
struct mq_schema *mq_schema=(struct mq_schema *)1;
|
||||
toml_table_t *conf = toml_parse((char*)gtest_module_spec_toml, NULL, 0);
|
||||
EXPECT_TRUE(conf!=NULL);
|
||||
|
||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, 10, mq_schema);
|
||||
FILE *fp = fmemopen((void *)gtest_module_spec_toml, strlen(gtest_module_spec_toml), "r");
|
||||
EXPECT_TRUE(fp!=NULL);
|
||||
|
||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, 10, mq_schema);
|
||||
fclose(fp);
|
||||
EXPECT_TRUE(mod_mgr!=NULL);
|
||||
|
||||
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "gtest")!=NULL);
|
||||
@@ -196,7 +188,6 @@ TEST(module_manager, basic_module) {
|
||||
EXPECT_EQ((long)stellar_module_manager_get_mq_runtime(mod_mgr), 2);
|
||||
|
||||
stellar_module_manager_free(mod_mgr);
|
||||
toml_free(conf);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -37,10 +37,12 @@ const char *gtest_mock_spec_toml =
|
||||
TEST(polling_manager, basic_polling_module) {
|
||||
|
||||
struct mq_schema *mq_schema=mq_schema_new();
|
||||
toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0);
|
||||
EXPECT_TRUE(conf!=NULL);
|
||||
|
||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, 10, mq_schema);
|
||||
FILE *fp = fmemopen((void *)gtest_mock_spec_toml, strlen(gtest_mock_spec_toml), "r");
|
||||
EXPECT_TRUE(fp!=NULL);
|
||||
|
||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, 10, mq_schema);
|
||||
fclose(fp);
|
||||
EXPECT_TRUE(mod_mgr!=NULL);
|
||||
|
||||
struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr);
|
||||
@@ -66,7 +68,6 @@ TEST(polling_manager, basic_polling_module) {
|
||||
}
|
||||
|
||||
stellar_module_manager_free(mod_mgr);
|
||||
toml_free(conf);
|
||||
|
||||
EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user