upd: compelete IO
This commit is contained in:
@ -35,13 +35,14 @@ class BlockingStringStream {
|
|||||||
BlockingStringStream &operator>>(T &val);
|
BlockingStringStream &operator>>(T &val);
|
||||||
BlockingStringStream &getline(std::string &str, char delim = '\n');
|
BlockingStringStream &getline(std::string &str, char delim = '\n');
|
||||||
std::stringstream internalStream;
|
std::stringstream internalStream;
|
||||||
void lock();
|
void readlock();
|
||||||
void unlock();
|
void unreadlock();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
std::mutex custom_mutex;
|
std::mutex custom_mutex;
|
||||||
std::condition_variable condition;
|
std::condition_variable condition;
|
||||||
|
std::atomic<bool> is_writing = false;
|
||||||
};
|
};
|
||||||
// Implementation of operator<<
|
// Implementation of operator<<
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -64,7 +65,9 @@ BlockingStringStream &BlockingStringStream::operator>>(T &val) {
|
|||||||
std::unique_lock<std::mutex> lock(mutex);
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
|
||||||
// Wait until data is available
|
// 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;
|
internalStream >> val;
|
||||||
|
|
||||||
|
@ -5,7 +5,9 @@ BlockingStringStream &BlockingStringStream::getline(std::string &str,
|
|||||||
std::unique_lock<std::mutex> lock(mutex);
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
|
||||||
// Wait until data is available
|
// 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);
|
std::getline(internalStream, str, delim);
|
||||||
|
|
||||||
@ -50,5 +52,5 @@ void debugPrint() {
|
|||||||
BookStore_ZYM::debug_Print_Mutex.unlock();
|
BookStore_ZYM::debug_Print_Mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BlockingStringStream::lock() { custom_mutex.lock(); }
|
void BlockingStringStream::readlock() { is_writing = true; }
|
||||||
void BlockingStringStream::unlock() { custom_mutex.unlock(); }
|
void BlockingStringStream::unreadlock() { is_writing = false; }
|
@ -7,40 +7,53 @@
|
|||||||
void BookStoreMain(bool is_server, std::string config_dir) {
|
void BookStoreMain(bool is_server, std::string config_dir) {
|
||||||
std::ios::sync_with_stdio(false);
|
std::ios::sync_with_stdio(false);
|
||||||
if (!is_server) {
|
if (!is_server) {
|
||||||
|
int cnt = 0;
|
||||||
BlockingStringStream input;
|
BlockingStringStream input;
|
||||||
BlockingStringStream output;
|
BlockingStringStream output;
|
||||||
BookStoreBackEndClass backend(config_dir, &input, &output);
|
BookStoreBackEndClass backend(config_dir, &input, &output);
|
||||||
std::thread backend_thread([&backend]() { backend.Run(); });
|
std::thread backend_thread([&backend]() { backend.Run(); });
|
||||||
|
input.readlock();
|
||||||
input << "#OpenSession INNERCLI\n";
|
input << "#OpenSession INNERCLI\n";
|
||||||
|
input.unreadlock();
|
||||||
std::string SessionToken, AuthenticationKey, tmp;
|
std::string SessionToken, AuthenticationKey, tmp;
|
||||||
output.getline(tmp);
|
output.getline(tmp);
|
||||||
output >> SessionToken >> AuthenticationKey;
|
output >> SessionToken >> AuthenticationKey;
|
||||||
debugPrint("SessionToken=", SessionToken,
|
debugPrint("SessionToken=", SessionToken,
|
||||||
" AuthenticationKey=", AuthenticationKey);
|
" AuthenticationKey=", AuthenticationKey);
|
||||||
std::string cmd;
|
std::string cmd;
|
||||||
|
output.getline(tmp);
|
||||||
while (getline(std::cin, cmd)) {
|
while (getline(std::cin, cmd)) {
|
||||||
if (cmd == "quit" || cmd == "exit") {
|
if (cmd == "quit" || cmd == "exit") {
|
||||||
|
input.readlock();
|
||||||
input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey
|
input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey
|
||||||
<< '\n';
|
<< '\n';
|
||||||
input << "#ShutDownSystem\n";
|
input << "#ShutDownSystem\n";
|
||||||
|
input.unreadlock();
|
||||||
backend_thread.join();
|
backend_thread.join();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
input << "#Request " << SessionToken << " I-T-D " << AuthenticationKey
|
input.readlock();
|
||||||
<< ' ' << cmd << '\n';
|
input << "#Request " << SessionToken << " I-T-D" << ++cnt << " "
|
||||||
|
<< AuthenticationKey << ' ' << cmd << '\n';
|
||||||
|
input.unreadlock();
|
||||||
std::string SessionToken;
|
std::string SessionToken;
|
||||||
std::string OperationToken;
|
std::string OperationToken;
|
||||||
int LineCounter;
|
int LineCounter;
|
||||||
continue;
|
|
||||||
output >> SessionToken >> OperationToken >> LineCounter;
|
output >> SessionToken >> OperationToken >> LineCounter;
|
||||||
|
// debugPrint("Get SessionToken=", SessionToken,
|
||||||
|
// " OperationToken=", OperationToken,
|
||||||
|
// " LineCounter=", LineCounter);
|
||||||
|
output.getline(tmp);
|
||||||
for (int i = 0; i < LineCounter; i++) {
|
for (int i = 0; i < LineCounter; i++) {
|
||||||
output.getline(tmp);
|
output.getline(tmp);
|
||||||
std::cout << tmp << std::endl;
|
std::cout << tmp << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
input.readlock();
|
||||||
input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey
|
input << "#CloseSession " << SessionToken << ' ' << AuthenticationKey
|
||||||
<< '\n';
|
<< '\n';
|
||||||
input << "#ShutDownSystem\n";
|
input << "#ShutDownSystem\n";
|
||||||
|
input.unreadlock();
|
||||||
backend_thread.join();
|
backend_thread.join();
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
@ -53,7 +66,9 @@ void BookStoreMain(bool is_server, std::string config_dir) {
|
|||||||
std::thread input_thread([&input]() {
|
std::thread input_thread([&input]() {
|
||||||
std::string data;
|
std::string data;
|
||||||
while (std::getline(std::cin, data)) {
|
while (std::getline(std::cin, data)) {
|
||||||
|
input.readlock();
|
||||||
input << data << '\n';
|
input << data << '\n';
|
||||||
|
input.unreadlock();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
std::thread output_thread([&output]() {
|
std::thread output_thread([&output]() {
|
||||||
|
@ -28,8 +28,10 @@ void BookStoreBackEndClass::Run() {
|
|||||||
new_session.SessionToken = new_SessionToken;
|
new_session.SessionToken = new_SessionToken;
|
||||||
new_session.OuthorizationKey = new_AuthenticationKey;
|
new_session.OuthorizationKey = new_AuthenticationKey;
|
||||||
session_map[new_SessionToken] = new_session;
|
session_map[new_SessionToken] = new_session;
|
||||||
|
(*output_ptr).readlock();
|
||||||
(*output_ptr) << TempChannelID << " IinitialOpt 1\n"
|
(*output_ptr) << TempChannelID << " IinitialOpt 1\n"
|
||||||
<< new_SessionToken << ' ' << new_AuthenticationKey << '\n';
|
<< new_SessionToken << ' ' << new_AuthenticationKey << '\n';
|
||||||
|
(*output_ptr).unreadlock();
|
||||||
} else if (request_data[1] == 'C') {
|
} else if (request_data[1] == 'C') {
|
||||||
;
|
;
|
||||||
} else if (request_data[1] == '_') {
|
} else if (request_data[1] == '_') {
|
||||||
@ -45,14 +47,15 @@ void BookStoreBackEndClass::Run() {
|
|||||||
ss >> cmd >> SessionToken >> OperationToken >> OuthenticationKey;
|
ss >> cmd >> SessionToken >> OperationToken >> OuthenticationKey;
|
||||||
ss.get();
|
ss.get();
|
||||||
std::getline(ss, cmd);
|
std::getline(ss, cmd);
|
||||||
debugPrint("SessionToken=", SessionToken,
|
// debugPrint("SessionToken=", SessionToken,
|
||||||
" OperationToken=", OperationToken,
|
// " OperationToken=", OperationToken,
|
||||||
" OuthenticationKey=", OuthenticationKey, " cmd=", cmd);
|
// " OuthenticationKey=", OuthenticationKey, " cmd=", cmd);
|
||||||
// (*output_ptr) << SessionToken << ' ' << OperationToken << " 0\n";
|
|
||||||
(*output_ptr) << SessionToken << ' ' << OperationToken
|
|
||||||
<< " 1\nECHO: " << cmd << '\n';
|
|
||||||
// std::cerr << "[]" << SessionToken << ' ' << OperationToken
|
// std::cerr << "[]" << SessionToken << ' ' << OperationToken
|
||||||
// << " 1\nECHO: " << cmd << '\n';
|
// << " 1\nECHO: " << cmd << '\n';
|
||||||
|
(*output_ptr).readlock();
|
||||||
|
(*output_ptr) << SessionToken << ' ' << OperationToken
|
||||||
|
<< " 1\nECHO: " << cmd << '\n';
|
||||||
|
(*output_ptr).unreadlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user