#include "plugin_manager_gtest_mock.h" #include "lua_plugin_manage.h" #include #include #include #include #include #include #define PLUGIN_CONFIG_PATH "./conf/plugin_manage.toml" #define LUA_CONFIG_PATH "./conf/lua_plugin_manage.toml" // #define DEBUG_PLUGIN_SCHEMA static struct lua_config_specific *config_load(const char *config_file_name, int *specific_num); #ifdef DEBUG_PLUGIN_SCHEMA static void debug_plugin_manage_schema(struct plugin_manager_schema *schema); #endif #define PACKET_COUNT 10 #define SESSION_COUNT 10 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); /* 初始化lua插件 */ struct lua_plugin_manage_schema *lua_schema = lua_plugin_manage_init(&st, num, specific); st.lua_plug_mgr = lua_schema; struct lua_config_specific add_specific = {NULL, NULL, NULL}; add_specific.config_specific_file = "./plugin/example_plugin-2.lua"; add_specific.config_specific_load_func = "plugin_load"; add_specific.config_specific_unload_func = "plugin_unload"; lua_plugin_manage_load_one_specific(st.lua_plug_mgr, &add_specific); #ifdef DEBUG_PLUGIN_SCHEMA debug_plugin_manage_schema(plug_mgr); #endif unsigned char ip_proto=6; struct packet pkt={&st, TCP, ip_proto}; struct session sess[SESSION_COUNT]; memset(&sess, 0, sizeof(sess)); for(int i=0; i < SESSION_COUNT; i++) { sess[i].plug_mgr_rt=plugin_manager_session_runtime_new(plug_mgr, &sess[i]); sess[i].type=SESSION_TYPE_TCP; } for (int j = 0; j < PACKET_COUNT; j++) { // plugin_manager_on_packet_ingress(plug_mgr, &pkt); for (int i = 0; i < SESSION_COUNT; i++) { sess[i].sess_pkt_cnt+=1; plugin_manager_on_session_ingress(&sess[i], &pkt); plugin_manager_on_session_egress(&sess[i], &pkt); } // plugin_manager_on_packet_egress(plug_mgr, &pkt); } for(int i=0; i < SESSION_COUNT; i++) { plugin_manager_on_session_closing(&sess[i]); plugin_manager_session_runtime_free(sess[i].plug_mgr_rt); } plugin_manager_exit(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; } #ifdef DEBUG_PLUGIN_SCHEMA 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; } #endif