From 3b7338ea6e24b5e2140af2190c092ac22c834943 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Tue, 22 Sep 2015 21:22:48 +0200 Subject: [PATCH] Added proper Response handling --- doorlockd/CMakeLists.txt | 1 + doorlockd/response.cpp | 22 ++++++++++++++++++++ doorlockd/response.h | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 doorlockd/response.cpp create mode 100644 doorlockd/response.h 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