upd: setting up structure

This commit is contained in:
2023-12-12 12:05:49 +00:00
parent 8065377db3
commit d8b62d568d
4 changed files with 44 additions and 15 deletions

View File

@ -11,6 +11,7 @@
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
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<std::string> &ret);
#endif // PROTECTOR_UTILITY_H

View File

@ -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

View File

@ -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<std::string> & 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();
}

View File

@ -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<std::string>({"[Error] AuthenticationKey is wrong"}));
return;
}
Respond(output_ptr, SessionToken, OperationToken, AuthenticationKey,
std::vector<std::string>({cmd}));
}