1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 10:24: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()), _logger(Logger::get()),
_door(serDev), _door(serDev),
_tokenTimeout(tokenTimeout), _tokenTimeout(tokenTimeout),
_onTokenUpdate(onTokenUpdate),
_ldapUri(ldapUri), _ldapUri(ldapUri),
_bindDN(bindDN), _bindDN(bindDN),
_webPrefix(webPrefix), _webPrefix(webPrefix)
_onTokenUpdate(onTokenUpdate)
{ {
srand(time(NULL)); srand(time(NULL));
_createNewToken(false); _createNewToken(false);
@ -52,24 +52,14 @@ Logic::~Logic()
_tokenUpdater.join(); _tokenUpdater.join();
} }
Logic::Response Logic::parseRequest(const string &str) Logic::Response Logic::parseRequest(const Json::Value &root)
{ {
unique_lock<mutex> l(_mutex); unique_lock<mutex> l(_mutex);
_logger(LogLevel::info, "Incoming request..."); _logger(LogLevel::info, "Incoming request...");
Json::Reader reader;
Json::Value root;
Response retval = Fail; Response retval = Fail;
string action, user, password, ip, token; 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 { try {
action = getJsonOrFail<string>(root, "action"); action = getJsonOrFail<string>(root, "action");
ip = getJsonOrFail<string>(root, "ip"); ip = getJsonOrFail<string>(root, "ip");

View File

@ -7,6 +7,8 @@
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
#include <json/json.h>
#include "config.h" #include "config.h"
#include "door.h" #include "door.h"
#include "logger.h" #include "logger.h"
@ -44,7 +46,7 @@ public:
}; };
// Parse incoming JSON Requests // Parse incoming JSON Requests
Response parseRequest(const std::string &str); Response parseRequest(const Json::Value &root);
// Returns the current Token // Returns the current Token
std::string getCurrentToken() const; std::string getCurrentToken() const;

View File

@ -8,6 +8,8 @@
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <json/json.h>
#include "daemon.h" #include "daemon.h"
#include "config.h" #include "config.h"
#include "logic.h" #include "logic.h"
@ -47,8 +49,19 @@ static void session(tcp::socket &&sock)
else if (error) else if (error)
throw boost::system::system_error(error); throw boost::system::system_error(error);
std::string request(data.begin(), data.begin()+length); const std::string request(data.begin(), data.begin()+length);
const auto rc = logic->parseRequest(request);
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"), sock.write_some(boost::asio::buffer(std::to_string(rc) + "\n"),
error); error);