【新增】
修改部分测试代码
This commit is contained in:
101
test_on_stellar/plugin_manage_on_stellar.c
Normal file
101
test_on_stellar/plugin_manage_on_stellar.c
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "stellar/stellar.h"
|
||||
#include "stellar/utils.h"
|
||||
#include "lua_plugin_manage.h"
|
||||
|
||||
#include <toml.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LUA_PLUGIN_CONF_PATH "/opt/tsg/sapp/lua_manage/lua_plugin_manage.toml"
|
||||
|
||||
static struct lua_config_spec *config_load(const char *config_file_name, int *specific_num);
|
||||
static void debug_plugin_manage_specific(struct lua_config_spec *specific, int num);
|
||||
|
||||
void *lua_plugin_manage_on_load(struct stellar *st __unused)
|
||||
{
|
||||
int num = 0;
|
||||
struct lua_config_spec *specific = config_load(LUA_PLUGIN_CONF_PATH, &num);
|
||||
debug_plugin_manage_specific(specific, num);
|
||||
|
||||
// struct lua_plugin_manage *lua_schema = NULL;
|
||||
struct lua_plugin_manage *lua_schema = lua_plugin_manage_init(st, specific, num);
|
||||
if (specific)
|
||||
free(specific);
|
||||
return (void *)lua_schema;
|
||||
}
|
||||
|
||||
void lua_plugin_manage_on_unload(void *plugin_env __unused)
|
||||
{
|
||||
if (plugin_env)
|
||||
lua_plugin_manage_exit((struct lua_plugin_manage *)plugin_env);
|
||||
return;
|
||||
}
|
||||
|
||||
static struct lua_config_spec *config_load(const char *config_file_name, int *specific_count)
|
||||
{
|
||||
if (!config_file_name)
|
||||
return NULL;
|
||||
int specific_num = 0;
|
||||
char errbuff[256] = {0};
|
||||
|
||||
if (access(config_file_name, F_OK))
|
||||
{
|
||||
printf("file access falied, filename is %s\n", config_file_name);
|
||||
return NULL;
|
||||
}
|
||||
FILE *fp = fopen(config_file_name, "r");
|
||||
if (!fp)
|
||||
return NULL;
|
||||
toml_table_t *conf = toml_parse_file(fp, errbuff, sizeof(errbuff));
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
if (!conf)
|
||||
{
|
||||
printf("parse config file failed, filename %s, err %s\n", config_file_name, errbuff);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
toml_array_t *plugin_array = toml_array_in(conf, "plugin");
|
||||
if (!plugin_array)
|
||||
return NULL;
|
||||
|
||||
specific_num = toml_array_nelem(plugin_array);
|
||||
struct lua_config_spec *new_spec = (struct lua_config_spec *)calloc(specific_num, sizeof(struct lua_config_spec));
|
||||
if (!new_spec)
|
||||
return NULL;
|
||||
struct lua_config_spec *specific = NULL;
|
||||
|
||||
for (int i = 0; i < specific_num; ++i)
|
||||
{
|
||||
toml_table_t *plugin = toml_table_at(plugin_array, i);
|
||||
const char *raw_filepath = toml_raw_in(plugin, "path");
|
||||
const char *raw_load_func_name = toml_raw_in(plugin, "init");
|
||||
const char *raw_unload_func_name = toml_raw_in(plugin, "exit");
|
||||
specific = &new_spec[i];
|
||||
|
||||
if (toml_rtos(raw_filepath, &specific->spec_file_path) ||
|
||||
toml_rtos(raw_load_func_name, &specific->spec_load_func_name) ||
|
||||
toml_rtos(raw_unload_func_name, &specific->spec_unload_func_name))
|
||||
{
|
||||
toml_free(conf);
|
||||
free(specific);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
*specific_count = specific_num;
|
||||
|
||||
return new_spec;
|
||||
}
|
||||
|
||||
static void debug_plugin_manage_specific(struct lua_config_spec *specific, int num)
|
||||
{
|
||||
printf("***** ***** ***** ***** ***** *****\n");
|
||||
printf("load config specifics count is %d\n", num);
|
||||
for (int i = 0; i < num; ++i)
|
||||
{
|
||||
printf("[%d]file path: %s\n", i, specific[i].spec_file_path);
|
||||
printf("[%d]load func: %s\n", i, specific[i].spec_load_func_name);
|
||||
printf("[%d]unload func: %s\n", i, specific[i].spec_unload_func_name);
|
||||
}
|
||||
printf("***** ***** ***** ***** ***** *****\n\n");
|
||||
}
|
||||
Reference in New Issue
Block a user