【新增】example测试代码

This commit is contained in:
niubinghui
2024-08-16 16:16:16 +08:00
parent 8a66645ea5
commit afe428938d
12 changed files with 507 additions and 39 deletions

View File

@@ -5,15 +5,15 @@ TARGET=example
EXAMPLE_FLAG = -DLUAPLUGIN_EXAMPLE
SRC := example.c
SRC := example_plugin_manage.c
OBJECTS := example.o
OBJECTS := example_plugin_manage.o
INCLUDE = -I$(TOPDIR)/output/include -I$(TOPDIR)/dependence/include
INCLUDE = -I$(TOPDIR)/output/include -I$(TOPDIR)/dependence/include -I$(TOPDIR)/example/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
LDLIBS += -L$(TOPDIR)/output/libs -lluaplugin -L$(TOPDIR)/dependence/lib -ltoml -lbitmap -lplugin_manager
all:$(OBJECTS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS) $(LDLIBS)

View File

@@ -0,0 +1,10 @@
# config.toml
[[plugin]]
path = "./plugin/example_plugin-1.lua"
init = "plugin_load"
exit = "plugin_unload"
[[plugin]]
path = "./plugin/example_plugin-2.lua"
init = "plugin_load"
exit = "plugin_unload"

View File

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

View File

@@ -1,30 +0,0 @@
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

View File

@@ -0,0 +1,95 @@
#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;
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "stellar/stellar.h"
struct plugin_manager_schema;
struct plugin_manager_runtime;
struct plugin_manager_schema *plugin_manager_init(struct stellar *st, const char *plugin_spec_file_path);
void plugin_manager_exit(struct plugin_manager_schema *plug_mgr);
void plugin_manager_on_packet_ingress(struct plugin_manager_schema *plug_mgr, struct packet *pkt);
void plugin_manager_on_packet_egress(struct plugin_manager_schema *plug_mgr, struct packet *pkt);
//return polling work state, 0: idle, 1: working
int plugin_manager_on_polling(struct plugin_manager_schema *plug_mgr);
//publish and dispatch session msg(msg, pkt) on session_mq
void plugin_manager_on_session_ingress(struct session *sess,struct packet *pkt);
void plugin_manager_on_session_egress(struct session *sess,struct packet *pkt);
void plugin_manager_on_session_closing(struct session *sess);
struct plugin_manager_runtime *plugin_manager_session_runtime_new(struct plugin_manager_schema *plug_mgr, struct session *sess);
void plugin_manager_session_runtime_free(struct plugin_manager_runtime *plug_mgr_rt);

View File

