From 3fa538d42d596e474b071f55bc10d34ecb361a17 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Mon, 26 Feb 2018 16:24:57 -0800 Subject: [PATCH] Add RawEntityData subclass --- assignment-client/src/octree/OctreeServer.cpp | 4 +- .../src/DomainContentBackupManager.cpp | 1 + domain-server/src/DomainServer.cpp | 18 ++++---- domain-server/src/EntitiesBackupHandler.cpp | 4 +- libraries/entities/src/EntityTree.h | 2 + libraries/networking/src/udt/Socket.cpp | 2 +- libraries/octree/src/Octree.h | 3 +- libraries/octree/src/OctreePersistThread.cpp | 4 +- libraries/octree/src/OctreeUtils.cpp | 43 +++++++++++++------ libraries/octree/src/OctreeUtils.h | 24 +++++++++-- 10 files changed, 70 insertions(+), 35 deletions(-) diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index f3859e99cf..f11e96963b 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -1157,7 +1157,7 @@ void OctreeServer::domainSettingsRequestComplete() { OctreeUtils::RawOctreeData data; qCDebug(octree_server) << "Reading octree data from" << _persistAbsoluteFilePath; - if (OctreeUtils::readOctreeDataInfoFromFile(_persistAbsoluteFilePath, &data)) { + if (data.readOctreeDataInfoFromFile(_persistAbsoluteFilePath)) { qCDebug(octree_server) << "Current octree data: ID(" << data.id << ") DataVersion(" << data.version << ")"; packet->writePrimitive(true); auto id = data.id.toRfc4122(); @@ -1189,7 +1189,7 @@ void OctreeServer::handleOctreeDataFileReply(QSharedPointer mes OctreeUtils::RawOctreeData data; qCDebug(octree_server) << "Reading octree data from" << _persistAbsoluteFilePath; - if (OctreeUtils::readOctreeDataInfoFromFile(_persistAbsoluteFilePath, &data)) { + if (data.readOctreeDataInfoFromFile(_persistAbsoluteFilePath)) { if (data.id.isNull()) { qCDebug(octree_server) << "Current octree data has a null id, updating"; data.resetIdAndVersion(); diff --git a/domain-server/src/DomainContentBackupManager.cpp b/domain-server/src/DomainContentBackupManager.cpp index 60327be1dd..85040d8c35 100644 --- a/domain-server/src/DomainContentBackupManager.cpp +++ b/domain-server/src/DomainContentBackupManager.cpp @@ -39,6 +39,7 @@ #include "DomainServer.h" const std::chrono::seconds DomainContentBackupManager::DEFAULT_PERSIST_INTERVAL { 30 }; + // Backup format looks like: daily_backup-TIMESTAMP.zip static const QString DATETIME_FORMAT { "yyyy-MM-dd_HH-mm-ss" }; static const QString DATETIME_FORMAT_RE { "\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}" }; diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index a33840462b..7ec86c8b3a 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -1745,9 +1745,9 @@ void DomainServer::processOctreeDataPersistMessage(QSharedPointerwritePrimitive(false); @@ -3344,8 +3344,8 @@ void DomainServer::setupGroupCacheRefresh() { void DomainServer::maybeHandleReplacementEntityFile() { const auto replacementFilePath = getEntitiesReplacementFilePath(); - OctreeUtils::RawOctreeData data; - if (!OctreeUtils::readOctreeDataInfoFromFile(replacementFilePath, &data)) { + OctreeUtils::RawEntityData data; + if (!data.readOctreeDataInfoFromFile(replacementFilePath)) { qCWarning(domain_server) << "Replacement file could not be read, it either doesn't exist or is invalid."; } else { qCDebug(domain_server) << "Replacing existing entity date with replacement file"; @@ -3381,8 +3381,8 @@ void DomainServer::handleOctreeFileReplacement(QByteArray octreeFile) { jsonOctree = compressedOctree; } - OctreeUtils::RawOctreeData data; - if (OctreeUtils::readOctreeDataInfoFromData(jsonOctree, &data)) { + OctreeUtils::RawEntityData data; + if (data.readOctreeDataInfoFromData(jsonOctree)) { data.resetIdAndVersion(); gzip(data.toByteArray(), compressedOctree); diff --git a/domain-server/src/EntitiesBackupHandler.cpp b/domain-server/src/EntitiesBackupHandler.cpp index 76b32094a6..9b4ff786a2 100644 --- a/domain-server/src/EntitiesBackupHandler.cpp +++ b/domain-server/src/EntitiesBackupHandler.cpp @@ -62,8 +62,8 @@ void EntitiesBackupHandler::recoverBackup(const QString& backupName, QuaZip& zip zipFile.close(); - OctreeUtils::RawOctreeData data; - if (!OctreeUtils::readOctreeDataInfoFromData(rawData, &data)) { + OctreeUtils::RawEntityData data; + if (!data.readOctreeDataInfoFromData(rawData)) { qCritical() << "Unable to parse octree data during backup recovery"; return; } diff --git a/libraries/entities/src/EntityTree.h b/libraries/entities/src/EntityTree.h index cb3d6d57e4..672cfbc931 100644 --- a/libraries/entities/src/EntityTree.h +++ b/libraries/entities/src/EntityTree.h @@ -115,6 +115,8 @@ public: virtual void update() override { update(true); } + std::unique_ptr getRawOctreeData(); + void update(bool simulate); // The newer API... diff --git a/libraries/networking/src/udt/Socket.cpp b/libraries/networking/src/udt/Socket.cpp index f22026cd70..019ae07c16 100644 --- a/libraries/networking/src/udt/Socket.cpp +++ b/libraries/networking/src/udt/Socket.cpp @@ -328,7 +328,7 @@ void Socket::checkForReadyReadBackup() { void Socket::readPendingDatagrams() { int packetSizeWithHeader = -1; - while ((packetSizeWithHeader = _udpSocket.pendingDatagramSize()) != -1) { + while ((packetSizeWithHeader = _udpSocket.pendingDatagramSize()) > 0) { // we're reading a packet so re-start the readyRead backup timer _readyReadBackupTimer->start(); diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 8954e53f8b..cb281593b1 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -28,6 +28,7 @@ #include "OctreeElementBag.h" #include "OctreePacketData.h" #include "OctreeSceneStats.h" +#include "OctreeUtils.h" class ReadBitstreamToTreeParams; class Octree; @@ -328,7 +329,7 @@ public: virtual void dumpTree() { } virtual void pruneTree() { } - void setEntityVersionInfo(QUuid id, int64_t dataVersion) { + void setOctreeVersionInfo(QUuid id, int64_t dataVersion) { _persistID = id; _persistDataVersion = dataVersion; } diff --git a/libraries/octree/src/OctreePersistThread.cpp b/libraries/octree/src/OctreePersistThread.cpp index a669b7d3bb..d35bc6c869 100644 --- a/libraries/octree/src/OctreePersistThread.cpp +++ b/libraries/octree/src/OctreePersistThread.cpp @@ -176,9 +176,9 @@ bool OctreePersistThread::process() { } OctreeUtils::RawOctreeData data; - if (OctreeUtils::readOctreeDataInfoFromFile(_filename, &data)) { + if (data.readOctreeDataInfoFromFile(_filename)) { qDebug() << "Setting entity version info to: " << data.id << data.version; - _tree->setEntityVersionInfo(data.id, data.version); + _tree->setOctreeVersionInfo(data.id, data.version); } bool persistentFileRead; diff --git a/libraries/octree/src/OctreeUtils.cpp b/libraries/octree/src/OctreeUtils.cpp index fd0d496a9d..d1608280d7 100644 --- a/libraries/octree/src/OctreeUtils.cpp +++ b/libraries/octree/src/OctreeUtils.cpp @@ -84,7 +84,7 @@ float getOrthographicAccuracySize(float octreeSizeScale, int boundaryLevelAdjust // Reads octree file and parses it into a QJsonDocument. Handles both gzipped and non-gzipped files. // Returns true if the file was successfully opened and parsed, otherwise false. // Example failures: file does not exist, gzipped file cannot be unzipped, invalid JSON. -bool OctreeUtils::readOctreeFile(QString path, QJsonDocument* doc) { +bool readOctreeFile(QString path, QJsonDocument* doc) { QFile file(path); if (!file.open(QIODevice::ReadOnly)) { qCritical() << "Cannot open json file for reading: " << path; @@ -102,18 +102,16 @@ bool OctreeUtils::readOctreeFile(QString path, QJsonDocument* doc) { return !doc->isNull(); } -bool readOctreeDataInfoFromJSON(QJsonObject root, OctreeUtils::RawOctreeData* octreeData) { +bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromJSON(QJsonObject root) { if (root.contains("Id") && root.contains("DataVersion")) { - octreeData->id = root["Id"].toVariant().toUuid(); - octreeData->version = root["DataVersion"].toInt(); - } - if (root.contains("Entities")) { - octreeData->octreeData = root["Entities"].toArray(); + id = root["Id"].toVariant().toUuid(); + version = root["DataVersion"].toInt(); } + readSubclassData(root); return true; } -bool OctreeUtils::readOctreeDataInfoFromData(QByteArray data, OctreeUtils::RawOctreeData* octreeData) { +bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromData(QByteArray data) { QByteArray jsonData; if (gunzip(data, jsonData)) { data = jsonData; @@ -125,30 +123,31 @@ bool OctreeUtils::readOctreeDataInfoFromData(QByteArray data, OctreeUtils::RawOc } auto root = doc.object(); - return readOctreeDataInfoFromJSON(root, octreeData); + return readOctreeDataInfoFromJSON(root); } // Reads octree file and parses it into a RawOctreeData object. // Returns false if readOctreeFile fails. -bool OctreeUtils::readOctreeDataInfoFromFile(QString path, OctreeUtils::RawOctreeData* octreeData) { +bool OctreeUtils::RawOctreeData::readOctreeDataInfoFromFile(QString path) { QJsonDocument doc; - if (!OctreeUtils::readOctreeFile(path, &doc)) { + if (!readOctreeFile(path, &doc)) { return false; } auto root = doc.object(); - return readOctreeDataInfoFromJSON(root, octreeData); + return readOctreeDataInfoFromJSON(root); } QByteArray OctreeUtils::RawOctreeData::toByteArray() { - const auto protocolVersion = (int)versionForPacketType(PacketType::EntityData); + const auto protocolVersion = (int)versionForPacketType(dataPacketType()); QJsonObject obj { { "DataVersion", QJsonValue((qint64)version) }, { "Id", QJsonValue(id.toString()) }, { "Version", protocolVersion }, - { "Entities", octreeData } }; + writeSubclassData(obj); + QJsonDocument doc; doc.setObject(obj); @@ -167,8 +166,24 @@ QByteArray OctreeUtils::RawOctreeData::toGzippedByteArray() { return gzData; } +PacketType OctreeUtils::RawOctreeData::dataPacketType() const { + Q_ASSERT(false); + qCritical() << "Attemping to read packet type for incomplete base type 'RawOctreeData'"; + return (PacketType)0; +} + void OctreeUtils::RawOctreeData::resetIdAndVersion() { id = QUuid::createUuid(); version = OctreeUtils::INITIAL_VERSION; qDebug() << "Reset octree data to: " << id << version; +} + +void OctreeUtils::RawEntityData::readSubclassData(QJsonObject root) { + if (root.contains("Entities")) { + entityData = root["Entities"].toArray(); + } +} + +void OctreeUtils::RawEntityData::writeSubclassData(QJsonObject root) const { + root["Entities"] = entityData; } \ No newline at end of file diff --git a/libraries/octree/src/OctreeUtils.h b/libraries/octree/src/OctreeUtils.h index 6fb0e62bcb..dd761c2a76 100644 --- a/libraries/octree/src/OctreeUtils.h +++ b/libraries/octree/src/OctreeUtils.h @@ -17,6 +17,10 @@ #include #include +#include + +#include + class AABox; class QJsonDocument; @@ -31,19 +35,31 @@ public: QUuid id { QUuid() }; Version version { -1 }; - QJsonArray octreeData; + virtual PacketType dataPacketType() const; + + virtual void readSubclassData(QJsonObject root) { } + virtual void writeSubclassData(QJsonObject root) const { } void resetIdAndVersion(); QByteArray toByteArray(); QByteArray toGzippedByteArray(); + + bool readOctreeDataInfoFromData(QByteArray data); + bool readOctreeDataInfoFromFile(QString path); + bool readOctreeDataInfoFromJSON(QJsonObject root); }; -bool readOctreeFile(QString path, QJsonDocument* doc); -bool readOctreeDataInfoFromData(QByteArray data, RawOctreeData* octreeData); -bool readOctreeDataInfoFromFile(QString path, RawOctreeData* octreeData); +class RawEntityData : public RawOctreeData { + PacketType dataPacketType() const override { return PacketType::EntityData; } + void readSubclassData(QJsonObject root) override; + void writeSubclassData(QJsonObject root) const override; + + QJsonArray entityData; +}; } + /// renderAccuracy represents a floating point "visibility" of an object based on it's view from the camera. At a simple /// level it returns 0.0f for things that are so small for the current settings that they could not be visible. float calculateRenderAccuracy(const glm::vec3& position,