summaryrefslogtreecommitdiff
path: root/example/example.c
diff options
context:
space:
mode:
Diffstat (limited to 'example/example.c')
-rw-r--r--example/example.c126
1 files changed, 85 insertions, 41 deletions
diff --git a/example/example.c b/example/example.c
index 18cb448..09f6e09 100644
--- a/example/example.c
+++ b/example/example.c
@@ -1,59 +1,103 @@
#include "lua_plugin_manage.h"
+#include <toml.h>
+#include <utarray.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
#include <string.h>
-int session_getid(struct lpm_state * state)
+#define CONFIG_PATH "../output/conf/lua_plugin_manage.toml"
+
+struct lua_config_specific * config_load(const char *config_file_name, int * specific_num);
+
+int main()
{
- if ( lpm_cbinding_get_params_count(state) != 1 )
- return 0;
- struct lpm_cdata data;
- lpm_cbinding_get_params(state, 1, &data);
- struct session * sess = (struct session *)data.data_user;
- lpm_cdata_clean(&data);
- int session_id = sess->id;
-
- data.data_type = LPM_DATATYPE_INT;
- lpm_cbinding_push_return(state, 1, &data);
- lpm_cdata_clean(&data);
- return 1;
-}
+ struct stellar *st = stellar_new();
+ int num = 0;
+ struct lua_config_specific * specific = config_load(CONFIG_PATH, &num);
+ struct lua_plugin_manage_schema *schema = lua_plugin_manage_init(st, num, specific);
-int binding_functions(struct lpm_state * state) {
- int count = 0;
- if ( lpm_cbinding_function_register(state, session_getid, "getid", "session") ) {
- count += 1;
- }
- return count;
-}
+ for (int i = 0; i < 1; ++i)
+ {
+ struct session *sess1 = session_new(10000, 20000);
-int binding_data(struct lpm_state * state) {
- int count = 0;
- const char * version = "v0.1";
- struct lpm_cdata data;
- data.data_type = LPM_DATATYPE_CSTRING;
- data.data_length = strlen(version);
- data.data_string = strdup(version);
- if ( lpm_cdata_register(state, &data, "version", NULL) ) {
- count += 1;
+ struct registered_session_plugin_schema *plugin = NULL;
+ while ((plugin = utarray_next(st->plugin_array, plugin)))
+ {
+ printf("call plugin id %d\n", plugin->plugin_id);
+ sess1->plugin_id = plugin->plugin_id;
+ void *temp_pointer = plugin->on_ctx_new(sess1, plugin->plugin_env);
+ struct session_data_pair pair = {plugin->plugin_id, temp_pointer};
+ utarray_push_back(sess1->session_plugin, &pair);
+ printf("debug session: %d, %d\n", sess1->session_id, sess1->session_type);
+ }
+ plugin = NULL;
+ while ((plugin = utarray_next(st->plugin_array, plugin)))
+ {
+ printf("call plugin id %d\n", plugin->plugin_id);
+ sess1->plugin_id = plugin->plugin_id;
+ void *temp_context = session_get_private(sess1, plugin->plugin_id);
+ plugin->on_ctx_free(sess1, temp_context, plugin->plugin_env);
+ }
+ utarray_free(sess1->session_plugin);
+ free(sess1);
}
- lpm_cdata_clean(&data);
- return count;
+
+ lua_plugin_manage_exit(schema);
+ return 0;
}
-int main(int argc, char * argv[])
+struct lua_config_specific * config_load(const char *config_file_name, int * specific_count)
{
- struct stellar st;
- struct lpm_state * state = lpm_state_instance_create();
- binding_functions(state);
- binding_data(state);
- lpm_state_instance_init(&st, state, "config.toml");
-
+ 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;
- lpm_state_instance_free(state);
- return 0;
+ return new_spec;
} \ No newline at end of file