plugin_manager adapt session_manager API and rewrite plugin_manger gtest

This commit is contained in:
luwenpeng
2022-08-26 17:33:03 +08:00
parent e3ea4d0729
commit 0ed9a0ccca
52 changed files with 1161 additions and 1575 deletions

View File

@@ -1,9 +1,4 @@
add_library(custom_event_plugin SHARED
custom_event_plugin.cpp
add_library(http_plugin SHARED
http_plugin.cpp
)
set_target_properties(custom_event_plugin PROPERTIES PREFIX "")
add_library(http_event_plugin SHARED
http_event_plugin.cpp
)
set_target_properties(http_event_plugin PROPERTIES PREFIX "")
set_target_properties(http_plugin PROPERTIES PREFIX "")

View File

@@ -1,134 +0,0 @@
#include "session.h"
#include "packet.h"
#include "plugin.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static char *g_handler = NULL;
static void *custom_decode(const char *payload, size_t len, void **ctx)
{
return NULL;
}
struct tcp_session_ctx
{
char data[16];
/* data */
};
struct custom_session_ctx
{
char data[16];
/* data */
};
static struct tcp_session_ctx *tcp_session_ctx_create()
{
struct tcp_session_ctx *ctx = (struct tcp_session_ctx *)calloc(1, sizeof(struct tcp_session_ctx));
return ctx;
}
static void tcp_session_ctx_destory(struct tcp_session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
static struct custom_session_ctx *custom_session_ctx_create()
{
struct custom_session_ctx *ctx = (struct custom_session_ctx *)calloc(1, sizeof(struct custom_session_ctx));
return ctx;
}
static void custom_session_ctx_destory(struct custom_session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
extern "C" void custom_event_plugin_tcp_entry(const struct stellar_session *session, enum session_event_type event, const char *payload, size_t len, void **ctx)
{
struct tcp_session_ctx **per_tcp_session_ctx = (struct tcp_session_ctx **)ctx;
printf("RUN custom_event_plugin_tcp_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_tcp_session_ctx == NULL)
{
struct tcp_session_ctx *cur_ctx = tcp_session_ctx_create();
memcpy(cur_ctx->data, "custom_event_plugin_tcp_entry", strlen("custom_event_plugin_tcp_entry"));
*per_tcp_session_ctx = *&cur_ctx;
}
}
if (event & SESSION_EVENT_ORDPKT)
{
struct session_event_extras *info = (struct session_event_extras *)custom_decode(payload, len, ctx);
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_META, info);
}
if (event & SESSION_EVENT_CLOSING)
{
tcp_session_ctx_destory(*per_tcp_session_ctx);
}
}
extern "C" void custom_event_plugin_custom_entry(const struct stellar_session *session, enum session_event_type event, const char *payload, size_t len, void **ctx)
{
struct custom_session_ctx **per_custom_session_ctx = (struct custom_session_ctx **)ctx;
printf("RUN custom_event_plugin_custom_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_custom_session_ctx == NULL)
{
struct custom_session_ctx *cur_ctx = custom_session_ctx_create();
memcpy(cur_ctx->data, "custom_event_plugin_custom_entry", strlen("custom_event_plugin_custom_entry"));
*per_custom_session_ctx = *&cur_ctx;
}
}
if (event & SESSION_EVENT_CLOSING)
{
custom_session_ctx_destory(*per_custom_session_ctx);
}
}
extern "C" int custom_event_plugin_init(void)
{
printf("RUN custom_event_plugin_init\n");
if (g_handler == NULL)
{
g_handler = (char *)malloc(1024);
snprintf(g_handler, 1024, "222222");
}
return 0;
}
extern "C" void custom_event_plugin_exit(void)
{
printf("RUN custom_event_plugin_exit\n");
if (g_handler)
{
free(g_handler);
g_handler = NULL;
}
}

View File

@@ -1,93 +0,0 @@
#include "session.h"
#include "packet.h"
#include "plugin.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static char *g_handler = NULL;
struct http_session_ctx
{
char data[16];
/* data */;
};
static struct http_session_ctx *http_session_ctx_create()
{
struct http_session_ctx *ctx = (struct http_session_ctx *)calloc(1, sizeof(struct http_session_ctx));
return ctx;
}
static void http_session_ctx_destory(struct http_session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
extern "C" void http_event_plugin_entry(const struct stellar_session *session, enum session_event_type event, const char *payload, size_t len, void **ctx)
{
struct http_session_ctx **per_http_session_ctx = (struct http_session_ctx **)ctx;
printf("RUN http_event_plugin_entry, event: %d\n", event);
if (event & SESSION_EVENT_OPENING)
{
if (*per_http_session_ctx == NULL)
{
struct http_session_ctx *cur_ctx = http_session_ctx_create();
memcpy(cur_ctx->data, "http_event_plugin_entry", strlen("http_event_plugin_entry"));
*per_http_session_ctx = *&cur_ctx;
}
}
if (event & SESSION_EVENT_RAWPKT)
{
// TODO
pm_session_dettach_me(session);
}
if (event & SESSION_EVENT_ORDPKT)
{
// TODO
pm_session_take_over(session);
}
if (event & SESSION_EVENT_META)
{
// TODO
}
if (event & SESSION_EVENT_CLOSING)
{
http_session_ctx_destory(*per_http_session_ctx);
}
}
extern "C" int http_event_plugin_init(void)
{
printf("RUN http_event_plugin_init\n");
if (g_handler == NULL)
{
g_handler = (char *)malloc(1024);
snprintf(g_handler, 1024, "111111");
}
return 0;
}
extern "C" void http_event_plugin_exit(void)
{
printf("RUN http_event_plugin_exit\n");
if (g_handler)
{
free(g_handler);
g_handler = NULL;
}
}

View File

@@ -0,0 +1,70 @@
#include <stdlib.h>
#include "session.h"
static char *g_handler = NULL;
struct session_ctx
{
char data[64];
};
static struct session_ctx *session_ctx_create()
{
struct session_ctx *ctx = (struct session_ctx *)calloc(1, sizeof(struct session_ctx));
return ctx;
}
static void session_ctx_destory(struct session_ctx *ctx)
{
if (ctx)
{
free(ctx);
ctx = NULL;
}
}
extern "C" void http_plugin_entry(struct stellar_session *session, enum session_state state, int thread_id, void **ctx)
{
struct session_ctx **sctx = (struct session_ctx **)ctx;
if (state & SESSION_STATE_OPENING)
{
if (*sctx == NULL)
{
struct session_ctx *cur_ctx = session_ctx_create();
memcpy(cur_ctx->data, "http_plugin_entry", 17);
*sctx = *&cur_ctx;
}
}
if (state & SESSION_STATE_ACTIVE)
{
// TODO
}
if (state & SESSION_STATE_CLOSING)
{
session_ctx_destory(*sctx);
*sctx = NULL;
}
}
extern "C" int http_plugin_init(void)
{
if (g_handler == NULL)
{
g_handler = (char *)malloc(1024);
}
return 0;
}
extern "C" void http_plugin_exit(void)
{
if (g_handler)
{
free(g_handler);
g_handler = NULL;
}
}

3
sdk/example/plugins.inf Normal file
View File

@@ -0,0 +1,3 @@
# Relative path, relative to the installation path of stellar
./plugins/http_plugin.inf

View File

@@ -1,14 +0,0 @@
[PLUGINFO]
INIT_FUNC="custom_event_plugin_init"
EXIT_FUNC="custom_event_plugin_exit"
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"
[SESSION_NAME.TCP]
SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
SESSION_EVENT_CALLBACK="custom_event_plugin_tcp_entry"
[SESSION_NAME.CUSTOM]
SESSION_EVENT_TYPE=["SESSION_EVENT_OPENING","SESSION_EVENT_ORDPKT","SESSION_EVENT_CLOSING"]
SESSION_EVENT_CALLBACK="custom_event_plugin_custom_entry"

View File

@@ -1,10 +0,0 @@
[PLUGINFO]
INIT_FUNC="http_event_plugin_init"
EXIT_FUNC="http_event_plugin_exit"
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"
[SESSION_NAME.HTTP]
SESSION_EVENT_TYPE=["SESSION_EVENT_ALL"]
SESSION_EVENT_CALLBACK="http_event_plugin_entry"

View File

@@ -0,0 +1,10 @@
[PLUGINFO]
INIT_FUNC="http_plugin_init"
EXIT_FUNC="http_plugin_exit"
LIBRARY_PATH="./plugins/http_plugin/http_plugin.so"
# Support SESSION_STATE: "SESSION_STATE_OPENING", "SESSION_STATE_ACTIVE", "SESSION_STATE_CLOSING", "SESSION_STATE_ALL"
[SESSION_NAME.HTTP]
SESSION_STATE=["SESSION_STATE_ALL"]
SESSION_ENTRY_CALLBACK="http_plugin_entry"

View File

@@ -1,4 +0,0 @@
# Relative path, relative to the installation path of stellar
./plugins/http_event_plugin/http_event_plugin.inf
./plugins/custom_event_plugin/custom_event_plugin.inf