@@ -0,0 +1,103 @@
#ifdef __cplusplus
extern "C"
{
#endif
#include "plugin_manager_interna.h"
#include "stellar_internal.h"
#include "stellar/session.h"
#include "lua_plugin_manage.h"
//mock stellar
struct stellar
{
struct plugin_manager_schema *plug_mgr;
struct lua_plugin_manage_schema * lua_plug_mgr;
};
struct packet
{
struct stellar *st;
enum packet_type type;
unsigned char ip_proto;
};
struct session
{
struct plugin_manager_runtime *plug_mgr_rt;
enum session_type type;
enum session_state state;
int sess_pkt_cnt;
};
enum session_state session_get_current_state(struct session *sess)
{
return sess->state;
}
enum session_type session_get_type(struct session *sess)
{
return sess->type;
}
int session_get_current_plugin_id(struct session *sess)
{
return sess->plug_mgr_rt->current_session_plugin_id;
}
struct plugin_manager_schema * stellar_plugin_manager_schema_get(struct stellar *st)
{
return st->plug_mgr;
}
int stellar_plugin_manager_schema_set(struct stellar *st, struct plugin_manager_schema *pm)
{
st->plug_mgr=pm;
return 0;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
int session_get_current_thread_id(struct session *sess)
{
return 0;
}
int stellar_get_worker_thread_num(struct stellar *st)
{
return 16;
}
int stellar_get_current_thread_id(struct stellar *st)
{
return 0;
}
#pragma GCC diagnostic pop
struct stellar * packet_stellar_get(struct packet *pkt)
{
return pkt->st;
}
struct plugin_manager_runtime * session_plugin_manager_runtime_get(struct session *sess)
{
return sess->plug_mgr_rt;
}
unsigned char packet_get_ip_protocol(struct packet *pkt)
{
return pkt->ip_proto;
}
enum packet_type packet_get_type(const struct packet *pkt)
{
return pkt->type;
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,199 @@
#include "plugin_manager.h"
#include "stellar/stellar.h"
#include "stellar/session_exdata.h"
#include "stellar/session_mq.h"
#include "stellar/packet_exdata.h"
#include "stellar/packet_mq.h"
#include "bitmap.h"
#include "utarray.h"
struct per_thread_exdata_array
{
struct stellar_exdata *exdata_array;
};
struct stellar_message;
struct plugin_manger_per_thread_data
{
struct per_thread_exdata_array per_thread_pkt_exdata_array;
struct stellar_message *priority_mq[SESSION_MQ_PRIORITY_MAX];// message list
struct stellar_message *dealth_letter_queue;// dlq list
long long pub_packet_msg_cnt;
};
struct plugin_manager_schema
{
struct stellar *st;
UT_array *plugin_load_specs_array;
UT_array *packet_exdata_schema_array;
UT_array *session_exdata_schema_array;
UT_array *stellar_mq_schema_array;
UT_array *registered_session_plugin_array;
UT_array *registered_packet_plugin_array;
UT_array *registered_polling_plugin_array;
int packet_mq_topic_num;
int session_mq_topic_num;
int packet_topic_subscriber_num;
int session_topic_subscriber_num;
int tcp_topic_id;
int tcp_stream_topic_id;
int udp_topic_id;
int egress_topic_id;
int control_packet_topic_id;
int max_message_dispatch;
struct plugin_manger_per_thread_data *per_thread_data;
}__attribute__((aligned(sizeof(void*))));
enum plugin_exdata_state
{ INIT, ACTIVE, EXIT };
struct stellar_exdata
{
void *exdata;
enum plugin_exdata_state state;
};
struct stellar_exdata_schema
{
char *name;
union
{
void *free_func;
session_exdata_free *sess_free_func;
packet_exdata_free *pkt_free_func;
};
void *free_arg;
int idx;
}__attribute__((aligned(sizeof(void*))));
enum stellar_topic_type
{
ON_SESSION_TOPIC,
ON_PACKET_TOPIC,
};
struct stellar_message
{
struct
{
int topic_id;
enum stellar_topic_type type;
enum session_mq_priority priority;
} header;
void *body;
struct stellar_message *next, *prev;
} __attribute__((aligned(sizeof(void *))));
typedef struct stellar_mq_subscriber
{
int topic_subscriber_idx;
int plugin_idx;
union
{
on_session_msg_cb_func *sess_msg_cb;
on_packet_msg_cb_func *pkt_msg_cb;
void *msg_cb;
};
struct stellar_mq_subscriber *next, *prev;
}stellar_mq_subscriber __attribute__((aligned(sizeof(void*))));
struct stellar_mq_topic_schema
{
char *topic_name;
void *free_cb_arg;
int topic_id;
int subscriber_cnt;
int is_destroyed;
union
{
void *free_cb;
session_msg_free_cb_func *sess_msg_free_cb;
packet_msg_free_cb_func *pkt_msg_free_cb;
};
struct stellar_mq_subscriber *subscribers;
}__attribute__((aligned(sizeof(void*))));
struct session_plugin_ctx_runtime
{
enum plugin_exdata_state state;
int session_plugin_id;
void *plugin_ctx;
}__attribute__((aligned(sizeof(void*))));
struct plugin_manager_runtime
{
struct plugin_manager_schema *plug_mgr;
struct session *sess;
struct bitmap *session_mq_status; //N * M bits, N topic, M subscriber
struct bitmap *session_topic_status; //N bits, N topic
struct stellar_exdata *sess_exdata_array;
struct session_plugin_ctx_runtime *plugin_ctx_array;//N plugins TODO: call alloc and free
int current_session_plugin_id;
int pub_session_msg_cnt;
}__attribute__((aligned(sizeof(void*))));
struct registered_packet_plugin_schema
{
char ip_protocol;
plugin_on_packet_func *on_packet;
void *plugin_env;
UT_array *registed_packet_mq_subscriber_info;
}__attribute__((aligned(sizeof(void*))));
struct registered_polling_plugin_schema
{
plugin_on_polling_func *on_polling;
void *plugin_env;
}__attribute__((aligned(sizeof(void*))));
struct stellar_mq_subscriber_info
{
int topic_id;
int subscriber_idx;
}__attribute__((aligned(sizeof(void*))));
struct registered_session_plugin_schema
{
session_ctx_new_func *on_ctx_new;
session_ctx_free_func *on_ctx_free;
void *plugin_env;
UT_array *registed_session_mq_subscriber_info;
}__attribute__((aligned(sizeof(void*))));
#define SESSION_PULGIN_ID_BASE 0x00000
#define PACKET_PULGIN_ID_BASE 0x10000
#define POLLING_PULGIN_ID_BASE 0x20000
/*******************************
* PLUGIN MANAGER INIT & EXIT *
*******************************/
#define MAX_MSG_PER_DISPATCH 128
#include <dlfcn.h>
struct plugin_specific
{
char plugin_name[256];
plugin_on_load_func *load_cb;
plugin_on_unload_func *unload_cb;
void *plugin_ctx;
}__attribute__((aligned(sizeof(void*))));

View File

@@ -0,0 +1,27 @@
#pragma once
#include "stellar/stellar.h"
struct plugin_manager_schema;
struct plugin_manager_runtime;
int stellar_plugin_manager_schema_set(struct stellar *st, struct plugin_manager_schema *pm);
struct plugin_manager_schema * stellar_plugin_manager_schema_get(struct stellar *st);
struct plugin_manager_runtime * session_plugin_manager_runtime_get(struct session *sess);
enum packet_type
{
UNKNOWN,
IPv4,
IPv6,
UDP,
TCP,
TCP_STREAM,
CONTROL,
};
enum packet_type packet_get_type(const struct packet *pkt);
struct stellar * packet_stellar_get(struct packet *pkt);

View File

@@ -13,14 +13,14 @@ 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.id = 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)
-- print(plug_env.__penv_pointer)
-- print(plug_env.data)
-- print(plug_env.newid)
-- print(plug_env.messid)
end

View File

@@ -0,0 +1,38 @@
function plugin_ctx_new(sess, plug_env, sess_context)
print("now create new ctx example-2, plugin id ", plug_env.id)
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 free_message()
print("free message")
end
function plugin_load(stellar, plug_env)
print("now begin to load plugin example-2")
plug_env.id = plugin_manage.register(stellar, plugin_ctx_new, plugin_ctx_free, plug_env)
topic_id = message.gettopicid(stellar, "TOPIC_SESSION_STAT")
print("get topic id is ", topic_id)
create_id = message.gettopicid(stellar, "TOPIC_LUA_SESSION_TEST")
if (create_id < 0)
then
print("no topic, create new one")
create_table = {}
create_id = message.createtopic(stellar, "TOPIC_LUA_SESSION_TEST", free_message, create_table)
print("create topic is ", create_id)
else
print("has created, id is ", create_id)
end
end
function plugin_unload(plug_env)
print("now running unload plugin example-2 function, plugin id is ", plug_env.id)
end

Binary file not shown.