1
0
mirror of https://github.com/fumiama/simple-kanban.git synced 2026-06-05 00:10:29 +08:00

增加配置文件

This commit is contained in:
fumiama
2021-05-17 18:30:26 +08:00
parent 84987babb1
commit fa8b2b5fcc
5 changed files with 81 additions and 21 deletions

View File

@@ -1,3 +1,4 @@
cmake_minimum_required(VERSION 3.0.0)
project(simple-kanban)
include(TestBigEndian)
@@ -6,10 +7,13 @@ if (${isBigEndian})
add_definitions(-DWORDS_BIGENDIAN)
endif()
#add_definitions(-DLISTEN_ON_IPV6)
add_definitions(-DLISTEN_ON_IPV6)
add_executable(simple-kanban server.c)
add_executable(simple-kanban-client client.c)
add_executable(cfgwriter cfgwriter.c)
add_library(spb SHARED simple-protobuf/protobuf.c)
target_link_libraries(simple-kanban pthread)
target_link_libraries(simple-kanban spb pthread)
target_link_libraries(simple-kanban-client pthread)
target_link_libraries(cfgwriter spb)

View File

@@ -24,10 +24,11 @@ make
## 0. 启动程序
```bash
./simple-kanban [-d] 7777 1 ./kanban.txt ./data.bin
./simple-kanban [-d] 7777 1 ./kanban.txt ./data.bin ./cfg.sp
```
其中-d为可选项如果添加程序将以daemon状态运行。
1. -d为可选项如果添加程序将以daemon状态运行。
2. `cfg.sp`为配置文件,通过编译生成的`cfgwriter`生成
## 1. 建立连接
@@ -49,18 +50,18 @@ make
## 4. 设置看板
- 发送:set
- 返回:set
- 发送ver+`SETPASS`
- 发送:`SETPASS`
- 返回:`SETPASS`
- 发送ver
- 返回data
- 发送:头+新的看板
- 返回succ
## 5. 设置数据
- 发送:set
- 返回:set
- 发送dat+`SETPASS`
- 发送:`SETPASS`
- 返回:`SETPASS`
- 发送dat
- 返回data
- 发送:头+新的数据
- 返回succ

43
cfgwriter.c Normal file
View File

@@ -0,0 +1,43 @@
#include <stdio.h>
#include <string.h>
#include "simple-protobuf/simple_protobuf.h"
struct CONFIG {
char pwd[64]; //password
char sps[64]; //set password
};
typedef struct CONFIG CONFIG;
CONFIG cfg;
uint64_t items_len[2] = {sizeof(cfg.pwd), sizeof(cfg.sps)};
uint8_t types_len[2];
int main() {
printf("Enter a password: ");
scanf("%s", cfg.pwd);
printf("Enter a set password: ");
scanf("%s", cfg.sps);
for(int i = 0; i < 2; i++) {
types_len[i] = first_set(items_len[i]);
printf("Item %d has type %d with size %llu\n", i, types_len[i], items_len[i]);
}
/*align_struct(types_len, 2, sizeof(CONFIG));
for(int i = 0; i < 5; i++) {
printf("Item %d's type after align: %u\n", i, types_len[i]);
}*/
FILE* fp = fopen("cfg.sp", "wb");
if(fp) {
set_pb(fp, types_len, sizeof(CONFIG), &cfg);
memset(&cfg, 0, sizeof(CONFIG));
fclose(fp);
puts("Write file succeed.");
fp = NULL;
fp = fopen("cfg.sp", "rb");
if(fp) {
SIMPLE_PB* spb = get_pb(fp);
memcpy(&cfg, spb->target, sizeof(CONFIG));
printf("set pwd: %s, sps: %s\n", cfg.pwd, cfg.sps);
} else perror("[SPB]");
} else perror("[SPB]");
}

View File

@@ -13,14 +13,19 @@
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include "simple-protobuf/simple_protobuf.h"
#if !__APPLE__
#include <sys/sendfile.h>
#endif
#define PASSWORD "fumiama"
struct CONFIG {
char pwd[64]; //password
char sps[64]; //set password
};
typedef struct CONFIG CONFIG;
#define SETPASS "minamoto"
CONFIG* cfg;
int fd;
@@ -51,7 +56,7 @@ struct THREADTIMER {
};
typedef struct THREADTIMER THREADTIMER;
#define showUsage(program) printf("Usage: %s [-d] listen_port try_times kanban_file data_file\n\t-d: As daemon\n", program)
#define showUsage(program) printf("Usage: %s [-d] listen_port try_times kanban_file data_file config_file\n\t-d: As daemon\n", program)
void accept_client();
void accept_timer(void *p);
@@ -168,13 +173,13 @@ int send_all(char* file_path, THREADTIMER *timer) {
}
int sm1_pwd(THREADTIMER *timer) {
if(!strcmp(PASSWORD, timer->data)) timer->status = 0;
if(!strcmp(cfg->pwd, timer->data)) timer->status = 0;
return !timer->status;
}
int s0_init(THREADTIMER *timer) {
if(!strcmp("get", timer->data)) timer->status = 1;
else if(!strcmp("set" SETPASS, timer->data)) timer->status = 2;
else if(!strcmp(cfg->sps, timer->data)) timer->status = 2;
else if(!strcmp("cat", timer->data)) return send_all(data_path, timer);
else if(!strcmp("quit", timer->data)) return 0;
return send_data(timer->accept_fd, timer->data, timer->numbytes);
@@ -380,11 +385,11 @@ void handle_accept(void *p) {
printf("Get %zd bytes: %s\n", timer_pointer_of(p)->numbytes, buff);
puts("Check buffer");
//处理部分粘连
take_word(p, PASSWORD);
take_word(p, cfg->pwd);
take_word(p, "get");
take_word(p, "cat");
take_word(p, "quit");
take_word(p, "set" SETPASS);
take_word(p, cfg->sps);
take_word(p, "ver");
take_word(p, "dat");
if(timer_pointer_of(p)->numbytes > 0) chkbuf(p);
@@ -459,7 +464,7 @@ void close_file(FILE *fp) {
}
int main(int argc, char *argv[]) {
if(argc != 5 && argc != 6) showUsage(argv[0]);
if(argc != 6 && argc != 7) showUsage(argv[0]);
else {
int port = 0;
int as_daemon = !strcmp("-d", argv[1]);
@@ -481,8 +486,15 @@ int main(int argc, char *argv[]) {
if(fp) {
data_path = argv[as_daemon?5:4];
fclose(fp);
if(bind_server(port, times)) if(listen_socket(times)) accept_client();
}
fp = NULL;
fp = fopen(argv[as_daemon?6:5], "rb+");
if(fp) {
SIMPLE_PB* spb = get_pb(fp);
cfg = spb->target;
fclose(fp);
if(bind_server(port, times)) if(listen_socket(times)) accept_client();
} else printf("Error opening config file: %s\n", argv[as_daemon?6:5]);
} else printf("Error opening data file: %s\n", argv[as_daemon?5:4]);
} else printf("Error opening kanban file: %s\n", argv[as_daemon?4:3]);
} else puts("Start daemon error");
} else printf("Error times: %d\n", times);