summaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorniubinghui <[email protected]>2024-08-13 17:23:13 +0800
committerniubinghui <[email protected]>2024-08-13 17:23:13 +0800
commit88a34dec4d64b4f2eea86fc21286504ed06eea16 (patch)
tree0065e989c095fa571eff9d7e075cab8424bd709f /example
parenta52c210f77a9cd5914abd955a32f372367bbcd29 (diff)
【修改】修改example样例,调用方式修改为传入已经加载完成的config specifig
Diffstat (limited to 'example')
-rw-r--r--example/Makefile26
-rw-r--r--example/example.c126
-rw-r--r--example/example_plugin-1.lua26
-rw-r--r--example/example_plugin-2.lua30
4 files changed, 167 insertions, 41 deletions
diff --git a/example/Makefile b/example/Makefile
new file mode 100644
index 0000000..47a4b3d
--- /dev/null
+++ b/example/Makefile
@@ -0,0 +1,26 @@
+TOPDIR = ./..
+CC=gcc
+MAKE=make
+TARGET=example
+
+EXAMPLE_FLAG = -DLUAPLUGIN_EXAMPLE
+
+SRC := example.c
+
+OBJECTS := example.o
+
+INCLUDE = -I$(TOPDIR)/output/include -I$(TOPDIR)/dependence/include
+CFLAGS = -g -Wextra -Wall -O0 -fPIC
+# CFLAGS += -pedantic -fsanitize=address
+# LDLIBS = -L$(TOPDIR)/output/lib -llua -ldl -lm
+LDLIBS += -L$(TOPDIR)/output/libs -lluaplugin -L$(TOPDIR)/dependence/lib -ltoml
+
+all:$(OBJECTS)
+ $(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) $(LDLIBS)
+
+$(OBJECTS):$(SRC)
+ $(CC) $(TEST_FLAG) $(INCLUDE) $(CFLAGS) $(SRC) -c $^
+
+clean:
+ rm -rf $(OBJECTS) $(TARGET)
+ rm -rf $(TOPDIR)/output/libs/$(TARGET) \ No newline at end of file
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
diff --git a/example/example_plugin-1.lua b/example/example_plugin-1.lua
new file mode 100644
index 0000000..52b0500
--- /dev/null
+++ b/example/example_plugin-1.lua
@@ -0,0 +1,26 @@
+function plugin_ctx_new(sess, plug_env, sess_context)
+ print("now begin to create new ctx context example-1")
+ print(plug_env.data)
+ sess_context.id = 100
+end
+
+function plugin_ctx_free(sess, sess_context, plug_env)
+ print("now begin to free ctx context example-1")
+ print(sess_context.id)
+end
+
+function plugin_load(stellar, plug_env)
+ print("now begin to load plugin example-1")
+ plug_env.data = "my example-1 plugin env"
+ plug_env.newid = 1000
+ plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
+ plug_env.messid = 100
+end
+
+function plugin_unload(plug_env)
+ print("now running unload plugin example-1 function")
+ print(plug_env.__penv_pointer)
+ print(plug_env.data)
+ print(plug_env.newid)
+ print(plug_env.messid)
+end \ No newline at end of file
diff --git a/example/example_plugin-2.lua b/example/example_plugin-2.lua
new file mode 100644
index 0000000..94e0578
--- /dev/null
+++ b/example/example_plugin-2.lua
@@ -0,0 +1,30 @@
+function plugin_ctx_new(sess, plug_env, sess_context)
+ print("now begin to create new ctx context example-2")
+ print(plug_env.data)
+ local sessid = session.getid(sess)
+ sess_context.id = 200
+ print("session id is ", sessid)
+ session.setid(sess, 50000)
+end
+
+function plugin_ctx_free(sess, sess_context, plug_env)
+ print(sess_context.id)
+ print("now begin to free ctx context example-2")
+end
+
+function plugin_load(stellar, plug_env)
+ print("now begin to load plugin example-2")
+ plug_env.data = "my example-2 plugin env"
+ plug_env.newid = 2000
+ id = plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
+ print(id)
+ plug_env.messid = 200
+end
+
+function plugin_unload(plug_env)
+ print("now running unload plugin example-2 function")
+ print(plug_env.penv_pointer)
+ print(plug_env.data)
+ print(plug_env.newid)
+ print(plug_env.messid)
+end \ No newline at end of file