From c01eb22f9196f7aa2a8accabe24a42c28b5ebcfd Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Tue, 22 Sep 2015 21:48:52 +0200 Subject: [PATCH] Moved Command handling from logic to main --- doorlockd/logic.cpp | 1 - doorlockd/main.cpp | 24 ++++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/doorlockd/logic.cpp b/doorlockd/logic.cpp index 2cf83be..f3aee5d 100644 --- a/doorlockd/logic.cpp +++ b/doorlockd/logic.cpp @@ -75,7 +75,6 @@ Response Logic::parseRequest(const Json::Value &root) goto out; } - _logger(" Command: " + command, LogLevel::notice); _logger(" User : " + user, LogLevel::notice); _logger(" IP : " + ip, LogLevel::notice); _logger(" Token : " + token, LogLevel::notice); diff --git a/doorlockd/main.cpp b/doorlockd/main.cpp index 37709f8..34f19ca 100644 --- a/doorlockd/main.cpp +++ b/doorlockd/main.cpp @@ -13,6 +13,7 @@ #include "daemon.h" #include "config.h" #include "logic.h" +#include "util.h" namespace po = boost::program_options; using boost::asio::ip::tcp; @@ -61,12 +62,31 @@ static void session(tcp::socket &&sock) response.code = Response::Code::JsonError; l(response.message, LogLevel::warning); } else { - response = logic->parseRequest(root); + std::string command; + try { + command = getJsonOrFail(root, "command"); + } + catch (...) + { + response.code = Response::Code::JsonError; + response.message = "Error parsing JSON"; + l(response.message, LogLevel::warning); + goto out; + } + + l(" Command: " + command, LogLevel::notice); + if (command == "lock" || command == "unlock") { + response = logic->parseRequest(root); + } else { + response.code = Response::Code::UnknownCommand; + response.message = "Received unknown command " + command; + l(response.message, LogLevel::warning); + } } + out: sock.write_some(boost::asio::buffer(response.toJson()), error); - if (error == boost::asio::error::eof) return; else if (error)