From 6334116070369afbffcd4ef4f88b17efe7bc7fdd Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 28 Jul 2014 12:07:19 -0700 Subject: [PATCH] request domain settings after successful connection to domain --- libraries/networking/src/DomainHandler.cpp | 60 +++++++++++++++++++++- libraries/networking/src/DomainHandler.h | 8 +++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index f603d21240..286909279b 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -9,6 +9,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include + #include "NodeList.h" #include "PacketHeaders.h" #include "UserActivityLogger.h" @@ -21,7 +23,9 @@ DomainHandler::DomainHandler(QObject* parent) : _sockAddr(HifiSockAddr(QHostAddress::Null, DEFAULT_DOMAIN_SERVER_PORT)), _assignmentUUID(), _isConnected(false), - _handshakeTimer(NULL) + _handshakeTimer(NULL), + _settingsObject(), + _failedSettingsRequests(0) { } @@ -37,8 +41,14 @@ void DomainHandler::clearConnectionInfo() { } } +void DomainHandler::clearSettings() { + _settingsObject = QJsonObject(); + _failedSettingsRequests = 0; +} + void DomainHandler::reset() { clearConnectionInfo(); + clearSettings(); _hostname = QString(); _sockAddr.setAddress(QHostAddress::Null); } @@ -109,10 +119,58 @@ void DomainHandler::setIsConnected(bool isConnected) { if (_isConnected) { emit connectedToDomain(_hostname); + + // we've connected to new domain - time to ask it for global settings + requestDomainSettings(); } } } +void DomainHandler::requestDomainSettings() const { + + // setup the URL required to grab settings JSON + QUrl settingsJSONURL; + settingsJSONURL.setScheme("http"); + settingsJSONURL.setHost(_hostname); + settingsJSONURL.setPort(DOMAIN_SERVER_HTTP_PORT); + settingsJSONURL.setPath("/settings.json"); + settingsJSONURL.setQuery(QString("type=%1").arg(NodeList::getInstance()->getOwnerType())); + + qDebug() << settingsJSONURL; + + QNetworkReply* reply = NetworkAccessManager::getInstance().get(QNetworkRequest(settingsJSONURL)); + connect(reply, &QNetworkReply::finished, this, &DomainHandler::settingsRequestFinished); +} + +const int MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS = 5; + +void DomainHandler::settingsRequestFinished() { + QNetworkReply* settingsReply = reinterpret_cast(sender()); + + if (settingsReply->error() == QNetworkReply::NoError) { + // parse the JSON to a QJsonObject and save it + _settingsObject = QJsonDocument::fromJson(settingsReply->readAll()).object(); + + qDebug() << settingsReply->attribute(QNetworkRequest::HttpStatusCodeAttribute); + qDebug() << "Received domain settings."; + + // reset failed settings requests to 0, we got them + _failedSettingsRequests = 0; + } else { + // error grabbing the settings - in some cases this means we are stuck + // so we should retry until we get it + qDebug() << "Error getting domain settings -" << settingsReply->errorString() << "- retrying"; + + if (++_failedSettingsRequests >= MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS) { + qDebug() << "Failed to retreive domain-server settings" << MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS << "times. Re-setting connection to domain."; + clearSettings(); + clearConnectionInfo(); + } + + requestDomainSettings(); + } +} + void DomainHandler::parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket) { // figure out the port that the DS wants us to use for us to talk to them with DTLS int numBytesPacketHeader = numBytesForPacketHeader(dtlsRequirementPacket); diff --git a/libraries/networking/src/DomainHandler.h b/libraries/networking/src/DomainHandler.h index 782332f06a..6761b8eb84 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -12,6 +12,7 @@ #ifndef hifi_DomainHandler_h #define hifi_DomainHandler_h +#include #include #include #include @@ -33,6 +34,7 @@ public: DomainHandler(QObject* parent = 0); void clearConnectionInfo(); + void clearSettings(); const QUuid& getUUID() const { return _uuid; } void setUUID(const QUuid& uuid) { _uuid = uuid; } @@ -54,10 +56,14 @@ public: bool isConnected() const { return _isConnected; } void setIsConnected(bool isConnected); + bool hasSettings() const { return !_settingsObject.isEmpty(); } + void requestDomainSettings() const; + void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket); private slots: void completedHostnameLookup(const QHostInfo& hostInfo); + void settingsRequestFinished(); signals: void hostnameChanged(const QString& hostname); void connectedToDomain(const QString& hostname); @@ -71,6 +77,8 @@ private: QUuid _assignmentUUID; bool _isConnected; QTimer* _handshakeTimer; + QJsonObject _settingsObject; + int _failedSettingsRequests; }; #endif // hifi_DomainHandler_h