diff --git a/doorlockd/CMakeLists.txt b/doorlockd/CMakeLists.txt index 0a5065d..8afed37 100644 --- a/doorlockd/CMakeLists.txt +++ b/doorlockd/CMakeLists.txt @@ -30,6 +30,7 @@ main.cpp logger.cpp door.cpp logic.cpp +response.cpp util.cpp daemon.cpp ) diff --git a/doorlockd/response.cpp b/doorlockd/response.cpp new file mode 100644 index 0000000..ab54fe9 --- /dev/null +++ b/doorlockd/response.cpp @@ -0,0 +1,22 @@ +#include + +#include "response.h" + +const std::string Response::_codeKey = "code"; +const std::string Response::_messageKey = "message"; + +Response::operator bool() const +{ + return code == Response::Code::Success; +} + +std::string Response::toJson() const +{ + Json::Value response; + Json::StyledWriter writer; + + response[_codeKey] = code; + response[_messageKey] = message; + + return writer.write(response); +} diff --git a/doorlockd/response.h b/doorlockd/response.h new file mode 100644 index 0000000..bf3c421 --- /dev/null +++ b/doorlockd/response.h @@ -0,0 +1,44 @@ +#ifndef RESPONSE_H +#define RESPONSE_H + +#include + +struct Response +{ + enum Code { + Success = 0, // Request successful + Fail, // General non-specified error + AlreadyUnlocked, // Authentication successful, but door is already unlocked + AlreadyLocked, // Authentication successful, but door is already locked + NotJson, // Request is not a valid JSON object + JsonError, // Request is valid JSON, but does not contain necessary material + InvalidToken, // Request contains invalid token + InvalidCredentials, // Invalid LDAP credentials + InvalidIP, // IP check failure + UnknownAction, // Unknown action + LDAPInit, // Ldap initialization failed + } code; + + std::string message; + std::string toJson() const; + operator bool() const; + + Response() : + code(Fail), + message("General failure") + { + } + + Response(Response::Code code, const std::string &message = "") : + code(code), + message(message) + { + } + +private: + + const static std::string _codeKey; + const static std::string _messageKey; +}; + +#endif // RESPONSE_H