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

Separated Logic

This commit is contained in:
Ralf Ramsauer 2015-05-12 01:49:26 +00:00
parent 0d7f56e647
commit c723002599
3 changed files with 109 additions and 106 deletions

104
logic.cpp
View File

@ -1,9 +1,4 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <cstdlib>
@ -18,7 +13,6 @@
using namespace std;
const string Logic::_lockPagePrefix = LOCKPAGE_PREFIX;
const string Logic::_fifoLocation = FIFO_LOCATION;
const string Logic::_ldapServer = LDAP_SERVER;
const string Logic::_bindDN = BINDDN;
@ -36,54 +30,14 @@ Logic::Logic() :
_epaper(Epaper::get())
{
srand(time(NULL));
_logger(LogLevel::debug, "Creating Fifo file");
if (access(_fifoLocation.c_str(), F_OK) == 0)
{
_logger(LogLevel::warning, "Fifo file aready existing, trying to delete");
if (unlink(_fifoLocation.c_str()) != 0)
{
throw("Unable to delete Fifo file");
}
}
umask(0);
if (mkfifo(_fifoLocation.c_str(), 0770) != 0)
{
throw("Unable to create Fifo");
}
_fifoHandle = open(_fifoLocation.c_str(), O_RDWR | O_NONBLOCK);
if (_fifoHandle == -1)
{
throw("Unable to open Fifo");
}
if (fchown(_fifoHandle, 0, 1001) != 0)
{
throw("Fifo chown failed");
}
_createNewToken(false);
createNewToken(false);
}
Logic::~Logic()
{
if (_fifoHandle != -1)
{
close(_fifoHandle);
}
_logger(LogLevel::debug, "Removing Fifo file");
if (unlink(_fifoLocation.c_str()) != 0)
{
throw("Unable to delete Fifo file");
}
}
int Logic::_parseRequest(const string &str)
int Logic::parseRequest(const string &str)
{
_logger("Parsing request...");
Json::Reader reader;
@ -162,7 +116,7 @@ void Logic::_lock()
}
_door.lock();
_state = LOCKED;
_createNewToken(false);
createNewToken(false);
}
void Logic::_unlock()
@ -174,55 +128,7 @@ void Logic::_unlock()
}
_door.unlock();
_state = UNLOCKED;
_createNewToken(false);
}
void Logic::run()
{
struct timeval tv;
fd_set set;
for (;;)
{
FD_ZERO(&set);
FD_SET(_fifoHandle, &set);
tv.tv_sec = _tokenTimeout;
tv.tv_usec = 0;
int i = select(_fifoHandle+1, &set, nullptr, nullptr, &tv);
if (i == 0)
{
_createNewToken(true);
continue;
} else if (i == -1) {
throw "Fifo select() failed";
}
if (!FD_ISSET(_fifoHandle, &set))
{
_logger(LogLevel::warning, "select(): Not my fd");
continue;
}
string payload;
for (;;)
{
constexpr int BUFSIZE = 2;
char tmp[BUFSIZE];
i = read(_fifoHandle, tmp, BUFSIZE);
if (i > 0) {
payload += string(tmp, i);
} else {
if (errno == EWOULDBLOCK)
{
break;
}
throw "read() fifo failed";
}
}
int rc = _parseRequest(payload);
}
createNewToken(false);
}
bool Logic::_checkIP(const string &ip)
@ -292,7 +198,7 @@ out2:
return retval;
}
void Logic::_createNewToken(const bool stillValid)
void Logic::createNewToken(const bool stillValid)
{
_prevToken = _curToken;
_prevValid = stillValid;

View File

@ -17,12 +17,12 @@ public:
static Logic &get();
~Logic();
void run();
int parseRequest(const std::string &str);
void createNewToken(const bool stillValid);
private:
Logic();
int _parseRequest(const std::string &str);
void _lock();
void _unlock();
@ -31,7 +31,6 @@ private:
bool _checkLDAP(const std::string &user, const std::string &password);
bool _checkIP(const std::string &ip);
void _createNewToken(const bool stillValid);
const Logger &_logger;
Door &_door;
@ -50,8 +49,6 @@ private:
const static std::string _ldapServer;
const static std::string _allowedIpPrefix;
int _fifoHandle = {-1};
static constexpr int _tokenTimeout = TOKEN_TIMEOUT;
enum {LOCKED, UNLOCKED} _state = { LOCKED };

104
main.cpp
View File

@ -2,8 +2,12 @@
#include <sstream>
#include <string>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "config.h"
#include "logic.h"
using namespace std;
@ -12,19 +16,115 @@ const static Logger &l = Logger::get();
int main(void)
{
int retval = -1;
l(LogLevel::notice, "Starting doorlockd");
int fifoHandle = -1;
l(LogLevel::debug, "Creating Fifo file");
if (access(FIFO_LOCATION, F_OK) == 0)
{
l(LogLevel::warning, "Fifo file aready existing, trying to delete");
if (unlink(FIFO_LOCATION) != 0)
{
fprintf(stderr, "Unable to delete Fifo file");
goto out;
}
}
umask(0);
if (mkfifo(FIFO_LOCATION, 0770) != 0)
{
fprintf(stderr, "Unable to create Fifo");
goto out;
}
fifoHandle = open(FIFO_LOCATION, O_RDWR | O_NONBLOCK);
if (fifoHandle == -1)
{
fprintf(stderr, "Unable to open Fifo");
goto out;
}
if (fchown(fifoHandle, 0, 1001) != 0)
{
fprintf(stderr, "Fifo chown failed");
goto out1;
}
try {
Logic &logic = Logic::get();
logic.run();
struct timeval tv;
fd_set set;
for (;;)
{
FD_ZERO(&set);
FD_SET(fifoHandle, &set);
tv.tv_sec = TOKEN_TIMEOUT;
tv.tv_usec = 0;
int i = select(fifoHandle+1, &set, nullptr, nullptr, &tv);
if (i == 0)
{
logic.createNewToken(true);
continue;
} else if (i == -1) {
throw "Fifo select() failed";
}
if (!FD_ISSET(fifoHandle, &set))
{
l(LogLevel::warning, "select(): Not my fd");
continue;
}
string payload;
for (;;)
{
constexpr int BUFSIZE = 2;
char tmp[BUFSIZE];
i = read(fifoHandle, tmp, BUFSIZE);
if (i > 0) {
payload += string(tmp, i);
} else {
if (errno == EWOULDBLOCK)
{
break;
}
throw "read() fifo failed";
}
}
int rc = logic.parseRequest(payload);
}
retval = 0;
}
catch (const char* const &ex) {
ostringstream str;
str << "FATAL ERROR: " << ex;
l(str, LogLevel::error);
retval = -1;
}
out1:
if (fifoHandle != -1)
{
close(fifoHandle);
}
l(LogLevel::debug, "Removing Fifo file");
if (unlink(FIFO_LOCATION) != 0)
{
throw("Unable to delete Fifo file");
}
out:
Door::get().lock();
l(LogLevel::notice, "Doorlockd stopped");
return 0;
return retval;
}