Plugin management support pm_session_dettach_me and pm_session_dettach_others

Add test cases for pm_session_dettach_me and pm_session_dettach_others
This commit is contained in:
luwenpeng
2022-08-03 19:46:43 +08:00
parent 50111e7cd0
commit 5c790085eb
26 changed files with 1348 additions and 382 deletions

View File

@@ -39,12 +39,26 @@ packet_io_loop()
## Plugin Manager ## Plugin Manager
Plugin Management APIs Plugin Management APIs
``` ```
pm_session_dettach_me(pm, session); /*
pm_session_dettach_others(pm, session); * The plugin manager just set the skip flag and don't call this event callback next.
* Before calling pm_session_dettach_me, the current plugin must release related resources for the current session.
*/
pm_session_dettach_me(session);
/*
* The plugin manager uses ERROR_EVENT_DETTACH to call other plugin error callbacks,
* and when the plugin error callback handler is called,
* the error callback handler must release the relevant resources for the current session.
*/
pm_session_dettach_others(session);
``` ```
## Session Manager ## Session Manager
Session Management APIs Session Management APIs
``` ```
session_drop_current_packet(session); session_drop_current_packet(session);
session_set_ratelimit_group(session, rl_group_id); session_set_ratelimit_group(session, rl_group_id);

View File

@@ -5,6 +5,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
static char *g_handler = NULL; static char *g_handler = NULL;
@@ -13,29 +14,121 @@ static void *custom_decode(const char *payload, uint16_t len, void **pme)
return NULL; return NULL;
} }
extern "C" void custom_plugin_tcp_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) struct tcp_session_pme
{ {
char **per_session_pme = (char **)pme; char data[16];
/* data */
};
printf("RUN custom_plugin_tcp_entry, event: %d\n", event); struct custom_session_pme
{
char data[16];
/* data */
};
static struct tcp_session_pme *tcp_session_pme_create()
{
struct tcp_session_pme *pme = (struct tcp_session_pme *)calloc(1, sizeof(struct tcp_session_pme));
return pme;
}
static void tcp_session_pme_destory(struct tcp_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
static struct custom_session_pme *custom_session_pme_create()
{
struct custom_session_pme *pme = (struct custom_session_pme *)calloc(1, sizeof(struct custom_session_pme));
return pme;
}
static void custom_session_pme_destory(struct custom_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
extern "C" void custom_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme)
{
if (strcmp(stellar_session_get_name(session), "TCP") == 0)
{
struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme;
printf("RUN custom_event_plugin_error, session_name: 'TCP', error_event_type: %d, pme->data: %s\n", event, (*per_tcp_session_pme) == NULL ? "NULL" : (*per_tcp_session_pme)->data);
tcp_session_pme_destory(*per_tcp_session_pme);
}
if (strcmp(stellar_session_get_name(session), "CUSTOM") == 0)
{
struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme;
printf("RUN custom_event_plugin_error, session_name: 'CUSTOM', error_event_type: %d, pme->data: %s\n", event, (*per_custom_session_pme) == NULL ? "NULL" : (*per_custom_session_pme)->data);
custom_session_pme_destory(*per_custom_session_pme);
}
}
extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{
struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme;
printf("RUN custom_event_plugin_tcp_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_tcp_session_pme == NULL)
{
struct tcp_session_pme *cur_ctx = tcp_session_pme_create();
snprintf(cur_ctx->data, 6, "tcp******");
*per_tcp_session_pme = *&cur_ctx;
}
}
if (event & SESSION_EVENT_ORDPKT)
{
struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme);
struct stellar_session *new_session = session_manager_session_derive(s, "CUSTOM"); struct stellar_session *new_session = session_manager_session_derive(session, "CUSTOM");
session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info);
session_manager_trigger_event(new_session, SESSION_EVENT_META, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info);
}
if (event & SESSION_EVENT_CLOSING)
{
tcp_session_pme_destory(*per_tcp_session_pme);
}
} }
extern "C" void custom_plugin_custom_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) extern "C" void custom_event_plugin_custom_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{ {
char **per_session_pme = (char **)pme; struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme;
printf("RUN custom_plugin_custom_entry, event: %d\n", event); printf("RUN custom_event_plugin_custom_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_custom_session_pme == NULL)
{
struct custom_session_pme *cur_ctx = custom_session_pme_create();
snprintf(cur_ctx->data, 6, "custom******");
*per_custom_session_pme = *&cur_ctx;
}
}
if (event & SESSION_EVENT_CLOSING)
{
custom_session_pme_destory(*per_custom_session_pme);
}
} }
extern "C" int custom_plugin_init(void) extern "C" int custom_event_plugin_init(void)
{ {
printf("RUN custom_plugin_init\n"); printf("RUN custom_event_plugin_init\n");
if (g_handler == NULL) if (g_handler == NULL)
{ {
@@ -46,9 +139,9 @@ extern "C" int custom_plugin_init(void)
return 0; return 0;
} }
extern "C" void custom_plugin_exit(void) extern "C" void custom_event_plugin_exit(void)
{ {
printf("RUN custom_plugin_exit\n"); printf("RUN custom_event_plugin_exit\n");
if (g_handler) if (g_handler)
{ {

View File

@@ -4,56 +4,77 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
static char *g_handler = NULL; static char *g_handler = NULL;
struct per_session_pme_info struct http_session_pme
{ {
int flag;
char data[16]; char data[16];
/* data */;
}; };
extern "C" void http_event_plugin_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) static struct http_session_pme *http_session_pme_create()
{ {
struct per_session_pme_info **per_session_pme = (struct per_session_pme_info **)pme; struct http_session_pme *pme = (struct http_session_pme *)calloc(1, sizeof(struct http_session_pme));
return pme;
}
static void http_session_pme_destory(struct http_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
extern "C" void http_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme)
{
if (strcmp(stellar_session_get_name(session), "HTTP") == 0)
{
struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
printf("RUN http_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data);
http_session_pme_destory(*per_http_session_pme);
}
}
extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{
struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
printf("RUN http_event_plugin_entry, event: %d\n", event); printf("RUN http_event_plugin_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING) if (event & SESSION_EVENT_OPENING)
{ {
if (*per_session_pme == NULL) if (*per_http_session_pme == NULL)
{ {
struct per_session_pme_info *cur_ctx = (struct per_session_pme_info *)malloc(sizeof(struct per_session_pme_info)); struct http_session_pme *cur_ctx = http_session_pme_create();
snprintf(cur_ctx->data, 6, "******"); snprintf(cur_ctx->data, 6, "http******");
*per_session_pme = *&cur_ctx; *per_http_session_pme = *&cur_ctx;
printf("http_event_plugin_entry->opening_handler\n");
} }
} }
if (event & SESSION_EVENT_RAWPKT) if (event & SESSION_EVENT_RAWPKT)
{ {
printf("http_event_plugin_entry->rawpkt_handler\n"); // TODO
pm_session_dettach_me(session);
} }
if (event & SESSION_EVENT_ORDPKT) if (event & SESSION_EVENT_ORDPKT)
{ {
printf("http_event_plugin_entry->ordpkt_handler\n"); // TODO
pm_session_dettach_others(session);
} }
if (event & SESSION_EVENT_META) if (event & SESSION_EVENT_META)
{ {
printf("http_event_plugin_entry->meta_handler\n"); // TODO
} }
if (event & SESSION_EVENT_CLOSING) if (event & SESSION_EVENT_CLOSING)
{ {
if (*per_session_pme) http_session_pme_destory(*per_http_session_pme);
{
printf("http_event_plugin_entry->closing_hanler\n");
free(*per_session_pme);
*per_session_pme = NULL;
}
} }
} }

View File

@@ -1,14 +1,15 @@
[PLUGINFO] [PLUGINFO]
INIT_FUNC="custom_plugin_init" INIT_FUNC="custom_event_plugin_init"
EXIT_FUNC="custom_plugin_exit" EXIT_FUNC="custom_event_plugin_exit"
ERROR_FUNC="custom_event_plugin_error"
LIBRARY_PATH="./plugins/custom_event_plugin/custom_event_plugin.so" LIBRARY_PATH="./plugins/custom_event_plugin/custom_event_plugin.so"
# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL"
[SESSION_NAME.TCP] [SESSION_NAME.TCP]
SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
SESSION_EVENT_CALLBACK="custom_plugin_tcp_entry" SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry"
[SESSION_NAME.CUSTOM] [SESSION_NAME.CUSTOM]
SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"] SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"]
SESSION_EVENT_CALLBACK="custom_plugin_custom_entry" SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry"

View File

@@ -1,6 +1,7 @@
[PLUGINFO] [PLUGINFO]
INIT_FUNC="http_event_plugin_init" INIT_FUNC="http_event_plugin_init"
EXIT_FUNC="http_event_plugin_exit" EXIT_FUNC="http_event_plugin_exit"
ERROR_FUNC="http_event_plugin_error"
LIBRARY_PATH="./plugins/http_event_plugin/http_event_plugin.so" LIBRARY_PATH="./plugins/http_event_plugin/http_event_plugin.so"
# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL"

View File

@@ -3,3 +3,4 @@
#include "session.h" #include "session.h"
void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); void http_decoder(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme);
void http_error_cb(const struct stellar_session *s, enum error_event_type event, void **pme);

View File

@@ -1,15 +1,25 @@
#pragma once #ifndef _PLUGIN_H
#define _PLUGIN_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "session.h" #include "session.h"
typedef int plugin_init_callback(void); typedef int plugin_init_callback(void);
typedef void plugin_exit_callback(void); typedef void plugin_exit_callback(void);
void plugin_remove_from_session_event(struct stellar_event *ev, struct stellar_session *s);
/****************************************************************************** /******************************************************************************
* Public API: between plugin and plugin_manager * Public API For Plugin
******************************************************************************/ ******************************************************************************/
// TODO void pm_session_dettach_me(const struct stellar_session *session);
// TODO void pm_session_dettach_others(const struct stellar_session *session);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,4 +1,10 @@
#pragma once #ifndef _SESSION_H
#define _SESSION_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "packet.h" #include "packet.h"
#include "event.h" #include "event.h"
@@ -34,9 +40,23 @@ enum session_event_type
SESSION_EVENT_ALL = (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5), SESSION_EVENT_ALL = (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5),
}; };
enum error_event_type
{
ERROR_EVENT_DETTACH = (0x1 << 10),
};
struct stellar_session_event_extras; struct stellar_session_event_extras;
typedef void(fn_session_error_callback)(const struct stellar_session *s, enum error_event_type event, void **pme);
typedef void(fn_session_event_callback)(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme); typedef void(fn_session_event_callback)(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme);
void session_manager_trigger_event(struct stellar_session *s, enum session_event_type type, struct stellar_session_event_extras *info); void session_manager_trigger_event(struct stellar_session *s, enum session_event_type type, struct stellar_session_event_extras *info);
struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, const char *session_name); struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session, const char *session_name);
const char *stellar_session_get_name(const struct stellar_session *session);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -59,12 +59,11 @@ int main(int argc, char ** argv)
struct plugin_manager *plug_mgr = plugin_manager_create(); struct plugin_manager *plug_mgr = plugin_manager_create();
// register build-in plugin // register build-in plugin
plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder); plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_decoder, http_error_cb);
// load external plugins // load external plugins
char prefix_path[] = "/op/tsg/stellar/";
char file_path[] = "./plugs/plugins.inf"; char file_path[] = "./plugs/plugins.inf";
plugin_manager_load(plug_mgr, prefix_path, file_path); plugin_manager_load(plug_mgr, file_path);
//packet_io_init //packet_io_init
struct packet_io_device *dev = packet_io_init(1, "stellar", "cap0"); struct packet_io_device *dev = packet_io_init(1, "stellar", "cap0");

