2023-12-08 11:16:23 +08:00
|
|
|
#include <stdio.h>
|
2024-01-09 18:03:24 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
#include "config.h"
|
2024-01-09 18:03:24 +08:00
|
|
|
#include "packet.h"
|
|
|
|
|
#include "timestamp.h"
|
|
|
|
|
#include "session_manager.h"
|
|
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
|
2024-01-09 18:03:24 +08:00
|
|
|
#ifndef STELLAR_LOG_ERROR
|
|
|
|
|
#define STELLAR_LOG_ERROR(format, ...) \
|
|
|
|
|
fprintf(stderr, "ERROR (stellar), " format "\n", ##__VA_ARGS__);
|
|
|
|
|
#endif
|
2024-01-26 14:41:40 +08:00
|
|
|
#define STELLAR_LOG_DEBUG(format, ...) LOG_DEBUG("stellar", format, ##__VA_ARGS__)
|
2024-01-09 18:03:24 +08:00
|
|
|
#ifndef STELLAR_LOG_DEBUG
|
|
|
|
|
#define STELLAR_LOG_DEBUG(format, ...) \
|
|
|
|
|
fprintf(stderr, "DEBUG (stellar), " format "\n", ##__VA_ARGS__);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED)
|
|
|
|
|
#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED)
|
|
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
struct thread_context
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
pthread_t tid;
|
|
|
|
|
uint16_t index;
|
|
|
|
|
uint64_t need_exit;
|
|
|
|
|
uint64_t is_runing;
|
|
|
|
|
struct session_manager *sess_mgr;
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
struct stellar_context
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
uint64_t need_exit;
|
2024-01-29 14:15:33 +08:00
|
|
|
struct config config;
|
2024-01-10 10:19:47 +08:00
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
struct thread_context thread_ctx[MAX_THREAD_NUM];
|
2024-01-09 18:03:24 +08:00
|
|
|
} g_stellar_ctx = {
|
|
|
|
|
.need_exit = 0,
|
2024-01-29 14:15:33 +08:00
|
|
|
};
|
2024-01-09 18:03:24 +08:00
|
|
|
|
|
|
|
|
static int recv_packet(const char **data)
|
|
|
|
|
{
|
|
|
|
|
static unsigned char packet_data[] = {
|
|
|
|
|
0x5c, 0x5e, 0xab, 0x2a, 0xa2, 0x00, 0x2c, 0x6b, 0xf5, 0x45, 0x88, 0x29, 0x08, 0x00, 0x45, 0x00, 0x00, 0x5c, 0x0b, 0x4d, 0x00, 0x00, 0x3b, 0x29, 0x09, 0xc8,
|
|
|
|
|
0xd2, 0x4d, 0x58, 0xa3, 0x3b, 0x42, 0x04, 0x32, 0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x06, 0x40, 0x20, 0x01, 0x0d, 0xa8, 0x02, 0x00, 0x90, 0x0e, 0x02, 0x00,
|
|
|
|
|
0x5e, 0xfe, 0xd2, 0x4d, 0x58, 0xa3, 0x26, 0x00, 0x14, 0x0e, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x17, 0x02, 0x10, 0x58, 0xcd, 0x4c, 0x00, 0x50,
|
|
|
|
|
0x81, 0x80, 0x5c, 0x76, 0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x20, 0x00, 0xf7, 0x57, 0x00, 0x00, 0x02, 0x04, 0x04, 0xc4, 0x01, 0x03, 0x03, 0x08, 0x01, 0x01,
|
|
|
|
|
0x04, 0x02};
|
|
|
|
|
|
|
|
|
|
*data = (const char *)packet_data;
|
|
|
|
|
return sizeof(packet_data);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 11:47:55 +08:00
|
|
|
static void send_packet(const char *data, uint16_t len)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 18:03:24 +08:00
|
|
|
static void signal_handler(int signo)
|
|
|
|
|
{
|
|
|
|
|
if (signo == SIGINT)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_DEBUG("recv SIGINT, exit !!!");
|
|
|
|
|
ATOMIC_SET(&g_stellar_ctx.need_exit, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (signo == SIGQUIT)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_DEBUG("recv SIGQUIT, exit !!!");
|
|
|
|
|
ATOMIC_SET(&g_stellar_ctx.need_exit, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (signo == SIGTERM)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_DEBUG("recv SIGTERM, exit !!!");
|
|
|
|
|
ATOMIC_SET(&g_stellar_ctx.need_exit, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
static void __packet_plugin_dispatch_example(const struct packet *pkt)
|
2024-01-26 14:41:40 +08:00
|
|
|
{
|
|
|
|
|
if (pkt == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf("=> packet dispatch: %p\n", pkt);
|
|
|
|
|
printf("<= packet dispatch\n\n");
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
static void __session_plugin_dispatch_example(struct session *sess)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
2024-01-26 14:41:40 +08:00
|
|
|
if (sess == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 11:21:11 +08:00
|
|
|
printf("\n");
|
|
|
|
|
printf("=> session dispatch: %p\n", sess);
|
2024-01-09 18:03:24 +08:00
|
|
|
session_dump(sess);
|
2024-01-23 14:30:46 +08:00
|
|
|
printf("<= session dispatch\n\n");
|
2024-01-15 11:21:11 +08:00
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
// after session dispatch, we should reset session current packet and direction
|
2024-01-15 11:21:11 +08:00
|
|
|
session_set0_cur_pkt(sess, NULL);
|
|
|
|
|
session_set_cur_dir(sess, SESSION_DIR_NONE);
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void *thread_cycle(void *arg)
|
|
|
|
|
{
|
|
|
|
|
uint16_t len = 0;
|
|
|
|
|
const char *data = NULL;
|
|
|
|
|
struct packet pkt;
|
|
|
|
|
struct session *sess = NULL;
|
2024-01-26 14:41:40 +08:00
|
|
|
struct thread_context *thread_ctx = (struct thread_context *)arg;
|
|
|
|
|
struct session_manager *sess_mgr = thread_ctx->sess_mgr;
|
|
|
|
|
char thd_name[16];
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
ATOMIC_SET(&thread_ctx->is_runing, 1);
|
|
|
|
|
snprintf(thd_name, sizeof(thd_name), "stellar:%d", thread_ctx->index);
|
|
|
|
|
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);
|
|
|
|
|
STELLAR_LOG_DEBUG("worker thread %s runing\n", thd_name);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
while (ATOMIC_READ(&thread_ctx->need_exit) == 0)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
|
|
|
|
len = recv_packet(&data);
|
2024-01-17 11:47:55 +08:00
|
|
|
if (data == NULL)
|
2024-01-10 10:19:47 +08:00
|
|
|
{
|
2024-01-17 11:47:55 +08:00
|
|
|
goto poll_wait;
|
2024-01-10 10:19:47 +08:00
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-17 11:47:55 +08:00
|
|
|
packet_parse(&pkt, data, len);
|
2024-01-29 14:15:33 +08:00
|
|
|
__packet_plugin_dispatch_example(&pkt);
|
2024-01-26 14:41:40 +08:00
|
|
|
|
2024-01-17 11:47:55 +08:00
|
|
|
sess = session_manager_update_session(sess_mgr, &pkt);
|
2024-01-29 14:15:33 +08:00
|
|
|
__session_plugin_dispatch_example(sess);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-17 11:47:55 +08:00
|
|
|
send_packet(data, len);
|
|
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
sess = session_manager_get_evicted_session(sess_mgr);
|
2024-01-29 14:15:33 +08:00
|
|
|
__session_plugin_dispatch_example(sess);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-17 11:47:55 +08:00
|
|
|
poll_wait:
|
2024-01-26 14:41:40 +08:00
|
|
|
sess = session_manager_get_expired_session(sess_mgr);
|
2024-01-29 14:15:33 +08:00
|
|
|
__session_plugin_dispatch_example(sess);
|
2024-01-26 14:41:40 +08:00
|
|
|
|
|
|
|
|
usleep(1000); // session_manager_get_expire_interval(sess_mgr); (seconds)
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
ATOMIC_SET(&thread_ctx->is_runing, 0);
|
|
|
|
|
STELLAR_LOG_DEBUG("worker thread %s exit\n", thd_name);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2023-12-08 11:16:23 +08:00
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
static int thread_context_init(struct stellar_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
struct system_config *sys_cfg = &ctx->config.sys_cfg;
|
|
|
|
|
struct session_manager_config *sess_mgr_cfg = &ctx->config.sess_mgr_cfg;
|
|
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < sys_cfg->nr_threads; i++)
|
|
|
|
|
{
|
|
|
|
|
struct thread_context *thread_ctx = &ctx->thread_ctx[i];
|
|
|
|
|
thread_ctx->index = i;
|
|
|
|
|
thread_ctx->need_exit = 0;
|
|
|
|
|
thread_ctx->is_runing = 0;
|
|
|
|
|
|
|
|
|
|
thread_ctx->sess_mgr = session_manager_create(sess_mgr_cfg);
|
|
|
|
|
if (thread_ctx->sess_mgr == NULL)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create session manager");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void thread_context_free(struct stellar_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
struct system_config *sys_cfg = &ctx->config.sys_cfg;
|
|
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < sys_cfg->nr_threads; i++)
|
|
|
|
|
{
|
|
|
|
|
struct thread_context *thread_ctx = &ctx->thread_ctx[i];
|
|
|
|
|
if (ATOMIC_READ(&thread_ctx->is_runing) == 0)
|
|
|
|
|
{
|
|
|
|
|
session_manager_destroy(thread_ctx->sess_mgr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int thread_create(struct stellar_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
struct system_config *sys_cfg = &ctx->config.sys_cfg;
|
|
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < sys_cfg->nr_threads; i++)
|
|
|
|
|
{
|
|
|
|
|
struct thread_context *thread_ctx = &ctx->thread_ctx[i];
|
|
|
|
|
if (pthread_create(&thread_ctx->tid, NULL, thread_cycle, (void *)thread_ctx) < 0)
|
|
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void thread_destroy(struct stellar_context *ctx)
|
|
|
|
|
{
|
|
|
|
|
struct system_config *sys_cfg = &ctx->config.sys_cfg;
|
|
|
|
|
|
|
|
|
|
for (uint16_t i = 0; i < sys_cfg->nr_threads; i++)
|
|
|
|
|
{
|
|
|
|
|
struct thread_context *thread_ctx = &ctx->thread_ctx[i];
|
|
|
|
|
ATOMIC_SET(&thread_ctx->need_exit, 1);
|
|
|
|
|
while (ATOMIC_READ(&thread_ctx->is_runing) == 1)
|
|
|
|
|
{
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 11:16:23 +08:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2024-01-29 14:15:33 +08:00
|
|
|
if (argc != 2)
|
|
|
|
|
{
|
|
|
|
|
printf("usage: %s <config file>\n", argv[0]);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config_load(&g_stellar_ctx.config, argv[1]) != 0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config_dump(&g_stellar_ctx.config);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
|
|
|
|
// TODO init log
|
|
|
|
|
|
|
|
|
|
// TODO init plugin
|
|
|
|
|
|
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
|
signal(SIGQUIT, signal_handler);
|
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
|
|
|
|
|
|
timestamp_update();
|
|
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
if (thread_context_init(&g_stellar_ctx) != 0)
|
2024-01-23 14:30:46 +08:00
|
|
|
{
|
|
|
|
|
STELLAR_LOG_ERROR("unable to init thread context");
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
2024-01-09 18:03:24 +08:00
|
|
|
|
2024-01-29 14:15:33 +08:00
|
|
|
if (thread_create(&g_stellar_ctx) != 0)
|
2024-01-09 18:03:24 +08:00
|
|
|
{
|
2024-01-29 14:15:33 +08:00
|
|
|
STELLAR_LOG_ERROR("unable to create worker thread");
|
|
|
|
|
goto error_out;
|
2024-01-09 18:03:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!g_stellar_ctx.need_exit)
|
|
|
|
|
{
|
|
|
|
|
timestamp_update();
|
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error_out:
|
2024-01-29 14:15:33 +08:00
|
|
|
thread_destroy(&g_stellar_ctx);
|
|
|
|
|
|
2024-01-26 14:41:40 +08:00
|
|
|
thread_context_free(&g_stellar_ctx);
|
2024-01-09 18:03:24 +08:00
|
|
|
|
|
|
|
|
// TODO free plugin
|
|
|
|
|
|
|
|
|
|
// TODO free log
|
|
|
|
|
|
2023-12-08 11:16:23 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|