Compare commits

...

8 Commits

19 changed files with 456 additions and 87 deletions

View File

@ -4,12 +4,13 @@ project (Onebot_back C)
add_executable(Start_Onebot_back main.c tem/ctl.c) add_executable(Start_Onebot_back main.c tem/ctl.c)
add_executable(Run_pluginmanager run_pluginmanager/run_pluginmanager.c) add_executable(Run_pluginmanager run_pluginmanager/run_pluginmanager.c)
add_library(Network SHARED network/network.c network/swap.c network/cJSON.c network/http_rel.c) add_library(Network SHARED network/network.c network/swap.c network/cJSON.c network/http/http_rel.c network/erroprocess/erroprocess.c)
add_library(Swmem SHARED network/swap.c) add_library(Swmem SHARED network/swap.c)
add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstall.c) add_library(Interpre SHARED interpreter/interpreter.c tools/pkgmanager/pkginstall.c)
add_library(Log SHARED tools/log/log.c) add_library(Log SHARED tools/log/log.c)
add_library(Toml SHARED tools/toml/toml.c) add_library(Toml SHARED tools/toml/toml.c)
add_library(Quit SHARED tools/quit/quit.c)
target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml) target_link_libraries(Start_Onebot_back Network Swmem Interpre Log Toml Quit)
include_directories(${PROJECT_SOURCE_DIR}) include_directories(${PROJECT_SOURCE_DIR})

View File

@ -9,12 +9,24 @@
#include "interpreter.h" #include "interpreter.h"
#include "tools/pkgmanager/pkginstall.h" #include "tools/pkgmanager/pkginstall.h"
int inter_in_log(const char *log,log_manager *manager)
{
if(strlen(log)>1024)
return -1;
logs* logss = (logs*)malloc(sizeof(logs));
memcpy(logss->log,log,strlen(log));
manager->in_log(logss,manager);
return 0;
}
int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2]) int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager)
{ {
printf("SYS:prepare env\n"); printf("SYS:prepare env\n");
inter_in_log("SYS:prepare env\n",log_manager);
printf("SYS:env ready\n"); printf("SYS:env ready\n");
inter_in_log("SYS:env ready\n",log_manager);
printf("SYS:loading cmd_dic\n"); printf("SYS:loading cmd_dic\n");
inter_in_log("SYS:loading cmd_dic\n",log_manager);
sprintf(cmd_dic[0].name, "pkginstall"); sprintf(cmd_dic[0].name, "pkginstall");
cmd_dic[0].cmd = INSTALL; cmd_dic[0].cmd = INSTALL;
@ -25,6 +37,7 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2])
cmd_dic[2].cmd = QUIT; cmd_dic[2].cmd = QUIT;
printf("SYS:cmd_dir load complite\n"); printf("SYS:cmd_dir load complite\n");
inter_in_log("SYS:cmd_dir load complite\n",log_manager);
for(int i =0;i<10;i++) for(int i =0;i<10;i++)
{ {
@ -32,8 +45,9 @@ int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2])
} }
self->arg = NULL; self->arg = NULL;
printf("SYS:Creating ctl fifo\n"); printf("SYS:Creating ctl fifo\n");
inter_in_log("SYS:Creating ctl fifo\n",log_manager);
memcpy(self->fifofd,fifo,2*sizeof(int)); memcpy(self->fifofd,fifo,2*sizeof(int));
self->log_manager = log_manager;
} }
int get_args(ctx *self) int get_args(ctx *self)
@ -57,6 +71,7 @@ int get_args(ctx *self)
arg->next = (args*)malloc(sizeof(args)); arg->next = (args*)malloc(sizeof(args));
if(arg->next == NULL){ if(arg->next == NULL){
perror("ERROR:fail to get mem"); perror("ERROR:fail to get mem");
inter_in_log("ERROR:fail to get mem\n",self->log_manager);
return -1; return -1;
} }
arg = arg->next; arg = arg->next;
@ -131,26 +146,33 @@ int exce(const int command,ctx *all_ctx)
{ {
case BAD_INPUT: case BAD_INPUT:
printf("SYS:bad input,try again\n"); printf("SYS:bad input,try again\n");
inter_in_log("SYS:bad input,try again\n",all_ctx->log_manager);
return BAD_INPUT; return BAD_INPUT;
case INSTALL: case INSTALL:
if(all_ctx->arg == NULL){ if(all_ctx->arg == NULL){
printf("SYS:Missing args\n"); printf("SYS:Missing args\n");
inter_in_log("SYS:Missng args\n",all_ctx->log_manager);
return 1; return 1;
} }
printf("SYS:init pkgmanager\n"); printf("SYS:init pkgmanager\n");
inter_in_log("SYS:init pkgmanager\n",all_ctx->log_manager);
pkger *manager = init_pkginstaller(); pkger *manager = init_pkginstaller();
printf("SYS:installing\n"); printf("SYS:installing\n");
inter_in_log("SYS:installing\n",all_ctx->log_manager);
manager->packup(manager); manager->packup(manager);
return 1; return 1;
case RUN: case RUN:
printf("runing\n"); printf("SYS:runing\n");
inter_in_log("SYS:running\n",all_ctx->log_manager);
return 1; return 1;
case QUIT: case QUIT:
printf("shuting down\n"); printf("SYS:shuting down\n");
inter_in_log("SYS:shuting down\n",all_ctx->log_manager);
all_ctx->statue = -1; all_ctx->statue = -1;
write(all_ctx->fifofd[1],"q",1);
return 1; return 1;
} }

