From 49eab847fd98e8ee83672340a5c617364500f885 Mon Sep 17 00:00:00 2001 From: Liv Date: Tue, 15 Aug 2017 13:58:02 -0700 Subject: [PATCH] Refactor handleOctreeFileReplacement, style changes, align with handler for other file types --- assignment-client/src/octree/OctreeServer.cpp | 98 +++++++------------ assignment-client/src/octree/OctreeServer.h | 2 + interface/src/Application.cpp | 17 ++-- 3 files changed, 42 insertions(+), 75 deletions(-) diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index d6a0cc7b92..ff155b89c0 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -935,39 +935,7 @@ void OctreeServer::handleOctreeFileReplacement(QSharedPointer m // so here we just store a special file at our persist path // and then force a stop of the server so that it can pick it up when it relaunches if (!_persistAbsoluteFilePath.isEmpty()) { - - // before we restart the server and make it try and load this data, let's make sure it is valid - auto compressedOctree = message->getMessage(); - QByteArray jsonOctree; - - // assume we have GZipped content - bool wasCompressed = gunzip(compressedOctree, jsonOctree); - if (!wasCompressed) { - // the source was not compressed, assume we were sent regular JSON data - jsonOctree = compressedOctree; - } - - // check the JSON data to verify it is an object - if (QJsonDocument::fromJson(jsonOctree).isObject()) { - if (!wasCompressed) { - // source was not compressed, we compress it before we write it locally - gzip(jsonOctree, compressedOctree); - } - - // write the compressed octree data to a special file - auto replacementFilePath = _persistAbsoluteFilePath.append(OctreePersistThread::REPLACEMENT_FILE_EXTENSION); - QFile replacementFile(replacementFilePath); - if (replacementFile.open(QIODevice::WriteOnly) && replacementFile.write(compressedOctree) != -1) { - // we've now written our replacement file, time to take the server down so it can - // process it when it comes back up - qInfo() << "Wrote octree replacement file to" << replacementFilePath << "- stopping server"; - setFinished(true); - } else { - qWarning() << "Could not write replacement octree data to file - refusing to process"; - } - } else { - qDebug() << "Received replacement octree file that is invalid - refusing to process"; - } + OctreeServer::replaceContentFromMessageData(message->getMessage()); } else { qDebug() << "Cannot perform octree file replacement since current persist file path is not yet known"; } @@ -995,37 +963,7 @@ void OctreeServer::handleOctreeFileReplacementFromURL(QSharedPointererror(); if (networkError == QNetworkReply::NoError) { QByteArray contents = reply->readAll(); - - // Like above, assume we have compressed data - auto compressedOctree = contents; - QByteArray jsonOctree; - - bool wasCompressed = gunzip(compressedOctree, jsonOctree); - if (!wasCompressed) { - // the source was not compressed, assume we were sent regular JSON data - jsonOctree = compressedOctree; - } - // check the JSON data to verify it is an object - if (QJsonDocument::fromJson(jsonOctree).isObject()) { - if (!wasCompressed) { - // source was not compressed, we compress it before we write it locally - gzip(jsonOctree, compressedOctree); - } - - // write the compressed octree data to a special file - auto replacementFilePath = _persistAbsoluteFilePath.append(OctreePersistThread::REPLACEMENT_FILE_EXTENSION); - QFile replacementFile(replacementFilePath); - if (replacementFile.open(QIODevice::WriteOnly) && replacementFile.write(compressedOctree) != -1) { - // we've now written our replacement file, time to take the server down so it can - // process it when it comes back up - qInfo() << "Wrote octree replacement file to" << replacementFilePath << "- stopping server"; - setFinished(true); - } else { - qWarning() << "Could not write replacement octree data to file - refusing to process"; - } - } else { - qDebug() << "Received replacement octree file that is invalid - refusing to process"; - } + OctreeServer::replaceContentFromMessageData(contents); } else { qDebug() << "Error downloading JSON from specified file"; } @@ -1037,6 +975,38 @@ void OctreeServer::handleOctreeFileReplacementFromURL(QSharedPointer(); DependencyManager::set(); DependencyManager::set(); - DependencyManager::set(); DependencyManager::set(true); DependencyManager::set(); DependencyManager::set(); @@ -2154,7 +2152,6 @@ void Application::initializeUi() { surfaceContext->setContextProperty("Render", _renderEngine->getConfiguration().get()); surfaceContext->setContextProperty("Reticle", getApplicationCompositor().getReticleInterface()); surfaceContext->setContextProperty("Snapshot", DependencyManager::get().data()); - surfaceContext->setContextProperty("DomainManagement", DependencyManager::get().data()); surfaceContext->setContextProperty("ApplicationCompositor", &getApplicationCompositor()); @@ -5834,7 +5831,6 @@ void Application::registerScriptEngineWithApplicationServices(ScriptEngine* scri scriptEngine->registerGlobalObject("OffscreenFlags", DependencyManager::get()->getFlags()); scriptEngine->registerGlobalObject("Desktop", DependencyManager::get().data()); - scriptEngine->registerGlobalObject("DomainManagement", DependencyManager::get().data()); qScriptRegisterMetaType(scriptEngine, wrapperToScriptValue, wrapperFromScriptValue); qScriptRegisterMetaType(scriptEngine, wrapperToScriptValue, wrapperFromScriptValue); @@ -6127,7 +6123,7 @@ bool Application::askToWearAvatarAttachmentUrl(const QString& url) { bool Application::askToReplaceDomainContent(const QString& url) { QString methodDetails; if (DependencyManager::get()->getThisNodeCanReplaceContent()) { - QUrl originURL{ url }; + QUrl originURL { url }; if (originURL.host().endsWith(MARKETPLACE_CDN_HOSTNAME)) { // Create a confirmation dialog when this call is made const int MAX_CHARACTERS_PER_LINE = 90; @@ -6143,15 +6139,14 @@ bool Application::askToReplaceDomainContent(const QString& url) { if (agreeToReplaceContent) { // Given confirmation, send request to domain server to replace content qCDebug(interfaceapp) << "Attempting to replace domain content: " << url; - QByteArray _url(url.toUtf8()); + QByteArray urlData(url.toUtf8()); auto limitedNodeList = DependencyManager::get(); limitedNodeList->eachMatchingNode([](const SharedNodePointer& node) { return node->getType() == NodeType::EntityServer && node->getActiveSocket(); - }, [&_url, limitedNodeList](const SharedNodePointer& octreeNode) { - auto octreeFilePacket = NLPacket::create(PacketType::OctreeFileReplacementFromUrl, _url.size(), true); - octreeFilePacket->write(_url); + }, [&urlData, limitedNodeList](const SharedNodePointer& octreeNode) { + auto octreeFilePacket = NLPacket::create(PacketType::OctreeFileReplacementFromUrl, urlData.size(), true); + octreeFilePacket->write(urlData); limitedNodeList->sendPacket(std::move(octreeFilePacket), *octreeNode); - return true; }); DependencyManager::get()->handleLookupString(DOMAIN_SPAWNING_POINT); methodDetails = "SuccessfulRequestToReplaceContent"; @@ -6171,7 +6166,7 @@ bool Application::askToReplaceDomainContent(const QString& url) { { "content_set_url", url } }; UserActivityLogger::getInstance().logAction("replace_domain_content", messageProperties); - return false; + return true; } void Application::displayAvatarAttachmentWarning(const QString& message) const {