// // SandboxUtils.cpp // libraries/networking/src // // Created by Brad Hefta-Gaub on 2016-10-15. // Copyright 2016 High Fidelity, Inc. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "SandboxUtils.h" #include "NetworkAccessManager.h" #include "NetworkLogging.h" void SandboxUtils::ifLocalSandboxRunningElse(std::function localSandboxRunningDoThis, std::function localSandboxNotRunningDoThat) { QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); QNetworkRequest sandboxStatus(SANDBOX_STATUS_URL); sandboxStatus.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); sandboxStatus.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); QNetworkReply* reply = networkAccessManager.get(sandboxStatus); connect(reply, &QNetworkReply::finished, this, [reply, localSandboxRunningDoThis, localSandboxNotRunningDoThat]() { if (reply->error() == QNetworkReply::NoError) { auto statusData = reply->readAll(); auto statusJson = QJsonDocument::fromJson(statusData); if (!statusJson.isEmpty()) { auto statusObject = statusJson.object(); auto serversValue = statusObject.value("servers"); if (!serversValue.isUndefined() && serversValue.isObject()) { auto serversObject = serversValue.toObject(); auto serversCount = serversObject.size(); const int MINIMUM_EXPECTED_SERVER_COUNT = 5; if (serversCount >= MINIMUM_EXPECTED_SERVER_COUNT) { localSandboxRunningDoThis(); return; } } } } localSandboxNotRunningDoThat(); }); } void SandboxUtils::runLocalSandbox(QString contentPath, bool autoShutdown, QString runningMarkerName, bool noUpdater) { QString applicationDirPath = QFileInfo(QCoreApplication::applicationFilePath()).path(); QString serverPath = applicationDirPath + "/server-console/server-console.exe"; qCDebug(networking) << "Application dir path is: " << applicationDirPath; qCDebug(networking) << "Server path is: " << serverPath; qCDebug(networking) << "autoShutdown: " << autoShutdown; qCDebug(networking) << "noUpdater: " << noUpdater; bool hasContentPath = !contentPath.isEmpty(); bool passArgs = autoShutdown || hasContentPath || noUpdater; QStringList args; if (passArgs) { args << "--"; } if (hasContentPath) { QString serverContentPath = applicationDirPath + "/" + contentPath; args << "--contentPath" << serverContentPath; } if (autoShutdown) { QString interfaceRunningStateFile = RunningMarker::getMarkerFilePath(runningMarkerName); args << "--shutdownWatcher" << interfaceRunningStateFile; } if (noUpdater) { args << "--noUpdater"; } qCDebug(networking) << applicationDirPath; qCDebug(networking) << "Launching sandbox with:" << args; qCDebug(networking) << QProcess::startDetached(serverPath, args); // Sleep a short amount of time to give the server a chance to start usleep(2000000); /// do we really need this?? }