plugin_manager adapt session_manager API and rewrite plugin_manger gtest
This commit is contained in:
91
src/main.cpp
91
src/main.cpp
@@ -17,92 +17,93 @@
|
||||
#include "plugin_manager.h"
|
||||
#include "http.h"
|
||||
|
||||
struct stellar_event_base_loop_arg
|
||||
struct worker_thread_ctx
|
||||
{
|
||||
pthread_t tid;
|
||||
struct packet_io_device *dev;
|
||||
struct session_manager *session_mgr;
|
||||
struct plugin_manager *plug_mgr;
|
||||
int tid;
|
||||
struct plugin_manager *plugin_mgr;
|
||||
int thread_id;
|
||||
};
|
||||
|
||||
void *stellar_event_base_loop(void *arg)
|
||||
void *worker_thread_cycle(void *arg)
|
||||
{
|
||||
struct stellar_packet *rx_pkt;
|
||||
struct stellar_session *session;
|
||||
struct stellar_event_base_loop_arg *thread_arg = (struct stellar_event_base_loop_arg *)arg;
|
||||
while(1)
|
||||
struct worker_thread_ctx *thread_arg = (struct worker_thread_ctx *)arg;
|
||||
while (1)
|
||||
{
|
||||
int fetch_num = packet_io_device_rx(thread_arg->dev, thread_arg->tid, &rx_pkt, 1);
|
||||
if(fetch_num > 0)
|
||||
if (packet_io_device_rx(thread_arg->dev, thread_arg->thread_id, &rx_pkt, 1) > 0)
|
||||
{
|
||||
session = session_manager_commit(thread_arg->session_mgr, rx_pkt, thread_arg->tid);
|
||||
while(session)
|
||||
session = session_manager_commit(thread_arg->session_mgr, rx_pkt, thread_arg->thread_id);
|
||||
while (session)
|
||||
{
|
||||
plugin_manager_dispatch(thread_arg->plug_mgr ,session);
|
||||
session = session_manager_fetch_session(thread_arg->session_mgr, session, thread_arg->tid);
|
||||
plugin_manager_dispatch(thread_arg->plugin_mgr, session, thread_arg->thread_id);
|
||||
session = session_manager_fetch_session(thread_arg->session_mgr, session, thread_arg->thread_id);
|
||||
}
|
||||
|
||||
//clean session_manager event queue
|
||||
packet_io_device_tx(thread_arg->dev, thread_arg->tid, &rx_pkt, 1);
|
||||
|
||||
// clean session_manager event queue
|
||||
packet_io_device_tx(thread_arg->dev, thread_arg->thread_id, &rx_pkt, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("no fetch num\n");
|
||||
//dispatch to time event
|
||||
// dispatch to time event
|
||||
|
||||
//dispatch to trigger polling event
|
||||
// dispatch to trigger polling event
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
struct packet_io_device *
|
||||
packet_io_init(const char *instance_name, const enum packet_io_run_mode mode, const int wrk_thread_num)
|
||||
struct packet_io_device *packet_io_init(const char *instance_name, const enum packet_io_run_mode mode, const int thread_num)
|
||||
{
|
||||
struct packet_io_instance *ppio_inst = packet_io_instance_create(instance_name, mode);
|
||||
if (nullptr == ppio_inst) {
|
||||
if (nullptr == ppio_inst)
|
||||
{
|
||||
log_error(ST_ERR_PIO_INSTANCE, "packet_io instance init failed.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
struct packet_io_device *ppio_dev = packet_io_device_open(ppio_inst, "eth1", wrk_thread_num, wrk_thread_num);
|
||||
if (nullptr == ppio_dev) {
|
||||
struct packet_io_device *ppio_dev = packet_io_device_open(ppio_inst, "eth1", thread_num, thread_num);
|
||||
if (nullptr == ppio_dev)
|
||||
{
|
||||
log_error(ST_ERR_PIO_DEVICE, "packet_io device open failed.");
|
||||
|
||||
}
|
||||
return ppio_dev;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
//packet_io_init
|
||||
struct packet_io_device *dev = packet_io_init("stellar", PACKET_IO_RUN_MODE_PCAP_LIVE, 2);
|
||||
|
||||
//manager_init
|
||||
struct session_manager *session_mgr = session_manager_init();
|
||||
struct plugin_manager *plug_mgr = plugin_manager_create();
|
||||
|
||||
// register build-in plugin
|
||||
plugin_manager_register(plug_mgr, "HTTP", SESSION_EVENT_ALL, http_entry);
|
||||
|
||||
// load external plugins
|
||||
int thread_num = 1;
|
||||
char file_path[] = "./plugs/plugins.inf";
|
||||
plugin_manager_load(plug_mgr, file_path);
|
||||
|
||||
//create_worker_thread
|
||||
stellar_event_base_loop_arg arg = {dev, session_mgr, plug_mgr, 0};
|
||||
pthread_t worker_pid;
|
||||
pthread_create(&worker_pid, nullptr, stellar_event_base_loop, (void *)&arg);
|
||||
//main_loop
|
||||
struct packet_io_device *dev = packet_io_init("stellar", PACKET_IO_RUN_MODE_PCAP_LIVE, thread_num);
|
||||
struct session_manager *session_mgr = session_manager_create(thread_num);
|
||||
struct plugin_manager *plugin_mgr = plugin_manager_create(thread_num);
|
||||
|
||||
plugin_manager_register(plugin_mgr, "HTTP", SESSION_STATE_ALL, http_entry);
|
||||
plugin_manager_load(plugin_mgr, file_path);
|
||||
|
||||
struct worker_thread_ctx *workers = (struct worker_thread_ctx *)calloc(sizeof(struct worker_thread_ctx), thread_num);
|
||||
|
||||
for (int i = 0; i < thread_num; i++)
|
||||
{
|
||||
workers[i].dev = dev;
|
||||
workers[i].session_mgr = session_mgr;
|
||||
workers[i].plugin_mgr = plugin_mgr;
|
||||
workers[i].thread_id = i;
|
||||
pthread_create(&workers[i].tid, nullptr, worker_thread_cycle, (void *)&workers[i]);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* main loop code */
|
||||
usleep(1);
|
||||
}
|
||||
|
||||
plugin_manager_unload(plug_mgr);
|
||||
plugin_manager_destory(plug_mgr);
|
||||
|
||||
plugin_manager_unload(plugin_mgr);
|
||||
plugin_manager_destory(plugin_mgr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user