From 8ed116bf7fdb1566f58d52fca1297348e46b3b41 Mon Sep 17 00:00:00 2001 From: Ralf Ramsauer Date: Thu, 24 Sep 2015 21:12:59 +0200 Subject: [PATCH] Added proper client and doormessage classes Signed-off-by: Ralf Ramsauer --- doorlockd/CMakeLists.txt | 22 ++++++++++++++++------ doorlockd/clientmessage.cpp | 23 +++++++++++++++++++++++ doorlockd/clientmessage.h | 24 ++++++++++++++++++++++++ doorlockd/door.h | 8 +------- doorlockd/doormessage.h | 10 ++++++++++ doorlockd/logic.cpp | 18 +++++++----------- doorlockd/logic.h | 7 ++++--- doorlockd/main.cpp | 2 +- 8 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 doorlockd/clientmessage.cpp create mode 100644 doorlockd/clientmessage.h create mode 100644 doorlockd/doormessage.h diff --git a/doorlockd/CMakeLists.txt b/doorlockd/CMakeLists.txt index 1f2b881..3721c70 100644 --- a/doorlockd/CMakeLists.txt +++ b/doorlockd/CMakeLists.txt @@ -52,13 +52,23 @@ include_directories(${JSON_INCLUDE_DIR}) find_package (Threads) set(SRCS -main.cpp -logger.cpp -door.cpp -logic.cpp -response.cpp -util.cpp +clientmessage.cpp +clientmessage.h daemon.cpp +daemon.h +door.cpp +door.h +doormessage.h +logger.cpp +logger.h +logic.cpp +logic.h +response.cpp +response.h +util.cpp +util.h + +main.cpp ) add_executable(doorlockd ${SRCS}) diff --git a/doorlockd/clientmessage.cpp b/doorlockd/clientmessage.cpp new file mode 100644 index 0000000..df94e62 --- /dev/null +++ b/doorlockd/clientmessage.cpp @@ -0,0 +1,23 @@ +#include + +#include "clientmessage.h" + +Clientmessage::Clientmessage(std::string token, + Doormessage doormessage) : + _token(token), + _doormessage(doormessage) +{ +} + +std::string Clientmessage::toJson() const +{ + Json::StyledWriter writer; + Json::Value message; + + message["token"] = _token; + message["unlockButton"] = _doormessage.isUnlockButton; + message["lockButton"] = _doormessage.isLockButton; + message["emergencyUnlock"] = _doormessage.isEmergencyUnlock; + + return writer.write(message); +} diff --git a/doorlockd/clientmessage.h b/doorlockd/clientmessage.h new file mode 100644 index 0000000..cb76875 --- /dev/null +++ b/doorlockd/clientmessage.h @@ -0,0 +1,24 @@ +#ifndef CLIENTMESSAGE_H +#define CLIENTMESSAGE_H + +#include + +#include "doormessage.h" + +class Clientmessage +{ + +public: + + Clientmessage(std::string token, + Doormessage doormessage); + + std::string toJson() const; + +private: + + std::string _token; + Doormessage _doormessage; +}; + +#endif diff --git a/doorlockd/door.h b/doorlockd/door.h index f1a237b..d307389 100644 --- a/doorlockd/door.h +++ b/doorlockd/door.h @@ -11,17 +11,11 @@ #include #include "logger.h" +#include "doormessage.h" class Door final { public: - - struct Doormessage { - bool isUnlockButton = { false }; - bool isLockButton = { false }; - bool isEmergencyUnlock = { false }; - }; - using DoorCallback = std::function; enum class State {Unlocked, Locked}; diff --git a/doorlockd/doormessage.h b/doorlockd/doormessage.h new file mode 100644 index 0000000..ee59b11 --- /dev/null +++ b/doorlockd/doormessage.h @@ -0,0 +1,10 @@ +#ifndef DOORMESSAGE_H +#define DOORMESSAGE_H + +struct Doormessage { + bool isUnlockButton = { false }; + bool isLockButton = { false }; + bool isEmergencyUnlock = { false }; +}; + +#endif diff --git a/doorlockd/logic.cpp b/doorlockd/logic.cpp index 62cb269..ea569dd 100644 --- a/doorlockd/logic.cpp +++ b/doorlockd/logic.cpp @@ -240,25 +240,21 @@ void Logic::_createNewToken(const bool stillValid) _onClientUpdate.notify_all(); } -std::string Logic::getClientMessage() +Clientmessage Logic::getClientMessage() { std::lock_guard l(_mutex); - Json::Value message; - Json::StyledWriter writer; - - message["token"] = _webPrefix + toHexString(_curToken); - message["unlockButton"] = _doormessage.isUnlockButton; - message["lockButton"] = _doormessage.isLockButton; - message["emergencyUnlock"] = _doormessage.isEmergencyUnlock; + Clientmessage retval(_webPrefix + toHexString(_curToken), + _doormessage); // Reset doormessage - _doormessage = Door::Doormessage(); + _doormessage = Doormessage(); - return writer.write(message); + return retval; } -void Logic::_doorCallback(Door::Doormessage doormessage) +void Logic::_doorCallback(Doormessage doormessage) { std::lock_guard l(_mutex); _doormessage = doormessage; + _onClientUpdate.notify_all(); } diff --git a/doorlockd/logic.h b/doorlockd/logic.h index 735481b..821c4d4 100644 --- a/doorlockd/logic.h +++ b/doorlockd/logic.h @@ -11,6 +11,7 @@ #include "door.h" #include "logger.h" #include "response.h" +#include "clientmessage.h" /* The "Logic" class * @@ -35,7 +36,7 @@ public: Response parseRequest(const Json::Value &root); // Returns the current Token - std::string getClientMessage(); + Clientmessage getClientMessage(); private: @@ -55,7 +56,7 @@ private: // stillValid indicates whether the old (previous) token is still valid void _createNewToken(const bool stillValid); - void _doorCallback(Door::Doormessage doormessage); + void _doorCallback(Doormessage doormessage); const Logger &_logger; @@ -83,7 +84,7 @@ private: // General mutex for concurrent data access mutable std::mutex _mutex = {}; - Door::Doormessage _doormessage = {}; + Doormessage _doormessage = {}; // This variable gets notified on token updates std::condition_variable &_onClientUpdate; diff --git a/doorlockd/main.cpp b/doorlockd/main.cpp index 2232189..2bff965 100644 --- a/doorlockd/main.cpp +++ b/doorlockd/main.cpp @@ -107,7 +107,7 @@ static void session(tcp::socket &&sock) } if (run) { - sock.write_some(boost::asio::buffer(logic->getClientMessage())); + sock.write_some(boost::asio::buffer(logic->getClientMessage().toJson())); } };