1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 02:14:26 +01:00

Moved JSON parsing to main.cpp

This commit is contained in:
Ralf Ramsauer 2015-09-22 18:22:15 +02:00
parent 60bbe3df76
commit 48565ef37e
3 changed files with 21 additions and 16 deletions

View File

@ -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<mutex> 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<string>(root, "action");
ip = getJsonOrFail<string>(root, "ip");

View File

@ -7,6 +7,8 @@
#include <condition_variable>
#include <mutex>
#include <json/json.h>
#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;

View File

@ -8,6 +8,8 @@
#include <boost/program_options.hpp>
#include <boost/asio.hpp>
#include <json/json.h>
#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);