View File

@ -6,6 +6,8 @@
#define SIG_MOD 0 #define SIG_MOD 0
#define FILE_MOD 1 #define FILE_MOD 1
#include "tools/log/log.h"
typedef struct typedef struct
{ {
char name[256]; char name[256];
@ -38,11 +40,12 @@ typedef struct ctx
char command[MAX_BUF];//当前行缓存 char command[MAX_BUF];//当前行缓存
int statue;//当前状态 int statue;//当前状态
int fifofd[2]; int fifofd[2];
log_manager *log_manager;
}ctx;//上下文管理 }ctx;//上下文管理
int interpret(int mod, ctx *all_ctx,Cmd *cmd_dic); int interpret(int mod, ctx *all_ctx,Cmd *cmd_dic);
int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2]); int init_interpreter(Cmd *cmd_dic,ctx *self,int fifo[2],log_manager *log_manager);
#define ARG_LENGTH 256 #define ARG_LENGTH 256

View File

@ -1,6 +1,8 @@
#define _GNU_SOURCE
#include "tem/ctl.h" #include "tem/ctl.h"
#include "network/network.h" #include "network/network.h"
#include "tools/toml/toml.h" #include "tools/toml/toml.h"
#include "tools/quit/quit.h"
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -35,6 +37,7 @@ int main()
init_loger(logsmanager); init_loger(logsmanager);
Ctl *teml = init_tem(logsmanager); Ctl *teml = init_tem(logsmanager);
teml->config = server;
//初始化终端对象 //初始化终端对象
int fifo[2]; int fifo[2];
if(pipe(fifo)==-1) if(pipe(fifo)==-1)
@ -45,14 +48,15 @@ int main()
pthread_t network_id; pthread_t network_id;
pthread_create(&network_id,NULL,networkmanager->run_network,(void*)networkmanager); pthread_create(&network_id,NULL,networkmanager->run_network,(void*)networkmanager);
//启动网络监听与线程池,并加载插件 //启动网络监听与线程池,并加载插件
alres *resource = (alres*)malloc(sizeof(alres));
resource->loger = logsmanager;
resource->network = networkmanager;
resource->tem = teml;
on_exit(quit_all,resource);
//注册清理函数
teml->run(teml,fifo); teml->run(teml,fifo);
//启动终端 //启动终端
pthread_join(network_id,NULL); pthread_join(network_id,NULL);
//等待网络管理器进程结束 //等待网络管理器进程结束
free(teml);
free(networkmanager);
free(logsmanager);
//释放内存
return 1; return 1;
} }

View File

@ -0,0 +1,16 @@
#include "erroprocess.h"
#include <stdio.h>
#include <stddef.h>
int give_upjobs(indiector *self)
{
if(self == NULL)
{
return -1;
}
}
int init_indector(indiector *self)
{
}

