upd: compelete IO

This commit is contained in:
2023-12-12 07:47:58 +00:00
parent ffb1a5f788
commit a0150ec6fb
4 changed files with 38 additions and 15 deletions

View File

@ -35,13 +35,14 @@ class BlockingStringStream {
BlockingStringStream &operator>>(T &val);
BlockingStringStream &getline(std::string &str, char delim = '\n');
std::stringstream internalStream;
void lock();
void unlock();
void readlock();
void unreadlock();
private:
std::mutex mutex;
std::mutex custom_mutex;
std::condition_variable condition;
std::atomic<bool> is_writing = false;
};
// Implementation of operator<<
template <typename T>
@ -64,7 +65,9 @@ BlockingStringStream &BlockingStringStream::operator>>(T &val) {
std::unique_lock<std::mutex> lock(mutex);
// Wait until data is available
condition.wait(lock, [this] { return internalStream.peek() != EOF; });
condition.wait(lock, [this] {
return internalStream.peek() != EOF && !is_writing;
});
internalStream >> val;

View File

@ -5,7 +5,9 @@ BlockingStringStream &BlockingStringStream::getline(std::string &str,
std::unique_lock<std::mutex> lock(mutex);
// Wait until data is available
condition.wait(lock, [this] { return internalStream.peek() != EOF; });
condition.wait(lock, [this] {
return internalStream.peek() != EOF && !is_writing;
});
std::getline(internalStream, str, delim);
@ -50,5 +52,5 @@ void debugPrint() {
BookStore_ZYM::debug_Print_Mutex.unlock();
}
void BlockingStringStream::lock() { custom_mutex.lock(); }
void BlockingStringStream::unlock() { custom_mutex.unlock(); }
void BlockingStringStream::readlock() { is_writing = true; }
void BlockingStringStream::unreadlock() { is_writing = false; }

View File

@ -7,40 +7,53 @@
void BookStoreMain(bool is_server, std::string 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::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;
}
input << "#Request " << SessionToken << " I-T-D " << AuthenticationKey
<< ' ' << cmd << '\n';
input.readlock();
input << "#Request " << SessionToken << " I-T-D" << ++cnt << " "
<< AuthenticationKey << ' ' << cmd << '\n';
input.unreadlock();
std::string SessionToken;
std::string OperationToken;
int LineCounter;
continue;
output >> SessionToken >> OperationToken >> 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;
}
}
input.readlock();
input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey
<< '\n';
input << "#ShutDownSystem\n";
input.unreadlock();
backend_thread.join();
return;
} else {
@ -53,7 +66,9 @@ void BookStoreMain(bool is_server, std::string config_dir) {
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]() {

View File

@ -28,8 +28,10 @@ void BookStoreBackEndClass::Run() {
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] == '_') {
@ -45,14 +47,15 @@ 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) << SessionToken << ' ' << OperationToken << " 0\n";
(*output_ptr) << SessionToken << ' ' << OperationToken
<< " 1\nECHO: " << cmd << '\n';
// debugPrint("SessionToken=", SessionToken,
// " OperationToken=", OperationToken,
// " OuthenticationKey=", OuthenticationKey, " cmd=", cmd);
// std::cerr << "[]" << SessionToken << ' ' << OperationToken
// << " 1\nECHO: " << cmd << '\n';
(*output_ptr).readlock();
(*output_ptr) << SessionToken << ' ' << OperationToken
<< " 1\nECHO: " << cmd << '\n';
(*output_ptr).unreadlock();
}
}
}