1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-23 02:34:27 +01:00
doorlockd-mirror/doorlockd/main.cpp

191 lines
4.5 KiB
C++
Raw Normal View History

2015-05-11 00:18:22 +02:00
#include <iostream>
#include <string>
2015-05-13 16:20:12 +02:00
#include <cstdlib>
#include <memory>
#include <utility>
2015-05-14 15:33:40 +02:00
#include <csignal>
2015-05-11 00:18:22 +02:00
2015-05-12 17:35:57 +02:00
#include <boost/program_options.hpp>
2015-05-13 16:20:12 +02:00
#include <boost/asio.hpp>
2015-05-11 00:18:22 +02:00
2015-05-14 15:33:40 +02:00
#include "daemon.h"
2015-05-12 03:49:26 +02:00
#include "config.h"
2015-05-11 00:18:22 +02:00
#include "logic.h"
using namespace std;
2015-05-12 17:35:57 +02:00
namespace po = boost::program_options;
2015-05-13 16:20:12 +02:00
using boost::asio::ip::tcp;
2015-05-12 17:35:57 +02:00
2015-05-11 00:18:22 +02:00
const static Logger &l = Logger::get();
2015-05-13 16:20:12 +02:00
static unique_ptr<Logic> logic = nullptr;
2015-05-14 15:33:40 +02:00
boost::asio::io_service io_service;
void signal_handler(int signum)
{
(void)signum;
io_service.stop();
logic.reset();
}
2015-05-21 13:35:30 +02:00
/*
* Session class handles asynchronosly handles one incoming TCP session
*/
2015-05-13 16:20:12 +02:00
class session
: public std::enable_shared_from_this<session>
2015-05-11 00:18:22 +02:00
{
2015-05-12 03:49:26 +02:00
2015-05-13 16:20:12 +02:00
public:
2015-05-12 03:49:26 +02:00
2015-05-13 16:20:12 +02:00
session(tcp::socket socket)
: _socket(std::move(socket))
2015-05-12 03:49:26 +02:00
{
}
2015-05-13 16:20:12 +02:00
void start()
2015-05-12 03:49:26 +02:00
{
2015-05-13 16:20:12 +02:00
auto self(shared_from_this());
_socket.async_read_some(boost::asio::buffer(_data, _maxLen),
[this, self](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
const string payload(_data, length);
const auto rc = logic->parseRequest(payload);
boost::asio::write(_socket, boost::asio::buffer(to_string(rc) + "\n"));
}
});
2015-05-12 03:49:26 +02:00
}
2015-05-13 16:20:12 +02:00
private:
2015-05-12 03:49:26 +02:00
2015-05-13 16:20:12 +02:00
tcp::socket _socket;
static constexpr int _maxLen = { 2048 };
char _data[_maxLen];
};
2015-05-12 17:35:57 +02:00
2015-05-21 13:35:30 +02:00
/*
* The TCP server
*/
2015-05-13 16:20:12 +02:00
class server
2015-05-12 17:35:57 +02:00
{
2015-05-13 16:20:12 +02:00
public:
2015-05-18 22:22:59 +02:00
server(boost::asio::io_service& io_service, short port)
: _acceptor(io_service, tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port)),
_socket(io_service)
{
do_accept();
}
2015-05-13 16:20:12 +02:00
private:
2015-05-12 17:35:57 +02:00
2015-05-13 16:20:12 +02:00
void do_accept()
2015-05-12 17:35:57 +02:00
{
2015-05-13 16:20:12 +02:00
_acceptor.async_accept(_socket,
[this](boost::system::error_code ec)
{
if (!ec)
{
std::make_shared<session>(std::move(_socket))->start();
}
do_accept();
});
2015-05-12 17:35:57 +02:00
}
2015-05-13 16:20:12 +02:00
tcp::acceptor _acceptor;
tcp::socket _socket;
};
2015-05-12 17:35:57 +02:00
void emergencyCB()
{
cout << "EMERGENCY!" << endl;
}
2015-05-12 17:35:57 +02:00
int main(int argc, char** argv)
{
int retval = -1;
2015-05-13 16:20:12 +02:00
short port;
2015-05-12 17:35:57 +02:00
std::chrono::seconds tokenTimeout;
2015-05-24 19:15:47 +02:00
string ldapUri;
2015-05-13 16:40:30 +02:00
string bindDN;
string lockPagePrefix;
2015-05-14 15:33:40 +02:00
string logfile;
string serDev;
2015-05-12 17:35:57 +02:00
2015-05-13 16:50:44 +02:00
l(LogLevel::notice, "Starting doorlockd");
2015-05-12 17:35:57 +02:00
try {
unsigned int timeout;
po::options_description desc("usage: doorlockd");
desc.add_options()
("help,h", "print help")
2015-05-13 16:50:44 +02:00
("tokentimeout,t", po::value<unsigned int>(&timeout)->default_value(DEFAULT_TOKEN_TIMEOUT), "Token timeout in seconds")
2015-05-13 16:40:30 +02:00
("port,p", po::value<short>(&port)->default_value(DEFAULT_PORT), "Port")
2015-05-24 19:15:47 +02:00
("ldap,s", po::value<string>(&ldapUri)->default_value(DEFAULT_LDAP_URI), "Ldap Server")
2015-05-13 16:50:44 +02:00
("bidndn,b", po::value<string>(&bindDN)->default_value(DEFAULT_BINDDN), "Bind DN, %s means username")
2015-05-13 16:40:30 +02:00
("web,w", po::value<string>(&lockPagePrefix)->default_value(DEFAULT_WEB_PREFIX), "Prefix of the webpage")
2015-05-14 15:33:40 +02:00
("logfile,l", po::value<string>(&logfile)->default_value(DEFAULT_LOG_FILE), "Log file")
("serial,i", po::value<string>(&serDev)->default_value(DEFAULT_SERIAL_DEVICE), "Serial port");
2015-05-12 17:35:57 +02:00
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
retval = 0;
goto out;
}
po::notify(vm);
tokenTimeout = std::chrono::seconds(timeout>3 ? timeout-3 : timeout); // Epaper refresh takes ~3 seconds
}
catch(const std::exception &e)
{
std::cerr << "Error: " << e.what() << "\n";
goto out;
}
daemonize("/",
2015-05-14 15:33:40 +02:00
"/dev/null",
logfile,
logfile);
2015-05-14 15:33:40 +02:00
signal(SIGINT, signal_handler);
signal(SIGKILL, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler);
2015-05-13 16:40:30 +02:00
logic = unique_ptr<Logic>(new Logic(tokenTimeout,
2015-05-24 19:15:47 +02:00
ldapUri,
2015-05-13 16:40:30 +02:00
bindDN,
lockPagePrefix,
serDev));
2015-05-12 17:35:57 +02:00
2015-05-11 00:18:22 +02:00
try {
2015-05-13 16:20:12 +02:00
server s(io_service, port);
io_service.run();
2015-05-11 00:18:22 +02:00
}
catch (const char* const &ex) {
ostringstream str;
str << "FATAL ERROR: " << ex;
l(str, LogLevel::error);
2015-05-12 03:49:26 +02:00
retval = -1;
2015-05-13 16:20:12 +02:00
goto out;
2015-05-12 03:49:26 +02:00
}
2015-05-12 17:35:57 +02:00
retval = 0;
2015-05-11 00:18:22 +02:00
2015-05-12 03:49:26 +02:00
out:
2015-05-11 00:18:22 +02:00
l(LogLevel::notice, "Doorlockd stopped");
2015-05-12 03:49:26 +02:00
return retval;
2015-05-11 00:18:22 +02:00
}