View File

@ -0,0 +1,24 @@
#ifndef ERROPROCESS
#define ERROPROCESS
#define BASE_INDIECTOR 2
#define MAX_index 5
#define CIR_TIME 20
typedef struct jobs
{
struct jobs* next;
int job;
}jobs;
typedef struct indiector
{
int status;//熔断标志位
int retreat_index;//退避指数
jobs *head_job;
jobs *rear_job;
int (*give_upjobs)(struct indiector *);
}indiector;
#endif

80
c/network/http/http_rel.c Normal file
View File

@ -0,0 +1,80 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "http_rel.h"
const char *http_get_body(const char *buf)
{
if (!buf) return NULL;
/* 找到 header 与 body 之间的空行 "\r\n\r\n" */
const char *sep = strstr(buf, "\r\n\r\n");
if (!sep) return NULL; /* 格式错误 */
const char *body = sep + 4; /* 跳过 "\r\n\r\n" */
/* 简单判断:如果后面还有数据,就认为是 body */
if (*body == '\0') return NULL; /* 没有 body */
return body;
}
/* 一次性读完一个 HTTP 请求,返回 malloc 得到的完整字符串(含头+体),调用者 free。
返回 NULL 表示读取出错或内存不足。
*/
char *recv_http_request(int cfd)
{
char head[8192];
int body_len = 0;
/* 1. 读头部到 \r\n\r\n */
size_t n = 0;
while (n < sizeof(head)) {
if (read(cfd, head + n, 1) != 1) goto err;
if (n >= 3 && memcmp(head + n - 3, "\r\n\r", 3) == 0) {
if (read(cfd, head + n, 1) != 1) goto err; /* 再吃一个 \n */
++n;
break;
}
++n;
}
head[n] = 0;
/* 2. 解析 Content-Length */
const char *p = head;
while ((p = strchr(p, '\n')) != NULL) {
if (sscanf(p, "\nContent-Length: %d", &body_len) == 1) break;
++p;
}
/* 3. 读 body */
char *body = NULL;
if (body_len > 0) {
body = malloc(body_len + 1);
if (!body) goto err;
size_t got = 0;
while (got < (size_t)body_len) {
ssize_t rd = read(cfd, body + got, body_len - got);
if (rd <= 0) { free(body); goto err; }
got += rd;
}
body[got] = 0;
}
/* 4. 拼成完整字符串 */
char *full = malloc(n + (body_len ? body_len : 0) + 1);
if (!full) { free(body); goto err; }
memcpy(full, head, n);
if (body_len) memcpy(full + n, body, body_len);
full[n + body_len] = 0;
free(body);
return full;
err:
return NULL;
}

View File

@ -2,5 +2,6 @@
#define HTTP_REL #define HTTP_REL
const char *http_get_body(const char *buf); const char *http_get_body(const char *buf);
char *recv_http_request(int cfd);
#endif #endif

View File

@ -1,24 +0,0 @@
#include <stddef.h>
#include <string.h>
#include "http_rel.h"
const char *http_get_body(const char *buf)
{
if (!buf) return NULL;
/* 找到 header 与 body 之间的空行 "\r\n\r\n" */
const char *sep = strstr(buf, "\r\n\r\n");
if (!sep) return NULL; /* 格式错误 */
const char *body = sep + 4; /* 跳过 "\r\n\r\n" */
/* 简单判断:如果后面还有数据,就认为是 body */
if (*body == '\0') return NULL; /* 没有 body */
return body;
}
const char *resave_http(int fd)
{
}

View File

