From 467e6e2a613b96456600eee3e7f2cc5e933f53e9 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Fri, 15 Dec 2023 10:33:19 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=97=A0=E9=9C=80=E9=89=B4=E6=9D=83=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/builtin-cli.cpp | 29 ++++++++++++++++++++++++++--- docs/develop/总体设计文档.md | 2 +- frontend/Web/index.js | 17 +++++++++++------ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/backend/src/builtin-cli.cpp b/backend/src/builtin-cli.cpp index 5761b28..ee08831 100644 --- a/backend/src/builtin-cli.cpp +++ b/backend/src/builtin-cli.cpp @@ -46,9 +46,32 @@ void BookStoreMain(bool is_server, std::string config_dir) { std::cout << temp_channel_id << " Init 1\n" << new_session_token << ' ' << new_outh_token << std::endl; std::cout.flush(); - } else if (cmd[1] == 'S') + } else if (cmd[1] == 'S') { + std::stringstream ss(cmd); + std::string session_token, operation_token, authentic_key; + ss >> session_token; + ss >> session_token >> operation_token >> authentic_key; + if (session_map.find(session_token) == session_map.end()) { + std::cout << session_token << ' ' << operation_token << " -1" + << std::endl; + std::cout.flush(); + continue; + } + if (session_map[session_token].OuthorizationKey != authentic_key) { + std::cout << session_token << ' ' << operation_token << " -1" + << std::endl; + std::cout.flush(); + continue; + } + if (session_map[session_token].login_stack.empty() || + session_map[session_token].login_stack.top().first != "root") { + std::cout << session_token << ' ' << operation_token << " -1" + << std::endl; + std::cout.flush(); + continue; + } return; - else if (cmd[1] == 'C') { + } else if (cmd[1] == 'C') { std::stringstream ss(cmd); std::string session_token, operation_token, authentic_key; ss >> session_token; @@ -117,7 +140,7 @@ void BookStoreMain(bool is_server, std::string config_dir) { std::getline(std::cin, cmd); auto ret = std::move( engine.Execute(cmd, session_map[session_token].login_stack)); - if(ret.empty()) ret.push_back("[empty]"); + if (ret.empty()) ret.push_back("[empty]"); std::cout << session_token << ' ' << operation_token << " " << ret.size() << std::endl; for (auto &line : ret) std::cout << line << std::endl; diff --git a/docs/develop/总体设计文档.md b/docs/develop/总体设计文档.md index 0a6ac3b..fb6d834 100644 --- a/docs/develop/总体设计文档.md +++ b/docs/develop/总体设计文档.md @@ -71,7 +71,7 @@ memoryriver类维护一个缓存,简单地缓存高频访问和连续访问; - `#CloseSession [SessionToken] [OperationToken] [OuthenticationKey]`:显示地告知调度模块停止某个会话 - `#Request [SessionToken] [OperationToken] [OuthenticationKey]\n[UserCommand]`:向后端发送一个请求 - `#Who [SessionToken] [OperationToken] [OuthenticationKey]`:查询自己是谁和权限 -- `#ShutDownSystem`:关闭整个系统 +- `#ShutDownSystem [SessionToken] [OperationToken] [OuthenticationKey]`:关闭整个系统 ### 后端向前端 - 字符串,`[SessionToken] [OperationToken] [LineCounter]\n[ResponseContent]`,其中,`[ResponseContent]`恰有`[LineCounter]`行,每行行末有且仅有一个`\n`。输出为空通过把`[LineCounter]`设置为0来实现,若对话验证失败,`[LineCounter]`设为-1 diff --git a/frontend/Web/index.js b/frontend/Web/index.js index ce13a67..3f6e4b4 100644 --- a/frontend/Web/index.js +++ b/frontend/Web/index.js @@ -113,12 +113,6 @@ io.on('connection', async (socket) => { console.log('message: ' + msg); const substrings = msg.trim().split('\n')[0].split(' '); const head=substrings[0]; - if(head[1]=='S') - { - backend.stdin.write("#ShutDownSystem\n"); - sleep(1000); - process.exit(0); - } const session_token=substrings[1]; if(head[1]=='O') { @@ -146,6 +140,17 @@ io.on('connection', async (socket) => { }); }); +backend.on('exit', (code, signal) => { + if (code !== null) { + console.log(`子进程退出,退出码: ${code}`); + } else if (signal !== null) { + console.log(`子进程被信号中断,信号: ${signal}`); + } else { + console.log('子进程退出'); + } + process.exit(0); +}); + server.listen(3000, () => { console.log('server running at http://localhost:3000'); }); \ No newline at end of file