From 1c000a045bd8ee58e11231b4119c0e8d4cee9d55 Mon Sep 17 00:00:00 2001 From: Emil Axelsson Date: Wed, 16 May 2018 12:47:06 +0200 Subject: [PATCH] Make it possible to unsubscribe to the time (#612) --- modules/server/include/timetopic.h | 1 + modules/server/src/topics/timetopic.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/server/include/timetopic.h b/modules/server/include/timetopic.h index 00d314984d..e3197d4916 100644 --- a/modules/server/include/timetopic.h +++ b/modules/server/include/timetopic.h @@ -41,6 +41,7 @@ private: nlohmann::json currentTime(); nlohmann::json deltaTime(); + bool _isDone; int _timeCallbackHandle; int _deltaTimeCallbackHandle; std::chrono::system_clock::time_point _lastUpdateTime; diff --git a/modules/server/src/topics/timetopic.cpp b/modules/server/src/topics/timetopic.cpp index 31f3a713a3..91c57b7783 100644 --- a/modules/server/src/topics/timetopic.cpp +++ b/modules/server/src/topics/timetopic.cpp @@ -31,6 +31,8 @@ namespace { const char* _loggerCat = "TimeTopic"; const char* PropertyKey = "property"; +const char* EventKey = "event"; +const char* UnsubscribeEvent = "stop_subscription"; const char* CurrentTimeKey = "currentTime"; const char* DeltaTimeKey = "deltaTime"; const int UNSET_ONCHANGE_HANDLE = -1; @@ -43,6 +45,7 @@ namespace openspace { TimeTopic::TimeTopic() : Topic() + , _isDone(false) , _timeCallbackHandle(UNSET_ONCHANGE_HANDLE) , _deltaTimeCallbackHandle(UNSET_ONCHANGE_HANDLE) , _lastUpdateTime(std::chrono::system_clock::now()) @@ -60,10 +63,15 @@ TimeTopic::~TimeTopic() { } bool TimeTopic::isDone() { - return false; + return _isDone; } void TimeTopic::handleJson(json j) { + std::string event = j.at(EventKey).get(); + if (event == UnsubscribeEvent) { + _isDone = true; + return; + } std::string requestedKey = j.at(PropertyKey).get(); LDEBUG("Subscribing to " + requestedKey); @@ -92,6 +100,7 @@ void TimeTopic::handleJson(json j) { } else { LWARNING("Cannot get " + requestedKey); + _isDone = true; } }