@ -2,13 +2,16 @@
#include "network.h" #include "network.h"
#include "swap.h" #include "swap.h"
#include "http_rel.h" #include "http/http_rel.h"
#include "cJSON.h" #include "cJSON.h"
#include "tools/log/log.h" #include "tools/log/log.h"
#include "tools/quit/quit.h"
#include "erroprocess/erroprocess.h"
#include <semaphore.h> #include <semaphore.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <string.h> #include <string.h>
#include <stddef.h> #include <stddef.h>
#include <errno.h> #include <errno.h>
@ -74,28 +77,39 @@ int rbt_parse_json(const char *json_text, rbt_msg *out)
return 0; // 成功 return 0; // 成功
} }
int init_network(int port) int init_http_network(int port)
{ {
int fd = socket(AF_INET, SOCK_STREAM, 0); int fd = socket(AF_INET, SOCK_STREAM, 0);
fcntl(fd, F_GETFL);
struct sockaddr_in addr = {0}; struct sockaddr_in addr = {0};
addr.sin_family = AF_INET; // 和 socket() 一致 addr.sin_family = AF_INET; // 和 socket() 一致
addr.sin_port = htons(port); // 端口号必须网络字节序 addr.sin_port = htons(port); // 端口号必须网络字节序
addr.sin_addr.s_addr = htonl(INADDR_ANY); // 0.0.0.0:本机所有网卡 addr.sin_addr.s_addr = htonl(INADDR_ANY); // 0.0.0.0:本机所有网卡
bind(fd, (struct sockaddr *)&addr, sizeof(addr)); bind(fd, (struct sockaddr *)&addr, sizeof(addr));
listen(fd,10);
return fd; return fd;
} }
ssize_t read_req(int fd, void *buf) ssize_t read_req(int fd, void *buf)
{ {
// TODO 修改读取任务函数
ssize_t n = read(fd, buf, MAX_MESSAGE_BUF); ssize_t n = read(fd, buf, MAX_MESSAGE_BUF);
if (n == 0) /* 写端已关闭,管道永不会再有数据 */ if (n == 0) /* 写端已关闭,管道永不会再有数据 */
return -1; return 0;
return (n > 0) ? n : -1; return (n > 0) ? n : -1;
} }
int process_message(char *req,log_manager *logger) int process_message(char *req,log_manager *logger)
{ {
const char *body = http_get_body(req); //TODO 修改管道命令解析
if(req ==NULL )
return 0;
int fd;
char front,rear;
sscanf(req,"%s/%d/%s",&rear,&fd,&rear);
char *req_buf = recv_http_request(fd);
const char *body = http_get_body(req_buf);
free(req_buf);
rbt_msg message; rbt_msg message;
rbt_parse_json(body,&message); rbt_parse_json(body,&message);
make_swap((void*)&message); make_swap((void*)&message);
@ -125,7 +139,7 @@ int iss_work(netm *self,char *command)
void *pth_module(void *args_p) void *pth_module(void *args_p)
{ {
args *argms = (args*)args_p; net_args *argms = (net_args*)args_p;
pth_m *pmd = argms->pth; pth_m *pmd = argms->pth;
log_manager *logger = argms->log; log_manager *logger = argms->log;
//参数解析 //参数解析
@ -144,11 +158,15 @@ void *pth_module(void *args_p)
NULL}; NULL};
execv("Run_pluhginmanager",args); execv("Run_pluhginmanager",args);
} }
logs *pth_log = (logs*)malloc(sizeof(logs));
sprintf(pth_log->log,"PID:%lu launched python plugines\n",pmd->pthread_id);
logger->in_log(pth_log,logger);
//拉起python插件管理器 //拉起python插件管理器
for(;;){ for(;;){
//线程池中,单个线程模型 //线程池中,单个线程模型
char req[64*1024]; char req[64];
//从管道中读取请求,并解析,无内容时休眠 //从管道中读取请求,并解析,无内容时休眠
int n = read_req(pmd->fifo_fd[0],req); int n = read_req(pmd->fifo_fd[0],req);
//管道关闭时退出; //管道关闭时退出;
@ -171,7 +189,7 @@ int start_pool(netm *self)
//为线程开辟管道 //为线程开辟管道
pipe(self->pool[i].fifo_fd); pipe(self->pool[i].fifo_fd);
//启动线程 //启动线程
args arg; net_args arg;
arg.pth =&self->pool[i]; arg.pth =&self->pool[i];
arg.log = self->logmanager; arg.log = self->logmanager;
self->pool[i].status = 1; self->pool[i].status = 1;
@ -183,8 +201,12 @@ int shutdown_pool(netm *self)
{ {
for(int i = 0;i<MAX_POOL;i++) for(int i = 0;i<MAX_POOL;i++)
{ {
close(self->pool[i].fifo_fd[0]); if(self->pool[i].status == -1)
continue;
self->pool[i].status = -1;
close(self->pool[i].fifo_fd[1]);
} }
return 1;
} }
int server_run(int port,int fifo_fd,netm *self) int server_run(int port,int fifo_fd,netm *self)
@ -194,36 +216,58 @@ int server_run(int port,int fifo_fd,netm *self)
perror("epoll_create1"); perror("epoll_create1");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
struct epoll_event ev;
//设置epoll同时监听控制管道与http请求
ev.events = EPOLLIN;
ev.data.fd = fifo_fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, fifo_fd, &ev);
char iss_buf[256]; char iss_buf[256];
int http_fd = init_network(port); self->http_fd = init_http_network(port);
struct epoll_event events;
ev.data.fd = self->http_fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, self->http_fd, &ev);
struct epoll_event events[10];
self->epoll_fd = epfd;
for(;;) for(;;)
{ {
int nf = epoll_wait(epfd,&events,1,-1); /*工作循环-----------------------------*/
int nf = epoll_wait(epfd,events,10,-1);
printf("%d\n",nf);
if (nf == -1) { if (nf == -1) {
perror("epoll_wait"); perror("epoll_wait");
break; break;
} }
if(events.data.fd ==http_fd) for(int i = 0; i<nf;i++){
{
sprintf(iss_buf,"s/%d/e",accept4(http_fd,NULL,NULL,SOCK_NONBLOCK | SOCK_CLOEXEC)); if(events[i].data.fd ==self->http_fd)
self->iss_work(self,iss_buf);
}
if(events.data.fd == fifo_fd)
{
char command;
while(read(fifo_fd,&command,1)==1)
{ {
switch(command){ int nt_fd = accept4(self->http_fd,NULL,NULL,SOCK_NONBLOCK | SOCK_CLOEXEC);
case 'q': printf("%d\n",nt_fd);
//退出逻辑 if(nt_fd == -1)
break; continue;
case 'u': sprintf(iss_buf,"s/%d/e",nt_fd);
//插件更新逻辑 self->iss_work(self,iss_buf);
break; }
if(events[i].data.fd == fifo_fd)
{
char command;
while(read(fifo_fd,&command,1)==1)
{
switch(command){
case 'q':
//退出逻辑
quit_server(self);
return 1;
break;
case 'u':
//插件更新逻辑
break;
}
} }
} }
} }
/*工作循环----------------------------*/
} }
} }
@ -231,7 +275,7 @@ void *run_network(void *self_d)
{ {
netm *self = (netm*)self_d; netm *self = (netm*)self_d;
self->start_pool(self); self->start_pool(self);
server_run(self->port,self->fifo_fd[1],self); server_run(self->port,self->fifo_fd[0],self);
self->shutdown_pool(self); self->shutdown_pool(self);
} }
@ -247,5 +291,6 @@ int init_networkmanager(netm *self,int *fifo,log_manager *logmanager,int port)
self->last_alc = 0; self->last_alc = 0;
//初始化参数 //初始化参数
self->logmanager = logmanager; self->logmanager = logmanager;
self->err_indictor = (indiector*)malloc(sizeof(indiector));
self->port = port; self->port = port;
} }