View File

@@ -1,48 +1,49 @@
#include "deps/uthash/uthash.h"
#include "session_manager.h"
#include "plugin_manager_util.h"
#include "plugin_manager_config.h"
#include "plugin_manager_module.h"
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include "uthash/uthash.h"
#include "session_manager.h"
#include "plugin_manager_module.h"
/****************************************************************************** /******************************************************************************
* CallBack Runtime (For Per Session) * CallBack Runtime (For Per Session)
******************************************************************************/ ******************************************************************************/
struct eventcb_runtime struct callback_runtime
{ {
int skip; int skip;
void *cb_args; void *cb_args;
enum session_event_type event; enum session_event_type event;
fn_session_event_callback *cb; fn_session_event_callback *event_cb;
fn_session_error_callback *error_cb;
}; };
struct session_plugin_ctx struct session_plugin_ctx
{ {
int current_plugin_index; int callback_index;
int eventcb_num; int callback_num;
struct eventcb_runtime *eventcbs; struct callback_runtime *callbacks;
}; };
/****************************************************************************** /******************************************************************************
* CallBack Static (For Per Plugin Manager) * CallBack Static (For Per Plugin Manager)
******************************************************************************/ ******************************************************************************/
struct eventcb_static struct callback_static
{ {
enum session_event_type event; enum session_event_type event;
fn_session_event_callback *cb; fn_session_event_callback *event_cb;
fn_session_error_callback *error_cb;
}; };
struct plugin_manager_eventcb struct plugin_manager_eventcb
{ {
char session_name[MAX_SESSION_NAME_LENGTH]; // key char session_name[MAX_SESSION_NAME_LENGTH]; // key
int eventcb_num; // val size int callback_num; // val size
struct eventcb_static *eventcbs; // val: dynamic array struct callback_static *callbacks; // val: dynamic array
UT_hash_handle hh; UT_hash_handle hh;
}; };
@@ -84,15 +85,16 @@ static struct session_plugin_ctx *plugin_manager_create_plugin_ctx(struct plugin
else else
{ {
struct session_plugin_ctx *plug_ctx = safe_alloc(struct session_plugin_ctx, 1); struct session_plugin_ctx *plug_ctx = safe_alloc(struct session_plugin_ctx, 1);
plug_ctx->eventcb_num = elem->eventcb_num; plug_ctx->callback_num = elem->callback_num;
plug_ctx->eventcbs = safe_alloc(struct eventcb_runtime, plug_ctx->eventcb_num); plug_ctx->callbacks = safe_alloc(struct callback_runtime, plug_ctx->callback_num);
for (int i = 0; i < plug_ctx->eventcb_num; i++) for (int i = 0; i < plug_ctx->callback_num; i++)
{ {
plug_ctx->eventcbs[i].skip = 0; plug_ctx->callbacks[i].skip = 0;
plug_ctx->eventcbs[i].event = elem->eventcbs[i].event; plug_ctx->callbacks[i].event = elem->callbacks[i].event;
plug_ctx->eventcbs[i].cb = elem->eventcbs[i].cb; plug_ctx->callbacks[i].event_cb = elem->callbacks[i].event_cb;
plug_ctx->eventcbs[i].cb_args = NULL; plug_ctx->callbacks[i].error_cb = elem->callbacks[i].error_cb;
plug_ctx->callbacks[i].cb_args = NULL;
} }
return plug_ctx; return plug_ctx;
@@ -103,7 +105,7 @@ static void plugin_manager_destory_plugin_ctx(struct session_plugin_ctx *plug_ct
{ {
if (plug_ctx) if (plug_ctx)
{ {
safe_free(plug_ctx->eventcbs); safe_free(plug_ctx->callbacks);
safe_free(plug_ctx); safe_free(plug_ctx);
} }
} }
@@ -112,26 +114,20 @@ static void plugin_manager_destory_plugin_ctx(struct session_plugin_ctx *plug_ct
* Tools for managing plugins * Tools for managing plugins
******************************************************************************/ ******************************************************************************/
static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const char *prefix, const char *file) static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const char *file)
{ {
char plugin_inf[4096] = {0};
char line_buffer[4096] = {0}; char line_buffer[4096] = {0};
if (strlen(prefix) <= 0)
{
plugin_manager_log(ERROR, "Invalid parameter, plugin config file prefix cannot be empty");
return -1;
}
if (strlen(file) <= 0) if (strlen(file) <= 0)
{ {
plugin_manager_log(ERROR, "Invalid parameter, plugin config file name cannot be empty"); plugin_manager_log(ERROR, "Invalid parameter, plugin config file name cannot be empty");
return -1; return -1;
} }
strcat_prefix_and_file(prefix, file, plugin_inf, sizeof(plugin_inf));
FILE *fp = fopen(plugin_inf, "r"); FILE *fp = fopen(file, "r");
if (fp == NULL) if (fp == NULL)
{ {
plugin_manager_log(ERROR, "can't open %s, %s", plugin_inf, strerror(errno)); plugin_manager_log(ERROR, "can't open %s, %s", file, strerror(errno));
return -1; return -1;
} }
@@ -142,6 +138,7 @@ static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const c
memset(line_buffer, 0, sizeof(line_buffer)); memset(line_buffer, 0, sizeof(line_buffer));
continue; continue;
} }
line_buffer[strcspn(line_buffer, "\r\n")] = 0;
if (plug_mgr->used_config_num >= MAX_PLUGIN_NUM) if (plug_mgr->used_config_num >= MAX_PLUGIN_NUM)
{ {
@@ -150,7 +147,7 @@ static int plugin_manager_parse_plugins(struct plugin_manager *plug_mgr, const c
} }
struct plugin_manager_config *config = plugin_mangager_config_create(); struct plugin_manager_config *config = plugin_mangager_config_create();
if (plugin_mangager_config_parse(config, prefix, line_buffer) == -1) if (plugin_mangager_config_parse(config, line_buffer) == -1)
{ {
plugin_mangager_config_destory(config); plugin_mangager_config_destory(config);
goto err; goto err;
@@ -186,6 +183,8 @@ static int plugin_manager_open_plugins(struct plugin_manager *plug_mgr)
{ {
return -1; return -1;
} }
plugin_manager_module_dump(module, config);
plug_mgr->modules[plug_mgr->used_module_num] = module; plug_mgr->modules[plug_mgr->used_module_num] = module;
plug_mgr->used_module_num++; plug_mgr->used_module_num++;
} }
@@ -267,9 +266,9 @@ static void plugin_manager_deparse_plugins(struct plugin_manager *plug_mgr)
* Public API for managing plugins * Public API for managing plugins
******************************************************************************/ ******************************************************************************/
int plugin_manager_load(struct plugin_manager *plug_mgr, const char *prefix, const char *file) int plugin_manager_load(struct plugin_manager *plug_mgr, const char *file)
{ {
if (plugin_manager_parse_plugins(plug_mgr, prefix, file) == -1) if (plugin_manager_parse_plugins(plug_mgr, file) == -1)
{ {
return -1; return -1;
} }
@@ -324,7 +323,7 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr)
{ {
HASH_DEL(plug_mgr->evcb_htable, elem); HASH_DEL(plug_mgr->evcb_htable, elem);
safe_free(elem->eventcbs); safe_free(elem->callbacks);
safe_free(elem); safe_free(elem);
} }
@@ -337,7 +336,7 @@ void plugin_manager_destory(struct plugin_manager *plug_mgr)
} }
} }
int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *cb) int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb, fn_session_error_callback *error_cb)
{ {
if (strlen(session_name) <= 0) if (strlen(session_name) <= 0)
{ {
@@ -351,23 +350,30 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session
return -1; return -1;
} }
if (cb == NULL) if (event_cb == NULL)
{ {
plugin_manager_log(ERROR, "invalid parameter, the callback corresponding to the session name '%s' is null", session_name); plugin_manager_log(ERROR, "invalid parameter, the event callback corresponding to the session name '%s' is null", session_name);
return -1;
}
if (error_cb == NULL)
{
plugin_manager_log(ERROR, "invalid parameter, the error callback corresponding to the session name '%s' is null", session_name);
return -1; return -1;
} }
struct plugin_manager_eventcb *elem; struct plugin_manager_eventcb *elem;
HASH_FIND_STR(plug_mgr->evcb_htable, session_name, elem); HASH_FIND_STR(plug_mgr->evcb_htable, session_name, elem);
// session_name exists, add a new cb to the end of the eventcbs dynamic array // session_name exists, add a new cb to the end of the callbacks dynamic array
if (elem) if (elem)
{ {
elem->eventcbs = (struct eventcb_static *)realloc(elem->eventcbs, (elem->eventcb_num + 1) * sizeof(struct eventcb_static)); elem->callbacks = (struct callback_static *)realloc(elem->callbacks, (elem->callback_num + 1) * sizeof(struct callback_static));
elem->eventcbs[elem->eventcb_num].event = event; elem->callbacks[elem->callback_num].event = event;
elem->eventcbs[elem->eventcb_num].cb = cb; elem->callbacks[elem->callback_num].event_cb = event_cb;
elem->callbacks[elem->callback_num].error_cb = error_cb;
elem->eventcb_num++; elem->callback_num++;
} }
// session_name does not exist, allocate a new node elem, and add elem to the hash table // session_name does not exist, allocate a new node elem, and add elem to the hash table
else else
@@ -375,12 +381,13 @@ int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session
elem = safe_alloc(struct plugin_manager_eventcb, 1); elem = safe_alloc(struct plugin_manager_eventcb, 1);
memcpy(elem->session_name, session_name, strlen(session_name)); memcpy(elem->session_name, session_name, strlen(session_name));
elem->eventcbs = (struct eventcb_static *)realloc(elem->eventcbs, (elem->eventcb_num + 1) * sizeof(struct eventcb_static)); elem->callbacks = (struct callback_static *)realloc(elem->callbacks, (elem->callback_num + 1) * sizeof(struct callback_static));
elem->eventcbs[elem->eventcb_num].event = event; elem->callbacks[elem->callback_num].event = event;
elem->eventcbs[elem->eventcb_num].cb = cb; elem->callbacks[elem->callback_num].event_cb = event_cb;
elem->callbacks[elem->callback_num].error_cb = error_cb;
elem->eventcb_num++; elem->callback_num++;
HASH_ADD_STR(plug_mgr->evcb_htable, session_name, elem); HASH_ADD_STR(plug_mgr->evcb_htable, session_name, elem);
} }
@@ -401,6 +408,9 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve
assert(seesion); assert(seesion);
assert(session_name); assert(session_name);
char event_str_buffer[1024] = {0};
session_event_type_int2str(event_type, event_str_buffer, 1024);
// the same session may trigger multi times opening events // the same session may trigger multi times opening events
if (event_type & SESSION_EVENT_OPENING) if (event_type & SESSION_EVENT_OPENING)
{ {
@@ -418,18 +428,20 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve
if (plug_ctx) if (plug_ctx)
{ {
for (int i = 0; i < plug_ctx->eventcb_num; i++) for (int i = 0; i < plug_ctx->callback_num; i++)
{ {
plug_ctx->current_plugin_index = i; struct callback_runtime *runtime = &plug_ctx->callbacks[i];
struct eventcb_runtime *runtime = &plug_ctx->eventcbs[i]; if (runtime->skip == 1)
if (runtime->skip)
{ {
plugin_manager_log(DEBUG, "dispatch, skip event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
continue; continue;
} }
if (runtime->event & event_type) if (runtime->event & event_type)
{ {
runtime->cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args); plug_ctx->callback_index = i;
plugin_manager_log(DEBUG, "dispatch, run event_cb: %p, session: %s, event: (%d, %s)", runtime->event_cb, session_name, event_type, event_str_buffer);
runtime->event_cb(seesion, event_type, packet, payload, payload_len, &runtime->cb_args);
} }
} }
} }
@@ -446,6 +458,55 @@ void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_eve
} }
} }
/******************************************************************************
* Public API For Plugin
******************************************************************************/
void pm_session_dettach_me(const struct stellar_session *session)
{
struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session);
assert(plugin_ctx);
struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index];
/*
* Just set the skip flag and don't call this event callback next.
* The plugin is closed before calling pm_session_dettach_me.
*/
runtime_me->skip = 1;
plugin_manager_log(DEBUG, "%p dettach me, disable event_cb: %p, session: %s", runtime_me->event_cb, runtime_me->event_cb, stellar_session_get_name(session));
}
void pm_session_dettach_others(const struct stellar_session *session)
{
struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session);
assert(plugin_ctx);
struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index];
for (int i = 0; i < plugin_ctx->callback_num; i++)
{
if (i != plugin_ctx->callback_index)
{
struct callback_runtime *runtime_other = &plugin_ctx->callbacks[i];
runtime_other->skip = 1;
plugin_manager_log(DEBUG, "%p dettach others, run error_cb: %p, session: %s", runtime_me->event_cb, runtime_other->error_cb, stellar_session_get_name(session));
runtime_other->error_cb(session, ERROR_EVENT_DETTACH, &runtime_other->cb_args);
}
}
}
/******************************************************************************
* Util For Gtest
******************************************************************************/
void *pm_session_get_plugin_pme(const struct stellar_session *session)
{
struct session_plugin_ctx *plugin_ctx = stellar_session_get_plugin_ctx(session);
assert(plugin_ctx);
struct callback_runtime *runtime_me = &plugin_ctx->callbacks[plugin_ctx->callback_index];
return runtime_me->cb_args;
}
/****************************************************************************** /******************************************************************************
* Suppport LUA plugins * Suppport LUA plugins
******************************************************************************/ ******************************************************************************/

