Clean-up: Include delta time steps in time topic

This commit is contained in:
Emma Broman
2020-08-25 08:55:30 +02:00
parent 08b42f32de
commit 157df5ff93
5 changed files with 18 additions and 190 deletions

View File

@@ -26,7 +26,6 @@
#include <modules/server/include/topics/authorizationtopic.h>
#include <modules/server/include/topics/bouncetopic.h>
#include <modules/server/include/topics/deltatimestepstopic.h>
#include <modules/server/include/topics/documentationtopic.h>
#include <modules/server/include/topics/flightcontrollertopic.h>
#include <modules/server/include/topics/getpropertytopic.h>
@@ -57,7 +56,6 @@ namespace {
constexpr const char* VersionTopicKey = "version";
constexpr const char* AuthenticationTopicKey = "authorize";
constexpr const char* DeltaTimeStepsTopicKey = "deltatimesteps";
constexpr const char* DocumentationTopicKey = "documentation";
constexpr const char* GetPropertyTopicKey = "get";
constexpr const char* LuaScriptTopicKey = "luascript";
@@ -97,7 +95,6 @@ Connection::Connection(std::unique_ptr<ghoul::io::Socket> s,
);
_topicFactory.registerClass<DocumentationTopic>(DocumentationTopicKey);
_topicFactory.registerClass<DeltaTimeStepsTopic>(DeltaTimeStepsTopicKey);
_topicFactory.registerClass<GetPropertyTopic>(GetPropertyTopicKey);
_topicFactory.registerClass<LuaScriptTopic>(LuaScriptTopicKey);
_topicFactory.registerClass<SessionRecordingTopic>(SessionRecordingTopicKey);

View File

@@ -1,127 +0,0 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2020 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include "modules/server/include/topics/deltatimestepstopic.h"
#include <modules/server/include/connection.h>
#include <openspace/engine/globals.h>
#include <openspace/properties/property.h>
#include <openspace/query/query.h>
#include <openspace/util/timemanager.h>
#include <ghoul/logging/logmanager.h>
namespace {
constexpr const char* EventKey = "event";
constexpr const char* SubscribeEvent = "start_subscription";
constexpr const char* UnsubscribeEvent = "stop_subscription";
} // namespace
using nlohmann::json;
namespace openspace {
DeltaTimeStepsTopic::DeltaTimeStepsTopic() {}
DeltaTimeStepsTopic::~DeltaTimeStepsTopic() {
if (_deltaTimeCallbackHandle != UnsetOnChangeHandle) {
global::timeManager.removeDeltaTimeChangeCallback(
_deltaTimeCallbackHandle
);
}
if (_deltaTimesListCallbackHandle != UnsetOnChangeHandle) {
global::timeManager.removeDeltaTimeStepsChangeCallback(
_deltaTimesListCallbackHandle
);
}
}
bool DeltaTimeStepsTopic::isDone() const {
return _isDone;
}
bool DeltaTimeStepsTopic::dataHasChanged() {
std::optional<double> nextStep = global::timeManager.nextDeltaTimeStep();
std::optional<double> prevStep = global::timeManager.previousDeltaTimeStep();
return (nextStep != _lastNextDeltaTime || prevStep != _lastPrevDeltaTime);
}
void DeltaTimeStepsTopic::handleJson(const nlohmann::json& json) {
std::string event = json.at(EventKey).get<std::string>();
if (event == UnsubscribeEvent) {
_isDone = true;
return;
}
sendDeltaTimesData();
if (event != SubscribeEvent) {
_isDone = true;
return;
}
_deltaTimeCallbackHandle = global::timeManager.addDeltaTimeChangeCallback(
[this]() {
if (dataHasChanged()) {
sendDeltaTimesData();
}
}
);
_deltaTimesListCallbackHandle = global::timeManager.addDeltaTimeStepsChangeCallback(
[this]() {
if (dataHasChanged()) {
sendDeltaTimesData();
}
}
);
}
void DeltaTimeStepsTopic::sendDeltaTimesData() {
std::optional<double> nextStep = global::timeManager.nextDeltaTimeStep();
std::optional<double> prevStep = global::timeManager.previousDeltaTimeStep();
bool hasNext = nextStep.has_value();
bool hasPrev = prevStep.has_value();
json deltaTimesListJson = {
{ "hasNextStep", hasNext },
{ "hasPrevStep", hasPrev }
};
if (hasNext) {
deltaTimesListJson["nextStep"] = nextStep.value();
}
if (hasPrev) {
deltaTimesListJson["prevStep"] = prevStep.value();
}
_connection->sendJson(wrappedPayload(deltaTimesListJson));
_lastNextDeltaTime = nextStep;
_lastPrevDeltaTime = prevStep;
}
} // namespace openspace

View File

@@ -30,6 +30,7 @@
#include <openspace/query/query.h>
#include <openspace/util/timemanager.h>
#include <ghoul/logging/logmanager.h>
#include <optional>
namespace {
constexpr const char* EventKey = "event";
@@ -110,13 +111,29 @@ void TimeTopic::sendFullTimeData() {
const double targetDeltaTime = global::timeManager.targetDeltaTime();
const bool isPaused = global::timeManager.isPaused();
const json timeJson = {
std::optional<double> nextStep = global::timeManager.nextDeltaTimeStep();
std::optional<double> prevStep = global::timeManager.previousDeltaTimeStep();
bool hasNext = nextStep.has_value();
bool hasPrev = prevStep.has_value();
json timeJson = {
{ "time", currentTime },
{ "deltaTime", deltaTime},
{ "targetDeltaTime", targetDeltaTime},
{ "isPaused", isPaused },
{ "hasNextStep", hasNext },
{ "hasPrevStep", hasPrev }
};
if (hasNext) {
timeJson["nextStep"] = nextStep.value();
}
if (hasPrev) {
timeJson["prevStep"] = prevStep.value();
}
_connection->sendJson(wrappedPayload(timeJson));
_lastUpdateTime = std::chrono::system_clock::now();
_lastPauseState = isPaused;