View File

@ -1,10 +1,11 @@
#ifndef NETWORK #ifndef NETWORK
#define NETWORK #define NETWORK
#define MAX_POOL 24 #define MAX_POOL 10
#define MAX_MESSAGE_BUF 10240 #define MAX_MESSAGE_BUF 10240
#include <pthread.h> #include <pthread.h>
#include<tools/log/log.h> #include "tools/log/log.h"
#include "erroprocess/erroprocess.h"
#include <stdatomic.h> #include <stdatomic.h>
//单个线程模型 //单个线程模型
typedef struct pthread_module typedef struct pthread_module
@ -14,11 +15,11 @@ typedef struct pthread_module
atomic_int status; atomic_int status;
}pth_m; }pth_m;
typedef struct args typedef struct net_args
{ {
log_manager *log; log_manager *log;
pth_m *pth; pth_m *pth;
}args; }net_args;
typedef struct network_manager typedef struct network_manager
{ {
@ -29,8 +30,11 @@ typedef struct network_manager
pth_m pool[MAX_POOL]; pth_m pool[MAX_POOL];
int fifo_fd[2]; int fifo_fd[2];
log_manager *logmanager; log_manager *logmanager;
indiector *err_indictor;
int last_alc; int last_alc;
int port; int port;
int epoll_fd;
int http_fd;
}netm; }netm;
typedef struct rebot_message typedef struct rebot_message

