From fa8b2b5fcc9fb341c7c49a893df64d58354683bd Mon Sep 17 00:00:00 2001 From: fumiama Date: Mon, 17 May 2021 18:30:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 8 ++++++-- README.md | 17 +++++++++-------- cfgwriter.c | 43 +++++++++++++++++++++++++++++++++++++++++++ server.c | 32 ++++++++++++++++++++++---------- simple-protobuf | 2 +- 5 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 cfgwriter.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d559f4..e02a551 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index b4ea6c5..2da602f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cfgwriter.c b/cfgwriter.c new file mode 100644 index 0000000..72d10bf --- /dev/null +++ b/cfgwriter.c @@ -0,0 +1,43 @@ +#include +#include +#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]"); +} diff --git a/server.c b/server.c index 31ba4ec..9a80f80 100644 --- a/server.c +++ b/server.c @@ -13,14 +13,19 @@ #include #include #include +#include "simple-protobuf/simple_protobuf.h" #if !__APPLE__ #include #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); diff --git a/simple-protobuf b/simple-protobuf index e0b88c0..db1b95a 160000 --- a/simple-protobuf +++ b/simple-protobuf @@ -1 +1 @@ -Subproject commit e0b88c0c63a9f2afb05693b3bae92d94bdd12d51 +Subproject commit db1b95a42e6abc9f4ed980d3bf819e534374fcdb