1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-12-22 18:34:25 +01:00
doorlockd-mirror/doorlockd/main.cpp

205 lines
5.6 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-09-22 18:22:15 +02:00
#include <json/json.h>
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"
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
// The receive buffer length of the TCP socket
const int constexpr SOCKET_BUFFERLENGTH = 2048;
2015-05-11 00:18:22 +02:00
const static Logger &l = Logger::get();
2015-09-22 17:47:42 +02:00
static std::unique_ptr<Logic> logic = nullptr;
static boost::asio::io_service io_service;
2015-05-13 16:20:12 +02:00
static std::condition_variable onTokenUpdate;
static void signal_handler(int signum)
2015-05-14 15:33:40 +02:00
{
l((std::string)"Received Signal " + std::to_string(signum),
LogLevel::warning);
2015-05-14 15:33:40 +02:00
io_service.stop();
}
static void session(tcp::socket &&sock)
2015-05-11 00:18:22 +02:00
{
boost::system::error_code error;
2015-05-12 03:49:26 +02:00
std::vector<char> data;
data.resize(SOCKET_BUFFERLENGTH);
2015-05-12 03:49:26 +02:00
try {
2015-09-22 17:47:42 +02:00
size_t length = sock.read_some(boost::asio::buffer(data),
error);
if (error == boost::asio::error::eof)
return;
else if (error)
throw boost::system::system_error(error);
2015-09-22 18:22:15 +02:00
const std::string request(data.begin(), data.begin()+length);
Json::Reader reader;
Json::Value root;
Response response;
2015-09-22 18:22:15 +02:00
if (reader.parse(request, root, false))
{
response.message = "Request is no valid JSON";
response.code = Response::Code::JsonError;
l(response.message, LogLevel::warning);
2015-09-22 18:22:15 +02:00
} else {
response = logic->parseRequest(root);
2015-09-22 18:22:15 +02:00
}
sock.write_some(boost::asio::buffer(response.toJson()),
2015-09-22 17:47:42 +02:00
error);
if (error == boost::asio::error::eof)
return;
else if (error)
throw boost::system::system_error(error);
2015-05-12 03:49:26 +02:00
}
catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
2015-05-12 03:49:26 +02:00
}
}
2015-05-12 03:49:26 +02:00
static void server(unsigned short port)
2015-05-12 17:35:57 +02:00
{
l(LogLevel::info, "Starting TCP Server");
2015-05-12 17:35:57 +02:00
const auto endpoint = tcp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), port);
tcp::acceptor a(io_service, endpoint);
2015-05-12 17:35:57 +02:00
tcp::socket sock(io_service);
2015-05-13 16:20:12 +02:00
std::function<void(void)> accept_connection = [&] () {
a.async_accept(sock,
[&] (boost::system::error_code ec) {
if (ec)
{
return;
}
std::thread(session, std::move(sock)).detach();
accept_connection();
});
};
2015-05-13 16:20:12 +02:00
accept_connection();
2015-05-12 17:35:57 +02:00
io_service.run();
l(LogLevel::info, "Stopped TCP Server");
}
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-09-22 17:47:42 +02:00
std::string ldapUri;
std::string bindDN;
std::string lockPagePrefix;
std::string logfile;
std::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()
2015-09-22 17:47:42 +02:00
("help,h",
"print help")
("tokentimeout,t",
po::value<unsigned int>(&timeout)->default_value(DEFAULT_TOKEN_TIMEOUT),
"Token timeout in seconds")
("port,p",
po::value<short>(&port)->default_value(DEFAULT_PORT),
"Port")
("ldap,s",
po::value<std::string>(&ldapUri)->default_value(DEFAULT_LDAP_URI),
"Ldap Server")
("bidndn,b",
po::value<std::string>(&bindDN)->default_value(DEFAULT_BINDDN),
"Bind DN, %s means username")
("web,w",
po::value<std::string>(&lockPagePrefix)->default_value(DEFAULT_WEB_PREFIX),
"Prefix of the webpage")
("logfile,l",
po::value<std::string>(&logfile)->default_value(DEFAULT_LOG_FILE),
"Log file")
("serial,i",
po::value<std::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-09-22 17:43:15 +02:00
l(LogLevel::info, "Starting Doorlock Logic");
2015-05-11 00:18:22 +02:00
try {
2015-09-22 17:47:42 +02:00
logic = std::unique_ptr<Logic>(new Logic(tokenTimeout,
ldapUri,
bindDN,
lockPagePrefix,
serDev,
onTokenUpdate));
server(port);
2015-05-11 00:18:22 +02:00
}
2015-09-22 17:43:15 +02:00
catch (...) {
l(LogLevel::error, "Fatal error, shutting down");
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-09-22 17:48:30 +02:00
if (logic) {
l(LogLevel::info, "Stopping Doorlock Logic");
logic.reset();
}
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
}