Refactored plugin management interface
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
[PLUGINFO]
|
||||
INIT_FUNC=custom_plugin_init
|
||||
DESTROY_FUNC=custom_plugin_destroy
|
||||
LIBARY_PATH=./plugins/custom_event_plugin/custom_event_plugin.so
|
||||
EXIT_FUNC=custom_plugin_exit
|
||||
LIBRARY_PATH=./plugins/custom_event_plugin/custom_event_plugin.so
|
||||
|
||||
|
||||
# Support SESSION_EVENT_TYPE
|
||||
# OPENING,RAWPKT,ORDPKT,META,CLOSING,ALL
|
||||
|
||||
[SESSION_TYPE.TCP]
|
||||
SESSION_EVENT_TYPE=ALL
|
||||
SESSION_EVENT_CALLBACK=custom_plugin_entry
|
||||
|
||||
[SESSION_TYPE.CUSTOM_A]
|
||||
SESSION_EVENT_TYPE=ALL
|
||||
SESSION_EVENT_TYPE=OPENING,ORDPKT,CLOSING
|
||||
SESSION_EVENT_CALLBACK=custom_event_plugin_entry
|
||||
@@ -1,9 +1,8 @@
|
||||
[PLUGINFO]
|
||||
INIT_FUNC=http_event_plugin_init
|
||||
DESTROY_FUNC=http_event_plugin_destroy
|
||||
LIBARY_PATH=./plugins/http_event_plugin/http_event_plugin.so
|
||||
EXIT_FUNC=http_event_plugin_exit
|
||||
LIBRARY_PATH=./plugins/http_event_plugin/http_event_plugin.so
|
||||
|
||||
|
||||
[SESSION_TYPE_HTTP]
|
||||
[SESSION_TYPE.HTTP]
|
||||
SESSION_EVENT_TYPE=ALL
|
||||
SESSION_EVENT_CALLBACK=http_event_plugin_entry
|
||||
@@ -3,4 +3,4 @@
|
||||
#include "packet.h"
|
||||
#include "session.h"
|
||||
|
||||
int http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme);
|
||||
void http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme);
|
||||
@@ -2,5 +2,8 @@
|
||||
|
||||
#include "session.h"
|
||||
|
||||
typedef int plugin_init_callback(void **pme);
|
||||
typedef void plugin_exit_callback(void *pme);
|
||||
|
||||
void plugin_remove_from_session_event(struct stellar_event *ev,
|
||||
struct stellar_session *s);
|
||||
@@ -23,22 +23,22 @@ enum stellar_session_type
|
||||
SESSION_TYPE_MAX,
|
||||
};
|
||||
|
||||
enum stellar_session_event_type
|
||||
enum session_event_type
|
||||
{
|
||||
SESSION_EVENT_OPENING = 1<<1,
|
||||
SESSION_EVENT_RAW_PKT = 1<<2,
|
||||
SESSION_EVENT_ORDERED_PKT = 1<<3,
|
||||
SESSION_EVENT_META = 1<<4,
|
||||
SESSION_EVENT_TIMEOUT = 1<<5,
|
||||
SESSION_EVENT_CLOSING = 1<<6,
|
||||
SESSION_EVENT_OPENING = (0x01 << 1),
|
||||
SESSION_EVENT_RAWPKT = (0x01 << 2),
|
||||
SESSION_EVENT_ORDPKT = (0x01 << 3),
|
||||
SESSION_EVENT_META = (0x01 << 4),
|
||||
SESSION_EVENT_CLOSING = (0x01 << 5),
|
||||
SESSION_EVENT_ALL = (0x01 << 1 | 0x01 << 2 | 0x01 << 3 | 0x01 << 4 | 0x01 << 5),
|
||||
};
|
||||
|
||||
struct stellar_session_event_extras;
|
||||
|
||||
typedef int (*fn_session_event_callback)(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme);
|
||||
typedef void (fn_session_event_callback)(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme);
|
||||
|
||||
void session_manager_trigger_event(struct stellar_session *s,
|
||||
stellar_session_event_type type,
|
||||
enum session_event_type type,
|
||||
struct stellar_session_event_extras *info);
|
||||
|
||||
struct stellar_session *session_manager_session_derive(const struct stellar_session *this_session,
|
||||
|
||||
10
src/main.cpp
10
src/main.cpp
@@ -56,12 +56,14 @@ int main(int argc, char ** argv)
|
||||
{
|
||||
//manager_init
|
||||
struct session_manager *session_mgr = session_manager_init();
|
||||
struct plugin_manager *plug_mgr = plugin_manager_init();
|
||||
struct plugin_manager *plug_mgr = plugin_manager_create();
|
||||
|
||||
// register build-in plugin
|
||||
plugin_manager_event_register(plug_mgr, "HTTP", http_decoder, nullptr);
|
||||
plugin_manager_register(plug_mgr, "HTTP", http_decoder);
|
||||
|
||||
// load external plugins
|
||||
plugin_manager_load("./plugins.inf");
|
||||
char absolute_plugin_file[] = "/op/tsg/sapp/plug/plugins.inf";
|
||||
plugin_manager_load(plug_mgr, absolute_plugin_file);
|
||||
|
||||
//packet_io_init
|
||||
struct packet_io_device *dev = packet_io_init(1, "stellar", "cap0");
|
||||
@@ -76,5 +78,7 @@ int main(int argc, char ** argv)
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
plugin_manager_destory(plug_mgr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,97 +1,153 @@
|
||||
#include <sys/queue.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "deps/uthash/uthash.h"
|
||||
#include "sdk/include/session.h"
|
||||
#include "sdk/include/plugin.h"
|
||||
#include "session_manager.h"
|
||||
#include "plugin_manager.h"
|
||||
|
||||
#if 0
|
||||
struct session_event_callback
|
||||
#include <sys/queue.h>
|
||||
#include <assert.h>
|
||||
|
||||
/******************************************************************************
|
||||
* CallBack Static Hash Table (For Global)
|
||||
******************************************************************************/
|
||||
|
||||
struct session_event_callback_static
|
||||
{
|
||||
const char *session_name;
|
||||
fn_session_event_callback callback;
|
||||
TAILQ_ENTRY(session_event_callback) entries;
|
||||
enum session_event_type event;
|
||||
fn_session_event_callback *callback;
|
||||
};
|
||||
|
||||
/*
|
||||
* Hast table
|
||||
*
|
||||
* key: string -> session_type
|
||||
* val: array -> event_cbs
|
||||
*/
|
||||
struct managed_session_event_callback
|
||||
{
|
||||
char session_type[16];
|
||||
struct session_event_callback_static event_cbs[0]; // dynamic array
|
||||
// Record the number of callback functions that want to process the current session
|
||||
int event_cbs_num;
|
||||
UT_hash_handle hh;
|
||||
};
|
||||
|
||||
TAILQ_HEAD(session_event_callback_list, session_event_callback);
|
||||
/******************************************************************************
|
||||
* CallBack Runtime Array (For Per Session)
|
||||
******************************************************************************/
|
||||
|
||||
struct session_event_callback_map
|
||||
struct session_event_callback_runtime
|
||||
{
|
||||
struct session_event_callback_list session_ev_cb_list;
|
||||
UT_hash_handle hh;
|
||||
int skip; // for skip this plugin callback
|
||||
void *callback_args;
|
||||
|
||||
enum session_event_type event;
|
||||
fn_session_event_callback *callback;
|
||||
};
|
||||
|
||||
struct session_plugin_ctx
|
||||
{
|
||||
int event_cbs_num;
|
||||
struct session_event_callback_runtime event_cbs[0]; // dynamic array
|
||||
};
|
||||
|
||||
/******************************************************************************
|
||||
* struct plugin_manager
|
||||
******************************************************************************/
|
||||
|
||||
/*
|
||||
* Each plugin has an init_cb and an exit_cb, but may have multiple entry_cb.
|
||||
* entry_cb is stored in the global_session_callback_ctx.
|
||||
*/
|
||||
struct plugin_ctx
|
||||
{
|
||||
char *name;
|
||||
char *library;
|
||||
/*
|
||||
* Stores the context generated by the plugin initialization,
|
||||
* which is used to release resources when the plugin ends.
|
||||
*/
|
||||
void *pme;
|
||||
plugin_init_callback *init_cb;
|
||||
plugin_exit_callback *exit_cb;
|
||||
};
|
||||
|
||||
struct plugin_manager
|
||||
{
|
||||
struct session_event_callback_list fixed_event_cbs[SESSION_TYPE_MAX];
|
||||
struct session_event_callback_map *session_ev_cb_map;
|
||||
struct managed_session_event_callback *cb_table;
|
||||
struct plugin_ctx plugins[MAX_PLUGIN_NUM];
|
||||
};
|
||||
|
||||
#endif
|
||||
/******************************************************************************
|
||||
* Private API
|
||||
******************************************************************************/
|
||||
|
||||
struct stellar_plugin_ctx
|
||||
static void plugin_manager_handle_opening_event(struct plugin_manager *plug_mgr, struct stellar_event *event)
|
||||
{
|
||||
void *call_back_arg;
|
||||
const struct timeval timeout;
|
||||
fn_session_event_callback call_back;
|
||||
TAILQ_ENTRY(stellar_plugin_ctx) tqe;
|
||||
};
|
||||
|
||||
TAILQ_HEAD(stellar_plugin_ctx_list, stellar_plugin_ctx);
|
||||
|
||||
struct stellar_plugin_data
|
||||
{
|
||||
stellar_plugin_ctx_list plugin_ctx_list;
|
||||
};
|
||||
|
||||
struct plugin_manager_handle *plugin_manager_init()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int plugin_manager_load(const char *plugin_conf_path)
|
||||
static void plugin_manager_handle_data_event(struct plugin_manager *plug_mgr, struct stellar_event *event)
|
||||
{
|
||||
}
|
||||
|
||||
static void plugin_manager_handle_closing_event(struct plugin_manager *plug_mgr, struct stellar_event *event)
|
||||
{
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Public API
|
||||
******************************************************************************/
|
||||
|
||||
struct plugin_manager *plugin_manager_create()
|
||||
{
|
||||
struct plugin_manager *plug_mgr = NULL;
|
||||
|
||||
return plug_mgr;
|
||||
}
|
||||
|
||||
void plugin_manager_destory(struct plugin_manager *plug_mgr)
|
||||
{
|
||||
if (plug_mgr)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
int plugin_manager_load(struct plugin_manager *plug_mgr, const char *plugin_file)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int plugin_manager_event_register(struct plugin_manager *h,
|
||||
const char *session_type_name,
|
||||
fn_session_event_callback call_back,
|
||||
const struct timeval *timeout)
|
||||
void plugin_manager_unload(struct plugin_manager *plug_mgr)
|
||||
{
|
||||
}
|
||||
|
||||
int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_type, fn_session_event_callback *callback)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void plugin_manager_dispatch(struct plugin_manager *h, struct stellar_event *event)
|
||||
void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event)
|
||||
{
|
||||
#if 0
|
||||
struct session *s = container_of(event, struct session, cur_ev);
|
||||
struct session_event_callback_map *t_map;
|
||||
struct session_event_callback *session_ev_cb;
|
||||
HASH_FIND(hh, g_plug_mgr.session_ev_cb_map, s->name, strlen(s->name), t_map);
|
||||
if(!t_map) return;
|
||||
switch (s->state)
|
||||
assert(event);
|
||||
struct stellar_session_event_data *event_data = event->session_event_data;
|
||||
assert(event_data);
|
||||
session_event_type type = event_data->type;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case SESSION_EVENT_OPENING:
|
||||
TAILQ_FOREACH(session_ev_cb, &t_map->session_ev_cb_list, session_event_cb_tq_entries)
|
||||
{
|
||||
struct session_event *ev = ALLOC(struct session_event, 1);
|
||||
ev->callback = session_ev_cb->callback;
|
||||
ev->callback(s, SESSION_EVENT_OPENING, NULL, NULL, 0, &ev->cb_pme);
|
||||
if(ev->state == 1)
|
||||
{
|
||||
TAILQ_INSERT_TAIL(&s->ev_list, ev, session_event_tq_entries);
|
||||
}
|
||||
else
|
||||
{
|
||||
FREE(&ev);// TODO;
|
||||
}
|
||||
}
|
||||
plugin_manager_handle_opening_event(plug_mgr, event);
|
||||
break;
|
||||
case SESSION_EVENT_RAWPKT: /* fall through */
|
||||
case SESSION_EVENT_ORDPKT: /* fall through */
|
||||
case SESSION_EVENT_META: /* fall through */
|
||||
plugin_manager_handle_data_event(plug_mgr, event);
|
||||
break;
|
||||
case SESSION_EVENT_CLOSING:
|
||||
plugin_manager_handle_closing_event(plug_mgr, event);
|
||||
break;
|
||||
default:
|
||||
// TODO log error
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
@@ -1,20 +1,20 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
|
||||
#include "sdk/include/session.h"
|
||||
#include "sdk/include/plugin.h"
|
||||
|
||||
#define MAX_PLUGIN_NUM 256
|
||||
|
||||
struct per_session_event_cbs;
|
||||
struct plugin_manager;
|
||||
struct plugin_manager *plugin_manager_init();
|
||||
|
||||
int plugin_manager_event_register(struct plugin_manager *h,
|
||||
const char *session_type_name,
|
||||
fn_session_event_callback call_back,
|
||||
const struct timeval *timeout);
|
||||
struct plugin_manager *plugin_manager_create();
|
||||
void plugin_manager_destory(struct plugin_manager *plug_mgr);
|
||||
|
||||
int plugin_manager_load(const char *plugin_conf_path);
|
||||
// return 0: success
|
||||
// return -1: error
|
||||
int plugin_manager_load(struct plugin_manager *plug_mgr, const char *plugin_file);
|
||||
void plugin_manager_unload(struct plugin_manager *plug_mgr);
|
||||
|
||||
void plugin_manager_dispatch(struct plugin_manager *h, struct stellar_event *ev);
|
||||
int plugin_manager_register(struct plugin_manager *plug_mgr, const char *session_type, fn_session_event_callback *callback);
|
||||
void plugin_manager_dispatch(struct plugin_manager *plug_mgr, struct stellar_event *event);
|
||||
@@ -1,13 +1,12 @@
|
||||
#include "http.h"
|
||||
#include "session_manager.h"
|
||||
|
||||
int http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint32_t len, void **pme)
|
||||
void http_decoder(const struct stellar_session *s, int what, struct stellar_packet *p, const char *payload, uint16_t len, void **pme)
|
||||
{
|
||||
struct stellar_session_event_extras *info;
|
||||
struct stellar_session *new_session=session_manager_session_derive(s, "HTTP");
|
||||
session_manager_trigger_event(new_session, SESSION_EVENT_OPENING, info);
|
||||
session_manager_trigger_event(new_session, SESSION_EVENT_META, info);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_peek(const struct stellar_session *s, const char *payload, uint32_t len)
|
||||
|
||||
@@ -12,7 +12,7 @@ struct session_manager *session_manager_init()
|
||||
|
||||
|
||||
void session_manager_trigger_event(struct stellar_session *s,
|
||||
stellar_session_event_type type,
|
||||
enum session_event_type type,
|
||||
struct stellar_session_event_extras *info)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
#include "sdk/include/session.h"
|
||||
|
||||
struct stellar_plugin_data;
|
||||
struct per_session_evcb_runtime_ctx;
|
||||
|
||||
struct stellar_session_event_data
|
||||
{
|
||||
struct stellar_session *s;
|
||||
stellar_session_event_type type;
|
||||
enum session_event_type type;
|
||||
long last_active;
|
||||
struct stellar_plugin_data *plugin_data;
|
||||
struct session_plugin_ctx *plugin_ctx;
|
||||
};
|
||||
|
||||
struct session_manager;
|
||||
@@ -18,6 +18,10 @@ struct session_manager *session_manager_init();
|
||||
struct stellar_event *session_manager_commit(struct session_manager *session_mgr, struct stellar_packet *pkt);
|
||||
struct stellar_event *session_manager_fetch_event(struct session_manager *session_mgr);
|
||||
|
||||
// interfaces for data interaction between stellar event and plugin manager
|
||||
struct session_plugin_ctx *stellar_event_get_plugin_ctx(struct stellar_event *event);
|
||||
void stellar_event_set_plugin_ctx(struct stellar_event *event, struct session_plugin_ctx *plugin_ctx);
|
||||
|
||||
struct stellar_session
|
||||
{
|
||||
stellar_session_type type;
|
||||
|
||||
Reference in New Issue
Block a user