From 6df317e8e604e8196077205285bcc542de5f17e0 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 21 Feb 2014 16:22:17 -0800 Subject: [PATCH] update user's domain in data-server after connection to new one --- interface/src/Application.cpp | 16 +++ interface/src/Application.h | 2 + libraries/shared/src/AccountManager.cpp | 12 +- libraries/shared/src/AccountManager.h | 1 + libraries/shared/src/DataServerClient.cpp | 148 ---------------------- libraries/shared/src/DomainInfo.cpp | 14 +- libraries/shared/src/DomainInfo.h | 5 + libraries/shared/src/NodeList.cpp | 3 + 8 files changed, 50 insertions(+), 151 deletions(-) delete mode 100644 libraries/shared/src/DataServerClient.cpp diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index b378898706..9b725d8be1 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -219,6 +219,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : audioThread->start(); connect(&nodeList->getDomainInfo(), SIGNAL(hostnameChanged(const QString&)), SLOT(domainChanged(const QString&))); + connect(&nodeList->getDomainInfo(), SIGNAL(connectedToDomain(const QString&)), SLOT(connectedToDomain(const QString&))); connect(nodeList, &NodeList::nodeAdded, this, &Application::nodeAdded); connect(nodeList, &NodeList::nodeKilled, this, &Application::nodeKilled); @@ -1484,6 +1485,8 @@ void Application::timer() { // ask the node list to check in with the domain server NodeList::getInstance()->sendDomainServerCheckIn(); + + } static glm::vec3 getFaceVector(BoxFace face) { @@ -3951,6 +3954,19 @@ void Application::domainChanged(const QString& domainHostname) { _particles.clear(); } +void Application::connectedToDomain(const QString& hostname) { + AccountManager& accountManager = AccountManager::getInstance(); + + if (accountManager.isLoggedIn()) { + // update our domain-server with the data-server we're logged in with + + QString domainPutJsonString = "{\"location\":{\"domain\":\"" + hostname + "\"}}"; + + accountManager.authenticatedRequest("/api/v1/users/location", QNetworkAccessManager::PutOperation, + JSONCallbackParameters(), domainPutJsonString.toUtf8()); + } +} + void Application::nodeAdded(SharedNodePointer node) { if (node->getType() == NodeType::AvatarMixer) { // new avatar mixer, send off our identity packet right away diff --git a/interface/src/Application.h b/interface/src/Application.h index 8bbbd51ba7..4a3a7ac0e4 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -253,6 +253,8 @@ public slots: private slots: void timer(); void idle(); + + void connectedToDomain(const QString& hostname); void setFullscreen(bool fullscreen); void setEnable3DTVMode(bool enable3DTVMode); diff --git a/libraries/shared/src/AccountManager.cpp b/libraries/shared/src/AccountManager.cpp index 61f7c07701..3b67cd4de1 100644 --- a/libraries/shared/src/AccountManager.cpp +++ b/libraries/shared/src/AccountManager.cpp @@ -104,8 +104,16 @@ void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager:: networkReply = _networkAccessManager.get(authenticatedRequest); break; case QNetworkAccessManager::PostOperation: - authenticatedRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); - networkReply = _networkAccessManager.post(authenticatedRequest, dataByteArray); + case QNetworkAccessManager::PutOperation: + authenticatedRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + + if (operation == QNetworkAccessManager::PostOperation) { + networkReply = _networkAccessManager.post(authenticatedRequest, dataByteArray); + } else { + networkReply = _networkAccessManager.put(authenticatedRequest, dataByteArray); + } + + break; default: // other methods not yet handled break; diff --git a/libraries/shared/src/AccountManager.h b/libraries/shared/src/AccountManager.h index fe8bf136fe..8ffd533949 100644 --- a/libraries/shared/src/AccountManager.h +++ b/libraries/shared/src/AccountManager.h @@ -44,6 +44,7 @@ public: void setRootURL(const QUrl& rootURL); + bool isLoggedIn() { return !_rootURL.isEmpty() && hasValidAccessToken(); } bool hasValidAccessToken(); bool checkAndSignalForAccessToken(); diff --git a/libraries/shared/src/DataServerClient.cpp b/libraries/shared/src/DataServerClient.cpp deleted file mode 100644 index 8903aa0524..0000000000 --- a/libraries/shared/src/DataServerClient.cpp +++ /dev/null @@ -1,148 +0,0 @@ -// -// DataServerClient.cpp -// hifi -// -// Created by Stephen Birarda on 10/7/13. -// Copyright (c) 2013 HighFidelity, Inc. All rights reserved. -// - -#include -#include - -#include "NodeList.h" -#include "PacketHeaders.h" -#include "UUID.h" - -#include "DataServerClient.h" - -QMap DataServerClient::_unmatchedPackets; -QMap DataServerClient::_callbackObjects; -quint8 DataServerClient::_sequenceNumber = 0; - -const char MULTI_KEY_VALUE_SEPARATOR = '|'; - -const char DATA_SERVER_HOSTNAME[] = "data.highfidelity.io"; -const unsigned short DATA_SERVER_PORT = 3282; - -const HifiSockAddr& DataServerClient::dataServerSockAddr() { - static HifiSockAddr dsSockAddr = HifiSockAddr(DATA_SERVER_HOSTNAME, DATA_SERVER_PORT); - return dsSockAddr; -} - -void DataServerClient::putValueForKeyAndUserString(const QString& key, const QString& value, const QString& userString) { - // setup the header for this packet and push packetStream to desired spot - QByteArray putPacket = byteArrayWithPopulatedHeader(PacketTypeDataServerPut); - QDataStream packetStream(&putPacket, QIODevice::Append); - - // pack our data for the put packet - packetStream << _sequenceNumber << userString << key << value; - - // add the putPacket to our vector of unconfirmed packets, will be deleted once put is confirmed - _unmatchedPackets.insert(_sequenceNumber, putPacket); - - // send this put request to the data server - NodeList::getInstance()->getNodeSocket().writeDatagram(putPacket, dataServerSockAddr().getAddress(), - dataServerSockAddr().getPort()); - - // push the sequence number forwards - _sequenceNumber++; -} - -void DataServerClient::putValueForKeyAndUUID(const QString& key, const QString& value, const QUuid& uuid) { - putValueForKeyAndUserString(key, value, uuidStringWithoutCurlyBraces(uuid)); -} - -void DataServerClient::getValueForKeyAndUUID(const QString& key, const QUuid &uuid, DataServerCallbackObject* callbackObject) { - getValuesForKeysAndUUID(QStringList(key), uuid, callbackObject); -} - -void DataServerClient::getValuesForKeysAndUUID(const QStringList& keys, const QUuid& uuid, - DataServerCallbackObject* callbackObject) { - if (!uuid.isNull()) { - getValuesForKeysAndUserString(keys, uuidStringWithoutCurlyBraces(uuid), callbackObject); - } -} - -void DataServerClient::getValuesForKeysAndUserString(const QStringList& keys, const QString& userString, - DataServerCallbackObject* callbackObject) { - if (!userString.isEmpty() && keys.size() <= UCHAR_MAX) { - QByteArray getPacket = byteArrayWithPopulatedHeader(PacketTypeDataServerGet); - QDataStream packetStream(&getPacket, QIODevice::Append); - - // pack our data for the getPacket - packetStream << _sequenceNumber << userString << keys.join(MULTI_KEY_VALUE_SEPARATOR); - - // add the getPacket to our map of unconfirmed packets, will be deleted once we get a response from the nameserver - _unmatchedPackets.insert(_sequenceNumber, getPacket); - _callbackObjects.insert(_sequenceNumber, callbackObject); - - // send the get to the data server - NodeList::getInstance()->getNodeSocket().writeDatagram(getPacket, dataServerSockAddr().getAddress(), - dataServerSockAddr().getPort()); - _sequenceNumber++; - } -} - -void DataServerClient::getValueForKeyAndUserString(const QString& key, const QString& userString, - DataServerCallbackObject* callbackObject) { - getValuesForKeysAndUserString(QStringList(key), userString, callbackObject); -} - -void DataServerClient::processConfirmFromDataServer(const QByteArray& packet) { - removeMatchedPacketFromMap(packet); -} - -void DataServerClient::processSendFromDataServer(const QByteArray& packet) { - // pull the user string from the packet so we know who to associate this with - QDataStream packetStream(packet); - packetStream.skipRawData(numBytesForPacketHeader(packet)); - - quint8 sequenceNumber = 0; - packetStream >> sequenceNumber; - - if (_callbackObjects.find(sequenceNumber) != _callbackObjects.end()) { - // remove the packet from our two maps, it's matched - DataServerCallbackObject* callbackObject = _callbackObjects.take(sequenceNumber); - _unmatchedPackets.remove(sequenceNumber); - - QString userString, keyListString, valueListString; - - packetStream >> userString >> keyListString >> valueListString; - - callbackObject->processDataServerResponse(userString, keyListString.split(MULTI_KEY_VALUE_SEPARATOR), - valueListString.split(MULTI_KEY_VALUE_SEPARATOR)); - } -} - -void DataServerClient::processMessageFromDataServer(const QByteArray& packet) { - switch (packetTypeForPacket(packet)) { - case PacketTypeDataServerSend: - processSendFromDataServer(packet); - break; - case PacketTypeDataServerConfirm: - processConfirmFromDataServer(packet); - break; - default: - break; - } -} - -void DataServerClient::removeMatchedPacketFromMap(const QByteArray& packet) { - quint8 sequenceNumber = packet[numBytesForPacketHeader(packet)]; - - // attempt to remove a packet with this sequence number from the QMap of unmatched packets - _unmatchedPackets.remove(sequenceNumber); -} - -void DataServerClient::resendUnmatchedPackets() { - if (_unmatchedPackets.size() > 0) { - qDebug() << "Resending" << _unmatchedPackets.size() << "packets to the data server."; - - foreach (const QByteArray& packet, _unmatchedPackets) { - // send the unmatched packet to the data server - NodeList::getInstance()->getNodeSocket().writeDatagram(packet.data(), packet.size(), - dataServerSockAddr().getAddress(), - dataServerSockAddr().getPort()); - } - } -} diff --git a/libraries/shared/src/DomainInfo.cpp b/libraries/shared/src/DomainInfo.cpp index 98d7d2c39b..cbdb0955e5 100644 --- a/libraries/shared/src/DomainInfo.cpp +++ b/libraries/shared/src/DomainInfo.cpp @@ -17,7 +17,8 @@ DomainInfo::DomainInfo() : _connectionSecret(), _registrationToken(), _rootAuthenticationURL(), - _publicKey() + _publicKey(), + _isConnected(false) { } @@ -31,6 +32,7 @@ void DomainInfo::reset() { _registrationToken = QByteArray(); _rootAuthenticationURL = QUrl(); _publicKey = QString(); + _isConnected = false; } void DomainInfo::parseAuthInformationFromJsonObject(const QJsonObject& jsonObject) { @@ -86,3 +88,13 @@ void DomainInfo::completedHostnameLookup(const QHostInfo& hostInfo) { // if we got here then we failed to lookup the address qDebug("Failed domain server lookup"); } + +void DomainInfo::setIsConnected(bool isConnected) { + if (_isConnected != isConnected) { + _isConnected = isConnected; + + if (_isConnected) { + emit connectedToDomain(_hostname); + } + } +} diff --git a/libraries/shared/src/DomainInfo.h b/libraries/shared/src/DomainInfo.h index 4a0e89f8e0..17c17a726f 100644 --- a/libraries/shared/src/DomainInfo.h +++ b/libraries/shared/src/DomainInfo.h @@ -51,10 +51,14 @@ public: const QUrl& getRootAuthenticationURL() const { return _rootAuthenticationURL; } void setRootAuthenticationURL(const QUrl& rootAuthenticationURL) { _rootAuthenticationURL = rootAuthenticationURL; } + bool isConnected() const { return _isConnected; } + void setIsConnected(bool isConnected); + private slots: void completedHostnameLookup(const QHostInfo& hostInfo); signals: void hostnameChanged(const QString& hostname); + void connectedToDomain(const QString& hostname); private: void reset(); @@ -66,6 +70,7 @@ private: QByteArray _registrationToken; QUrl _rootAuthenticationURL; QString _publicKey; + bool _isConnected; }; #endif /* defined(__hifi__DomainInfo__) */ diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index 27c6892baa..319ac54860 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -567,6 +567,9 @@ void NodeList::setSessionUUID(const QUuid& sessionUUID) { int NodeList::processDomainServerList(const QByteArray& packet) { // this is a packet from the domain server, reset the count of un-replied check-ins _numNoReplyDomainCheckIns = 0; + + // if this was the first domain-server list from this domain, we've now connected + _domainInfo.setIsConnected(true); int readNodes = 0;