View File

@@ -1,4 +1,10 @@
#pragma once #ifndef _PLUGIN_MANAGER_H
#define _PLUGIN_MANAGER_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "sdk/include/session.h" #include "sdk/include/session.h"
@@ -7,10 +13,17 @@ struct plugin_manager;
struct plugin_manager *plugin_manager_create(); struct plugin_manager *plugin_manager_create();
void plugin_manager_destory(struct plugin_manager *plug_mgr); void plugin_manager_destory(struct plugin_manager *plug_mgr);
// return 0: success int plugin_manager_load(struct plugin_manager *plug_mgr, const char *file);
// return -1: error
int plugin_manager_load(struct plugin_manager *plug_mgr, const char *prefix, const char *file);
void plugin_manager_unload(struct plugin_manager *plug_mgr); void plugin_manager_unload(struct plugin_manager *plug_mgr);
int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *callback); int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_name, enum session_event_type event, fn_session_event_callback *event_cb, fn_session_error_callback *error_cb);
void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event); void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event);
// only use for gtest
void *pm_session_get_plugin_pme(const struct stellar_session *session);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,65 +1,10 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "toml/toml.h" #include "toml/toml.h"
#include "sdk/include/session.h"
#include "plugin_manager_util.h"
#include "plugin_manager_config.h" #include "plugin_manager_config.h"
struct event_type_map
{
const char *type_str;
enum session_event_type type_int;
};
static struct event_type_map evtype_map[] =
{
{"SESSION_EVENT_OPENING", SESSION_EVENT_OPENING},
{"SESSION_EVENT_RAWPKT", SESSION_EVENT_RAWPKT},
{"SESSION_EVENT_ORDPKT", SESSION_EVENT_ORDPKT},
{"SESSION_EVENT_META", SESSION_EVENT_META},
{"SESSION_EVENT_CLOSING", SESSION_EVENT_CLOSING},
{"SESSION_EVENT_ALL", SESSION_EVENT_ALL},
};
/******************************************************************************
* Private API (For Event Type)
******************************************************************************/
static enum session_event_type event_type_str2int(const char *evtype_str)
{
int num = sizeof(evtype_map) / sizeof(evtype_map[0]);
for (int i = 0; i < num; i++)
{
if (strcmp(evtype_str, evtype_map[i].type_str) == 0)
{
return evtype_map[i].type_int;
}
}
return SESSION_EVENT_UNKNOWN;
}
static void event_type_int2str(enum session_event_type evtype_int, char *buffer, int size)
{
int used = 0;
int num = sizeof(evtype_map) / sizeof(evtype_map[0]);
for (int i = 0; i < num; i++)
{
if (evtype_map[i].type_int & evtype_int)
{
if (evtype_map[i].type_int == SESSION_EVENT_ALL && evtype_int != SESSION_EVENT_ALL)
{
continue;
}
used += snprintf(buffer + used, size - used, "%s ", evtype_map[i].type_str);
}
}
}
/****************************************************************************** /******************************************************************************
* Private API (For Parse) * Private API (For Parse)
******************************************************************************/ ******************************************************************************/
@@ -117,6 +62,11 @@ static int toml_parse_plugin_section(toml_table_t *root, struct plugin_manager_c
return -1; return -1;
} }
if (toml_parse_string(plugin_section, "ERROR_FUNC", file, &config->plugin_section.error_func_name) == -1)
{
return -1;
}
if (toml_parse_string(plugin_section, "LIBRARY_PATH", file, &config->plugin_section.lib_path) == -1) if (toml_parse_string(plugin_section, "LIBRARY_PATH", file, &config->plugin_section.lib_path) == -1)
{ {
return -1; return -1;
@@ -193,7 +143,7 @@ static int toml_parse_session_section(toml_table_t *root, struct plugin_manager_
return -1; return -1;
} }
type_int = event_type_str2int(type_str.u.s); type_int = session_event_type_str2int(type_str.u.s);
if (type_int == SESSION_EVENT_UNKNOWN) if (type_int == SESSION_EVENT_UNKNOWN)
{ {
plugin_manager_log(ERROR, "invalid value '%s' for 'SESSION_EVENT_TYPE' configuration item in '[SESSION_NAME.%s]' section of %s", type_str.u.s, session_name, file); plugin_manager_log(ERROR, "invalid value '%s' for 'SESSION_EVENT_TYPE' configuration item in '[SESSION_NAME.%s]' section of %s", type_str.u.s, session_name, file);
@@ -219,12 +169,12 @@ struct plugin_manager_config *plugin_mangager_config_create()
{ {
struct plugin_manager_config *config = safe_alloc(struct plugin_manager_config, 1); struct plugin_manager_config *config = safe_alloc(struct plugin_manager_config, 1);
config->prefix_path = NULL;
config->file_path = NULL; config->file_path = NULL;
config->session_section_num = 0; config->session_section_num = 0;
config->session_section = NULL; config->session_section = NULL;
config->plugin_section.init_func_name = NULL; config->plugin_section.init_func_name = NULL;
config->plugin_section.exit_func_name = NULL; config->plugin_section.exit_func_name = NULL;
config->plugin_section.error_func_name = NULL;
config->plugin_section.lib_path = NULL; config->plugin_section.lib_path = NULL;
return config; return config;
@@ -234,11 +184,11 @@ void plugin_mangager_config_destory(struct plugin_manager_config *config)
{ {
if (config) if (config)
{ {
safe_free(config->prefix_path);
safe_free(config->file_path); safe_free(config->file_path);
safe_free(config->plugin_section.init_func_name); safe_free(config->plugin_section.init_func_name);
safe_free(config->plugin_section.exit_func_name); safe_free(config->plugin_section.exit_func_name);
safe_free(config->plugin_section.error_func_name);
safe_free(config->plugin_section.lib_path); safe_free(config->plugin_section.lib_path);
if (config->session_section) if (config->session_section)
@@ -260,10 +210,10 @@ void plugin_mangager_config_dump(struct plugin_manager_config *config)
{ {
if (config) if (config)
{ {
plugin_manager_log(DEBUG, "[CONFIG_PREFIX] : %s", config->prefix_path);
plugin_manager_log(DEBUG, "[CONFIG_FILE] : %s", config->file_path); plugin_manager_log(DEBUG, "[CONFIG_FILE] : %s", config->file_path);
plugin_manager_log(DEBUG, "[PLUGINFO]->INIT_FUNC : %s", config->plugin_section.init_func_name); plugin_manager_log(DEBUG, "[PLUGINFO]->INIT_FUNC : %s", config->plugin_section.init_func_name);
plugin_manager_log(DEBUG, "[PLUGINFO]->EXIT_FUNC : %s", config->plugin_section.exit_func_name); plugin_manager_log(DEBUG, "[PLUGINFO]->EXIT_FUNC : %s", config->plugin_section.exit_func_name);
plugin_manager_log(DEBUG, "[PLUGINFO]->ERROR_FUNC : %s", config->plugin_section.error_func_name);
plugin_manager_log(DEBUG, "[PLUGINFO]->LIBRARY_PATH : %s", config->plugin_section.lib_path); plugin_manager_log(DEBUG, "[PLUGINFO]->LIBRARY_PATH : %s", config->plugin_section.lib_path);
if (config->session_section) if (config->session_section)
@@ -272,7 +222,7 @@ void plugin_mangager_config_dump(struct plugin_manager_config *config)
{ {
char tmp_buffer[1024] = {0}; char tmp_buffer[1024] = {0};
struct session_section_config *temp = &config->session_section[i]; struct session_section_config *temp = &config->session_section[i];
event_type_int2str(temp->event, tmp_buffer, 1024); session_event_type_int2str(temp->event, tmp_buffer, 1024);
plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_TYPE : %d, %s", temp->session_name, temp->event, tmp_buffer); plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_TYPE : %d, %s", temp->session_name, temp->event, tmp_buffer);
plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_CALLBACK : %s", temp->session_name, temp->cb_func_name); plugin_manager_log(DEBUG, "[SESSION_NAME.%s]->SESSION_EVENT_CALLBACK : %s", temp->session_name, temp->cb_func_name);
} }
@@ -280,21 +230,18 @@ void plugin_mangager_config_dump(struct plugin_manager_config *config)
} }
} }
int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *prefix, const char *file) int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *file)
{ {
FILE *fp = NULL; FILE *fp = NULL;
toml_table_t *root = NULL; toml_table_t *root = NULL;
char errbuf[200] = {0}; char errbuf[200] = {0};
char tmp_buffer[4096] = {0};
config->prefix_path = safe_dup(prefix);
config->file_path = safe_dup(file); config->file_path = safe_dup(file);
strcat_prefix_and_file(prefix, file, tmp_buffer, sizeof(tmp_buffer));
fp = fopen(tmp_buffer, "r"); fp = fopen(config->file_path, "r");
if (fp == NULL) if (fp == NULL)
{ {
plugin_manager_log(ERROR, "can't open %s, %s", tmp_buffer, strerror(errno)); plugin_manager_log(ERROR, "can't open %s, %s", config->file_path, strerror(errno));
return -1; return -1;
} }
@@ -318,6 +265,8 @@ int plugin_mangager_config_parse(struct plugin_manager_config *config, const cha
toml_free(root); toml_free(root);
fclose(fp); fclose(fp);
plugin_manager_log(INFO, "plugin config file '%s' parse success", config->file_path);
return 0; return 0;
err: err:
@@ -334,25 +283,3 @@ err:
return -1; return -1;
} }
/******************************************************************************
* Test
******************************************************************************/
#ifdef PLUGIN_MANAGER_TEST
int main(int argc, char *argv[])
{
struct plugin_manager_config *config = plugin_mangager_config_create();
if (plugin_mangager_config_parse(config, argv[1], argv[2]) == -1)
{
plugin_manager_log(ERROR, "can't parser %s %s", argv[1], argv[2]);
}
plugin_mangager_config_dump(config);
plugin_mangager_config_destory(config);
return 0;
}
#endif

View File

@@ -1,4 +1,10 @@
#pragma once #ifndef _PLUGIN_MANAGER_CONFIG_H
#define _PLUGIN_MANAGER_CONFIG_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "plugin_manager_util.h" #include "plugin_manager_util.h"
#include "sdk/include/session.h" #include "sdk/include/session.h"
@@ -14,13 +20,13 @@ struct plugin_section_config
{ {
char *init_func_name; char *init_func_name;
char *exit_func_name; char *exit_func_name;
char *error_func_name;
char *lib_path; char *lib_path;
}; };
struct plugin_manager_config struct plugin_manager_config
{ {
char *prefix_path; // absolute path to stellar installation char *file_path;
char *file_path; // relative path to stellar installation
int session_section_num; int session_section_num;
struct session_section_config *session_section; // array struct session_section_config *session_section; // array
@@ -30,5 +36,11 @@ struct plugin_manager_config
struct plugin_manager_config *plugin_mangager_config_create(); struct plugin_manager_config *plugin_mangager_config_create();
void plugin_mangager_config_destory(struct plugin_manager_config *config); void plugin_mangager_config_destory(struct plugin_manager_config *config);
int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *prefix, const char *file); int plugin_mangager_config_parse(struct plugin_manager_config *config, const char *file);
void plugin_mangager_config_dump(struct plugin_manager_config *config); void plugin_mangager_config_dump(struct plugin_manager_config *config);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,11 +1,9 @@
#include <time.h> #include <time.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "sdk/include/plugin.h" #include "sdk/include/plugin.h"
#include "plugin_manager_util.h" #include "plugin_manager_module.h"
#include "plugin_manager_config.h"
#include "plugin_manager.h" #include "plugin_manager.h"
struct plugin_manager_module_evcb struct plugin_manager_module_evcb
@@ -17,12 +15,12 @@ struct plugin_manager_module_evcb
struct plugin_manager_module struct plugin_manager_module
{ {
char *prefix;
char *lib_path; char *lib_path;
void *dl_handle; void *dl_handle;
plugin_init_callback *init_cb_ptr; plugin_init_callback *init_cb_ptr;
plugin_exit_callback *exit_cb_ptr; plugin_exit_callback *exit_cb_ptr;
fn_session_error_callback *error_cb_ptr;
struct plugin_manager_module_evcb *evcbs; struct plugin_manager_module_evcb *evcbs;
int evcbs_num; int evcbs_num;
@@ -43,7 +41,6 @@ void plugin_manager_module_close(struct plugin_manager_module *module)
module->dl_handle = NULL; module->dl_handle = NULL;
} }
safe_free(module->prefix);
safe_free(module->lib_path); safe_free(module->lib_path);
safe_free(module->evcbs); safe_free(module->evcbs);
module->evcbs_num = 0; module->evcbs_num = 0;
@@ -59,18 +56,16 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c
return NULL; return NULL;
} }
char dynamic_lib_path[4096] = {0};
struct plugin_manager_module *module = safe_alloc(struct plugin_manager_module, 1); struct plugin_manager_module *module = safe_alloc(struct plugin_manager_module, 1);
module->prefix = safe_dup(config->prefix_path);
module->lib_path = safe_dup(config->plugin_section.lib_path); module->lib_path = safe_dup(config->plugin_section.lib_path);
strcat_prefix_and_file(module->prefix, module->lib_path, dynamic_lib_path, sizeof(dynamic_lib_path));
// RTLD_NOW | RTLD_GLOBAL
module->evcbs_num = 0; module->evcbs_num = 0;
module->dl_handle = dlopen(dynamic_lib_path, RTLD_LAZY | RTLD_GLOBAL | RTLD_DEEPBIND); module->dl_handle = dlopen(module->lib_path, RTLD_NOW | RTLD_GLOBAL | RTLD_DEEPBIND);
if (module->dl_handle == NULL) if (module->dl_handle == NULL)
{ {
plugin_manager_log(ERROR, "can't dlopen %s, %s", dynamic_lib_path, dlerror()); plugin_manager_log(ERROR, "can't dlopen %s, %s", module->lib_path, dlerror());
goto err; goto err;
} }
@@ -78,7 +73,7 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c
if (module->init_cb_ptr == NULL) if (module->init_cb_ptr == NULL)
{ {
plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s",
config->plugin_section.init_func_name, dynamic_lib_path, dlerror()); config->plugin_section.init_func_name, module->lib_path, dlerror());
goto err; goto err;
} }
@@ -86,7 +81,15 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c
if (module->exit_cb_ptr == NULL) if (module->exit_cb_ptr == NULL)
{ {
plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s",
config->plugin_section.exit_func_name, dynamic_lib_path, dlerror()); config->plugin_section.exit_func_name, module->lib_path, dlerror());
goto err;
}
module->error_cb_ptr = (fn_session_error_callback *)(dlsym(module->dl_handle, config->plugin_section.error_func_name));
if (module->error_cb_ptr == NULL)
{
plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s",
config->plugin_section.error_func_name, module->lib_path, dlerror());
goto err; goto err;
} }
@@ -106,12 +109,14 @@ struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_c
if (event_cb->event_cb_ptr == NULL) if (event_cb->event_cb_ptr == NULL)
{ {
plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s", plugin_manager_log(ERROR, "can't find symbol name of '%s' in dynamic library %s, %s",
session_config->cb_func_name, dynamic_lib_path, dlerror()); session_config->cb_func_name, module->lib_path, dlerror());
goto err; goto err;
} }
} }
} }
plugin_manager_log(INFO, "plugin dynamic library '%s' dlopen success", module->lib_path);
return module; return module;
err: err:
@@ -120,6 +125,43 @@ err:
return NULL; return NULL;
} }
int plugin_manager_module_init(struct plugin_manager_module *module)
{
struct timespec start;
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &start);
int ret = module->init_cb_ptr();
clock_gettime(CLOCK_MONOTONIC, &end);
if (ret == -1)
{
plugin_manager_log(ERROR, "dynamic library '%s' initialization failed", module->lib_path);
return -1;
}
long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
plugin_manager_log(INFO, "plugin dynamic library '%s' init success, using '%lld' us", module->lib_path, elapsed);
return 0;
}
void plugin_manager_module_exit(struct plugin_manager_module *module)
{
struct timespec start;
struct timespec end;
if (module && module->exit_cb_ptr)
{
clock_gettime(CLOCK_MONOTONIC, &start);
module->exit_cb_ptr();
clock_gettime(CLOCK_MONOTONIC, &end);
module->exit_cb_ptr = NULL;
long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
plugin_manager_log(INFO, "plugin dynamic library '%s' exit success, using '%lld' us", module->lib_path, elapsed);
}
}
int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module) int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module)
{ {
if (module && module->evcbs) if (module && module->evcbs)
@@ -128,7 +170,7 @@ int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugi
{ {
struct plugin_manager_module_evcb *event_cb = &module->evcbs[i]; struct plugin_manager_module_evcb *event_cb = &module->evcbs[i];
if (plugin_manager_register(plug_mgr, event_cb->session_name, event_cb->event, event_cb->event_cb_ptr) == -1) if (plugin_manager_register(plug_mgr, event_cb->session_name, event_cb->event, event_cb->event_cb_ptr, module->error_cb_ptr) == -1)
{ {
plugin_manager_log(ERROR, "dynamic library '%s' failed to register the event callback function of session '%s'", module->lib_path, event_cb->session_name); plugin_manager_log(ERROR, "dynamic library '%s' failed to register the event callback function of session '%s'", module->lib_path, event_cb->session_name);
return -1; return -1;
@@ -139,30 +181,23 @@ int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugi
return 0; return 0;
} }
int plugin_manager_module_init(struct plugin_manager_module *module) void plugin_manager_module_dump(struct plugin_manager_module *module, struct plugin_manager_config *config)
{ {
struct timespec start; if (module)
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &start);
int ret = module->init_cb_ptr();
if (ret == -1)
{ {
plugin_manager_log(ERROR, "dynamic library '%s' initialization failed", module->lib_path); plugin_manager_log(DEBUG, "[LIBRARY] : %s, %p", config->plugin_section.lib_path, module->dl_handle);
return -1; plugin_manager_log(DEBUG, "[INIT_FUNC] : %s, %p", config->plugin_section.init_func_name, module->init_cb_ptr);
plugin_manager_log(DEBUG, "[EXIT_FUNC] : %s, %p", config->plugin_section.exit_func_name, module->exit_cb_ptr);
plugin_manager_log(DEBUG, "[ERROR_FUNC] : %s, %p", config->plugin_section.error_func_name, module->error_cb_ptr);
for (int i = 0; i < module->evcbs_num; i++)
{
struct session_section_config *session_config = &config->session_section[i];
struct plugin_manager_module_evcb *event_cb = &module->evcbs[i];
char event_str_buffer[1024] = {0};
session_event_type_int2str(event_cb->event, event_str_buffer, 1024);
plugin_manager_log(DEBUG, "[EVENT_FUNC] : %s, %p, %s, (%d: %s)", session_config->cb_func_name, event_cb->event_cb_ptr, event_cb->session_name, event_cb->event, event_str_buffer);
} }
clock_gettime(CLOCK_MONOTONIC, &end);
long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
plugin_manager_log(INFO, "plugin '%s' init success, using '%lld' us", module->lib_path, elapsed);
return 0;
}
void plugin_manager_module_exit(struct plugin_manager_module *module)
{
if (module && module->exit_cb_ptr)
{
module->exit_cb_ptr();
} }
} }

View File

@@ -1,12 +1,26 @@
#pragma once #ifndef _PLUGIN_MANAGER_MODULE_H
#define _PLUGIN_MANAGER_MODULE_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include "plugin_manager_config.h" #include "plugin_manager_config.h"
struct plugin_manager_module; struct plugin_manager_module;
struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_config *config); struct plugin_manager_module *plugin_manager_module_open(struct plugin_manager_config *config);
int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module);
int plugin_manager_module_init(struct plugin_manager_module *module);
void plugin_manager_module_exit(struct plugin_manager_module *module);
void plugin_manager_module_close(struct plugin_manager_module *module); void plugin_manager_module_close(struct plugin_manager_module *module);
int plugin_manager_module_init(struct plugin_manager_module *module);
void plugin_manager_module_exit(struct plugin_manager_module *module);
void plugin_manager_module_dump(struct plugin_manager_module *module, struct plugin_manager_config *config);
int plugin_manager_module_register(struct plugin_manager *plug_mgr, struct plugin_manager_module *module);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -1,8 +1,11 @@
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "plugin_manager_util.h" #include "plugin_manager_util.h"
/******************************************************************************
* Malloc
******************************************************************************/
char *safe_dup(const char *str) char *safe_dup(const char *str)
{ {
if (str == NULL) if (str == NULL)
@@ -16,25 +19,81 @@ char *safe_dup(const char *str)
return dup; return dup;
} }
void strcat_prefix_and_file(const char *prefix, const char *file, char *buff, int size) /******************************************************************************
* Session Event Type
******************************************************************************/
struct event_type_map
{ {
char prefix_buffer[4096] = {0}; const char *type_str;
char file_buffer[4096] = {0}; enum session_event_type type_int;
char *ptr = NULL; };
memcpy(prefix_buffer, prefix, strlen(prefix)); static struct event_type_map evtype_map[] =
memcpy(file_buffer, file, strlen(file));
if (prefix_buffer[strlen(prefix_buffer) - 1] != '/')
{ {
prefix_buffer[strlen(prefix_buffer)] = '/'; {"SESSION_EVENT_UNKNOWN", SESSION_EVENT_UNKNOWN},
} {"SESSION_EVENT_OPENING", SESSION_EVENT_OPENING},
file_buffer[strcspn(file_buffer, "\r\n")] = 0; {"SESSION_EVENT_RAWPKT", SESSION_EVENT_RAWPKT},
{"SESSION_EVENT_ORDPKT", SESSION_EVENT_ORDPKT},
{"SESSION_EVENT_META", SESSION_EVENT_META},
{"SESSION_EVENT_CLOSING", SESSION_EVENT_CLOSING},
{"SESSION_EVENT_ALL", SESSION_EVENT_ALL},
};
ptr = file_buffer; enum session_event_type session_event_type_str2int(const char *evtype_str)
if (file_buffer[0] == '.' && file_buffer[1] == '/') {
enum session_event_type evtype_int = SESSION_EVENT_UNKNOWN;
int num = sizeof(evtype_map) / sizeof(evtype_map[0]);
char *buffer = safe_dup(evtype_str);
char *token = strtok(buffer, "|");
while (token)
{ {
ptr += 2; for (int i = 0; i < num; i++)
{
if (strcmp(token, evtype_map[i].type_str) == 0)
{
evtype_int = (enum session_event_type)(evtype_int | evtype_map[i].type_int);
}
}
token = strtok(NULL, "|");
}
safe_free(buffer);
return evtype_int;
}
void session_event_type_int2str(enum session_event_type evtype_int, char *buffer, int size)
{
int used = 0;
int num = sizeof(evtype_map) / sizeof(evtype_map[0]);
if (evtype_int == SESSION_EVENT_UNKNOWN)
{
snprintf(buffer, size, "%s", "SESSION_EVENT_UNKNOWN");
return;
}
if (evtype_int == SESSION_EVENT_ALL)
{
snprintf(buffer, size, "%s", "SESSION_EVENT_ALL");
return;
}
for (int i = 0; i < num; i++)
{
if (evtype_map[i].type_int & evtype_int)
{
if (evtype_map[i].type_int == SESSION_EVENT_ALL && evtype_int != SESSION_EVENT_ALL)
{
continue;
}
used += snprintf(buffer + used, size - used, "%s|", evtype_map[i].type_str);
}
}
if (used)
{
buffer[used - 1] = '\0';
} }
snprintf(buff, size, "%s%s", prefix_buffer, ptr);
} }

View File

@@ -1,6 +1,15 @@
#pragma once #ifndef _PLUGIN_MANAGER_UTIL_H
#define _PLUGIN_MANAGER_UTIL_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "sdk/include/session.h"
#define MAX_PLUGIN_NUM 512 #define MAX_PLUGIN_NUM 512
#define MAX_SESSION_NAME_LENGTH 32 #define MAX_SESSION_NAME_LENGTH 32
@@ -55,7 +64,14 @@ enum plugin_manager_log_level
#endif #endif
/****************************************************************************** /******************************************************************************
* Str * Session Event Type
******************************************************************************/ ******************************************************************************/
void strcat_prefix_and_file(const char *prefix, const char *file, char *buff, int size); enum session_event_type session_event_type_str2int(const char *evtype_str);
void session_event_type_int2str(enum session_event_type evtype_int, char *buffer, int size);
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -11,6 +11,9 @@ target_link_libraries(
dl dl
) )
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--export-dynamic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--export-dynamic")
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(gtest_plugin_manager) gtest_discover_tests(gtest_plugin_manager)

