#include "admission.hpp" #include #include namespace admission { namespace { std::vector tokenize(const std::string &line) { std::istringstream input(line); std::vector tokens; std::string token; while (input >> token) { tokens.push_back(token); } return tokens; } std::string prefix(const char *operation, const std::string &name) { return std::string(operation) + " " + name + " "; } std::string triple_prefix(const char *operation, const std::string &name, const std::string &key) { return std::string(operation) + " " + name + " " + key + " "; } bool parse_policy_fields(const std::vector &tokens, Policy &policy) { return parse_policy(tokens, 2, policy); } std::string client_text(const ClientView &view) { std::ostringstream output; output << view.start << ' ' << view.last << ' ' << view.used << ' ' << view.admitted << ' ' << view.rejected; return output.str(); } std::string statistics_text(const Statistics &value) { std::ostringstream output; output << value.clients << ' ' << value.admitted << ' ' << value.rejected << ' ' << value.policy.limit << ' ' << value.policy.window << ' ' << value.policy.capacity; return output.str(); } } // namespace Engine *Session::find(const std::string &name) { auto found = engines_.find(name); if (found == engines_.end()) { return nullptr; } return &found->second; } const Engine *Session::find(const std::string &name) const { auto found = engines_.find(name); if (found == engines_.end()) { return nullptr; } return &found->second; } std::vector Session::names() const { std::vector result; for (const auto &entry : engines_) { result.push_back(entry.first); } return result; } std::string Session::execute(const std::string &line) { std::vector tokens = tokenize(line); if (tokens.empty()) { return {}; } if (!tokens[0].empty() && tokens[0][0] == '#') { return {}; } if (tokens[0] == "NEW") { return execute_new(tokens); } if (tokens[0] == "REQ") { return execute_request(tokens); } if (tokens[0] == "CFG") { return execute_config(tokens); } if (tokens[0] == "GET") { return execute_get(tokens); } if (tokens[0] == "DROP") { return execute_drop(tokens); } if (tokens[0] == "RESET") { return execute_reset(tokens); } if (tokens[0] == "STATS") { return execute_stats(tokens); } if (tokens[0] == "DEL") { return execute_delete(tokens); } if (tokens[0] == "LIST") { return execute_list(tokens); } return "ERR PARSE"; } std::string Session::execute_new(const std::vector &tokens) { if (tokens.size() != 5) { return "ERR PARSE"; } const std::string &name = tokens[1]; Policy policy; if (!parse_policy_fields(tokens, policy)) { return "ERR NUMBER"; } if (!valid_key(name) || !valid_policy(policy)) { return prefix("NEW", name) + "POLICY"; } if (find(name) != nullptr) { return prefix("NEW", name) + "EXISTS"; } engines_.emplace(name, Engine(policy)); return prefix("NEW", name) + "OK"; } std::string Session::execute_request(const std::vector &tokens) { if (tokens.size() != 4) { return "ERR PARSE"; } const std::string &name = tokens[1]; const std::string &key = tokens[2]; std::uint64_t tick = 0; if (!parse_u64(tokens[3], tick)) { return "ERR NUMBER"; } Engine *engine = find(name); if (engine == nullptr) { return triple_prefix("REQ", name, key) + "NO_INSTANCE"; } return triple_prefix("REQ", name, key) + request_name(engine->request(key, tick)); } std::string Session::execute_config(const std::vector &tokens) { if (tokens.size() != 5) { return "ERR PARSE"; } const std::string &name = tokens[1]; Policy policy; if (!parse_policy_fields(tokens, policy)) { return "ERR NUMBER"; } Engine *engine = find(name); if (engine == nullptr) { return prefix("CFG", name) + "NO_INSTANCE"; } return prefix("CFG", name) + config_name(engine->configure(policy)); } std::string Session::execute_get(const std::vector &tokens) { if (tokens.size() != 3) { return "ERR PARSE"; } const std::string &name = tokens[1]; const std::string &key = tokens[2]; const Engine *engine = find(name); if (engine == nullptr) { return triple_prefix("GET", name, key) + "NO_INSTANCE"; } ClientView view; if (!engine->inspect(key, view)) { return triple_prefix("GET", name, key) + "NONE"; } return triple_prefix("GET", name, key) + client_text(view); } std::string Session::execute_drop(const std::vector &tokens) { if (tokens.size() != 3) { return "ERR PARSE"; } const std::string &name = tokens[1]; const std::string &key = tokens[2]; Engine *engine = find(name); if (engine == nullptr) { return triple_prefix("DROP", name, key) + "NO_INSTANCE"; } return triple_prefix("DROP", name, key) + drop_name(engine->drop(key)); } std::string Session::execute_reset(const std::vector &tokens) { if (tokens.size() != 2) { return "ERR PARSE"; } Engine *engine = find(tokens[1]); if (engine == nullptr) { return prefix("RESET", tokens[1]) + "NO_INSTANCE"; } engine->reset(); return prefix("RESET", tokens[1]) + "OK"; } std::string Session::execute_stats(const std::vector &tokens) { if (tokens.size() != 2) { return "ERR PARSE"; } const Engine *engine = find(tokens[1]); if (engine == nullptr) { return prefix("STATS", tokens[1]) + "NO_INSTANCE"; } return prefix("STATS", tokens[1]) + statistics_text(engine->statistics()); } std::string Session::execute_delete(const std::vector &tokens) { if (tokens.size() != 2) { return "ERR PARSE"; } if (engines_.erase(tokens[1]) == 0) { return prefix("DEL", tokens[1]) + "MISSING"; } return prefix("DEL", tokens[1]) + "OK"; } std::string Session::execute_list(const std::vector &tokens) { if (tokens.size() != 1) { return "ERR PARSE"; } std::string result = "LIST"; for (const std::string &name : names()) { result += ' '; result += name; } return result; } } // namespace admission