【新增】

修改部分测试代码
This commit is contained in:
niubinghui
2024-10-08 15:13:06 +08:00
parent 6417137294
commit ad865134dc
8 changed files with 222 additions and 19 deletions

View File

@@ -12,11 +12,11 @@ OBJECTS := example_plugin_manage.o
SIMPLE_SRC := simple_example_plugin.c
SIMPLE_OBJECTS := simple_example_plugin.o
INCLUDE = -I$(TOPDIR)/output/include -I$(TOPDIR)/dependence/include -I$(TOPDIR)/example/include
INCLUDE = -I$(TOPDIR)/output/include -I$(TOPDIR)/dependence/include -I$(TOPDIR)/test/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 -lbitmap -lplugin_manager
LDLIBS += -L$(TOPDIR)/output/libs -lluaplugin -L$(TOPDIR)/dependence/lib -ltoml -lbitmap -lplugin_manager -lstellar_on_sapp
TARGET:$(OBJECTS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) $(LDLIBS)

View File

@@ -1,4 +1,9 @@
[[plugin]]
path = "./plugin/simple_stellar_plugin.so"
init = "simple_plugin_sub_session_stat_init"
exit = "simple_plugin_sub_session_stat_exit"
exit = "simple_plugin_sub_session_stat_exit"
[[plugin]]
path = "./plugin/simple_stellar_plugin.so"
init = "simple_session_packet_plugin_init"
exit = "simple_session_packet_plugin_exit"

View File

@@ -0,0 +1,65 @@
function plugin_ctx_new(sess, plug_env, sess_context)
print("now lua plugin create sess ctx, plugin id", plug_env.id)
sess_context.count = 0
sess_context.payload_len = 0
msg = {}
msg.data = "this is lua plugin message"
session_mq.publish_message(sess, plug_env.topic_id, msg)
end
function plugin_ctx_free(sess, sess_context, plug_env)
print("now lua plugin begin to free sess ctx")
print("total packet count is", sess_context.count)
print("total paylaod length is", sess_context.payload_len)
end
function on_message_fn(sess, topic_id, msg, sess_context, env)
sess_context.count = sess_context.count + 1
print("lua plugin call on message fn", sess_context.count)
payload, payload_len = session.get_payload(sess)
sess_context.payload_len = sess_context.payload_len + payload_len
print(payload)
end
function free_message(sess, msg, msg_free_arg)
print("message need to free is", msg.data)
print("free arg data is", msg_free_arg.data)
end
function plugin_load(stellar, plug_env)
print("now begin to load lua example plugin")
plug_env.st = stellar
plug_env.id = session_plugin.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
plug_env.data = "this is lua example plug env data"
--[[ 获取TCP topic, 并完成订阅 ]]
tcp_topic_id = session_mq.get_topic_id(stellar, "TCP")
print("lua plugin get TCP topic id is", tcp_topic_id)
session_mq.subscribe_topic(stellar, tcp_topic_id, on_message_fn, plug_env.id)
plug_env.tcp_topic_id = tcp_topic_id
--[[ 创建TOPIC_SESSION_STAT, 并完成函数注册 ]]
msg_free_arg = {}
msg_free_arg.data = "this is lua plugin message free arg data"
test_topic_id = session_mq.get_topic_id(stellar, "LUA_TOPIC_SESSION_STAT")
if (test_topic_id < 0)
then
--[[ 该消息未创建, 创建该topic ]]
test_topic_id = session_mq.create_topic(stellar, "LUA_TOPIC_SESSION_STAT", free_message, msg_free_arg)
print("create topic is", test_topic_id)
else
--[[ 如果该消息已经注册, 更新其注册函数 ]]
session_mq.update_topic(stellar, test_topic_id, free_message, msg_free_arg)
print("topic already created, id is", test_topic_id)
end
plug_env.tcp_topic_id = tcp_topic_id
plug_env.topic_id = test_topic_id
end
function plugin_unload(plug_env)
print("now unload lua example plugin")
print("plugin env data is", plug_env.data)
print("now destory topic id is", plug_env.topic_id)
session_mq.destory_topic(plug_env.st, plug_env.topic_id)
end

View File

