From 92d19bfc984a9f003152877e25af357840006fbf Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Thu, 24 Sep 2015 19:26:31 +0200 Subject: [PATCH] Added subscriptions --- doorlockd/main.cpp | 50 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/doorlockd/main.cpp b/doorlockd/main.cpp index c69f9e4..2232189 100644 --- a/doorlockd/main.cpp +++ b/doorlockd/main.cpp @@ -32,13 +32,17 @@ const static Logger &l = Logger::get(); static std::unique_ptr logic = nullptr; static boost::asio::io_service io_service; -static std::condition_variable onTokenUpdate; +static std::mutex mutex; +static std::condition_variable onClientMessage; +static volatile bool run = true; static void signal_handler(int signum) { l((std::string)"Received Signal " + std::to_string(signum), LogLevel::warning); io_service.stop(); + run = false; + onClientMessage.notify_all(); } static void session(tcp::socket &&sock) @@ -87,6 +91,27 @@ static void session(tcp::socket &&sock) l(" Command: " + command, LogLevel::notice); if (command == "lock" || command == "unlock") { response = logic->parseRequest(root); + } else if (command == "subscribe") { + if (remoteIP.is_loopback() == false) { + response.code = Response::Code::AccessDenied; + response.message = "Subscriptions are only allowed from localhost"; + l(response.message, LogLevel::warning); + goto out; + } + while (run) { + std::unique_lock lock(mutex); + onClientMessage.wait(lock); + + if (sock.is_open() == false) { + goto out; + } + + if (run) { + sock.write_some(boost::asio::buffer(logic->getClientMessage())); + } + }; + + response.code = Response::Code::Success; } else { response.code = Response::Code::UnknownCommand; response.message = "Received unknown command " + command; @@ -94,16 +119,21 @@ static void session(tcp::socket &&sock) } out: - sock.write_some(boost::asio::buffer(response.toJson()), - error); - if (error == boost::asio::error::eof) - return; - else if (error) - throw boost::system::system_error(error); + if (sock.is_open()) { + sock.write_some(boost::asio::buffer(response.toJson()), + error); + if (error == boost::asio::error::eof) + return; + else if (error) + throw boost::system::system_error(error); + } } - catch (std::exception& e) { - std::cerr << "Exception in thread: " << e.what() << "\n"; + catch (const std::exception &e) { + std::string message = "Exception in session " + remoteIP.to_string() + + ": " + e.what(); + l(message, LogLevel::error); } + l("Closing TCP connection from " + remoteIP.to_string(), LogLevel::notice); } static void server(unsigned short port) @@ -222,7 +252,7 @@ int main(int argc, char** argv) lockPagePrefix, serDev, baudrate, - onTokenUpdate)); + onClientMessage)); server(port); } catch (...) {