diff --git a/backend/include/bs-utility.h b/backend/include/bs-utility.h index 9a92a4f..63bd8d2 100644 --- a/backend/include/bs-utility.h +++ b/backend/include/bs-utility.h @@ -96,9 +96,11 @@ class ReadWriteLock { }; class SessionClass { public: - std::stack login_stack; + std::stack> login_stack; std::string SessionToken; std::string OuthorizationKey; + SessionClass(std::string _SessionToken, std::string _OuthorizationKey) + : SessionToken(_SessionToken), OuthorizationKey(_OuthorizationKey) {} }; void debugPrint(); @@ -158,4 +160,6 @@ enum OperationType { __Klog, __Kreport, }; + +std::string GenerateRandomString(int length); #endif // PROTECTOR_UTILITY_H \ No newline at end of file diff --git a/backend/include/engine.h b/backend/include/engine.h index 5b1ece6..819c36e 100644 --- a/backend/include/engine.h +++ b/backend/include/engine.h @@ -18,6 +18,7 @@ class BookStoreEngineClass { public: BookStoreEngineClass() = delete; BookStoreEngineClass(std::string __config_dir, bool __is_server); + std::string QueryUserInfo(const std::string &user_name); std::vector Execute( const std::string &cmd, std::stack> &login_stack); diff --git a/backend/src/bs-utility.cpp b/backend/src/bs-utility.cpp index 7f03bc4..5582d1f 100644 --- a/backend/src/bs-utility.cpp +++ b/backend/src/bs-utility.cpp @@ -1,5 +1,6 @@ #include "bs-utility.h" +#include namespace BookStore_ZYM { std::mutex debug_Print_Mutex; bool shut_down = false; @@ -78,4 +79,21 @@ void Respond(BlockingStringStream *output_ptr, std::string SessionToken, for (int i = 0; i < ret.size(); i++) (*output_ptr) << ret[i] << '\n'; (*output_ptr).unreadlock(); output_mutex.unlock(); +} + +std::string GenerateRandomString(int length) { + static std::random_device rd; + static std::mt19937 gen(rd()); + static std::uniform_int_distribution<> dis(0, 61); + std::string ret; + for (int i = 0; i < length; i++) { + int x = dis(gen); + if (x < 10) + ret += '0' + x; + else if (x < 36) + ret += 'a' + x - 10; + else + ret += 'A' + x - 36; + } + return ret; } \ No newline at end of file diff --git a/backend/src/builtin-cli.cpp b/backend/src/builtin-cli.cpp index fe6dc04..accec2f 100644 --- a/backend/src/builtin-cli.cpp +++ b/backend/src/builtin-cli.cpp @@ -26,7 +26,89 @@ void BookStoreMain(bool is_server, std::string config_dir) { if (BookStore_ZYM::shut_down) return; } } else { - throw FatalError("Server mode has not been implemented yet", 1); + // throw FatalError("Server mode has not been implemented yet", 1); std::unordered_map session_map; + std::string cmd; + while (std::getline(std::cin, cmd)) { + if (cmd[1] == 'O') //`#OpenSession [TempChannelID]` + { + std::string new_session_token = GenerateRandomString(10); + std::string new_outh_token = GenerateRandomString(16); + session_map[new_session_token] = + SessionClass(new_session_token, new_outh_token); + std::stringstream ss(cmd); + std::string temp_channel_id; + ss >> temp_channel_id; + ss >> temp_channel_id; + std::cout << temp_channel_id << " Init 1\n" + << new_session_token << ' ' << new_outh_token << std::endl; + } else if (cmd[1] == 'S') + return; + else if (cmd[1] == 'C') { + 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; + continue; + } + if (session_map[session_token].OuthorizationKey != authentic_key) { + std::cout << session_token << ' ' << operation_token << " -1" + << std::endl; + continue; + } + session_map.erase(session_token); + std::cout << session_token << ' ' << operation_token << " 0" + << std::endl; + } else if (cmd[1] == 'W') { + 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; + continue; + } + if (session_map[session_token].OuthorizationKey != authentic_key) { + std::cout << session_token << ' ' << operation_token << " -1" + << std::endl; + continue; + } + if (session_map[session_token].login_stack.size()) + std::cout << session_token << ' ' << operation_token << " 1\n" + << engine.QueryUserInfo( + session_map[session_token].login_stack.top().first) + << std::endl; + else { + std::cout << session_token << ' ' << operation_token + << " 1\n[nobody] -1" << std::endl; + } + } else if (cmd[1] == 'R') { + 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; + continue; + } + if (session_map[session_token].OuthorizationKey != authentic_key) { + std::cout << session_token << ' ' << operation_token << " -1" + << std::endl; + continue; + } + std::getline(std::cin, cmd); + auto ret = std::move( + engine.ExecuteRegister(cmd, + session_map[session_token].login_stack)); + std::cout << session_token << ' ' << operation_token << " " + << ret.size() << std::endl; + for (auto &line : ret) std::cout << line << std::endl; + } + } } } \ No newline at end of file diff --git a/backend/src/engine.cpp b/backend/src/engine.cpp index c651cd9..6f7cc8a 100644 --- a/backend/src/engine.cpp +++ b/backend/src/engine.cpp @@ -451,4 +451,8 @@ std::vector BookStoreEngineClass::ExecuteReport( return ret; } return std::vector(); +} + +std::string BookStoreEngineClass::QueryUserInfo(const std::string &user_name) { + return user_name + std::to_string(user_data_base.GetPrevilege(user_name)); } \ No newline at end of file diff --git a/docs/develop/总体设计文档.md b/docs/develop/总体设计文档.md index 9f1573f..0a6ac3b 100644 --- a/docs/develop/总体设计文档.md +++ b/docs/develop/总体设计文档.md @@ -69,7 +69,7 @@ memoryriver类维护一个缓存,简单地缓存高频访问和连续访问; ### 前端向后端 - `#OpenSession [TempChannelID]`:向调度模块申请一个新会话 - `#CloseSession [SessionToken] [OperationToken] [OuthenticationKey]`:显示地告知调度模块停止某个会话 -- `#Request [SessionToken] [OperationToken] [OuthenticationKey] [UserCommand]`:向后端发送一个请求 +- `#Request [SessionToken] [OperationToken] [OuthenticationKey]\n[UserCommand]`:向后端发送一个请求 - `#Who [SessionToken] [OperationToken] [OuthenticationKey]`:查询自己是谁和权限 - `#ShutDownSystem`:关闭整个系统