@@ -8,19 +8,22 @@ end
function on_message(sess, topic_id, msg, sess_context, env)
-- print("message call on message function, id", topic_id)
print("message is", msg.data)
print("get message is", topic_id, msg.data)
end
function plugin_load(stellar, plug_env)
print("now begin to load plugin example messaga")
print("this example aims to test topic functions")
plug_env.st = stellar
plug_env.id = session_plugin.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
plug_env.data = "this is message example plug env data"
--[[ 订阅TOPIC_SESSION_STAT, 并完成函数注册 ]]
test_topic_id = session_mq.get_topic_id(stellar, "TOPIC_SESSION_STAT")
test_topic_id = session_mq.get_topic_id(stellar, "LUA_TOPIC_SESSION_STAT")
if (test_topic_id < 0)
then
--[[ 该消息未创建, 创建该topic ]]
test_topic_id = session_mq.create_topic(stellar, "LUA_TOPIC_SESSION_STAT", nil, nil)
print("create topic is", test_topic_id)
end
session_mq.subscribe_topic(stellar, test_topic_id, on_message, plug_env.id)
end

View File

@@ -1,6 +1,7 @@
function plugin_ctx_new(sess, plug_env, sess_context)
print("now create new ctx example topic, plugin id", plug_env.id)
sess_context.count = 0
sess_context.payload_len = 0
msg = {}
msg.data = "this is message"
session_mq.publish_message(sess, plug_env.topic_id, msg)
@@ -8,17 +9,20 @@ end
function plugin_ctx_free(sess, sess_context, plug_env)
print("now begin to free ctx context example topic")
print(sess_context.count)
print("total packet count is", sess_context.count)
print("total paylaod length is", sess_context.payload_len)
end
function on_message(sess, topic_id, msg, sess_context, env)
function on_message_fn(sess, topic_id, msg, sess_context, env)
sess_context.count = sess_context.count + 1
print("topic call on message function, id", topic_id)
-- print("topic call on message function, id", topic_id)
payload, payload_len = session.get_payload(sess)
sess_context.payload_len = sess_context.payload_len + payload_len
end
function free_message(sess, msg, private_env)
function free_message(sess, msg, msg_free_arg)
print("message need to free is", msg.data)
print("env id is ", private_env.id)
print("env id is ", msg_free_arg.id)
end
function plugin_load(stellar, plug_env)
@@ -32,21 +36,21 @@ function plugin_load(stellar, plug_env)
--[[ 获取TCP topic, 并完成订阅 ]]
tcp_topic_id = session_mq.get_topic_id(stellar, "TCP")
print("get TCP topic id is", tcp_topic_id)
session_mq.subscribe_topic(stellar, tcp_topic_id, on_message, plug_env.id)
session_mq.subscribe_topic(stellar, tcp_topic_id, on_message_fn, plug_env.id)
plug_env.tcp_topic_id = tcp_topic_id
--[[ 创建TOPIC_SESSION_STAT, 并完成函数注册 ]]
msg_private_table = {}
msg_private_table.data = "this is example topic msg private data"
test_topic_id = session_mq.get_topic_id(stellar, "TOPIC_SESSION_STAT")
msg_free_arg = {}
msg_free_arg.data = "this is example topic msg private data"
test_topic_id = session_mq.get_topic_id(stellar, "LUA_TOPIC_SESSION_STAT")
if (test_topic_id < 0)
then
--[[ 该消息未创建, 创建该topic ]]
test_topic_id = session_mq.create_topic(stellar, "TOPIC_SESSION_STAT", free_message, msg_private_table)
test_topic_id = session_mq.create_topic(stellar, "LUA_TOPIC_SESSION_STAT", free_message, msg_free_arg)
print("create topic is", test_topic_id)
else
--[[ 如果该消息已经注册, 更新其注册函数 ]]
session_mq.update_topic(stellar, test_topic_id, free_message, msg_private_table)
session_mq.update_topic(stellar, test_topic_id, free_message, msg_free_arg)
print("topic already created, id is", test_topic_id)
end
plug_env.tcp_topic_id = tcp_topic_id

View File

@@ -101,6 +101,7 @@ static void simple_plugin_on_session_msg(struct session *sess, int topic_id, con
size_t payload_len = 0;
session_get0_current_payload(sess, &payload_len);
printf("get packet length is %lu\n", payload_len);
int dir = packet_get_direction(pkt);
if (dir==PACKET_DIRECTION_C2S)
{

24
test_on_stellar/Makefile Normal file
View File

@@ -0,0 +1,24 @@
TOPDIR = ./..
CC=gcc
MAKE=make
TARGET=lua_plugin_manage_example.so
EXAMPLE_FLAG = -DLUAPLUGIN_EXAMPLE
SRC := plugin_manage_on_stellar.c
OBJECTS := plugin_manage_on_stellar.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) -shared -o $(TARGET) $(OBJECTS) $(LDLIBS)
$(OBJECTS):$(SRC)
$(CC) $(TEST_FLAG) $(INCLUDE) $(CFLAGS) $(SRC) -c $^
clean:
rm -rf $(OBJECTS) $(TARGET)

View 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");
}