From d8b62d568df6989b0246c87e4323ef75f9cb4730 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Tue, 12 Dec 2023 12:05:49 +0000 Subject: [PATCH] upd: setting up structure --- backend/include/bs-utility.h | 5 +++++ backend/include/schedule.h | 2 ++ backend/src/bs-utility.cpp | 13 ++++++++++++ backend/src/schedule.cpp | 39 ++++++++++++++++++++++-------------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/backend/include/bs-utility.h b/backend/include/bs-utility.h index 5b158fa..e15a385 100644 --- a/backend/include/bs-utility.h +++ b/backend/include/bs-utility.h @@ -11,6 +11,7 @@ #include #include #include +#include class FatalError : public std::exception { public: FatalError(const char *__message, int __code) @@ -104,4 +105,8 @@ void debugPrint(Args... args) { std::cerr << std::endl; BookStore_ZYM::debug_Print_Mutex.unlock(); } + +void Respond(BlockingStringStream *output_ptr, std::string SessionToken, + std::string OperationToken, std::string AuthenticationKey, + const std::vector &ret); #endif // PROTECTOR_UTILITY_H \ No newline at end of file diff --git a/backend/include/schedule.h b/backend/include/schedule.h index 5c18994..982d24e 100644 --- a/backend/include/schedule.h +++ b/backend/include/schedule.h @@ -22,5 +22,7 @@ class BookStoreBackEndClass { } ~BookStoreBackEndClass() { delete engine_ptr; } void Run(); + void PostRequest(std::string SessionToken, std::string OperationToken, + std::string AuthenticationKey, std::string cmd); }; #endif // PROTECTOR_SCHEDULE_H \ No newline at end of file diff --git a/backend/src/bs-utility.cpp b/backend/src/bs-utility.cpp index d7da0d6..c56564f 100644 --- a/backend/src/bs-utility.cpp +++ b/backend/src/bs-utility.cpp @@ -62,4 +62,17 @@ void BlockingStringStream::unreadlock() { is_writing = false; mutex.unlock(); condition.notify_all(); +} + +void Respond(BlockingStringStream *output_ptr, std::string SessionToken, + std::string OperationToken, std::string AuthenticationKey, + const std::vector & ret) { + static std::mutex output_mutex; + output_mutex.lock(); + (*output_ptr).readlock(); + (*output_ptr) << SessionToken << ' ' << OperationToken << " " << ret.size() + << '\n'; + for (int i = 0; i < ret.size(); i++) (*output_ptr) << ret[i] << '\n'; + (*output_ptr).unreadlock(); + output_mutex.unlock(); } \ No newline at end of file diff --git a/backend/src/schedule.cpp b/backend/src/schedule.cpp index 26821e1..d2cfe49 100644 --- a/backend/src/schedule.cpp +++ b/backend/src/schedule.cpp @@ -32,14 +32,20 @@ void BookStoreBackEndClass::Run() { (*output_ptr).readlock(); (*output_ptr) << TempChannelID << " IinitialOpt 1\n" << new_SessionToken << ' ' << new_AuthenticationKey << '\n'; - // assert((*output_ptr).internalStream.peek() != EOF); (*output_ptr).unreadlock(); - // assert((*output_ptr).is_writing == false); - // debugPrint("Sent Response Init"); } else if (request_data[1] == 'C') { ; } else if (request_data[1] == '_') { - ; + std::stringstream ss(request_data); + std::string SessionToken; + std::string OperationToken; + std::string OuthenticationKey; + std::string cmd; + ss >> cmd >> SessionToken >> OperationToken >> OuthenticationKey; + (*output_ptr).readlock(); + (*output_ptr) << SessionToken << ' ' << OperationToken << " 1\n" + << "[Internal Error] This API shouldn't be called\n"; + (*output_ptr).unreadlock(); } else if (request_data[1] == 'S') { return; } else if (request_data[1] == 'R') { @@ -51,17 +57,20 @@ void BookStoreBackEndClass::Run() { ss >> cmd >> SessionToken >> OperationToken >> OuthenticationKey; ss.get(); std::getline(ss, cmd); - // debugPrint("SessionToken=", SessionToken, - // " OperationToken=", OperationToken, - // " OuthenticationKey=", OuthenticationKey, " cmd=", cmd); - (*output_ptr).readlock(); - (*output_ptr) << SessionToken << ' ' << OperationToken << " 1\n" - << cmd << '\n'; - // assert((*output_ptr).internalStream.peek() != EOF); - (*output_ptr).unreadlock(); - // assert((*output_ptr).is_writing == false); - // debugPrint("Sent Response id=", OperationToken); - // debugPrint(SessionToken, ' ', OperationToken, " 1\n", cmd); + PostRequest(SessionToken, OperationToken, OuthenticationKey, cmd); } } +} + +void BookStoreBackEndClass::PostRequest(std::string SessionToken, + std::string OperationToken, + std::string AuthenticationKey, + std::string cmd) { + if (session_map[SessionToken].OuthorizationKey != AuthenticationKey) { + Respond(output_ptr, SessionToken, OperationToken, AuthenticationKey, + std::vector({"[Error] AuthenticationKey is wrong"})); + return; + } + Respond(output_ptr, SessionToken, OperationToken, AuthenticationKey, + std::vector({cmd})); } \ No newline at end of file