From 60bbe3df76b54f03fd318968e555821bdcf7ad33 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Tue, 22 Sep 2015 18:08:12 +0200 Subject: [PATCH] Added condition variable, which is notified on Token updates --- doorlockd/logic.cpp | 23 +++++++++++------------ doorlockd/logic.h | 9 ++++++++- doorlockd/main.cpp | 6 ++++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/doorlockd/logic.cpp b/doorlockd/logic.cpp index 908323e..209a0f4 100644 --- a/doorlockd/logic.cpp +++ b/doorlockd/logic.cpp @@ -17,13 +17,15 @@ Logic::Logic(const chrono::seconds tokenTimeout, const string &ldapUri, const string &bindDN, const string &webPrefix, - const string &serDev) : + const string &serDev, + condition_variable &onTokenUpdate) : _logger(Logger::get()), _door(serDev), _tokenTimeout(tokenTimeout), _ldapUri(ldapUri), _bindDN(bindDN), - _webPrefix(webPrefix) + _webPrefix(webPrefix), + _onTokenUpdate(onTokenUpdate) { srand(time(NULL)); _createNewToken(false); @@ -218,17 +220,14 @@ void Logic::_createNewToken(const bool stillValid) _curToken = (((uint64_t)rand())<<32) | ((uint64_t)rand()); - // TODO make things more pretty - const string uri = _webPrefix + toHexString(_curToken); - - const int ARRAY_SIZE=1024; - char buffer[ARRAY_SIZE]; - snprintf(buffer, ARRAY_SIZE, - "qrencode -l M -d 100 -s 5 \"%s\" -t png -o /tmp/qr.png", uri.c_str()); - system(buffer); - - ostringstream message; message << "New Token generated: " << toHexString(_curToken) << " old Token: " << toHexString(_prevToken) << " is " << (_prevValid?"still":"not") << " valid"; _logger(message, LogLevel::info); + + _onTokenUpdate.notify_all(); +} + +std::string Logic::getCurrentToken() const +{ + return _webPrefix + toHexString(_curToken); } diff --git a/doorlockd/logic.h b/doorlockd/logic.h index e537418..9cc9049 100644 --- a/doorlockd/logic.h +++ b/doorlockd/logic.h @@ -25,7 +25,8 @@ public: const std::string &ldapUri, const std::string &bindDN, const std::string &webPrefix, - const std::string &serDev); + const std::string &serDev, + std::condition_variable &onTokenUpdate); ~Logic(); enum Response { @@ -45,6 +46,9 @@ public: // Parse incoming JSON Requests Response parseRequest(const std::string &str); + // Returns the current Token + std::string getCurrentToken() const; + private: // Internal lock wrapper @@ -89,6 +93,9 @@ private: // Token mutex std::mutex _mutex = {}; + // This variable gets notified on token updates + std::condition_variable &_onTokenUpdate; + // The URI of the ldap server const std::string _ldapUri; // LDAP bindDN diff --git a/doorlockd/main.cpp b/doorlockd/main.cpp index c23cb9d..9859a81 100644 --- a/doorlockd/main.cpp +++ b/doorlockd/main.cpp @@ -23,6 +23,8 @@ 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 void signal_handler(int signum) { l((std::string)"Received Signal " + std::to_string(signum), @@ -159,14 +161,14 @@ int main(int argc, char** argv) signal(SIGUSR1, signal_handler); signal(SIGUSR2, signal_handler); - l(LogLevel::info, "Starting Doorlock Logic"); try { logic = std::unique_ptr(new Logic(tokenTimeout, ldapUri, bindDN, lockPagePrefix, - serDev)); + serDev, + onTokenUpdate)); server(port); } catch (...) {