View File

@ -5,7 +5,11 @@
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
#include <termios.h> #include <termios.h>
#include <pthread.h>
#include <signal.h>
#include "ctl.h" #include "ctl.h"
#include "interpreter/interpreter.h" #include "interpreter/interpreter.h"
#include "tools/log/log.h" #include "tools/log/log.h"
@ -231,10 +235,11 @@ int teml(Ctl *self,int fifo[2])
char input[MAX_BUF] = {'\0'}; char input[MAX_BUF] = {'\0'};
ctx *command = (ctx*)malloc(sizeof(ctx)); ctx *command = (ctx*)malloc(sizeof(ctx));
Cmd cmd_dir[10]; Cmd cmd_dir[10];
init_interpreter(cmd_dir,command,fifo);//初始化解释器 init_interpreter(cmd_dir,command,fifo,self->logmanager);//初始化解释器
//创建线程用于定期清理日志 //创建线程用于定期清理日志
pthread_create(&self->logwathcher,NULL,self->logmanager->clear_log,self->logmanager); pthread_create(&self->logwathcher,NULL,self->logmanager->clear_log,self->logmanager);
command->statue = 0; command->statue = 0;
self->command = command;
do do
{ //设置缓冲区,接收用户输入 { //设置缓冲区,接收用户输入
write(STDOUT_FILENO,PROMPT,sizeof(PROMPT)); write(STDOUT_FILENO,PROMPT,sizeof(PROMPT));
@ -243,14 +248,22 @@ int teml(Ctl *self,int fifo[2])
perror("sys error"); perror("sys error");
//将用户输入入队 //将用户输入入队
infifo(self,input); infifo(self,input);
logs *log = (logs*)malloc(sizeof(logs));
memcpy(log->log,input,sizeof(input));
self->logmanager->in_log(log,self->logmanager);
memcpy(command->command,input,sizeof(input)); memcpy(command->command,input,sizeof(input));
interpret(SIG_MOD,command,cmd_dir); interpret(SIG_MOD,command,cmd_dir);
const char fexp[256] = {'\0'}; const char fexp[256] = {'\0'};
memcpy(&input,&fexp,MAX_BUF); memcpy(&input,&fexp,MAX_BUF);
}while(command->statue == 0); }while(command->statue == 0);
log_manager_stop(self->logmanager);
pthread_join(self->logwathcher,NULL);
//关闭log定期清理程序
close(fifo[0]); close(fifo[0]);
close(fifo[1]); close(fifo[1]);
free_history(self); free_history(self);
self->command = NULL;
free(command); free(command);
} }
@ -262,6 +275,8 @@ Ctl *init_tem(log_manager *logmanager)
tem->infifo = infifo; tem->infifo = infifo;
tem->index = 0; tem->index = 0;
tem->logmanager = logmanager; tem->logmanager = logmanager;
char *his_buf[HISTORY_BUF] = {NULL};
memcpy(tem->history,his_buf,HISTORY_BUF);
for(int i =0;i<6;i++) for(int i =0;i<6;i++)
{ {
tem->history[i] = NULL; tem->history[i] = NULL;

View File

@ -2,10 +2,12 @@
#define CTL #define CTL
#include <pthread.h> #include <pthread.h>
#include "tools/toml/toml.h"
#include "tools/log/log.h" #include "tools/log/log.h"
#include "interpreter/interpreter.h"
#define MAX_BUF 256 #define MAX_BUF 256
#define HISTORY_BUF 256 #define HISTORY_BUF 210
#define PROMPT "chatbot$$ " #define PROMPT "chatbot$$ "
@ -17,8 +19,12 @@ typedef struct Ctl
char *history[HISTORY_BUF]; char *history[HISTORY_BUF];
pthread_t logwathcher; pthread_t logwathcher;
log_manager *logmanager; log_manager *logmanager;
ctx *command;//解释器上下文
toml_table_t *config;
}Ctl; }Ctl;
Ctl *init_tem(log_manager *logmanager); Ctl *init_tem(log_manager *logmanager);
int free_history(Ctl *self);
#endif #endif

