From a0150ec6fb39129fd9147ecbfa1fd92c67820222 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Tue, 12 Dec 2023 07:47:58 +0000 Subject: [PATCH] upd: compelete IO --- backend/include/bs-utility.h | 9 ++++++--- backend/src/bs-utility.cpp | 8 +++++--- backend/src/builtin-cli.cpp | 21 ++++++++++++++++++--- backend/src/schedule.cpp | 15 +++++++++------ 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/backend/include/bs-utility.h b/backend/include/bs-utility.h index c9e5cfe..c35b051 100644 --- a/backend/include/bs-utility.h +++ b/backend/include/bs-utility.h @@ -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 is_writing = false; }; // Implementation of operator<< template @@ -64,7 +65,9 @@ BlockingStringStream &BlockingStringStream::operator>>(T &val) { std::unique_lock 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; diff --git a/backend/src/bs-utility.cpp b/backend/src/bs-utility.cpp index e584e4a..5d7c48d 100644 --- a/backend/src/bs-utility.cpp +++ b/backend/src/bs-utility.cpp @@ -5,7 +5,9 @@ BlockingStringStream &BlockingStringStream::getline(std::string &str, std::unique_lock 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(); } \ No newline at end of file +void BlockingStringStream::readlock() { is_writing = true; } +void BlockingStringStream::unreadlock() { is_writing = false; } \ No newline at end of file diff --git a/backend/src/builtin-cli.cpp b/backend/src/builtin-cli.cpp index c77736c..3821081 100644 --- a/backend/src/builtin-cli.cpp +++ b/backend/src/builtin-cli.cpp @@ -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]() { diff --git a/backend/src/schedule.cpp b/backend/src/schedule.cpp index c84c61c..9b95546 100644 --- a/backend/src/schedule.cpp +++ b/backend/src/schedule.cpp @@ -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(); } } } \ No newline at end of file