write first version of user system
This commit is contained in:
308
src/engine.cpp
308
src/engine.cpp
@ -1,9 +1,11 @@
|
||||
#include "engine.h"
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include "basic_defs.h"
|
||||
#include "data.h"
|
||||
#include "utils.h"
|
||||
const hash_t add_user_hash = 1294763820278197867ull; // SplitMix64Hash(std::string_view("add_user"));
|
||||
const hash_t login_hash = 2711532776857333909ull; // SplitMix64Hash(std::string_view("login"));
|
||||
@ -39,7 +41,7 @@ std::string TicketSystemEngine::Execute(const std::string &command) {
|
||||
// LOG->debug("clean_hash: {}", clean_hash);
|
||||
// LOG->debug("exit_hash: {}", exit_hash);
|
||||
char command_name[20];
|
||||
sscanf(command.c_str(), "%s", command_name);
|
||||
sscanf(command.c_str(), "%*s %s", command_name);
|
||||
hash_t command_name_hash = SplitMix64Hash(std::string_view(command_name));
|
||||
switch (command_name_hash) {
|
||||
case add_user_hash:
|
||||
@ -90,20 +92,303 @@ std::string TicketSystemEngine::Execute(const std::string &command) {
|
||||
case exit_hash:
|
||||
LOG->debug("match exit");
|
||||
PrepareExit();
|
||||
return std::move(Exit());
|
||||
return std::move(Exit(command));
|
||||
}
|
||||
throw std::invalid_argument("Invalid command.");
|
||||
}
|
||||
|
||||
std::string TicketSystemEngine::AddUser(const std::string &command) { return "AddUser"; }
|
||||
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) { return "LoginUser"; }
|
||||
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) { return "LogoutUser"; }
|
||||
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) { return "QueryProfile"; }
|
||||
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<int>(dat.privilege);
|
||||
return response_stream.str();
|
||||
}
|
||||
|
||||
std::string TicketSystemEngine::ModifyProfile(const std::string &command) { return "ModifyProfile"; }
|
||||
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<int>(dat.privilege);
|
||||
return response_stream.str();
|
||||
}
|
||||
|
||||
std::string TicketSystemEngine::AddTrain(const std::string &command) { return "AddTrain"; }
|
||||
|
||||
@ -125,6 +410,13 @@ std::string TicketSystemEngine::QueryTicket(const std::string &command) { return
|
||||
|
||||
std::string TicketSystemEngine::Clean() { return "Clean"; }
|
||||
|
||||
std::string TicketSystemEngine::Exit() { return "bye"; }
|
||||
std::string TicketSystemEngine::Exit(const std::string &command) {
|
||||
command_id_t command_id;
|
||||
sscanf(command.c_str(), "[%llu]", &command_id);
|
||||
LOG->debug("command id: {}", command_id);
|
||||
std::stringstream response_stream;
|
||||
response_stream << '[' << command_id << "] bye";
|
||||
return response_stream.str();
|
||||
}
|
||||
|
||||
void TicketSystemEngine::PrepareExit() { LOG->info("Preparing exit"); }
|
@ -16,5 +16,8 @@ constexpr bool global_log_enabled = false;
|
||||
constexpr bool global_log_enabled = true;
|
||||
#endif
|
||||
extern const bool optimize_enabled;
|
||||
#define LOG if constexpr (global_log_enabled) if (logger_ptr) logger_ptr
|
||||
#define LOG \
|
||||
if constexpr (global_log_enabled) \
|
||||
if (logger_ptr) logger_ptr
|
||||
typedef unsigned long long command_id_t;
|
||||
#endif
|
@ -1,4 +1,12 @@
|
||||
#ifndef DATA_H
|
||||
#define DATA_H
|
||||
|
||||
#include <cstdint>
|
||||
#include "utils.h"
|
||||
struct FullUserData {
|
||||
char username[21];
|
||||
hash_t password_hash;
|
||||
char name[21];
|
||||
char mailAddr[31];
|
||||
uint8_t privilege;
|
||||
};
|
||||
#endif
|
@ -7,17 +7,23 @@
|
||||
#include "dataguard/snapshot.h"
|
||||
#endif
|
||||
#include <vector>
|
||||
#include "data.h"
|
||||
#include "storage/disk_map.hpp"
|
||||
#include "utils.h"
|
||||
class TicketSystemEngine {
|
||||
#ifdef ENABLE_ADVANCED_FEATURE
|
||||
SnapShotManager snapshot_manager;
|
||||
#endif
|
||||
std::string data_directory;
|
||||
std::map<hash_t, bool> online_users;
|
||||
std::map<hash_t, uint8_t> online_users;
|
||||
DiskMap<hash_t, FullUserData> user_data;
|
||||
void PrepareExit();
|
||||
|
||||
public:
|
||||
inline TicketSystemEngine(std::string data_directory) : data_directory(data_directory) {}
|
||||
inline TicketSystemEngine(std::string data_directory)
|
||||
: data_directory(data_directory),
|
||||
user_data("user_data.idx", data_directory + "/user_data.idx", "user_data.val",
|
||||
data_directory + "/user_data.val") {}
|
||||
std::string Execute(const std::string &command);
|
||||
|
||||
// 用户相关函数
|
||||
@ -42,6 +48,6 @@ class TicketSystemEngine {
|
||||
std::string QueryTransfer(const std::string &command);
|
||||
std::string QueryTicket(const std::string &command);
|
||||
std::string Clean();
|
||||
std::string Exit();
|
||||
std::string Exit(const std::string &command);
|
||||
};
|
||||
#endif
|
58
src/main.cpp
58
src/main.cpp
@ -1,4 +1,5 @@
|
||||
#include <sockpp/tcp_acceptor.h>
|
||||
#include <exception>
|
||||
#include "basic_defs.h"
|
||||
#include "dataguard/dataguard.h"
|
||||
#include "engine.h"
|
||||
@ -17,7 +18,7 @@ const bool optimize_enabled = false;
|
||||
// const bool global_log_enabled = true;
|
||||
// #endif
|
||||
int main(int argc, char *argv[]) {
|
||||
argparse::ArgumentParser program("core-cli", main_version + "-" + build_version);
|
||||
argparse::ArgumentParser program("zts-core", main_version + "-" + build_version);
|
||||
argparse::ArgumentParser fsck_command("fsck");
|
||||
fsck_command.add_description("Check and fix data");
|
||||
program.add_subparser(fsck_command);
|
||||
@ -29,6 +30,9 @@ int main(int argc, char *argv[]) {
|
||||
.default_value(std::string("127.0.0.1"))
|
||||
.nargs(1, 1);
|
||||
program.add_subparser(server_command);
|
||||
argparse::ArgumentParser snapshot_command("snapshot");
|
||||
snapshot_command.add_description("Manage snapshots");
|
||||
program.add_subparser(snapshot_command);
|
||||
program.add_argument("-d", "--directory").help("Directory to serve").default_value(std::string(".")).nargs(1, 1);
|
||||
auto &group = program.add_mutually_exclusive_group();
|
||||
group.add_argument("-c", "--consolelog").help("Enable console log").default_value(false).implicit_value(true);
|
||||
@ -81,29 +85,37 @@ int main(int argc, char *argv[]) {
|
||||
LOG->info("Data directory: {}", data_directory);
|
||||
bool is_server = program.is_subcommand_used("server");
|
||||
LOG->info("Server mode: {}", is_server);
|
||||
if (is_server) {
|
||||
auto port = server_command.get<int>("--port");
|
||||
auto address = server_command.get<std::string>("--address");
|
||||
LOG->info("Server port: {}", port);
|
||||
LOG->info("Server address: {}", address);
|
||||
LOG->info("Starting server");
|
||||
sockpp::tcp_acceptor acceptor(sockpp::inet_address(address, port));
|
||||
if (!acceptor) {
|
||||
LOG->error("Error creating acceptor: {}", acceptor.last_error_str());
|
||||
return 1;
|
||||
} else
|
||||
LOG->info("successfully bind to address {} port {}", address, port);
|
||||
throw std::runtime_error("Server mode not implemented");
|
||||
} else {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::cout.tie(nullptr);
|
||||
TicketSystemEngine engine(data_directory);
|
||||
std::string cmd;
|
||||
while (std::getline(std::cin, cmd)) {
|
||||
std::cout << engine.Execute(cmd) << '\n';
|
||||
std::cout.flush();
|
||||
try {
|
||||
if (is_server) {
|
||||
auto port = server_command.get<int>("--port");
|
||||
auto address = server_command.get<std::string>("--address");
|
||||
LOG->info("Server port: {}", port);
|
||||
LOG->info("Server address: {}", address);
|
||||
LOG->info("Starting server");
|
||||
sockpp::tcp_acceptor acceptor(sockpp::inet_address(address, port));
|
||||
if (!acceptor) {
|
||||
LOG->error("Error creating acceptor: {}", acceptor.last_error_str());
|
||||
return 1;
|
||||
} else
|
||||
LOG->info("successfully bind to address {} port {}", address, port);
|
||||
throw std::runtime_error("Server mode not implemented");
|
||||
} else {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::cout.tie(nullptr);
|
||||
TicketSystemEngine engine(data_directory);
|
||||
std::string cmd;
|
||||
while (std::getline(std::cin, cmd)) {
|
||||
std::cout << engine.Execute(cmd) << '\n';
|
||||
std::cout.flush();
|
||||
}
|
||||
}
|
||||
} catch (std::exception &e) {
|
||||
LOG->error("Exception: {}", e.what());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
LOG->error("Unknown exception");
|
||||
return 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user