View File

@@ -1,23 +1,150 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "sdk/include/session.h"
#include "../plugin_manager_util.h"
#include "../plugin_manager_config.h" #include "../plugin_manager_config.h"
#include "../plugin_manager_module.h"
#include "../plugin_manager.h" #include "../plugin_manager.h"
#include "session_manager.h" #include "session_manager.h"
TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_HTTP) /******************************************************************************
* Test plugin_mangager_util API
******************************************************************************/
#if 1
TEST(PLUGIN_MANAGER_TEST, plugin_mangager_util_int2str)
{
enum session_event_type type_int;
char buffer[1024] = {0};
type_int = SESSION_EVENT_UNKNOWN;
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_UNKNOWN");
type_int = SESSION_EVENT_OPENING;
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING");
type_int = SESSION_EVENT_RAWPKT;
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_RAWPKT");
type_int = SESSION_EVENT_ORDPKT;
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_ORDPKT");
type_int = SESSION_EVENT_META;
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_META");
type_int = SESSION_EVENT_CLOSING;
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_CLOSING");
type_int = SESSION_EVENT_ALL;
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_ALL");
type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT);
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT");
type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT);
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT");
type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT | SESSION_EVENT_META);
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT|SESSION_EVENT_META");
type_int = (enum session_event_type)(SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT | SESSION_EVENT_META | SESSION_EVENT_CLOSING);
memset(buffer, 0, sizeof(buffer));
session_event_type_int2str(type_int, buffer, sizeof(buffer));
EXPECT_STREQ(buffer, "SESSION_EVENT_ALL");
}
#endif
#if 1
TEST(PLUGIN_MANAGER_TEST, plugin_mangager_util_str2int)
{
enum session_event_type type_int;
const char *type_str1 = "SESSION_EVENT_UNKNOWN";
const char *type_str2 = "SESSION_EVENT_OPENING";
const char *type_str3 = "SESSION_EVENT_RAWPKT";
const char *type_str4 = "SESSION_EVENT_ORDPKT";
const char *type_str5 = "SESSION_EVENT_META";
const char *type_str6 = "SESSION_EVENT_CLOSING";
const char *type_str7 = "SESSION_EVENT_ALL";
const char *type_str8 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT";
const char *type_str9 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT";
const char *type_str10 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT|SESSION_EVENT_META";
const char *type_str11 = "SESSION_EVENT_OPENING|SESSION_EVENT_RAWPKT|SESSION_EVENT_ORDPKT|SESSION_EVENT_META|SESSION_EVENT_CLOSING";
type_int = session_event_type_str2int(type_str1);
EXPECT_TRUE(type_int == SESSION_EVENT_UNKNOWN);
type_int = session_event_type_str2int(type_str2);
EXPECT_TRUE(type_int == SESSION_EVENT_OPENING);
type_int = session_event_type_str2int(type_str3);
EXPECT_TRUE(type_int == SESSION_EVENT_RAWPKT);
type_int = session_event_type_str2int(type_str4);
EXPECT_TRUE(type_int == SESSION_EVENT_ORDPKT);
type_int = session_event_type_str2int(type_str5);
EXPECT_TRUE(type_int == SESSION_EVENT_META);
type_int = session_event_type_str2int(type_str6);
EXPECT_TRUE(type_int == SESSION_EVENT_CLOSING);
type_int = session_event_type_str2int(type_str7);
EXPECT_TRUE(type_int == SESSION_EVENT_ALL);
type_int = session_event_type_str2int(type_str8);
EXPECT_TRUE(type_int == (SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT));
type_int = session_event_type_str2int(type_str9);
EXPECT_TRUE(type_int == (SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT));
type_int = session_event_type_str2int(type_str10);
EXPECT_TRUE(type_int == (SESSION_EVENT_OPENING | SESSION_EVENT_RAWPKT | SESSION_EVENT_ORDPKT | SESSION_EVENT_META));
type_int = session_event_type_str2int(type_str11);
EXPECT_TRUE(type_int == SESSION_EVENT_ALL);
}
#endif
/******************************************************************************
* Test plugin_mangager_config API
******************************************************************************/
#if 1
TEST(PLUGIN_MANAGER_TEST, plugin_mangager_config_HTTP)
{ {
char prefix_path[] = "./";
char file_path[] = "./plugins_config/http_event_plugin/http_event_plugin.inf"; char file_path[] = "./plugins_config/http_event_plugin/http_event_plugin.inf";
struct plugin_manager_config *config = plugin_mangager_config_create(); struct plugin_manager_config *config = plugin_mangager_config_create();
EXPECT_TRUE(config != nullptr); EXPECT_TRUE(config != nullptr);
EXPECT_TRUE(plugin_mangager_config_parse(config, prefix_path, file_path) == 0); EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0);
EXPECT_STREQ(config->prefix_path, "./");
EXPECT_STREQ(config->file_path, "./plugins_config/http_event_plugin/http_event_plugin.inf"); EXPECT_STREQ(config->file_path, "./plugins_config/http_event_plugin/http_event_plugin.inf");
EXPECT_STREQ(config->plugin_section.init_func_name, "http_event_plugin_init"); EXPECT_STREQ(config->plugin_section.init_func_name, "http_event_plugin_init");
EXPECT_STREQ(config->plugin_section.exit_func_name, "http_event_plugin_exit"); EXPECT_STREQ(config->plugin_section.exit_func_name, "http_event_plugin_exit");
EXPECT_STREQ(config->plugin_section.error_func_name, "http_event_plugin_error");
EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/http_event_plugin_test.so"); EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/http_event_plugin_test.so");
EXPECT_TRUE(config->session_section_num == 1); EXPECT_TRUE(config->session_section_num == 1);
@@ -28,127 +155,421 @@ TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_HTTP)
plugin_mangager_config_dump(config); plugin_mangager_config_dump(config);
plugin_mangager_config_destory(config); plugin_mangager_config_destory(config);
} }
#endif
TEST(PLUGIN_MANAGER_TEST, plugin_manager_config_CUSTOM) #if 1
TEST(PLUGIN_MANAGER_TEST, plugin_mangager_config_CUSTOM)
{ {
char prefix_path[] = "./";
char file_path[] = "./plugins_config/custom_event_plugin/custom_event_plugin.inf"; char file_path[] = "./plugins_config/custom_event_plugin/custom_event_plugin.inf";
struct plugin_manager_config *config = plugin_mangager_config_create(); struct plugin_manager_config *config = plugin_mangager_config_create();
EXPECT_TRUE(config != nullptr); EXPECT_TRUE(config != nullptr);
EXPECT_TRUE(plugin_mangager_config_parse(config, prefix_path, file_path) == 0); EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0);
EXPECT_STREQ(config->prefix_path, "./");
EXPECT_STREQ(config->file_path, "./plugins_config/custom_event_plugin/custom_event_plugin.inf"); EXPECT_STREQ(config->file_path, "./plugins_config/custom_event_plugin/custom_event_plugin.inf");
EXPECT_STREQ(config->plugin_section.init_func_name, "custom_plugin_init"); EXPECT_STREQ(config->plugin_section.init_func_name, "custom_event_plugin_init");
EXPECT_STREQ(config->plugin_section.exit_func_name, "custom_plugin_exit"); EXPECT_STREQ(config->plugin_section.exit_func_name, "custom_event_plugin_exit");
EXPECT_STREQ(config->plugin_section.error_func_name, "custom_event_plugin_error");
EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/custom_event_plugin_test.so"); EXPECT_STREQ(config->plugin_section.lib_path, "./test_plugins/plugins_library/custom_event_plugin_test.so");
EXPECT_TRUE(config->session_section_num == 2); EXPECT_TRUE(config->session_section_num == 3);
EXPECT_STREQ(config->session_section[0].session_name, "TCP"); EXPECT_STREQ(config->session_section[0].session_name, "TCP");
EXPECT_STREQ(config->session_section[0].cb_func_name, "custom_plugin_tcp_entry"); EXPECT_STREQ(config->session_section[0].cb_func_name, "custom_event_plugin_tcp_entry");
EXPECT_TRUE(config->session_section[0].event == (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5)); EXPECT_TRUE(config->session_section[0].event == (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5));
EXPECT_STREQ(config->session_section[1].session_name, "CUSTOM");
EXPECT_STREQ(config->session_section[1].cb_func_name, "custom_plugin_custom_entry"); EXPECT_STREQ(config->session_section[1].session_name, "HTTP");
EXPECT_TRUE(config->session_section[1].event == (0x01 << 1) | (0x01 << 3) | (0x01 << 5)); EXPECT_STREQ(config->session_section[1].cb_func_name, "custom_event_plugin_http_entry");
EXPECT_TRUE(config->session_section[1].event == (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5));
EXPECT_STREQ(config->session_section[2].session_name, "CUSTOM");
EXPECT_STREQ(config->session_section[2].cb_func_name, "custom_event_plugin_custom_entry");
EXPECT_TRUE(config->session_section[2].event == (0x01 << 1) | (0x01 << 3) | (0x01 << 5));
plugin_mangager_config_dump(config); plugin_mangager_config_dump(config);
plugin_mangager_config_destory(config); plugin_mangager_config_destory(config);
} }
#endif
/******************************************************************************
* Test plugin_mangager_module API
******************************************************************************/
#if 1
TEST(PLUGIN_MANAGER_TEST, plugin_mangager_module_HTTP)
{
char file_path[] = "./plugins_config/http_event_plugin/http_event_plugin.inf";
struct plugin_manager_config *config = plugin_mangager_config_create();
EXPECT_TRUE(config != nullptr);
EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0);
struct plugin_manager_module *module = plugin_manager_module_open(config);
EXPECT_TRUE(module != nullptr);
EXPECT_TRUE(plugin_manager_module_init(module) == 0);
plugin_manager_module_dump(module, config);
plugin_manager_module_exit(module);
plugin_manager_module_close(module);
plugin_mangager_config_destory(config);
}
#endif
#if 1
TEST(PLUGIN_MANAGER_TEST, plugin_mangager_module_CUSTOM)
{
char file_path[] = "./plugins_config/custom_event_plugin/custom_event_plugin.inf";
struct plugin_manager_config *config = plugin_mangager_config_create();
EXPECT_TRUE(config != nullptr);
EXPECT_TRUE(plugin_mangager_config_parse(config, file_path) == 0);
struct plugin_manager_module *module = plugin_manager_module_open(config);
EXPECT_TRUE(module != nullptr);
EXPECT_TRUE(plugin_manager_module_init(module) == 0);
plugin_manager_module_dump(module, config);
plugin_manager_module_exit(module);
plugin_manager_module_close(module);
plugin_mangager_config_destory(config);
}
#endif
/******************************************************************************
* Test plugin_mangager API
******************************************************************************/
#if 1
TEST(PLUGIN_MANAGER_TEST, plugin_manager_load) TEST(PLUGIN_MANAGER_TEST, plugin_manager_load)
{ {
char prefix_path[] = "./";
char file_path[] = "./plugins_config/plugins.inf"; char file_path[] = "./plugins_config/plugins.inf";
struct plugin_manager *plug_mgr = plugin_manager_create(); struct plugin_manager *plug_mgr = plugin_manager_create();
EXPECT_TRUE(plug_mgr != nullptr); EXPECT_TRUE(plug_mgr != nullptr);
EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0);
plugin_manager_unload(plug_mgr);
plugin_manager_destory(plug_mgr);
}
TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP)
{
char prefix_path[] = "./";
char file_path[] = "./plugins_config/plugins.inf";
const char *session_name = "HTTP";
struct stellar_session session;
session.name = session_name;
struct stellar_session_event_data event_data;
event_data.s = &session;
event_data.plugin_ctx = NULL; // must be init to NULL
struct stellar_event event;
event.session_event_data = &event_data;
struct plugin_manager *plug_mgr = plugin_manager_create();
EXPECT_TRUE(plug_mgr != nullptr);
EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0);
event_data.type = SESSION_EVENT_OPENING;
plugin_manager_dispatch(plug_mgr, &event);
event_data.type = SESSION_EVENT_RAWPKT;
plugin_manager_dispatch(plug_mgr, &event);
event_data.type = SESSION_EVENT_ORDPKT;
plugin_manager_dispatch(plug_mgr, &event);
event_data.type = SESSION_EVENT_META;
plugin_manager_dispatch(plug_mgr, &event);
event_data.type = SESSION_EVENT_CLOSING;
plugin_manager_dispatch(plug_mgr, &event);
event_data.type = SESSION_EVENT_ALL;
plugin_manager_dispatch(plug_mgr, &event);
plugin_manager_unload(plug_mgr); plugin_manager_unload(plug_mgr);
plugin_manager_destory(plug_mgr); plugin_manager_destory(plug_mgr);
} }
#endif
#if 1
// only SESSION_EVENT_OPENING | SESSION_EVENT_ORDPKT | SESSION_EVENT_CLOSING can trigger event callback
TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_CUSTOM) TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_CUSTOM)
{ {
char prefix_path[] = "./"; /*
* [SESSION_NAME.CUSTOM]
* SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"]
* SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry"
*/
struct custom_session_pme
{
char data[64];
int flags;
/* data */
};
struct custom_session_pme *pme;
char file_path[] = "./plugins_config/plugins.inf"; char file_path[] = "./plugins_config/plugins.inf";
const char *session_name = "CUSTOM"; const char *session_name = "CUSTOM";
struct stellar_session session; struct stellar_session session;
session.name = session_name; session.name = session_name;
struct stellar_session_event_data event_data; struct stellar_session_event_data event_data;
event_data.s = &session; event_data.s = &session;
event_data.plugin_ctx = NULL; // must be init to NULL event_data.plugin_ctx = NULL; // must be init to NULL
struct stellar_event event; struct stellar_event event;
event.session_event_data = &event_data; event.session_event_data = &event_data;
session.event_data = &event_data; // must be set
struct plugin_manager *plug_mgr = plugin_manager_create(); struct plugin_manager *plug_mgr = plugin_manager_create();
EXPECT_TRUE(plug_mgr != nullptr); EXPECT_TRUE(plug_mgr != nullptr);
EXPECT_TRUE(plugin_manager_load(plug_mgr, prefix_path, file_path) == 0); EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0);
// run evencb
event_data.type = SESSION_EVENT_OPENING; event_data.type = SESSION_EVENT_OPENING;
plugin_manager_dispatch(plug_mgr, &event); plugin_manager_dispatch(plug_mgr, &event);
pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING);
EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry");
// unrun evencb
event_data.type = SESSION_EVENT_RAWPKT; event_data.type = SESSION_EVENT_RAWPKT;
plugin_manager_dispatch(plug_mgr, &event); plugin_manager_dispatch(plug_mgr, &event);
pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING);
EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry");
// run evencb
event_data.type = SESSION_EVENT_ORDPKT; event_data.type = SESSION_EVENT_ORDPKT;
plugin_manager_dispatch(plug_mgr, &event); plugin_manager_dispatch(plug_mgr, &event);
pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT);
EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry");
// unrun evencb
event_data.type = SESSION_EVENT_META; event_data.type = SESSION_EVENT_META;
plugin_manager_dispatch(plug_mgr, &event); plugin_manager_dispatch(plug_mgr, &event);
pme = (struct custom_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT);
EXPECT_STREQ(pme->data, "custom_event_plugin_custom_entry");
// run evencb
event_data.type = SESSION_EVENT_CLOSING; event_data.type = SESSION_EVENT_CLOSING;
plugin_manager_dispatch(plug_mgr, &event); plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
// run evencb
event_data.type = SESSION_EVENT_ALL; event_data.type = SESSION_EVENT_ALL;
plugin_manager_dispatch(plug_mgr, &event); plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
plugin_manager_unload(plug_mgr); plugin_manager_unload(plug_mgr);
plugin_manager_destory(plug_mgr); plugin_manager_destory(plug_mgr);
} }
#endif
#if 1
// ALL SESSION_EVENT can trigger event callback
TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_TCP)
{
/*
* [SESSION_NAME.TCP]
* SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
* SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry"
*/
struct tcp_session_pme
{
char data[64];
int flags;
/* data */
};
struct tcp_session_pme *pme;
char file_path[] = "./plugins_config/plugins.inf";
const char *session_name = "TCP";
struct stellar_session session;
session.name = session_name;
struct stellar_session_event_data event_data;
event_data.s = &session;
event_data.plugin_ctx = NULL; // must be init to NULL
struct stellar_event event;
event.session_event_data = &event_data;
session.event_data = &event_data; // must be set
struct plugin_manager *plug_mgr = plugin_manager_create();
EXPECT_TRUE(plug_mgr != nullptr);
EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0);
// run evencb
event_data.type = SESSION_EVENT_OPENING;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING);
EXPECT_STREQ(pme->data, "tcp***");
// run evencb
event_data.type = SESSION_EVENT_RAWPKT;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_RAWPKT);
EXPECT_STREQ(pme->data, "tcp***");
// run evencb
event_data.type = SESSION_EVENT_ORDPKT;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT);
EXPECT_STREQ(pme->data, "tcp***");
// run evencb
event_data.type = SESSION_EVENT_META;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct tcp_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_META);
EXPECT_STREQ(pme->data, "tcp***");
// run evencb
event_data.type = SESSION_EVENT_CLOSING;
plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
// run evencb
event_data.type = SESSION_EVENT_ALL;
plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
plugin_manager_unload(plug_mgr);
plugin_manager_destory(plug_mgr);
}
#endif
#if 1
// http_event_plugin_entry + SESSION_EVENT_RAWPKT ==> pm_session_dettach_me
TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_dettach_me)
{
/*
* [SESSION_NAME.HTTP]
* SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
* SESSION_EVENT_CALLBACK="http_event_plugin_entry"
*
* [SESSION_NAME.HTTP]
* SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
* SESSION_EVENT_CALLBACK="custom_event_plugin_http_entry"
*/
struct http_session_pme
{
char data[64];
int flags;
/* data */;
};
struct http_session_pme *pme = NULL;
char file_path[] = "./plugins_config/plugins.inf";
const char *session_name = "HTTP";
struct stellar_session session;
session.name = session_name;
struct stellar_session_event_data event_data;
event_data.s = &session;
event_data.plugin_ctx = NULL; // must be init to NULL
struct stellar_event event;
event.session_event_data = &event_data;
session.event_data = &event_data; // must be set
struct plugin_manager *plug_mgr = plugin_manager_create();
EXPECT_TRUE(plug_mgr != nullptr);
EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0);
// run evencb
event_data.type = SESSION_EVENT_OPENING;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING);
EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry");
// http_event_plugin_entry + SESSION_EVENT_RAWPKT ==> pm_session_dettach_me
event_data.type = SESSION_EVENT_RAWPKT;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_RAWPKT);
EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry");
// run evencb
event_data.type = SESSION_EVENT_META;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_META);
EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry");
// run evencb
event_data.type = SESSION_EVENT_CLOSING;
plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
// run evencb
event_data.type = SESSION_EVENT_ALL;
plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
plugin_manager_unload(plug_mgr);
plugin_manager_destory(plug_mgr);
}
#endif
#if 1
// http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_dettach_others
TEST(PLUGIN_MANAGER_TEST, plugin_manager_dispatch_HTTP_dettach_other)
{
/*
* [SESSION_NAME.HTTP]
* SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
* SESSION_EVENT_CALLBACK="http_event_plugin_entry"
*
* [SESSION_NAME.HTTP]
* SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
* SESSION_EVENT_CALLBACK="custom_event_plugin_http_entry"
*/
struct http_session_pme
{
char data[64];
int flags;
/* data */;
};
struct http_session_pme *pme = NULL;
char file_path[] = "./plugins_config/plugins.inf";
const char *session_name = "HTTP";
struct stellar_session session;
session.name = session_name;
struct stellar_session_event_data event_data;
event_data.s = &session;
event_data.plugin_ctx = NULL; // must be init to NULL
struct stellar_event event;
event.session_event_data = &event_data;
session.event_data = &event_data; // must be set
struct plugin_manager *plug_mgr = plugin_manager_create();
EXPECT_TRUE(plug_mgr != nullptr);
EXPECT_TRUE(plugin_manager_load(plug_mgr, file_path) == 0);
// run evencb
event_data.type = SESSION_EVENT_OPENING;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_OPENING);
EXPECT_STREQ(pme->data, "custom_event_plugin_http_entry");
// http_event_plugin_entry + SESSION_EVENT_ORDPKT ==> pm_session_dettach_others
event_data.type = SESSION_EVENT_ORDPKT;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_ORDPKT);
EXPECT_STREQ(pme->data, "http_event_plugin_entry");
// run evencb
event_data.type = SESSION_EVENT_META;
plugin_manager_dispatch(plug_mgr, &event);
pme = (struct http_session_pme *)pm_session_get_plugin_pme(&session);
EXPECT_TRUE(pme->flags == SESSION_EVENT_META);
EXPECT_STREQ(pme->data, "http_event_plugin_entry");
// run evencb
event_data.type = SESSION_EVENT_CLOSING;
plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
// run evencb
event_data.type = SESSION_EVENT_ALL;
plugin_manager_dispatch(plug_mgr, &event);
EXPECT_TRUE(stellar_session_get_plugin_ctx(&session) == nullptr);
plugin_manager_unload(plug_mgr);
plugin_manager_destory(plug_mgr);
}
#endif
int main(int argc, char **argv) int main(int argc, char **argv)
{ {

View File

@@ -1,14 +1,19 @@
[PLUGINFO] [PLUGINFO]
INIT_FUNC="custom_plugin_init" INIT_FUNC="custom_event_plugin_init"
EXIT_FUNC="custom_plugin_exit" EXIT_FUNC="custom_event_plugin_exit"
ERROR_FUNC="custom_event_plugin_error"
LIBRARY_PATH="./test_plugins/plugins_library/custom_event_plugin_test.so" LIBRARY_PATH="./test_plugins/plugins_library/custom_event_plugin_test.so"
# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL"
[SESSION_NAME.TCP] [SESSION_NAME.TCP]
SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"] SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
SESSION_EVENT_CALLBACK="custom_plugin_tcp_entry" SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry"
[SESSION_NAME.HTTP]
SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
SESSION_EVENT_CALLBACK="custom_event_plugin_http_entry"
[SESSION_NAME.CUSTOM] [SESSION_NAME.CUSTOM]
SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"] SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"]
SESSION_EVENT_CALLBACK="custom_plugin_custom_entry" SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry"

View File

@@ -1,6 +1,7 @@
[PLUGINFO] [PLUGINFO]
INIT_FUNC="http_event_plugin_init" INIT_FUNC="http_event_plugin_init"
EXIT_FUNC="http_event_plugin_exit" EXIT_FUNC="http_event_plugin_exit"
ERROR_FUNC="http_event_plugin_error"
LIBRARY_PATH="./test_plugins/plugins_library/http_event_plugin_test.so" LIBRARY_PATH="./test_plugins/plugins_library/http_event_plugin_test.so"
# Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL" # Support SESSION_EVENT_TYPE: "SESSION_EVENT_OPENING", "SESSION_EVENT_RAWPKT", "SESSION_EVENT_ORDPKT", "SESSION_EVENT_META", "SESSION_EVENT_CLOSING", "SESSION_EVENT_ALL"

View File

@@ -2,9 +2,9 @@
#include "packet.h" #include "packet.h"
#include "plugin.h" #include "plugin.h"
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
static char *g_handler = NULL; static char *g_handler = NULL;
@@ -13,30 +13,214 @@ static void *custom_decode(const char *payload, uint16_t len, void **pme)
return NULL; return NULL;
} }
extern "C" void custom_plugin_tcp_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) struct tcp_session_pme
{ {
char **per_session_pme = (char **)pme; char data[64];
int flags;
/* data */
};
printf("RUN custom_plugin_tcp_entry, event: %d\n", event); struct custom_session_pme
{
char data[64];
int flags;
/* data */
};
struct http_session_pme
{
char data[64];
int flags;
/* data */;
};
static struct tcp_session_pme *tcp_session_pme_create()
{
struct tcp_session_pme *pme = (struct tcp_session_pme *)calloc(1, sizeof(struct tcp_session_pme));
return pme;
}
static void tcp_session_pme_destory(struct tcp_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
static struct custom_session_pme *custom_session_pme_create()
{
struct custom_session_pme *pme = (struct custom_session_pme *)calloc(1, sizeof(struct custom_session_pme));
return pme;
}
static void custom_session_pme_destory(struct custom_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
static struct http_session_pme *http_session_pme_create()
{
struct http_session_pme *pme = (struct http_session_pme *)calloc(1, sizeof(struct http_session_pme));
return pme;
}
static void http_session_pme_destory(struct http_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
extern "C" void custom_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme)
{
if (strcmp(stellar_session_get_name(session), "TCP") == 0)
{
struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme;
printf("RUN custom_event_plugin_error, session_name: 'TCP', error_event_type: %d, pme->data: %s\n", event, (*per_tcp_session_pme) == NULL ? "NULL" : (*per_tcp_session_pme)->data);
tcp_session_pme_destory(*per_tcp_session_pme);
}
if (strcmp(stellar_session_get_name(session), "CUSTOM") == 0)
{
struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme;
printf("RUN custom_event_plugin_error, session_name: 'CUSTOM', error_event_type: %d, pme->data: %s\n", event, (*per_custom_session_pme) == NULL ? "NULL" : (*per_custom_session_pme)->data);
custom_session_pme_destory(*per_custom_session_pme);
}
if (strcmp(stellar_session_get_name(session), "HTTP") == 0)
{
struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
printf("RUN custom_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data);
http_session_pme_destory(*per_http_session_pme);
}
}
extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{
struct tcp_session_pme **per_tcp_session_pme = (struct tcp_session_pme **)pme;
if (event & SESSION_EVENT_OPENING)
{
if (*per_tcp_session_pme == NULL)
{
struct tcp_session_pme *cur_ctx = tcp_session_pme_create();
memcpy(cur_ctx->data, "tcp***", 6);
cur_ctx->flags = SESSION_EVENT_OPENING;
*per_tcp_session_pme = *&cur_ctx;
}
}
if (event & SESSION_EVENT_RAWPKT)
{
(*per_tcp_session_pme)->flags = SESSION_EVENT_RAWPKT;
}
if (event & SESSION_EVENT_ORDPKT)
{
(*per_tcp_session_pme)->flags = SESSION_EVENT_ORDPKT;
struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme); struct stellar_session_event_extras *info = (struct stellar_session_event_extras *)custom_decode(payload, len, pme);
struct stellar_session *new_session = session_manager_session_derive(s, "CUSTOM"); struct stellar_session *new_session = session_manager_session_derive(session, "CUSTOM");
session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info);
session_manager_trigger_event(new_session, SESSION_EVENT_META, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info);
}
if (event & SESSION_EVENT_META)
{
(*per_tcp_session_pme)->flags = SESSION_EVENT_META;
}
if (event & SESSION_EVENT_CLOSING)
{
tcp_session_pme_destory(*per_tcp_session_pme);
*per_tcp_session_pme = NULL;
}
} }
extern "C" void custom_plugin_custom_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) extern "C" void custom_event_plugin_custom_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{ {
char **per_session_pme = (char **)pme; struct custom_session_pme **per_custom_session_pme = (struct custom_session_pme **)pme;
printf("RUN custom_plugin_custom_entry, event: %d\n", event); if (event & SESSION_EVENT_OPENING)
{
if (*per_custom_session_pme == NULL)
{
struct custom_session_pme *cur_ctx = custom_session_pme_create();
memcpy(cur_ctx->data, "custom_event_plugin_custom_entry", strlen("custom_event_plugin_custom_entry"));
cur_ctx->flags = SESSION_EVENT_OPENING;
*per_custom_session_pme = *&cur_ctx;
}
}
if (event & SESSION_EVENT_RAWPKT)
{
(*per_custom_session_pme)->flags = SESSION_EVENT_RAWPKT;
}
if (event & SESSION_EVENT_ORDPKT)
{
(*per_custom_session_pme)->flags = SESSION_EVENT_ORDPKT;
}
if (event & SESSION_EVENT_META)
{
(*per_custom_session_pme)->flags = SESSION_EVENT_META;
}
if (event & SESSION_EVENT_CLOSING)
{
custom_session_pme_destory(*per_custom_session_pme);
*per_custom_session_pme = NULL;
}
} }
extern "C" int custom_plugin_init(void) extern "C" void custom_event_plugin_http_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{ {
printf("RUN custom_plugin_init\n"); struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
if (event & SESSION_EVENT_OPENING)
{
if (*per_http_session_pme == NULL)
{
struct http_session_pme *cur_ctx = http_session_pme_create();
memcpy(cur_ctx->data, "custom_event_plugin_http_entry", strlen("custom_event_plugin_http_entry"));
cur_ctx->flags = SESSION_EVENT_OPENING;
*per_http_session_pme = *&cur_ctx;
}
}
if (event & SESSION_EVENT_RAWPKT)
{
(*per_http_session_pme)->flags = SESSION_EVENT_RAWPKT;
}
if (event & SESSION_EVENT_ORDPKT)
{
(*per_http_session_pme)->flags = SESSION_EVENT_ORDPKT;
}
if (event & SESSION_EVENT_META)
{
(*per_http_session_pme)->flags = SESSION_EVENT_META;
}
if (event & SESSION_EVENT_CLOSING)
{
http_session_pme_destory(*per_http_session_pme);
*per_http_session_pme = NULL;
}
}
extern "C" int custom_event_plugin_init(void)
{
if (g_handler == NULL) if (g_handler == NULL)
{ {
g_handler = (char *)malloc(1024); g_handler = (char *)malloc(1024);
@@ -46,10 +230,8 @@ extern "C" int custom_plugin_init(void)
return 0; return 0;
} }
extern "C" void custom_plugin_exit(void) extern "C" void custom_event_plugin_exit(void)
{ {
printf("RUN custom_plugin_exit\n");
if (g_handler) if (g_handler)
{ {
free(g_handler); free(g_handler);

View File

@@ -4,63 +4,92 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
static char *g_handler = NULL; static char *g_handler = NULL;
struct per_session_pme_info struct http_session_pme
{ {
int flag; char data[64];
char data[16]; int flags;
/* data */;
}; };
extern "C" void http_event_plugin_entry(const struct stellar_session *s, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme) static struct http_session_pme *http_session_pme_create()
{ {
struct per_session_pme_info **per_session_pme = (struct per_session_pme_info **)pme; struct http_session_pme *pme = (struct http_session_pme *)calloc(1, sizeof(struct http_session_pme));
return pme;
}
printf("RUN http_event_plugin_entry, event: %d\n", event); static void http_session_pme_destory(struct http_session_pme *pme)
{
if (pme)
{
free(pme);
pme = NULL;
}
}
extern "C" void http_event_plugin_error(const struct stellar_session *session, enum error_event_type event, void **pme)
{
if (strcmp(stellar_session_get_name(session), "HTTP") == 0)
{
struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
printf("RUN http_event_plugin_error, session_name: 'HTTP', error_event_type: %d, pme->data: %s\n", event, (*per_http_session_pme) == NULL ? "NULL" : (*per_http_session_pme)->data);
http_session_pme_destory(*per_http_session_pme);
}
}
extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
{
struct http_session_pme **per_http_session_pme = (struct http_session_pme **)pme;
if (event & SESSION_EVENT_OPENING) if (event & SESSION_EVENT_OPENING)
{ {
if (*per_session_pme == NULL) if (*per_http_session_pme == NULL)
{ {
struct per_session_pme_info *cur_ctx = (struct per_session_pme_info *)malloc(sizeof(struct per_session_pme_info)); struct http_session_pme *cur_ctx = http_session_pme_create();
snprintf(cur_ctx->data, 6, "******"); memcpy(cur_ctx->data, "http_event_plugin_entry", strlen("http_event_plugin_entry"));
*per_session_pme = *&cur_ctx; cur_ctx->flags = SESSION_EVENT_OPENING;
printf("http_event_plugin_entry->opening_handler\n"); *per_http_session_pme = *&cur_ctx;
} }
} }
if (event & SESSION_EVENT_RAWPKT) if (event & SESSION_EVENT_RAWPKT)
{ {
printf("http_event_plugin_entry->rawpkt_handler\n"); /*
* Note: pm_session_dettach_me()
* The plugin manager just set the skip flag and don't call this event callback next.
* Before calling pm_session_dettach_me, the current plugin must release related resources for the current session.
*/
http_session_pme_destory(*per_http_session_pme);
*per_http_session_pme = NULL;
pm_session_dettach_me(session);
return;
} }
if (event & SESSION_EVENT_ORDPKT) if (event & SESSION_EVENT_ORDPKT)
{ {
printf("http_event_plugin_entry->ordpkt_handler\n"); // TODO
(*per_http_session_pme)->flags = SESSION_EVENT_ORDPKT;
pm_session_dettach_others(session);
} }
if (event & SESSION_EVENT_META) if (event & SESSION_EVENT_META)
{ {
printf("http_event_plugin_entry->meta_handler\n"); // TODO
(*per_http_session_pme)->flags = SESSION_EVENT_META;
} }
if (event & SESSION_EVENT_CLOSING) if (event & SESSION_EVENT_CLOSING)
{ {
if (*per_session_pme) http_session_pme_destory(*per_http_session_pme);
{ *per_http_session_pme = NULL;
printf("http_event_plugin_entry->closing_hanler\n");
free(*per_session_pme);
*per_session_pme = NULL;
}
} }
} }
extern "C" int http_event_plugin_init(void) extern "C" int http_event_plugin_init(void)
{ {
printf("RUN http_event_plugin_init\n");
if (g_handler == NULL) if (g_handler == NULL)
{ {
g_handler = (char *)malloc(1024); g_handler = (char *)malloc(1024);
@@ -72,8 +101,6 @@ extern "C" int http_event_plugin_init(void)
extern "C" void http_event_plugin_exit(void) extern "C" void http_event_plugin_exit(void)
{ {
printf("RUN http_event_plugin_exit\n");
if (g_handler) if (g_handler)
{ {
free(g_handler); free(g_handler);

View File

@@ -8,3 +8,7 @@ void http_decoder(const struct stellar_session *s, enum session_event_type event
session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info); session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info);
session_manager_trigger_event(new_session, SESSION_EVENT_META, info); session_manager_trigger_event(new_session, SESSION_EVENT_META, info);
} }
void http_error_cb(const struct stellar_session *s, enum error_event_type event, void **pme)
{
}

View File

@@ -35,7 +35,7 @@ struct stellar_event *session_manager_fetch_event(struct session_manager *h)
} }
/****************************************************************************** /******************************************************************************
* API: between stellar_event and plugin_manager * stellar_event API For Plugin Manager
******************************************************************************/ ******************************************************************************/
struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event) struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event)
@@ -80,3 +80,22 @@ uint16_t stellar_event_get_payload_length(struct stellar_event *event)
{ {
return 0; return 0;
} }
/******************************************************************************
* stellar_session API For Plugin Manager
******************************************************************************/
struct session_plugin_ctx *stellar_session_get_plugin_ctx(const struct stellar_session *session)
{
struct stellar_session_event_data *evdata = session->event_data;
return evdata->plugin_ctx;
}
/******************************************************************************
* stellar_session API For Plugin
******************************************************************************/
const char *stellar_session_get_name(const struct stellar_session *session)
{
return session->name;
}

View File

@@ -22,10 +22,11 @@ struct stellar_session
const char *name; const char *name;
void *addr; void *addr;
void *data; void *data;
struct stellar_session_event_data *event_data;
}; };
/****************************************************************************** /******************************************************************************
* API: between stellar_event and plugin_manager * stellar_event API For Plugin Manager
******************************************************************************/ ******************************************************************************/
struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event); struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event);
@@ -38,3 +39,9 @@ struct stellar_packet *stellar_event_get_packet(struct stellar_event *event);
const char *stellar_event_get_payload(struct stellar_event *event); const char *stellar_event_get_payload(struct stellar_event *event);
uint16_t stellar_event_get_payload_length(struct stellar_event *event); uint16_t stellar_event_get_payload_length(struct stellar_event *event);
/******************************************************************************
* stellar_session API For Plugin Manager
******************************************************************************/
struct session_plugin_ctx *stellar_session_get_plugin_ctx(const struct stellar_session *session);