use ResourceRequest in Agent to handle ATP scripts

This commit is contained in:
Stephen Birarda 2016-04-08 14:38:26 -07:00
parent b87e8220c4
commit 1df0dce971
3 changed files with 31 additions and 25 deletions

View file

@ -164,52 +164,57 @@ void Agent::requestScript() {
scriptURL = QUrl(_payload); scriptURL = QUrl(_payload);
} }
// setup a network access manager and // make sure this is not a script request for the file scheme
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); if (scriptURL.scheme() == URL_SCHEME_FILE) {
qWarning() << "Cannot load script for Agent from local filesystem.";
scriptRequestFinished();
return;
}
QNetworkDiskCache* cache = new QNetworkDiskCache(); auto request = ResourceManager::createResourceRequest(this, scriptURL);
QString cachePath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "agentCache");
networkAccessManager.setCache(cache);
QNetworkRequest networkRequest = QNetworkRequest(scriptURL); if (!request) {
networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); qWarning() << "Could not create ResourceRequest for Agent script at" << scriptURL.toString();
scriptRequestFinished();
return;
}
// setup a timeout for script request // setup a timeout for script request
static const int SCRIPT_TIMEOUT_MS = 10000; static const int SCRIPT_TIMEOUT_MS = 10000;
_scriptRequestTimeout = new QTimer(this); connect(&_scriptRequestTimeout, &QTimer::timeout, this, &Agent::scriptRequestFinished);
connect(_scriptRequestTimeout, &QTimer::timeout, this, &Agent::scriptRequestFinished); _scriptRequestTimeout.start(SCRIPT_TIMEOUT_MS);
_scriptRequestTimeout->start(SCRIPT_TIMEOUT_MS);
connect(request, &ResourceRequest::finished, this, &Agent::scriptRequestFinished);
qDebug() << "Downloading script at" << scriptURL.toString(); qInfo() << "Requesting script at URL" << qPrintable(request->getUrl().toString());
QNetworkReply* reply = networkAccessManager.get(networkRequest);
connect(reply, &QNetworkReply::finished, this, &Agent::scriptRequestFinished); request->send();
} }
void Agent::scriptRequestFinished() { void Agent::scriptRequestFinished() {
auto reply = qobject_cast<QNetworkReply*>(sender()); auto request = qobject_cast<ResourceRequest*>(sender());
_scriptRequestTimeout->stop(); _scriptRequestTimeout.stop();
if (reply && reply->error() == QNetworkReply::NoError) { if (request && request->getResult() == ResourceRequest::Success) {
_scriptContents = reply->readAll(); _scriptContents = request->getData();
qDebug() << "Downloaded script:" << _scriptContents; qInfo() << "Downloaded script:" << _scriptContents;
// we could just call executeScript directly - we use a QueuedConnection to allow scriptRequestFinished // we could just call executeScript directly - we use a QueuedConnection to allow scriptRequestFinished
// to return before calling executeScript // to return before calling executeScript
QMetaObject::invokeMethod(this, "executeScript", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "executeScript", Qt::QueuedConnection);
} else { } else {
if (reply) { if (request) {
qDebug() << "Failed to download script at" << reply->url().toString() << " - bailing on assignment."; qWarning() << "Failed to download script at" << request->getUrl().toString() << " - bailing on assignment.";
qDebug() << "QNetworkReply error was" << reply->errorString(); qWarning() << "ResourceRequest error was" << request->getResult();
} else { } else {
qDebug() << "Failed to download script - request timed out. Bailing on assignment."; qWarning() << "Failed to download script - request timed out. Bailing on assignment.";
} }
setFinished(true); setFinished(true);
} }
reply->deleteLater(); request->deleteLater();
} }
void Agent::executeScript() { void Agent::executeScript() {

View file

@ -83,7 +83,7 @@ private:
void sendAvatarBillboardPacket(); void sendAvatarBillboardPacket();
QString _scriptContents; QString _scriptContents;
QTimer* _scriptRequestTimeout { nullptr }; QTimer _scriptRequestTimeout;
bool _isListeningToAudioStream = false; bool _isListeningToAudioStream = false;
SharedSoundPointer _avatarSound; SharedSoundPointer _avatarSound;
int _numAvatarSoundSentBytes = 0; int _numAvatarSoundSentBytes = 0;

View file

@ -20,6 +20,7 @@ void ResourceRequest::send() {
QMetaObject::invokeMethod(this, "send", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "send", Qt::QueuedConnection);
return; return;
} }
Q_ASSERT(_state == NotStarted); Q_ASSERT(_state == NotStarted);
_state = InProgress; _state = InProgress;