View File

@ -1,8 +1,13 @@
#define _POSIX_C_SOURCE 200112L
#include "log.h" #include "log.h"
#include <stdio.h> #include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int in_log(logs *log,log_manager *self) int in_log(logs *log,log_manager *self)
{ {
@ -11,6 +16,8 @@ int in_log(logs *log,log_manager *self)
if(self->log == NULL){ if(self->log == NULL){
self->log = log; self->log = log;
self->rear = log; self->rear = log;
self->count++;
sem_post(&self->log_sem);
return 0; return 0;
} }
self->count++; self->count++;
@ -25,6 +32,11 @@ logs *out_log(log_manager *self)
{ {
sem_wait(&self->log_sem); sem_wait(&self->log_sem);
logs *buf = self->log; logs *buf = self->log;
if(self->log==NULL)
{
sem_post(&self->log_sem);
return NULL;
}
if(self->log->next ==NULL) if(self->log->next ==NULL)
self->log = self->rear = NULL; self->log = self->rear = NULL;
self->count--; self->count--;
@ -33,27 +45,90 @@ logs *out_log(log_manager *self)
return buf; return buf;
} }
int sleep_with_signal(log_manager *self)
{
struct timespec ts;
int rc;
/* 计算绝对超时:当前 + 1000 s */
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
return -1; /* 罕见失败 */
ts.tv_sec += 1000;
/* 纳秒部分无需处理1000 s 整不会溢出 */
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
while (1) {
rc = pthread_cond_timedwait(&self->cond, &self->mtx, &ts);
if (rc == ETIMEDOUT) { /* 1000 s 到点 */
pthread_mutex_unlock(&self->mtx);
return 1; /* 正常超时 */
}
if (rc != 0) { /* 其他错误 */
pthread_mutex_unlock(&self->mtx);
return -1;
}
/* 被 signal / broadcast 提前唤醒,检查 stop */
if (self->stop == 1) {/* 主线程要求退出 */
pthread_mutex_unlock(&self->mtx);
return 0; /* 告诉调用者:该结束了 */
}
}
}
int cleanup(log_manager *self)
{
if(self->log ==NULL)
return 1;
logs *tobeclean,*loc;
sem_wait(&self->log_sem);//获取信号量
loc = self->log;
self->log = NULL;
self->count = 0;//摘取log链
sem_post(&self->log_sem);
//释放信号量
while(loc->next !=NULL)
{
tobeclean = loc;
loc = loc->next;
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0777);
if(fd == -1)
perror("file:");
write(fd,loc->log,strlen(loc->log));
close(fd);
free(tobeclean);
}
free(loc);
}
void log_manager_stop(log_manager *self)
{
pthread_mutex_lock(&self->mtx);
self->stop = 1; /* 置退出标志 */
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
printf("SYS:stopping loger\n");
pthread_mutex_unlock(&self->mtx);
printf("SYS:done\n");
}
//定期清理函数 //定期清理函数
void *clear_log(void *self_p) void *clear_log(void *self_p)
{ {
log_manager *self = (log_manager*)self_p; log_manager *self = (log_manager*)self_p;
for(;;) for(;;)
{ {
sleep(1000); sleep_with_signal(self);
sem_wait(&self->log_sem); sem_wait(&self->log_sem);
if(self->count<256){ if((self->count<256||self->log==NULL)&&self->stop !=1){
sem_post(&self->log_sem); sem_post(&self->log_sem);
continue; continue;
} }
logs* buf = self->log;
self->log = self->rear =NULL;
sem_post(&self->log_sem); sem_post(&self->log_sem);
logs* tobeclear; cleanup(self);
while(buf->next !=NULL) if(self->stop == 1){
{ return NULL;
tobeclear = buf;
buf = buf->next;
free(tobeclear);
} }
} }
} }
@ -66,8 +141,13 @@ int init_loger(log_manager *self)
return -1; return -1;
} }
sem_init(&self->log_sem, 0,1); sem_init(&self->log_sem, 0,1);
pthread_mutex_init(&self->mtx,NULL);
pthread_cond_init(&self->cond,NULL);
self->in_log = in_log; self->in_log = in_log;
self->out_log = out_log; self->out_log = out_log;
self->clear_log = clear_log; self->clear_log = clear_log;
self->log = NULL; self->log = NULL;
self->stop = 0;
self->cleanup = cleanup;
self->count = 0;
} }

View File

@ -2,6 +2,7 @@
#define LOG #define LOG
#include <semaphore.h> #include <semaphore.h>
#include <pthread.h>
#define MAX_LOG 256 #define MAX_LOG 256
@ -16,12 +17,17 @@ typedef struct log_manager
int (*in_log)(logs *,struct log_manager*); int (*in_log)(logs *,struct log_manager*);
logs* (*out_log)(struct log_manager*); logs* (*out_log)(struct log_manager*);
void *(*clear_log)(void*); void *(*clear_log)(void*);
int (*cleanup)(struct log_manager*);
sem_t log_sem; sem_t log_sem;
logs *log; logs *log;
logs *rear; logs *rear;
int count; int count;
pthread_mutex_t mtx;
pthread_cond_t cond;
int stop;
}log_manager; }log_manager;
void log_manager_stop(log_manager *self);
int init_loger(log_manager *self); int init_loger(log_manager *self);
#endif #endif

View File

View File

View File

@ -1,7 +1,79 @@
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include<unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include "quit.h"
#include "tem/ctl.h"
#include "tools/toml/toml.h"
void *quit_all(void *self_d) int quit_server(netm *self)
{ {
if(self ==NULL)
return -1;
if(self->epoll_fd != -1)
{
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->http_fd,NULL);
epoll_ctl(self->epoll_fd,EPOLL_CTL_DEL,self->fifo_fd[0],NULL);
self->epoll_fd = -1;
}
//关闭epoll监听
if(self->http_fd != -1)
{
close(self->http_fd);
self->http_fd =-1;
}
//关闭socket监听
if(self->fifo_fd[0] != -1)
{
close(self->fifo_fd[0]);
self->fifo_fd[0] = -1;
}
//关闭管道监听
}
} void quit_all(int status,void *self_p)
{
alres *resouce =(alres*)self_p;
//转换参数
resouce->network->shutdown_pool(resouce->network);
logs *netlog = (logs*)malloc(sizeof(logs));
netlog->next = NULL;
memcpy(netlog->log,"shuting down networkserver",27);
quit_server(resouce->network);
resouce->loger->in_log(netlog,resouce->loger);
free(resouce->network);
//释放网络资源
if(resouce->tem->command !=NULL){
free_history(resouce->tem);
if(resouce->tem->command->arg != NULL)
{
args* arg = resouce->tem->command->arg;
if(arg->next !=NULL)
{
while(arg->next != NULL){
args* tobefree = arg;
arg = arg->next;
free(tobefree);
}
free(arg);
}
}
toml_free(resouce->tem->config);
free(resouce->tem->command);
}
//释放终端资源
pthread_mutex_destroy(&resouce->loger->mtx);
resouce->loger->cleanup(resouce->loger);
sem_destroy(&resouce->loger->log_sem);
//销毁信号量
free(resouce->loger);
//清理日志
}

View File

@ -1,6 +1,20 @@
#ifndef QUIT #ifndef QUIT_LIB
#define QUIT #define QUIT_LIB
#include "network/network.h"
#include "tem/ctl.h"
#include "tools/log/log.h"
typedef struct all_resources
{
Ctl *tem;
netm *network;
log_manager *loger;
}alres;
void quit_all(int status,void *self_p);
int quit_server(netm *self);
void *quitall(int status,void *arg);
#endif #endif