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
niubinghui-luapluginmanage/example/example_plugin_manage.c
2024-08-16 16:16:16 +08:00

96 lines
3.2 KiB
C

#include "plugin_manager_gtest_mock.h"
#include "lua_plugin_manage.h"
#include <toml.h>
#include <utarray.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define PLUGIN_CONFIG_PATH "./conf/plugin_manage.toml"
#define LUA_CONFIG_PATH "./conf/lua_plugin_manage.toml"
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_num);
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema);
int main()
{
struct stellar st;
memset(&st, 0, sizeof(st));
int num = 0;
struct lua_config_specific *specific = config_load(LUA_CONFIG_PATH, &num);
struct plugin_manager_schema *plug_mgr = plugin_manager_init(&st, PLUGIN_CONFIG_PATH);
struct lua_plugin_manage_schema *lua_schema = lua_plugin_manage_init(&st, num, specific);
st.lua_plug_mgr = lua_schema;
debug_plugin_manage_schema(plug_mgr);
lua_plugin_manage_exit(lua_schema);
return 0;
}
static struct lua_config_specific *config_load(const char *config_file_name, int *specific_count)
{
if (__glibc_unlikely(!config_file_name))
return NULL;
int specific_num = 0;
char errbuff[256] = {0};
if (access(config_file_name, F_OK))
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_specific *new_spec = (struct lua_config_specific *)calloc(specific_num, sizeof(struct lua_config_specific));
if (!new_spec)
return NULL;
struct lua_config_specific *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->config_specific_file) ||
toml_rtos(raw_load_func_name, &specific->config_specific_load_func) ||
toml_rtos(raw_unload_func_name, &specific->config_specific_unload_func))
{
toml_free(conf);
free(specific);
return NULL;
}
}
*specific_count = specific_num;
return new_spec;
}
static void debug_plugin_manage_schema(struct plugin_manager_schema *schema)
{
struct registered_session_plugin_schema * plugin = NULL;
for (int i = 0; i < (int)utarray_len(schema->registered_session_plugin_array); ++i) {
plugin = (struct registered_session_plugin_schema *)utarray_eltptr(schema->registered_session_plugin_array, (unsigned int)i);
printf("plugin[%d]: new func %p, free func %p, env %p\n", i, plugin->on_ctx_new, plugin->on_ctx_free, plugin->plugin_env);
}
return;
}