From a7fcafca83cf1d2558b6ad5bf29ca77893bfde73 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Tue, 12 Dec 2023 07:01:21 +0000 Subject: [PATCH] upd: writing cli --- backend/include/bs-utility.h | 1 + backend/src/builtin-cli.cpp | 30 +++++++++++++++++++++++++++++- backend/src/main.cpp | 2 -- backend/src/schedule.cpp | 35 ++++++++++++++++++++++++++++------- docs/develop/总体设计文档.md | 12 ++++++------ 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/backend/include/bs-utility.h b/backend/include/bs-utility.h index f08a5cd..325cce7 100644 --- a/backend/include/bs-utility.h +++ b/backend/include/bs-utility.h @@ -82,6 +82,7 @@ class ReadWriteLock { void endWrite(); }; class SessionClass { + public: std::stack login_stack; std::string SessionToken; std::string OuthorizationKey; diff --git a/backend/src/builtin-cli.cpp b/backend/src/builtin-cli.cpp index 92d67ca..6d2b0db 100644 --- a/backend/src/builtin-cli.cpp +++ b/backend/src/builtin-cli.cpp @@ -7,7 +7,35 @@ void BookStoreMain(bool is_server, std::string config_dir) { std::ios::sync_with_stdio(false); if (!is_server) { - ; // TODO: run as client + BlockingStringStream input; + BlockingStringStream output; + BookStoreBackEndClass backend(config_dir, &input, &output); + std::thread backend_thread([&backend]() { backend.Run(); }); + input << "#OpenSession INNERCLI\n"; + std::string SessionToken, AuthenticationKey, tmp; + output.getline(tmp); + output >> SessionToken >> AuthenticationKey; + debugPrint("SessionToken=", SessionToken, + " AuthenticationKey=", AuthenticationKey); + std::string cmd; + while (getline(std::cin, cmd)) { + if (cmd == "quit" || cmd == "exit") { + input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey + << '\n'; + input << "#ShutDownSystem\n"; + backend_thread.join(); + return; + } + input << "#Request " << SessionToken << " I-T-D " << AuthenticationKey + << ' ' << cmd << '\n'; + output.getline(tmp); + std::cout << tmp << std::endl; + } + input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey + << '\n'; + input << "#ShutDownSystem\n"; + backend_thread.join(); + return; } else { std::cin.tie(nullptr); std::cout.rdbuf(nullptr); diff --git a/backend/src/main.cpp b/backend/src/main.cpp index cb932d9..d8648bb 100644 --- a/backend/src/main.cpp +++ b/backend/src/main.cpp @@ -5,8 +5,6 @@ #include "builtin-cli.h" #include "clipp/clipp.h" int main(int argc, char **argv) { - // test(); - // return 0; bool is_server = false; std::string config_dir = ""; bool custom_config_dir = false; diff --git a/backend/src/schedule.cpp b/backend/src/schedule.cpp index 62947a8..b0e434e 100644 --- a/backend/src/schedule.cpp +++ b/backend/src/schedule.cpp @@ -1,13 +1,34 @@ #include "schedule.h" -#include "engine.h" -void BookStoreBackEndClass::Run() -{ +#include +#include + +#include "engine.h" +void BookStoreBackEndClass::Run() { std::string request_data; - while(true) - { + const unsigned int RndSeed = std::random_device{}(); + std::mt19937 rnd(RndSeed); + while (true) { input_ptr->getline(request_data, '\n'); - // std::string response_data = Engine::Process(request_data); - // output_ptr << response_data << '\n'; + debugPrint("request_data=", request_data); + if (request_data[1] == 'O') // #OpenSession [TempChannelID] + { + std::stringstream ss; + ss << request_data; + ss >> request_data; + std::string TempChannelID; + ss >> TempChannelID; + SessionClass new_session; + std::string new_SessionToken; + std::string new_AuthenticationKey; + for (int i = 0; i < 16; i++) new_SessionToken.push_back(rnd() % 26 + 'A'); + for (int i = 0; i < 16; i++) + new_AuthenticationKey.push_back(rnd() % 26 + 'A'); + new_session.SessionToken = new_SessionToken; + new_session.OuthorizationKey = new_AuthenticationKey; + session_map[new_SessionToken] = new_session; + (*output_ptr) << TempChannelID << " IinitialOpt 1\n" + << new_SessionToken << ' ' << new_AuthenticationKey << '\n'; + } } } \ No newline at end of file diff --git a/docs/develop/总体设计文档.md b/docs/develop/总体设计文档.md index ebee4c4..ceab551 100644 --- a/docs/develop/总体设计文档.md +++ b/docs/develop/总体设计文档.md @@ -74,11 +74,11 @@ memoryriver类维护一个缓存,简单地缓存高频访问和连续访问; # 用户交互设计 ## 内部命令 -- `# OpenSession`:向调度模块申请一个新会话 -- `# CloseSession [SessionToken] [OuthenticationKey]`:显示地告知调度模块停止某个会话 -- `# ReRequest [SessionToken] [OperationToken] [OuthenticationKey]`:向服务端重新请求调取某次操作响应的缓存 -- `# Request [SessionToken] [OuthenticationKey] # [UserCommand]`:向后端发送一个请求 -- `# ShutDownSystem`:关闭整个系统 +- `#OpenSession [TempChannelID]`:向调度模块申请一个新会话 +- `#CloseSession [SessionToken] [OuthenticationKey]`:显示地告知调度模块停止某个会话 +- `#_Request [SessionToken] [OperationToken] [OuthenticationKey]`:向服务端重新请求调取某次操作响应的缓存 +- `#Request [SessionToken] [OperationToken] [OuthenticationKey] [UserCommand]`:向后端发送一个请求 +- `#ShutDownSystem`:关闭整个系统 ## 向用户提供的命令 - `quit`和`exit`:正常退出系统 @@ -107,7 +107,7 @@ memoryriver类维护一个缓存,简单地缓存高频访问和连续访问; - WebUI:图形界面 ## 响应 -注意,对于申请Session时,响应用`[TempChannelID]`替代`[SessionToken]`,返回信息内容是`[SessionToken]`和`[AuthenticationKey]`。 +注意,对于申请Session时,响应用`[TempChannelID]`替代`[SessionToken]`,仅用于保证信息投递正确,返回信息内容是`[SessionToken]`和`[AuthenticationKey]`。 - 引擎返回:`std::string` - 调度模块返回:字符串,`[SessionToken] [OperationToken] [LineCounter]\n[ResponseContent]`,其中,`[ResponseContent]`恰有`[LineCounter]`行,每行行末有且仅有一个`\n`。输出为空通过把`[LineCounter]`设置为0来实现。 - 内置交互模块(interactive模式)返回:字符串,`[ResponseContent]`