Showing the changes to libraries/script-engine/src/ScriptCache.cpp (1 of 8 files). Show all
Unified · Split
| @@ -0,0 +1,146 @@ | ||
| 1 | +// | |
| 2 | +// ScriptCache.cpp | |
| 3 | +// libraries/script-engine/src | |
| 4 | +// | |
| 5 | +// Created by Brad Hefta-Gaub on 2015-03-30 | |
| 6 | +// Copyright 2015 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include <QCoreApplication> | |
| 13 | +#include <QEventLoop> | |
| 14 | +#include <QNetworkAccessManager> | |
| 15 | +#include <QNetworkReply> | |
| 16 | +#include <QObject> | |
| 17 | + | |
| 18 | +#include <NetworkAccessManager.h> | |
| 19 | +#include <SharedUtil.h> | |
| 20 | + | |
| 21 | +#include "ScriptCache.h" | |
| 22 | + | |
| 23 | +ScriptCache::ScriptCache(QObject* parent) { | |
| 24 | + // nothing to do here... | |
| 25 | +} | |
| 26 | + | |
| 27 | +// QHash<QUrl, QString> _scriptCache; | |
| 28 | + | |
| 29 | +void ScriptCache::getScript(const QUrl& url, ScriptUser* scriptUser) { | |
| 30 | + if (_scriptCache.contains(url)) { | |
| 31 | + scriptUser->scriptContentsAvailable(url, _scriptCache[url]); | |
| 32 | + return; | |
| 33 | + } | |
| 34 | + _scriptUsers.insert(url, scriptUser); | |
| 35 | + QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 36 | + QNetworkRequest networkRequest = QNetworkRequest(url); | |
| 37 | + networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); | |
| 38 | + QNetworkReply* reply = networkAccessManager.get(networkRequest); | |
| 39 | + connect(reply, &QNetworkReply::finished, this, &ScriptCache::scriptDownloaded); | |
| 40 | +} | |
| 41 | + | |
| 42 | +void ScriptCache::scriptDownloaded() { | |
| 43 | + QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); | |
| 44 | + QUrl url = reply->url(); | |
| 45 | + QList<ScriptUser*> scriptUsers = _scriptUsers.values(url); | |
| 46 | + _scriptUsers.remove(url); | |
| 47 | + | |
| 48 | + if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 49 | + _scriptCache[url] = reply->readAll(); | |
| 50 | + qDebug() << "_scriptCache.size:" << _scriptCache.size(); | |
| 51 | + | |
| 52 | + foreach(ScriptUser* user, scriptUsers) { | |
| 53 | + user->scriptContentsAvailable(url, _scriptCache[url]); | |
| 54 | + } | |
| 55 | + } else { | |
| 56 | + qDebug() << "ERROR Loading file:" << reply->url().toString(); | |
| 57 | + foreach(ScriptUser* user, scriptUsers) { | |
| 58 | + user->errorInLoadingScript(url); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + reply->deleteLater(); | |
| 62 | +} | |
| 63 | + | |
| 64 | +QString ScriptCache::getScript(const QUrl& url) { | |
| 65 | + qDebug() << "Script requested: " << url.toString(); | |
| 66 | + QString scriptContents; | |
| 67 | + if (_scriptCache.contains(url)) { | |
| 68 | + qDebug() << "Script found in cache: " << url.toString(); | |
| 69 | + scriptContents = _scriptCache[url]; | |
| 70 | + } else { | |
| 71 | + qDebug() << "Script not found in cache: " << url.toString(); | |
| 72 | + | |
| 73 | + if (_blockingScriptsPending.contains(url)) { | |
| 74 | + // if we're already downloading this script, wait for it to complete... | |
| 75 | + qDebug() << "Already downloading script: " << url.toString(); | |
| 76 | + while (_blockingScriptsPending.contains(url)) { | |
| 77 | + QEventLoop loop; | |
| 78 | + qDebug() << "about to call loop.processEvents()...."; | |
| 79 | + loop.processEvents(QEventLoop::AllEvents, 1); | |
| 80 | + qDebug() << "AFTER loop.processEvents()...."; | |
| 81 | + } | |
| 82 | + qDebug() << "Done waiting on downloading script: " << url.toString(); | |
| 83 | + scriptContents = _scriptCache[url]; | |
| 84 | + } else { | |
| 85 | + _blockingScriptsPending.insert(url); | |
| 86 | + qDebug() << "_blockingScriptsPending: " << _blockingScriptsPending; | |
| 87 | + | |
| 88 | + QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); | |
| 89 | + QNetworkRequest networkRequest = QNetworkRequest(url); | |
| 90 | + networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); | |
| 91 | + QNetworkReply* reply = networkAccessManager.get(networkRequest); | |
| 92 | + | |
| 93 | + | |
| 94 | + connect(reply, &QNetworkReply::finished, this, &ScriptCache::scriptDownloadedSyncronously); | |
| 95 | + qDebug() << "Downloading script at" << url.toString(); | |
| 96 | + //QEventLoop loop; | |
| 97 | + //QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); | |
| 98 | + //qDebug() << "starting loop...."; | |
| 99 | + //loop.exec(); | |
| 100 | + //qDebug() << "after loop...."; | |
| 101 | + | |
| 102 | + /* | |
| 103 | + if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 104 | + scriptContents = reply->readAll(); | |
| 105 | + _scriptCache[url] = scriptContents; | |
| 106 | + qDebug() << "_scriptCache.size:" << _scriptCache.size(); | |
| 107 | + } else { | |
| 108 | + qDebug() << "ERROR Loading file:" << url.toString(); | |
| 109 | + } | |
| 110 | + delete reply; | |
| 111 | + _blockingScriptsPending.remove(url); | |
| 112 | + qDebug() << "_blockingScriptsPending: " << _blockingScriptsPending; | |
| 113 | + */ | |
| 114 | + while (_blockingScriptsPending.contains(url)) { | |
| 115 | + QEventLoop loop; | |
| 116 | + qDebug() << "about to call loop.processEvents()...."; | |
| 117 | + loop.processEvents(QEventLoop::AllEvents, 1); | |
| 118 | + qDebug() << "AFTER loop.processEvents()...."; | |
| 119 | + } | |
| 120 | + qDebug() << "Done waiting on downloading script: " << url.toString(); | |
| 121 | + scriptContents = _scriptCache[url]; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + return scriptContents; | |
| 125 | +} | |
| 126 | + | |
| 127 | +void ScriptCache::scriptDownloadedSyncronously() { | |
| 128 | + | |
| 129 | + qDebug() << "ScriptCache::scriptDownloadedSyncronously()...."; | |
| 130 | + | |
| 131 | + QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); | |
| 132 | + QUrl url = reply->url(); | |
| 133 | + | |
| 134 | + if (reply->error() == QNetworkReply::NoError && reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) { | |
| 135 | + _scriptCache[url] = reply->readAll(); | |
| 136 | + qDebug() << "_scriptCache.size:" << _scriptCache.size(); | |
| 137 | + } else { | |
| 138 | + qDebug() << "ERROR Loading file:" << reply->url().toString(); | |
| 139 | + } | |
| 140 | + | |
| 141 | + reply->deleteLater(); | |
| 142 | + | |
| 143 | + _blockingScriptsPending.remove(url); | |
| 144 | + qDebug() << "_blockingScriptsPending: " << _blockingScriptsPending; | |
| 145 | +} | |
| 146 | + | |