diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b6da90d..dcabc5f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,7 @@ set(BACKEND_EXETUABLE_NAME "zts-core") if(OJ_TEST_BACKEND) set(BACKEND_EXETUABLE_NAME "code") endif() -add_executable(${BACKEND_EXETUABLE_NAME} main.cpp engine.cpp utils.cpp data.cpp) +add_executable(${BACKEND_EXETUABLE_NAME} main.cpp engine.cpp utils.cpp data.cpp user_system.cpp train_system.cpp transaction_system.cpp) target_link_libraries(${BACKEND_EXETUABLE_NAME} argparse) target_link_libraries(${BACKEND_EXETUABLE_NAME} spdlog::spdlog) target_link_libraries(${BACKEND_EXETUABLE_NAME} sockpp) diff --git a/src/engine.cpp b/src/engine.cpp index b697120..9b74c7c 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -97,317 +97,6 @@ std::string TicketSystemEngine::Execute(const std::string &command) { throw std::invalid_argument("Invalid command."); } -std::string TicketSystemEngine::AddUser(const std::string &command) { - command_id_t command_id; - sscanf(command.c_str(), "[%llu]", &command_id); - LOG->debug("command id: {}", command_id); - LOG->debug("command: {}", command); - std::stringstream command_stream(command), response_stream; - std::string token, cur_username, username, password, name, mailAddr; - uint8_t privilege; - command_stream >> token >> token; - while (command_stream >> token) { - switch (token[1]) { - case 'c': { - command_stream >> cur_username; - break; - } - case 'u': { - command_stream >> username; - break; - } - case 'p': { - command_stream >> password; - break; - } - case 'n': { - command_stream >> name; - break; - } - case 'm': { - command_stream >> mailAddr; - break; - } - case 'g': { - int tmp; - command_stream >> tmp; - privilege = tmp; - break; - } - default: - throw std::invalid_argument("arg parse fatal error in add_user"); - } - } - if (user_data.size() == 0) { - // special case, no need to check current user's privilege - FullUserData dat; - dat.privilege = 10; - strcpy(dat.username, username.c_str()); - dat.password_hash = SplitMix64Hash(password); - strcpy(dat.name, name.c_str()); - strcpy(dat.mailAddr, mailAddr.c_str()); - user_data.Put(SplitMix64Hash(username), dat); - LOG->debug("stored user_name hash: {}", SplitMix64Hash(username)); - LOG->debug("stored user_name: {}", dat.username); - LOG->debug("stored password hash: {}", dat.password_hash); - LOG->debug("stored name: {}", dat.name); - LOG->debug("stored mailAddr: {}", dat.mailAddr); - LOG->debug("stored privilege: {}", dat.privilege); - response_stream << '[' << command_id << "] 0"; - return response_stream.str(); - } - hash_t current_user_username_hash = SplitMix64Hash(cur_username); - if (online_users.find(current_user_username_hash) == online_users.end()) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - if (privilege >= online_users[current_user_username_hash]) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - hash_t new_user_username_hash = SplitMix64Hash(username); - if (user_data.HasKey(new_user_username_hash)) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - FullUserData dat; - dat.privilege = privilege; - strcpy(dat.username, username.c_str()); - dat.password_hash = SplitMix64Hash(password); - strcpy(dat.name, name.c_str()); - strcpy(dat.mailAddr, mailAddr.c_str()); - user_data.Put(new_user_username_hash, dat); - LOG->debug("stored user_name hash: {}", new_user_username_hash); - LOG->debug("stored user_name: {}", dat.username); - LOG->debug("stored password hash: {}", dat.password_hash); - LOG->debug("stored name: {}", dat.name); - LOG->debug("stored mailAddr: {}", dat.mailAddr); - LOG->debug("stored privilege: {}", dat.privilege); - response_stream << '[' << command_id << "] 0"; - return response_stream.str(); -} - -std::string TicketSystemEngine::LoginUser(const std::string &command) { - command_id_t command_id; - sscanf(command.c_str(), "[%llu]", &command_id); - LOG->debug("command id: {}", command_id); - std::stringstream command_stream(command), response_stream; - std::string token, user_name, password; - command_stream >> token >> token; - while (command_stream >> token) { - switch (token[1]) { - case 'u': { - command_stream >> user_name; - break; - } - case 'p': { - command_stream >> password; - break; - } - default: - throw std::invalid_argument("arg parse fatal error in login"); - } - } - hash_t user_name_hash = SplitMix64Hash(user_name); - hash_t password_hash = SplitMix64Hash(password); - if (online_users.find(user_name_hash) != online_users.end()) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - FullUserData dat; - try { - user_data.Get(user_name_hash, dat); - if (dat.password_hash != password_hash) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - } catch (std::runtime_error &e) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - online_users[user_name_hash] = dat.privilege; - response_stream << '[' << command_id << "] 0"; - return response_stream.str(); -} - -std::string TicketSystemEngine::LogoutUser(const std::string &command) { - command_id_t command_id; - sscanf(command.c_str(), "[%llu]", &command_id); - LOG->debug("command id: {}", command_id); - std::stringstream command_stream(command), response_stream; - std::string token, user_name; - command_stream >> token >> token; - while (command_stream >> token) { - switch (token[1]) { - case 'u': { - command_stream >> user_name; - break; - } - default: - throw std::invalid_argument("arg parse fatal error in logout"); - } - } - hash_t user_name_hash = SplitMix64Hash(user_name); - if (online_users.find(user_name_hash) == online_users.end()) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - online_users.erase(user_name_hash); - response_stream << '[' << command_id << "] 0"; - return response_stream.str(); -} - -std::string TicketSystemEngine::QueryProfile(const std::string &command) { - command_id_t command_id; - sscanf(command.c_str(), "[%llu]", &command_id); - LOG->debug("command id: {}", command_id); - std::stringstream command_stream(command), response_stream; - std::string token, current_user_name, user_name; - command_stream >> token >> token; - while (command_stream >> token) { - switch (token[1]) { - case 'c': { - command_stream >> current_user_name; - break; - } - case 'u': { - command_stream >> user_name; - break; - } - default: - throw std::invalid_argument("arg parse fatal error in query_profile"); - } - } - hash_t current_user_name_hash = SplitMix64Hash(current_user_name); - hash_t user_name_hash = SplitMix64Hash(user_name); - if (online_users.find(current_user_name_hash) == online_users.end()) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - FullUserData dat; - LOG->debug("user_name_hash: {}", user_name_hash); - LOG->debug("mailAddr: {}", dat.mailAddr); - if (current_user_name_hash != user_name_hash) { - try { - user_data.Get(user_name_hash, dat); - if (online_users[current_user_name_hash] <= dat.privilege) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - } catch (std::runtime_error &e) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - } else { - user_data.Get(user_name_hash, dat); - } - LOG->debug("mailAddr: {}", dat.mailAddr); - response_stream << '[' << command_id << "] " << dat.username << ' ' << dat.name << ' ' << dat.mailAddr << ' ' - << static_cast(dat.privilege); - return response_stream.str(); -} - -std::string TicketSystemEngine::ModifyProfile(const std::string &command) { - command_id_t command_id; - sscanf(command.c_str(), "[%llu]", &command_id); - LOG->debug("command id: {}", command_id); - std::stringstream command_stream(command), response_stream; - std::string token, current_user_name, user_name, password, name, mailAddr; - uint8_t privilege = 11; - command_stream >> token >> token; - while (command_stream >> token) { - switch (token[1]) { - case 'c': { - command_stream >> current_user_name; - break; - } - case 'u': { - command_stream >> user_name; - break; - } - case 'p': { - command_stream >> password; - break; - } - case 'n': { - command_stream >> name; - break; - } - case 'm': { - command_stream >> mailAddr; - break; - } - case 'g': { - int tmp; - command_stream >> tmp; - privilege = tmp; - break; - } - default: - throw std::invalid_argument("arg parse fatal error in add_user"); - } - } - hash_t current_user_name_hash = SplitMix64Hash(current_user_name); - hash_t user_name_hash = SplitMix64Hash(user_name); - if (online_users.find(current_user_name_hash) == online_users.end()) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - FullUserData dat; - if (current_user_name_hash != user_name_hash) { - try { - user_data.Get(user_name_hash, dat); - if (online_users[current_user_name_hash] <= dat.privilege) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - } catch (std::runtime_error &e) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - } else { - user_data.Get(user_name_hash, dat); - } - if (privilege != 11 && privilege >= online_users[current_user_name_hash]) { - response_stream << '[' << command_id << "] -1"; - return response_stream.str(); - } - if (privilege != 11) { - dat.privilege = privilege; - } - if (password != "") { - dat.password_hash = SplitMix64Hash(password); - } - if (name != "") { - strcpy(dat.name, name.c_str()); - } - if (mailAddr != "") { - strcpy(dat.mailAddr, mailAddr.c_str()); - } - user_data.Put(user_name_hash, dat); - response_stream << '[' << command_id << "] " << dat.username << ' ' << dat.name << ' ' << dat.mailAddr << ' ' - << static_cast(dat.privilege); - return response_stream.str(); -} - -std::string TicketSystemEngine::AddTrain(const std::string &command) { return "AddTrain"; } - -std::string TicketSystemEngine::DeleteTrain(const std::string &command) { return "DeleteTrain"; } - -std::string TicketSystemEngine::ReleaseTrain(const std::string &command) { return "ReleaseTrain"; } - -std::string TicketSystemEngine::QueryTrain(const std::string &command) { return "QueryTrain"; } - -std::string TicketSystemEngine::BuyTicket(const std::string &command) { return "BuyTicket"; } - -std::string TicketSystemEngine::QueryOrder(const std::string &command) { return "QueryOrder"; } - -std::string TicketSystemEngine::RefundTicket(const std::string &command) { return "RefundTicket"; } - -std::string TicketSystemEngine::QueryTransfer(const std::string &command) { return "QueryTransfer"; } - -std::string TicketSystemEngine::QueryTicket(const std::string &command) { return "QueryTicket"; } - std::string TicketSystemEngine::Clean() { return "Clean"; } std::string TicketSystemEngine::Exit(const std::string &command) { diff --git a/src/train_system.cpp b/src/train_system.cpp new file mode 100644 index 0000000..6ee1089 --- /dev/null +++ b/src/train_system.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include +#include "basic_defs.h" +#include "data.h" +#include "engine.h" +#include "utils.h" + +std::string TicketSystemEngine::AddTrain(const std::string &command) { return "AddTrain"; } + +std::string TicketSystemEngine::DeleteTrain(const std::string &command) { return "DeleteTrain"; } + +std::string TicketSystemEngine::ReleaseTrain(const std::string &command) { return "ReleaseTrain"; } + +std::string TicketSystemEngine::QueryTrain(const std::string &command) { return "QueryTrain"; } \ No newline at end of file diff --git a/src/transaction_system.cpp b/src/transaction_system.cpp new file mode 100644 index 0000000..26d7755 --- /dev/null +++ b/src/transaction_system.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include +#include "basic_defs.h" +#include "data.h" +#include "engine.h" +#include "utils.h" + +std::string TicketSystemEngine::BuyTicket(const std::string &command) { return "BuyTicket"; } + +std::string TicketSystemEngine::QueryOrder(const std::string &command) { return "QueryOrder"; } + +std::string TicketSystemEngine::RefundTicket(const std::string &command) { return "RefundTicket"; } + +std::string TicketSystemEngine::QueryTransfer(const std::string &command) { return "QueryTransfer"; } + +std::string TicketSystemEngine::QueryTicket(const std::string &command) { return "QueryTicket"; } \ No newline at end of file diff --git a/src/user_system.cpp b/src/user_system.cpp new file mode 100644 index 0000000..be327f3 --- /dev/null +++ b/src/user_system.cpp @@ -0,0 +1,302 @@ +#include +#include +#include +#include +#include +#include "basic_defs.h" +#include "data.h" +#include "engine.h" +#include "utils.h" + +std::string TicketSystemEngine::AddUser(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + LOG->debug("command: {}", command); + std::stringstream command_stream(command), response_stream; + std::string token, cur_username, username, password, name, mailAddr; + uint8_t privilege; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'c': { + command_stream >> cur_username; + break; + } + case 'u': { + command_stream >> username; + break; + } + case 'p': { + command_stream >> password; + break; + } + case 'n': { + command_stream >> name; + break; + } + case 'm': { + command_stream >> mailAddr; + break; + } + case 'g': { + int tmp; + command_stream >> tmp; + privilege = tmp; + break; + } + default: + throw std::invalid_argument("arg parse fatal error in add_user"); + } + } + if (user_data.size() == 0) { + // special case, no need to check current user's privilege + FullUserData dat; + dat.privilege = 10; + strcpy(dat.username, username.c_str()); + dat.password_hash = SplitMix64Hash(password); + strcpy(dat.name, name.c_str()); + strcpy(dat.mailAddr, mailAddr.c_str()); + user_data.Put(SplitMix64Hash(username), dat); + LOG->debug("stored user_name hash: {}", SplitMix64Hash(username)); + LOG->debug("stored user_name: {}", dat.username); + LOG->debug("stored password hash: {}", dat.password_hash); + LOG->debug("stored name: {}", dat.name); + LOG->debug("stored mailAddr: {}", dat.mailAddr); + LOG->debug("stored privilege: {}", dat.privilege); + response_stream << '[' << command_id << "] 0"; + return response_stream.str(); + } + hash_t current_user_username_hash = SplitMix64Hash(cur_username); + if (online_users.find(current_user_username_hash) == online_users.end()) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + if (privilege >= online_users[current_user_username_hash]) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + hash_t new_user_username_hash = SplitMix64Hash(username); + if (user_data.HasKey(new_user_username_hash)) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + FullUserData dat; + dat.privilege = privilege; + strcpy(dat.username, username.c_str()); + dat.password_hash = SplitMix64Hash(password); + strcpy(dat.name, name.c_str()); + strcpy(dat.mailAddr, mailAddr.c_str()); + user_data.Put(new_user_username_hash, dat); + LOG->debug("stored user_name hash: {}", new_user_username_hash); + LOG->debug("stored user_name: {}", dat.username); + LOG->debug("stored password hash: {}", dat.password_hash); + LOG->debug("stored name: {}", dat.name); + LOG->debug("stored mailAddr: {}", dat.mailAddr); + LOG->debug("stored privilege: {}", dat.privilege); + response_stream << '[' << command_id << "] 0"; + return response_stream.str(); +} + +std::string TicketSystemEngine::LoginUser(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token, user_name, password; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'u': { + command_stream >> user_name; + break; + } + case 'p': { + command_stream >> password; + break; + } + default: + throw std::invalid_argument("arg parse fatal error in login"); + } + } + hash_t user_name_hash = SplitMix64Hash(user_name); + hash_t password_hash = SplitMix64Hash(password); + if (online_users.find(user_name_hash) != online_users.end()) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + FullUserData dat; + try { + user_data.Get(user_name_hash, dat); + if (dat.password_hash != password_hash) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + } catch (std::runtime_error &e) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + online_users[user_name_hash] = dat.privilege; + response_stream << '[' << command_id << "] 0"; + return response_stream.str(); +} + +std::string TicketSystemEngine::LogoutUser(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token, user_name; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'u': { + command_stream >> user_name; + break; + } + default: + throw std::invalid_argument("arg parse fatal error in logout"); + } + } + hash_t user_name_hash = SplitMix64Hash(user_name); + if (online_users.find(user_name_hash) == online_users.end()) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + online_users.erase(user_name_hash); + response_stream << '[' << command_id << "] 0"; + return response_stream.str(); +} + +std::string TicketSystemEngine::QueryProfile(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token, current_user_name, user_name; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'c': { + command_stream >> current_user_name; + break; + } + case 'u': { + command_stream >> user_name; + break; + } + default: + throw std::invalid_argument("arg parse fatal error in query_profile"); + } + } + hash_t current_user_name_hash = SplitMix64Hash(current_user_name); + hash_t user_name_hash = SplitMix64Hash(user_name); + if (online_users.find(current_user_name_hash) == online_users.end()) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + FullUserData dat; + LOG->debug("user_name_hash: {}", user_name_hash); + LOG->debug("mailAddr: {}", dat.mailAddr); + if (current_user_name_hash != user_name_hash) { + try { + user_data.Get(user_name_hash, dat); + if (online_users[current_user_name_hash] <= dat.privilege) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + } catch (std::runtime_error &e) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + } else { + user_data.Get(user_name_hash, dat); + } + LOG->debug("mailAddr: {}", dat.mailAddr); + response_stream << '[' << command_id << "] " << dat.username << ' ' << dat.name << ' ' << dat.mailAddr << ' ' + << static_cast(dat.privilege); + return response_stream.str(); +} + +std::string TicketSystemEngine::ModifyProfile(const std::string &command) { + command_id_t command_id; + sscanf(command.c_str(), "[%llu]", &command_id); + LOG->debug("command id: {}", command_id); + std::stringstream command_stream(command), response_stream; + std::string token, current_user_name, user_name, password, name, mailAddr; + uint8_t privilege = 11; + command_stream >> token >> token; + while (command_stream >> token) { + switch (token[1]) { + case 'c': { + command_stream >> current_user_name; + break; + } + case 'u': { + command_stream >> user_name; + break; + } + case 'p': { + command_stream >> password; + break; + } + case 'n': { + command_stream >> name; + break; + } + case 'm': { + command_stream >> mailAddr; + break; + } + case 'g': { + int tmp; + command_stream >> tmp; + privilege = tmp; + break; + } + default: + throw std::invalid_argument("arg parse fatal error in add_user"); + } + } + hash_t current_user_name_hash = SplitMix64Hash(current_user_name); + hash_t user_name_hash = SplitMix64Hash(user_name); + if (online_users.find(current_user_name_hash) == online_users.end()) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + FullUserData dat; + if (current_user_name_hash != user_name_hash) { + try { + user_data.Get(user_name_hash, dat); + if (online_users[current_user_name_hash] <= dat.privilege) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + } catch (std::runtime_error &e) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + } else { + user_data.Get(user_name_hash, dat); + } + if (privilege != 11 && privilege >= online_users[current_user_name_hash]) { + response_stream << '[' << command_id << "] -1"; + return response_stream.str(); + } + if (privilege != 11) { + dat.privilege = privilege; + } + if (password != "") { + dat.password_hash = SplitMix64Hash(password); + } + if (name != "") { + strcpy(dat.name, name.c_str()); + } + if (mailAddr != "") { + strcpy(dat.mailAddr, mailAddr.c_str()); + } + user_data.Put(user_name_hash, dat); + response_stream << '[' << command_id << "] " << dat.username << ' ' << dat.name << ' ' << dat.mailAddr << ' ' + << static_cast(dat.privilege); + return response_stream.str(); +} \ No newline at end of file diff --git a/test/ojtest.py b/test/ojtest.py index 4cf2efa..f6bcc60 100755 --- a/test/ojtest.py +++ b/test/ojtest.py @@ -92,6 +92,7 @@ def RunTestGroup(name): has_unpassed_test=True continue if disk_limit<0: + print("removing disk files") disk_limit = -disk_limit # remove directory /tmp/$name shutil.rmtree(playground_dir, ignore_errors=True)