From 3292808342c10598c994a6dd3ce9a8b6d87b9789 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 1 Oct 2014 16:19:10 -0700 Subject: [PATCH] update domain network address updating for IP auto networking --- domain-server/src/DomainServer.cpp | 93 ++++++++----------- domain-server/src/DomainServer.h | 4 +- .../src/DomainServerSettingsManager.cpp | 36 ++++++- .../src/DomainServerSettingsManager.h | 1 + 4 files changed, 74 insertions(+), 60 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 155de22af8..130bb101dc 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -75,7 +75,7 @@ DomainServer::DomainServer(int argc, char* argv[]) : loadExistingSessionsFromSettings(); // check if we have the flag that enables dynamic IP - setupDynamicSocketUpdating(); + setupAutomaticNetworking(); } } @@ -305,54 +305,48 @@ bool DomainServer::optionallySetupAssignmentPayment() { return true; } -void DomainServer::setupDynamicSocketUpdating() { +void DomainServer::setupAutomaticNetworking() { const QString METAVERSE_AUTOMATIC_NETWORKING_KEY_PATH = "metaverse.automatic_networking"; - const QVariant* automaticNetworkVariant = valueForKeyPath(_settingsManager.getSettingsMap(), - METAVERSE_AUTOMATIC_NETWORKING_KEY_PATH); - const QString FULL_AUTOMATIC_NETWORKING_VALUE = "full"; const QString IP_ONLY_AUTOMATIC_NETWORKING_VALUE = "ip"; - if (automaticNetworkVariant) { + QString automaticNetworkValue = + _settingsManager.valueOrDefaultValueForKeyPath(METAVERSE_AUTOMATIC_NETWORKING_KEY_PATH).toString(); + + if (automaticNetworkValue == IP_ONLY_AUTOMATIC_NETWORKING_VALUE) { + if (!didSetupAccountManagerWithAccessToken()) { + qDebug() << "Cannot enable domain-server automatic networking without an access token."; + qDebug() << "Please add an access token to your config file or via the web interface."; + + return; + } - QString automaticNetworkValue = automaticNetworkVariant->toString(); - if ((automaticNetworkValue == FULL_AUTOMATIC_NETWORKING_VALUE - || automaticNetworkValue == IP_ONLY_AUTOMATIC_NETWORKING_VALUE)) { + LimitedNodeList* nodeList = LimitedNodeList::getInstance(); + const QUuid& domainID = nodeList->getSessionUUID(); + + if (!domainID.isNull()) { + qDebug() << "domain-server" << automaticNetworkValue << "automatic networking enabled for ID" + << uuidStringWithoutCurlyBraces(domainID) << "via" << _oauthProviderURL.toString(); - if (!didSetupAccountManagerWithAccessToken()) { - qDebug() << "Cannot enable domain-server automatic networking without an access token."; - qDebug() << "Please add an access token to your config file or via the web interface."; - - return; - } + const int STUN_IP_ADDRESS_CHECK_INTERVAL_MSECS = 30 * 1000; - LimitedNodeList* nodeList = LimitedNodeList::getInstance(); - const QUuid& domainID = nodeList->getSessionUUID(); + // setup our timer to check our IP via stun every 30 seconds + QTimer* dynamicIPTimer = new QTimer(this); + connect(dynamicIPTimer, &QTimer::timeout, this, &DomainServer::requestCurrentPublicSocketViaSTUN); + dynamicIPTimer->start(STUN_IP_ADDRESS_CHECK_INTERVAL_MSECS); - if (!domainID.isNull()) { - qDebug() << "domain-server" << automaticNetworkValue << "automatic networking enabled for ID" - << uuidStringWithoutCurlyBraces(domainID) << "via" << _oauthProviderURL.toString(); - - const int STUN_IP_ADDRESS_CHECK_INTERVAL_MSECS = 30 * 1000; - - // setup our timer to check our IP via stun every 30 seconds - QTimer* dynamicIPTimer = new QTimer(this); - connect(dynamicIPTimer, &QTimer::timeout, this, &DomainServer::requestCurrentPublicSocketViaSTUN); - dynamicIPTimer->start(STUN_IP_ADDRESS_CHECK_INTERVAL_MSECS); - - // send public socket changes to the data server so nodes can find us at our new IP - connect(nodeList, &LimitedNodeList::publicSockAddrChanged, this, &DomainServer::sendNewSocketsToDataServer); - - // attempt to update our sockets now - requestCurrentPublicSocketViaSTUN(); - - } else { - qDebug() << "Cannot enable domain-server automatic networking without a domain ID." - << "Please add an ID to your config file or via the web interface."; - - return; - } + // send public socket changes to the data server so nodes can find us at our new IP + connect(nodeList, &LimitedNodeList::publicSockAddrChanged, this, &DomainServer::performIPAddressUpdate); + + // attempt to update our sockets now + requestCurrentPublicSocketViaSTUN(); + + } else { + qDebug() << "Cannot enable domain-server automatic networking without a domain ID." + << "Please add an ID to your config file or via the web interface."; + + return; } } } @@ -934,25 +928,16 @@ QJsonObject jsonForDomainSocketUpdate(const HifiSockAddr& socket) { return socketObject; } -void DomainServer::sendNewSocketsToDataServer(const HifiSockAddr& newPublicSockAddr) { +void DomainServer::performIPAddressUpdate(const HifiSockAddr& newPublicSockAddr) { const QString DOMAIN_UPDATE = "/api/v1/domains/%1"; const QUuid& domainID = LimitedNodeList::getInstance()->getSessionUUID(); - // we're not mointoring for local socket changes yet - // the first time this is called grab whatever it is and assume it'll stay the same - static HifiSockAddr localSockAddr = HifiSockAddr(QHostAddress(getHostOrderLocalAddress()), - LimitedNodeList::getInstance()->getNodeSocket().localPort()); - // setup the domain object to send to the data server - const QString PUBLIC_SOCKET_ATTRIBUTES_KEY = "public_socket_attributes"; - const QString LOCAL_SOCKET_ATTRIBUTES_KEY = "local_socket_attributes"; + const QString PUBLIC_NETWORK_ADDRESS_KEY = "network_address"; + const QString AUTOMATIC_NETWORKING_KEY = "automatic_networking"; - QJsonObject domainObject; - domainObject[PUBLIC_SOCKET_ATTRIBUTES_KEY] = jsonForDomainSocketUpdate(newPublicSockAddr); - domainObject[LOCAL_SOCKET_ATTRIBUTES_KEY] = jsonForDomainSocketUpdate(localSockAddr); - - const QString DOMAIN_JSON_OBJECT = "{\"domain\":%1}"; - QString domainUpdateJSON = DOMAIN_JSON_OBJECT.arg(QString(QJsonDocument(domainObject).toJson())); + const QString DOMAIN_JSON_OBJECT = "{\"domain\": { \"network_address\": \"%1\", \"automatic_networking\": \"ip\" }"; + QString domainUpdateJSON = DOMAIN_JSON_OBJECT.arg(newPublicSockAddr.getAddress().toString()); AccountManager::getInstance().authenticatedRequest(DOMAIN_UPDATE.arg(uuidStringWithoutCurlyBraces(domainID)), QNetworkAccessManager::PutOperation, diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index b09602e2e6..44b215006b 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -63,14 +63,14 @@ private slots: void sendPendingTransactionsToServer(); void requestCurrentPublicSocketViaSTUN(); - void sendNewSocketsToDataServer(const HifiSockAddr& newPublicSockAddr); + void performIPAddressUpdate(const HifiSockAddr& newPublicSockAddr); private: void setupNodeListAndAssignments(const QUuid& sessionUUID = QUuid::createUuid()); bool optionallySetupOAuth(); bool optionallyReadX509KeyAndCertificate(); bool didSetupAccountManagerWithAccessToken(); bool optionallySetupAssignmentPayment(); - void setupDynamicSocketUpdating(); + void setupAutomaticNetworking(); void processDatagram(const QByteArray& receivedPacket, const HifiSockAddr& senderSockAddr); diff --git a/domain-server/src/DomainServerSettingsManager.cpp b/domain-server/src/DomainServerSettingsManager.cpp index b5d7df65cf..8d866a318c 100644 --- a/domain-server/src/DomainServerSettingsManager.cpp +++ b/domain-server/src/DomainServerSettingsManager.cpp @@ -26,6 +26,10 @@ const QString SETTINGS_DESCRIPTION_RELATIVE_PATH = "/resources/describe-settings.json"; +const QString DESCRIPTION_SETTINGS_KEY = "settings"; +const QString SETTING_DEFAULT_KEY = "default"; +const QString DESCRIPTION_NAME_KEY = "name"; + DomainServerSettingsManager::DomainServerSettingsManager() : _descriptionArray(), _configMap() @@ -63,6 +67,34 @@ void DomainServerSettingsManager::setupConfigMap(const QStringList& argumentList } } +QVariant DomainServerSettingsManager::valueOrDefaultValueForKeyPath(const QString &keyPath) { + const QVariant* foundValue = valueForKeyPath(_configMap.getMergedConfig(), keyPath); + + if (foundValue) { + return *foundValue; + } else { + int dotIndex = keyPath.indexOf('.'); + + QString groupKey = keyPath.mid(0, dotIndex); + QString settingKey = keyPath.mid(dotIndex); + + foreach(const QVariant& group, _descriptionArray.toVariantList()) { + QVariantMap groupMap = group.toMap(); + + if (groupMap[DESCRIPTION_NAME_KEY].toString() == groupKey) { + foreach(const QVariant& setting, groupMap[DESCRIPTION_SETTINGS_KEY].toList()) { + QVariantMap settingMap = setting.toMap(); + if (settingMap[DESCRIPTION_NAME_KEY].toString() == settingKey) { + return settingMap[SETTING_DEFAULT_KEY]; + } + } + } + } + } + + return *foundValue; +} + const QString SETTINGS_PATH = "/settings.json"; bool DomainServerSettingsManager::handlePublicHTTPRequest(HTTPConnection* connection, const QUrl &url) { @@ -127,10 +159,6 @@ bool DomainServerSettingsManager::handleAuthenticatedHTTPRequest(HTTPConnection return false; } -const QString DESCRIPTION_SETTINGS_KEY = "settings"; -const QString SETTING_DEFAULT_KEY = "default"; -const QString DESCRIPTION_NAME_KEY = "name"; - QJsonObject DomainServerSettingsManager::responseObjectForType(const QString& typeValue, bool isAuthenticated) { QJsonObject responseObject; diff --git a/domain-server/src/DomainServerSettingsManager.h b/domain-server/src/DomainServerSettingsManager.h index b60cb32dfd..c2e8a7d90d 100644 --- a/domain-server/src/DomainServerSettingsManager.h +++ b/domain-server/src/DomainServerSettingsManager.h @@ -26,6 +26,7 @@ public: bool handleAuthenticatedHTTPRequest(HTTPConnection* connection, const QUrl& url); void setupConfigMap(const QStringList& argumentList); + QVariant valueOrDefaultValueForKeyPath(const QString& keyPath); QVariantMap& getSettingsMap() { return _configMap.getMergedConfig(); } private: