✨ feat(module manager internal API): remove new_with_file
This commit is contained in:
@@ -9,36 +9,25 @@
|
|||||||
|
|
||||||
|
|
||||||
/*******************************************
|
/*******************************************
|
||||||
* module manager internal API *
|
* module manager API *
|
||||||
*******************************************/
|
*******************************************/
|
||||||
|
|
||||||
#include "toml/toml.h"
|
#include "toml/toml.h"
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.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(const char *module_spec_toml_path, int max_thread_num, struct mq_schema *mq_schema)
|
||||||
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);
|
struct stellar_module_manager *mod_mgr = CALLOC(struct stellar_module_manager, 1);
|
||||||
mod_mgr->schema.max_thread_num=max_thread_num;
|
mod_mgr->schema.max_thread_num=max_thread_num;
|
||||||
mod_mgr->schema.mq_schema=mq_schema;
|
mod_mgr->schema.mq_schema=mq_schema;
|
||||||
|
if(module_spec_toml_path==NULL)return mod_mgr;
|
||||||
|
FILE *fp = fopen(module_spec_toml_path, "r");
|
||||||
if (fp == NULL)return mod_mgr;
|
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_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");
|
||||||
@@ -46,7 +35,7 @@ struct stellar_module_manager *stellar_module_manager_new_with_file(FILE *fp, in
|
|||||||
char *init_func_name = NULL;
|
char *init_func_name = NULL;
|
||||||
char *exit_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_num = toml_array_nelem(mod_array);
|
||||||
mod_mgr->module_specs = CALLOC(struct module_spec_load, mod_num);
|
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) ||
|
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))
|
||||||
{
|
{
|
||||||
goto MODULE_SPEC_LOAD_ERROR;
|
goto MODULE_SPEC_LOAD_END;
|
||||||
}
|
}
|
||||||
void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
|
void* handle = dlopen(path, RTLD_NOW|RTLD_LAZY|RTLD_GLOBAL);
|
||||||
if (!handle) {
|
if (!handle) {
|
||||||
fprintf(stderr, "Error loading module %s: %s\n", path, dlerror());
|
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);
|
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);
|
fprintf(stderr, "Module %s already exists\n", mod_mgr->module_specs[i].mod->name);
|
||||||
if (mod_mgr->module_specs[i].on_exit_cb)
|
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);
|
mod_mgr->module_specs[i].on_exit_cb(mod_mgr, mod_mgr->module_specs[i].mod);
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
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].path=path;
|
||||||
mod_mgr->module_specs[i].init_cb_name=init_func_name;
|
mod_mgr->module_specs[i].init_cb_name=init_func_name;
|
||||||
mod_mgr->module_specs[i].exit_cb_name=exit_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);
|
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);
|
if(fp)fclose(fp);
|
||||||
return mod_mgr;
|
return mod_mgr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
|
void stellar_module_manager_free(struct stellar_module_manager *mod_mgr)
|
||||||
{
|
{
|
||||||
if(mod_mgr==NULL)return;
|
if(mod_mgr==NULL)return;
|
||||||
|
|||||||
@@ -46,8 +46,6 @@ struct stellar_module_manager
|
|||||||
}__attribute__((aligned(sizeof(void*))));
|
}__attribute__((aligned(sizeof(void*))));
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@@ -5,6 +5,10 @@
|
|||||||
|
|
||||||
#include "module_manager/module_manager_interna.h"
|
#include "module_manager/module_manager_interna.h"
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
/***********************************
|
/***********************************
|
||||||
* TEST MODUEL MANAGER INTERNAL API *
|
* TEST MODUEL MANAGER INTERNAL API *
|
||||||
***********************************/
|
***********************************/
|
||||||
@@ -20,22 +24,27 @@ const char *gtest_mock_spec_toml =
|
|||||||
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;
|
||||||
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);
|
char toml_template[] = "./stellar.toml.XXXXXX";
|
||||||
fclose(fp);
|
int fd = mkstemp(toml_template);
|
||||||
|
EXPECT_TRUE(fd>=0);
|
||||||
|
write(fd, gtest_mock_spec_toml, strlen(gtest_mock_spec_toml));
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema);
|
||||||
|
|
||||||
EXPECT_TRUE(mod_mgr!=NULL);
|
EXPECT_TRUE(mod_mgr!=NULL);
|
||||||
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==NULL);
|
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "test")==NULL);
|
||||||
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
|
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
|
||||||
EXPECT_EQ(stellar_module_manager_get_mq_schema(mod_mgr), mq_schema);
|
EXPECT_EQ(stellar_module_manager_get_mq_schema(mod_mgr), mq_schema);
|
||||||
|
EXPECT_STREQ(stellar_module_manager_get_toml_path(mod_mgr), toml_template);
|
||||||
|
|
||||||
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), -1);// no thread registered
|
EXPECT_EQ(stellar_module_manager_get_thread_id(mod_mgr), -1);// no thread registered
|
||||||
EXPECT_TRUE(stellar_module_manager_get_mq_runtime(mod_mgr)==NULL);
|
EXPECT_TRUE(stellar_module_manager_get_mq_runtime(mod_mgr)==NULL);
|
||||||
|
|
||||||
stellar_module_manager_free(mod_mgr);
|
stellar_module_manager_free(mod_mgr);
|
||||||
|
|
||||||
|
unlink(toml_template);
|
||||||
}
|
}
|
||||||
|
|
||||||
/***********************************
|
/***********************************
|
||||||
@@ -148,17 +157,20 @@ TEST(module_manager, basic_module) {
|
|||||||
|
|
||||||
struct mq_schema *mq_schema=(struct mq_schema *)1;
|
struct mq_schema *mq_schema=(struct mq_schema *)1;
|
||||||
|
|
||||||
FILE *fp = fmemopen((void *)gtest_module_spec_toml, strlen(gtest_module_spec_toml), "r");
|
char toml_template[] = "./stellar.toml.XXXXXX";
|
||||||
EXPECT_TRUE(fp!=NULL);
|
int fd = mkstemp(toml_template);
|
||||||
|
EXPECT_TRUE(fd>=0);
|
||||||
|
write(fd, gtest_module_spec_toml, strlen(gtest_module_spec_toml));
|
||||||
|
close(fd);
|
||||||
|
|
||||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, 10, mq_schema);
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema);
|
||||||
fclose(fp);
|
|
||||||
EXPECT_TRUE(mod_mgr!=NULL);
|
EXPECT_TRUE(mod_mgr!=NULL);
|
||||||
|
|
||||||
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "gtest")!=NULL);
|
EXPECT_TRUE(stellar_module_manager_get_module(mod_mgr, "gtest")!=NULL);
|
||||||
|
|
||||||
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
|
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
|
||||||
EXPECT_EQ((long)stellar_module_manager_get_mq_schema(mod_mgr), 1);
|
EXPECT_EQ((long)stellar_module_manager_get_mq_schema(mod_mgr), 1);
|
||||||
|
EXPECT_STREQ(stellar_module_manager_get_toml_path(mod_mgr), toml_template);
|
||||||
|
|
||||||
struct mq_runtime *mq_rt = (struct mq_runtime*)2;
|
struct mq_runtime *mq_rt = (struct mq_runtime*)2;
|
||||||
stellar_module_manager_register_thread(mod_mgr, 1, mq_rt);
|
stellar_module_manager_register_thread(mod_mgr, 1, mq_rt);
|
||||||
@@ -167,7 +179,7 @@ TEST(module_manager, basic_module) {
|
|||||||
EXPECT_EQ((long)stellar_module_manager_get_mq_runtime(mod_mgr), 2);
|
EXPECT_EQ((long)stellar_module_manager_get_mq_runtime(mod_mgr), 2);
|
||||||
|
|
||||||
stellar_module_manager_free(mod_mgr);
|
stellar_module_manager_free(mod_mgr);
|
||||||
|
unlink(toml_template);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************************************
|
/**********************************************
|
||||||
|
|||||||
@@ -38,11 +38,13 @@ TEST(polling_manager, basic_polling_module) {
|
|||||||
|
|
||||||
struct mq_schema *mq_schema=mq_schema_new();
|
struct mq_schema *mq_schema=mq_schema_new();
|
||||||
|
|
||||||
FILE *fp = fmemopen((void *)gtest_mock_spec_toml, strlen(gtest_mock_spec_toml), "r");
|
char toml_template[] = "./stellar.toml.XXXXXX";
|
||||||
EXPECT_TRUE(fp!=NULL);
|
int fd = mkstemp(toml_template);
|
||||||
|
EXPECT_TRUE(fd>=0);
|
||||||
|
write(fd, gtest_mock_spec_toml, strlen(gtest_mock_spec_toml));
|
||||||
|
close(fd);
|
||||||
|
|
||||||
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_file(fp, 10, mq_schema);
|
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(toml_template, 10, mq_schema);
|
||||||
fclose(fp);
|
|
||||||
EXPECT_TRUE(mod_mgr!=NULL);
|
EXPECT_TRUE(mod_mgr!=NULL);
|
||||||
|
|
||||||
struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr);
|
struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr);
|
||||||
@@ -71,6 +73,8 @@ TEST(polling_manager, basic_polling_module) {
|
|||||||
|
|
||||||
EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count);
|
EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count);
|
||||||
|
|
||||||
|
unlink(toml_template);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************************************
|
/**********************************************
|
||||||
|
|||||||
Reference in New Issue
Block a user