diff options
author | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-10-11 19:28:50 +0200 |
---|---|---|
committer | Jannis M. Hoffmann <jannis@fehcom.de> | 2023-10-11 19:28:50 +0200 |
commit | 3ea7edf8c9bf7583c426178d4aaff4fb5b736bd2 (patch) | |
tree | ae78ebf9e9d1f70da8a6fdad1504063644ee8f1e /src/broker.cc | |
parent | 1a7c572099edf744d6bb67318cff215de3fcd897 (diff) |
use some enum classes
move ParseResult to recursivedescent.h
Diffstat (limited to 'src/broker.cc')
-rw-r--r-- | src/broker.cc | 88 |
1 files changed, 45 insertions, 43 deletions
diff --git a/src/broker.cc b/src/broker.cc index 0a063f5..4fa9ec8 100644 --- a/src/broker.cc +++ b/src/broker.cc @@ -20,9 +20,9 @@ using std::string; BrokerFactory::BrokerFactory() { - brokers[Session::NONAUTHENTICATED] = new Broker(); - brokers[Session::AUTHENTICATED] = new Broker(); - brokers[Session::SELECTED] = new Broker(); + brokers[Session::State::NONAUTHENTICATED] = new Broker(); + brokers[Session::State::AUTHENTICATED] = new Broker(); + brokers[Session::State::SELECTED] = new Broker(); } BrokerFactory::~BrokerFactory() @@ -59,7 +59,7 @@ void BrokerFactory::assign(const string &fname, Operator *o) } } -Operator *BrokerFactory::getOperator(int state, const string &name) const +Operator *BrokerFactory::getOperator(Session::State state, const string &name) const { if (brokers.find(state) == brokers.end()) return nullptr; @@ -67,20 +67,17 @@ Operator *BrokerFactory::getOperator(int state, const string &name) const return brokers.find(state)->second->get(name); } -Broker *BrokerFactory::getBroker(int state) +Broker *BrokerFactory::getBroker(Session::State state) { - if (brokers.find(state) == brokers.end()) { + auto it = brokers.find(state); + if (it == brokers.end()) { setLastError("No appropriate broker for state."); return nullptr; } - return brokers[state]; + return it->second; } -Broker::Broker() {} - -Broker::~Broker() {} - void Broker::assign(const string &fname, Operator *o, bool deletable) { deletables[fname] = deletable; @@ -93,47 +90,52 @@ Operator *Broker::get(const string &name) const return operators.find(name)->second; } -Operator::ParseResult Broker::parseStub(Request &command) +Parser::ParseResult Broker::parseStub(Request &command) { + constexpr auto ACCEPT = Parser::ParseResult::ACCEPT; + constexpr auto REJECT = Parser::ParseResult::REJECT; + constexpr auto ERROR = Parser::ParseResult::ERROR; + constexpr auto TIMEOUT = Parser::ParseResult::TIMEOUT; + Session &session = Session::getInstance(); string tag; string cmd; switch (expectTag(tag)) { - case Operator::ACCEPT: + case ACCEPT: break; - case Operator::REJECT: + case REJECT: session.setLastError("Syntax error; first token must be a tag"); [[fallthrough]]; - case Operator::ERROR: - return Operator::ERROR; - case Operator::TIMEOUT: - return Operator::TIMEOUT; + case ERROR: + return ERROR; + case TIMEOUT: + return TIMEOUT; } switch (expectSPACE()) { - case Operator::ACCEPT: + case ACCEPT: break; - case Operator::REJECT: + case REJECT: session.setLastError("Syntax error; second token must be a SPACE"); [[fallthrough]]; - case Operator::ERROR: - return Operator::ERROR; - case Operator::TIMEOUT: - return Operator::TIMEOUT; + case ERROR: + return ERROR; + case TIMEOUT: + return TIMEOUT; } switch (expectAstring(cmd)) { - case Operator::ACCEPT: + case ACCEPT: break; - case Operator::REJECT: + case REJECT: session.setLastError("Syntax error; third token must be a command"); [[fallthrough]]; - case Operator::ERROR: - return Operator::ERROR; - case Operator::TIMEOUT: - return Operator::TIMEOUT; + case ERROR: + return ERROR; + case TIMEOUT: + return TIMEOUT; } uppercase(cmd); @@ -142,27 +144,27 @@ Operator::ParseResult Broker::parseStub(Request &command) command.setUidMode(); switch (expectSPACE()) { - case Operator::ACCEPT: + case ACCEPT: break; - case Operator::REJECT: + case REJECT: session.setLastError("Syntax error; after UID there must come a SPACE"); [[fallthrough]]; - case Operator::ERROR: - return Operator::ERROR; - case Operator::TIMEOUT: - return Operator::TIMEOUT; + case ERROR: + return ERROR; + case TIMEOUT: + return TIMEOUT; } switch (expectAstring(cmd)) { - case Operator::ACCEPT: + case ACCEPT: break; - case Operator::REJECT: + case REJECT: session.setLastError("Syntax error; after UID SPACE there must come a command"); [[fallthrough]]; - case Operator::ERROR: - return Operator::ERROR; - case Operator::TIMEOUT: - return Operator::TIMEOUT; + case ERROR: + return ERROR; + case TIMEOUT: + return TIMEOUT; } uppercase(cmd); @@ -171,5 +173,5 @@ Operator::ParseResult Broker::parseStub(Request &command) command.setTag(tag); command.setName(cmd); - return Operator::ACCEPT; + return ACCEPT; } |