From 48565ef37eef48b8450c54781b40a0c89926f7fc Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Tue, 22 Sep 2015 18:22:15 +0200 Subject: [PATCH] Moved JSON parsing to main.cpp --- doorlockd/logic.cpp | 16 +++------------- doorlockd/logic.h | 4 +++- doorlockd/main.cpp | 17 +++++++++++++++-- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/doorlockd/logic.cpp b/doorlockd/logic.cpp index 209a0f4..e1d7c34 100644 --- a/doorlockd/logic.cpp +++ b/doorlockd/logic.cpp @@ -22,10 +22,10 @@ Logic::Logic(const chrono::seconds tokenTimeout, _logger(Logger::get()), _door(serDev), _tokenTimeout(tokenTimeout), + _onTokenUpdate(onTokenUpdate), _ldapUri(ldapUri), _bindDN(bindDN), - _webPrefix(webPrefix), - _onTokenUpdate(onTokenUpdate) + _webPrefix(webPrefix) { srand(time(NULL)); _createNewToken(false); @@ -52,24 +52,14 @@ Logic::~Logic() _tokenUpdater.join(); } -Logic::Response Logic::parseRequest(const string &str) +Logic::Response Logic::parseRequest(const Json::Value &root) { unique_lock l(_mutex); _logger(LogLevel::info, "Incoming request..."); - Json::Reader reader; - Json::Value root; Response retval = Fail; string action, user, password, ip, token; - bool suc = reader.parse(str, root, false); - if (!suc) - { - _logger(LogLevel::warning, "Request ist not valid JSON!"); - retval = NotJson; - goto out; - } - try { action = getJsonOrFail(root, "action"); ip = getJsonOrFail(root, "ip"); diff --git a/doorlockd/logic.h b/doorlockd/logic.h index 9cc9049..07fd65a 100644 --- a/doorlockd/logic.h +++ b/doorlockd/logic.h @@ -7,6 +7,8 @@ #include #include +#include + #include "config.h" #include "door.h" #include "logger.h" @@ -44,7 +46,7 @@ public: }; // Parse incoming JSON Requests - Response parseRequest(const std::string &str); + Response parseRequest(const Json::Value &root); // Returns the current Token std::string getCurrentToken() const; diff --git a/doorlockd/main.cpp b/doorlockd/main.cpp index 9859a81..831b19c 100644 --- a/doorlockd/main.cpp +++ b/doorlockd/main.cpp @@ -8,6 +8,8 @@ #include #include +#include + #include "daemon.h" #include "config.h" #include "logic.h" @@ -47,8 +49,19 @@ static void session(tcp::socket &&sock) else if (error) throw boost::system::system_error(error); - std::string request(data.begin(), data.begin()+length); - const auto rc = logic->parseRequest(request); + const std::string request(data.begin(), data.begin()+length); + + Json::Reader reader; + Json::Value root; + Logic::Response rc = Logic::Response::Fail; + + if (reader.parse(request, root, false)) + { + l(LogLevel::warning, "Request ist not valid JSON!"); + } else { + rc = logic->parseRequest(root); + } + sock.write_some(boost::asio::buffer(std::to_string(rc) + "\n"), error);