Refactored plugin management interface

This commit is contained in:
luwenpeng
2022-07-27 15:51:07 +08:00
parent dd54381cb0
commit 61e3c264f3
11 changed files with 168 additions and 100 deletions

View File

@@ -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;
UT_hash_handle hh;
enum session_event_type event;
fn_session_event_callback *callback;
};
TAILQ_HEAD(session_event_callback_list, session_event_callback);
struct session_event_callback_map
/*
* Hast table
*
* key: string -> session_type
* val: array -> event_cbs
*/
struct managed_session_event_callback
{
struct session_event_callback_list session_ev_cb_list;
UT_hash_handle hh;
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;
};
/******************************************************************************
* CallBack Runtime Array (For Per Session)
******************************************************************************/
struct session_event_callback_runtime
{
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;
}