upd: restructrued to avoid useless concurrency and simplify the code
This commit is contained in:
@ -96,7 +96,8 @@ class SessionClass {
|
||||
};
|
||||
namespace BookStore_ZYM {
|
||||
extern std::mutex debug_Print_Mutex;
|
||||
}
|
||||
extern bool shut_down;
|
||||
} // namespace BookStore_ZYM
|
||||
void debugPrint();
|
||||
template <typename... Args>
|
||||
void debugPrint(Args... args) {
|
||||
|
@ -1,11 +1,14 @@
|
||||
#ifndef PROTECTOR_ENGINE_H
|
||||
#define PROTECTOR_ENGINE_H
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
class BookStoreEngineClass {
|
||||
std::string config_dir;
|
||||
|
||||
public:
|
||||
BookStoreEngineClass() = delete;
|
||||
BookStoreEngineClass(std::string config_dir) : config_dir(config_dir) {}
|
||||
std::vector<std::string> Execute(const std::string &cmd, std::stack<std::string> &login_stack);
|
||||
};
|
||||
#endif // PROTECTOR_ENGINE_H
|
@ -1,29 +0,0 @@
|
||||
#ifndef PROTECTOR_SCHEDULE_H
|
||||
#define PROTECTOR_SCHEDULE_H
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "bs-utility.h"
|
||||
#include "engine.h"
|
||||
class BookStoreBackEndClass {
|
||||
std::string config_dir;
|
||||
BlockingStringStream *input_ptr;
|
||||
BlockingStringStream *output_ptr;
|
||||
BookStoreEngineClass *engine_ptr;
|
||||
std::unordered_map<std::string, SessionClass> session_map;
|
||||
std::unordered_map<std::string, std::queue<std::thread>> worker_theads_queue;
|
||||
|
||||
public:
|
||||
BookStoreBackEndClass() = delete;
|
||||
BookStoreBackEndClass(std::string config_dir, BlockingStringStream *input_ptr,
|
||||
BlockingStringStream *output_ptr)
|
||||
: config_dir(config_dir), input_ptr(input_ptr), output_ptr(output_ptr) {
|
||||
engine_ptr = new BookStoreEngineClass(config_dir);
|
||||
}
|
||||
~BookStoreBackEndClass() { delete engine_ptr; }
|
||||
void Run();
|
||||
void PostRequest(std::string SessionToken, std::string OperationToken,
|
||||
std::string AuthenticationKey, std::string cmd);
|
||||
};
|
||||
#endif // PROTECTOR_SCHEDULE_H
|
@ -45,7 +45,8 @@ void ReadWriteLock::endWrite() {
|
||||
}
|
||||
namespace BookStore_ZYM {
|
||||
std::mutex debug_Print_Mutex;
|
||||
}
|
||||
bool shut_down = false;
|
||||
} // namespace BookStore_ZYM
|
||||
void debugPrint() {
|
||||
BookStore_ZYM::debug_Print_Mutex.lock();
|
||||
std::cerr << std::endl;
|
||||
@ -66,7 +67,7 @@ void BlockingStringStream::unreadlock() {
|
||||
|
||||
void Respond(BlockingStringStream *output_ptr, std::string SessionToken,
|
||||
std::string OperationToken, std::string AuthenticationKey,
|
||||
const std::vector<std::string> & ret) {
|
||||
const std::vector<std::string> &ret) {
|
||||
static std::mutex output_mutex;
|
||||
output_mutex.lock();
|
||||
(*output_ptr).readlock();
|
||||
|
@ -2,93 +2,25 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
#include "bs-utility.h"
|
||||
#include "schedule.h"
|
||||
#include "engine.h"
|
||||
void BookStoreMain(bool is_server, std::string config_dir) {
|
||||
BookStoreEngineClass engine(config_dir);
|
||||
std::ios::sync_with_stdio(false);
|
||||
if (!is_server) {
|
||||
int cnt = 0;
|
||||
BlockingStringStream input;
|
||||
BlockingStringStream output;
|
||||
BookStoreBackEndClass backend(config_dir, &input, &output);
|
||||
std::thread backend_thread([&backend]() { backend.Run(); });
|
||||
input.readlock();
|
||||
input << "#OpenSession INNERCLI\n";
|
||||
input.unreadlock();
|
||||
std::string SessionToken, AuthenticationKey, tmp;
|
||||
output.getline(tmp);
|
||||
output >> SessionToken >> AuthenticationKey;
|
||||
// debugPrint("SessionToken=", SessionToken,
|
||||
// " AuthenticationKey=", AuthenticationKey);
|
||||
std::stack<std::string> login_stack;
|
||||
std::string cmd;
|
||||
output.getline(tmp);
|
||||
while (getline(std::cin, cmd)) {
|
||||
if (cmd == "quit" || cmd == "exit") {
|
||||
input.readlock();
|
||||
input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey
|
||||
<< '\n';
|
||||
input << "#ShutDownSystem\n";
|
||||
input.unreadlock();
|
||||
backend_thread.join();
|
||||
return;
|
||||
while (std::getline(std::cin, cmd)) {
|
||||
auto result = std::move(engine.Execute(cmd, login_stack));
|
||||
for (auto &line : result) {
|
||||
std::cout << line << std::endl;
|
||||
}
|
||||
input.readlock();
|
||||
input << "#Request " << SessionToken << " I-T-D" << ++cnt << " "
|
||||
<< AuthenticationKey << ' ' << cmd << '\n';
|
||||
// assert(input.internalStream.peek() != EOF);
|
||||
input.unreadlock();
|
||||
// assert(input.is_writing == false);
|
||||
// debugPrint("Sent Request ", cnt, " cmd=", cmd);
|
||||
std::string SessionToken;
|
||||
std::string OperationToken;
|
||||
int LineCounter;
|
||||
output >> SessionToken >> OperationToken >> LineCounter;
|
||||
// debugPrint("Get the Head of response id=", OperationToken,
|
||||
// " LineCounter=", LineCounter);
|
||||
// debugPrint("Get SessionToken=", SessionToken,
|
||||
// " OperationToken=", OperationToken,
|
||||
// " LineCounter=", LineCounter);
|
||||
output.getline(tmp);
|
||||
for (int i = 0; i < LineCounter; i++) {
|
||||
output.getline(tmp);
|
||||
std::cout << tmp << std::endl;
|
||||
// std::cerr << tmp << std::endl;
|
||||
// debugPrint(tmp);
|
||||
}
|
||||
// std::cout.flush();
|
||||
if (BookStore_ZYM::shut_down) return;
|
||||
}
|
||||
input.readlock();
|
||||
input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey
|
||||
<< '\n';
|
||||
input << "#ShutDownSystem\n";
|
||||
input.unreadlock();
|
||||
backend_thread.join();
|
||||
return;
|
||||
} else {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::cout.rdbuf(nullptr);
|
||||
BlockingStringStream input;
|
||||
BlockingStringStream output;
|
||||
BookStoreBackEndClass backend(config_dir, &input, &output);
|
||||
std::thread backend_thread([&backend]() { backend.Run(); });
|
||||
std::thread input_thread([&input]() {
|
||||
std::string data;
|
||||
while (std::getline(std::cin, data)) {
|
||||
input.readlock();
|
||||
input << data << '\n';
|
||||
input.unreadlock();
|
||||
}
|
||||
});
|
||||
std::thread output_thread([&output]() {
|
||||
std::string data;
|
||||
while (true) {
|
||||
output.getline(data, '\n');
|
||||
std::cout << data << std::endl;
|
||||
}
|
||||
});
|
||||
input_thread.join();
|
||||
output_thread.join();
|
||||
backend_thread.join();
|
||||
throw FatalError("Not implemented yet", 1);
|
||||
}
|
||||
}
|
@ -1 +1,14 @@
|
||||
#include "engine.h"
|
||||
#include "engine.h"
|
||||
|
||||
#include <stack>
|
||||
#include <string>
|
||||
|
||||
#include "bs-utility.h"
|
||||
std::vector<std::string> BookStoreEngineClass::Execute(
|
||||
const std::string &cmd, std::stack<std::string> &login_stack) {
|
||||
if (cmd == "quit" || cmd == "exit") {
|
||||
BookStore_ZYM::shut_down = true;
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
return std::vector<std::string>({cmd});
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
#include "schedule.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
|
||||
#include "engine.h"
|
||||
void BookStoreBackEndClass::Run() {
|
||||
std::string request_data;
|
||||
const unsigned int RndSeed = std::random_device{}();
|
||||
std::mt19937 rnd(RndSeed);
|
||||
while (true) {
|
||||
input_ptr->getline(request_data, '\n');
|
||||
// debugPrint("Get_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).readlock();
|
||||
(*output_ptr) << TempChannelID << " IinitialOpt 1\n"
|
||||
<< new_SessionToken << ' ' << new_AuthenticationKey << '\n';
|
||||
(*output_ptr).unreadlock();
|
||||
} 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') {
|
||||
std::stringstream ss(request_data);
|
||||
std::string SessionToken;
|
||||
std::string OperationToken;
|
||||
std::string OuthenticationKey;
|
||||
std::string cmd;
|
||||
ss >> cmd >> SessionToken >> OperationToken >> OuthenticationKey;
|
||||
ss.get();
|
||||
std::getline(ss, 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}));
|
||||
}
|
Reference in New Issue
Block a user