request domain settings after successful connection to domain

This commit is contained in:
Stephen Birarda 2014-07-28 12:07:19 -07:00
parent 707db4f5d8
commit 6334116070
2 changed files with 67 additions and 1 deletions

View file

@ -9,6 +9,8 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QtCore/QJsonDocument>
#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<QNetworkReply*>(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);

View file

@ -12,6 +12,7 @@
#ifndef hifi_DomainHandler_h
#define hifi_DomainHandler_h
#include <QtCore/QJsonObject>
#include <QtCore/QObject>
#include <QtCore/QTimer>
#include <QtCore/QUuid>
@ -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