From d0070796174b64b43348f13f770487e4658953cb Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 10:30:11 -0700 Subject: [PATCH 01/41] associate username with agent nodes in domain-server --- domain-server/src/DomainServer.cpp | 33 +++++++++++++++++----- domain-server/src/DomainServer.h | 2 +- domain-server/src/DomainServerNodeData.cpp | 1 + domain-server/src/DomainServerNodeData.h | 4 +++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 366a5016f9..b57f3ac383 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -426,11 +426,14 @@ void DomainServer::handleConnectRequest(const QByteArray& packet, const HifiSock } } + + QString connectedUsername; if (!isAssignment && !_oauthProviderURL.isEmpty() && _argumentVariantMap.contains(ALLOWED_ROLES_CONFIG_KEY)) { // this is an Agent, and we require authentication so we can compare the user's roles to our list of allowed ones if (_sessionAuthenticationHash.contains(packetUUID)) { - if (!_sessionAuthenticationHash.value(packetUUID)) { + connectedUsername = _sessionAuthenticationHash.take(packetUUID); + if (connectedUsername.isEmpty()) { // we've decided this is a user that isn't allowed in, return out // TODO: provide information to the user so they know why they can't connect return; @@ -473,6 +476,9 @@ void DomainServer::handleConnectRequest(const QByteArray& packet, const HifiSock // now that we've pulled the wallet UUID and added the node to our list, delete the pending assignee data delete pendingAssigneeData; } + + // if we have a username from an OAuth connect request, set it on the DomainServerNodeData + nodeData->setUsername(connectedUsername); nodeData->setSendingSockAddr(senderSockAddr); @@ -860,6 +866,7 @@ const char JSON_KEY_LOCAL_SOCKET[] = "local"; const char JSON_KEY_POOL[] = "pool"; const char JSON_KEY_PENDING_CREDITS[] = "pending_credits"; const char JSON_KEY_WAKE_TIMESTAMP[] = "wake_timestamp"; +const char JSON_KEY_USERNAME[] = "username"; QJsonObject DomainServer::jsonObjectForNode(const SharedNodePointer& node) { QJsonObject nodeJson; @@ -884,6 +891,10 @@ QJsonObject DomainServer::jsonObjectForNode(const SharedNodePointer& node) { // if the node has pool information, add it DomainServerNodeData* nodeData = reinterpret_cast(node->getLinkedData()); + + // add the node username, if it exists + nodeJson[JSON_KEY_USERNAME] = nodeData->getUsername(); + SharedAssignmentPointer matchingAssignment = _allAssignments.value(nodeData->getAssignmentUUID()); if (matchingAssignment) { nodeJson[JSON_KEY_POOL] = matchingAssignment->getPool(); @@ -1254,22 +1265,30 @@ void DomainServer::handleProfileRequestFinished() { QJsonArray allowedRolesArray = _argumentVariantMap.value(ALLOWED_ROLES_CONFIG_KEY).toJsonValue().toArray(); - bool shouldAllowUserToConnect = false; + QString connectableUsername; + QString profileUsername = profileJSON.object()["data"].toObject()["user"].toObject()["username"].toString(); foreach(const QJsonValue& roleValue, userRolesArray) { if (allowedRolesArray.contains(roleValue)) { // the user has a role that lets them in // set the bool to true and break - shouldAllowUserToConnect = true; + connectableUsername = profileUsername; break; } } - - qDebug() << "Confirmed authentication state for user" << uuidStringWithoutCurlyBraces(matchingSessionUUID) - << "-" << shouldAllowUserToConnect; + + if (connectableUsername.isEmpty()) { + qDebug() << "User" << profileUsername << "with session UUID" + << uuidStringWithoutCurlyBraces(matchingSessionUUID) + << "does not have an allowable role. Refusing connection."; + } else { + qDebug() << "User" << profileUsername << "with session UUID" + << uuidStringWithoutCurlyBraces(matchingSessionUUID) + << "has an allowable role. Can connect."; + } // insert this UUID and a flag that indicates if they are allowed to connect - _sessionAuthenticationHash.insert(matchingSessionUUID, shouldAllowUserToConnect); + _sessionAuthenticationHash.insert(matchingSessionUUID, connectableUsername); } } } diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index cc44bd95a8..e15e277aaf 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -108,7 +108,7 @@ private: QString _oauthClientSecret; QString _hostname; QMap _networkReplyUUIDMap; - QHash _sessionAuthenticationHash; + QHash _sessionAuthenticationHash; DomainServerSettingsManager _settingsManager; }; diff --git a/domain-server/src/DomainServerNodeData.cpp b/domain-server/src/DomainServerNodeData.cpp index 68903cc106..fd95ea9a67 100644 --- a/domain-server/src/DomainServerNodeData.cpp +++ b/domain-server/src/DomainServerNodeData.cpp @@ -21,6 +21,7 @@ DomainServerNodeData::DomainServerNodeData() : _sessionSecretHash(), _assignmentUUID(), _walletUUID(), + _username(), _paymentIntervalTimer(), _statsJSONObject(), _sendingSockAddr(), diff --git a/domain-server/src/DomainServerNodeData.h b/domain-server/src/DomainServerNodeData.h index a7d7233874..366ee8c730 100644 --- a/domain-server/src/DomainServerNodeData.h +++ b/domain-server/src/DomainServerNodeData.h @@ -35,6 +35,9 @@ public: void setWalletUUID(const QUuid& walletUUID) { _walletUUID = walletUUID; } const QUuid& getWalletUUID() const { return _walletUUID; } + void setUsername(const QString& username) { _username = username; } + const QString& getUsername() const { return _username; } + QElapsedTimer& getPaymentIntervalTimer() { return _paymentIntervalTimer; } void setSendingSockAddr(const HifiSockAddr& sendingSockAddr) { _sendingSockAddr = sendingSockAddr; } @@ -50,6 +53,7 @@ private: QHash _sessionSecretHash; QUuid _assignmentUUID; QUuid _walletUUID; + QString _username; QElapsedTimer _paymentIntervalTimer; QJsonObject _statsJSONObject; HifiSockAddr _sendingSockAddr; From 624bb33b1d2294932961958b5aadc67481f73a36 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 10:34:12 -0700 Subject: [PATCH 02/41] display the node's username in underscore template --- domain-server/resources/web/index.shtml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/domain-server/resources/web/index.shtml b/domain-server/resources/web/index.shtml index f0315a113f..7d9f3c80c5 100644 --- a/domain-server/resources/web/index.shtml +++ b/domain-server/resources/web/index.shtml @@ -10,6 +10,7 @@ Type UUID Pool + Username Public Local Uptime (s) @@ -24,6 +25,7 @@ <%- node.type %> <%- node.uuid %> <%- node.pool %> + <%- node.username %> <%- node.public.ip %><%- node.public.port %> <%- node.local.ip %><%- node.local.port %> <%- ((Date.now() - node.wake_timestamp) / 1000).toLocaleString() %> From a77e49d2abc0a7015cacd38f71bb8bdbfa239a86 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 10:47:07 -0700 Subject: [PATCH 03/41] add back missing colon for node port --- domain-server/resources/web/index.shtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/domain-server/resources/web/index.shtml b/domain-server/resources/web/index.shtml index 7d9f3c80c5..2f83172d4a 100644 --- a/domain-server/resources/web/index.shtml +++ b/domain-server/resources/web/index.shtml @@ -26,8 +26,8 @@ <%- node.uuid %> <%- node.pool %> <%- node.username %> - <%- node.public.ip %><%- node.public.port %> - <%- node.local.ip %><%- node.local.port %> + <%- node.public.ip %>:<%- node.public.port %> + <%- node.local.ip %>:<%- node.local.port %> <%- ((Date.now() - node.wake_timestamp) / 1000).toLocaleString() %> <%- (typeof node.pending_credits == 'number' ? node.pending_credits.toLocaleString() : 'N/A') %> From f45a50950891f1cf6eb188a8d7e0f0a4d71d92f5 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 16:54:27 -0700 Subject: [PATCH 04/41] initial hook-in of OAuth authentication for DS web pages --- domain-server/src/DomainServer.cpp | 177 +++++++++++++++--- domain-server/src/DomainServer.h | 7 + .../embedded-webserver/src/HTTPConnection.cpp | 1 + .../embedded-webserver/src/HTTPConnection.h | 1 + 4 files changed, 162 insertions(+), 24 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index b57f3ac383..b0248d3b57 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -42,6 +42,8 @@ DomainServer::DomainServer(int argc, char* argv[]) : _hostname(), _networkReplyUUIDMap(), _sessionAuthenticationHash(), + _webAuthenticationStateSet(), + _cookieProfileJSONHash(), _settingsManager() { setOrganizationName("High Fidelity"); @@ -932,7 +934,13 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url const QString URI_NODES = "/nodes"; const QString UUID_REGEX_STRING = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; - + + if (!isAuthenticatedRequest(connection, url)) { + // this is not an authenticated request + // return true from the handler since it was handled with a 401 or re-direct to auth + return true; + } + if (connection->requestOperation() == QNetworkAccessManager::GetOperation) { if (url.path() == "/assignments.json") { // user is asking for json list of assignments @@ -1176,6 +1184,8 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url return _settingsManager.handleHTTPRequest(connection, url); } +const QString HIFI_SESSION_COOKIE_KEY = "DS_WEB_SESSION_UUID"; + bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &url) { const QString URI_OAUTH = "/oauth"; qDebug() << "HTTPS request received at" << url.toString(); @@ -1189,7 +1199,6 @@ bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &u const QString STATE_QUERY_KEY = "state"; QUuid stateUUID = QUuid(codeURLQuery.queryItemValue(STATE_QUERY_KEY)); - if (!authorizationCode.isEmpty() && !stateUUID.isNull()) { // fire off a request with this code and state to get an access token for the user @@ -1204,15 +1213,45 @@ bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &u QNetworkRequest tokenRequest(tokenRequestUrl); tokenRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); - + QNetworkReply* tokenReply = NetworkAccessManager::getInstance().post(tokenRequest, tokenPostBody.toLocal8Bit()); - - qDebug() << "Requesting a token for user with session UUID" << uuidStringWithoutCurlyBraces(stateUUID); - - // insert this to our pending token replies so we can associate the returned access token with the right UUID - _networkReplyUUIDMap.insert(tokenReply, stateUUID); - - connect(tokenReply, &QNetworkReply::finished, this, &DomainServer::handleTokenRequestFinished); + + if (_webAuthenticationStateSet.remove(stateUUID)) { + // this is a web user who wants to auth to access web interface + // we hold the response back to them until we get their profile information + // and can decide if they are let in or not + + QEventLoop loop; + connect(tokenReply, &QNetworkReply::finished, &loop, &QEventLoop::quit); + + // start the loop for the token request + loop.exec(); + + QNetworkReply* profileReply = profileRequestGivenTokenReply(tokenReply); + + // stop the loop once the profileReply is complete + connect(profileReply, &QNetworkReply::finished, &loop, &QEventLoop::quit); + + // restart the loop for the profile request + loop.exec(); + + // call helper method to get cookieHeaders + Headers cookieHeaders = setupCookieHeadersFromProfileReply(profileReply); + + connection->respond(HTTPConnection::StatusCode302, QByteArray(), + HTTPConnection::DefaultContentType, cookieHeaders); + + // we've redirected the user back to our homepage + return true; + + } else { + qDebug() << "Requesting a token for user with session UUID" << uuidStringWithoutCurlyBraces(stateUUID); + + // insert this to our pending token replies so we can associate the returned access token with the right UUID + _networkReplyUUIDMap.insert(tokenReply, stateUUID); + + connect(tokenReply, &QNetworkReply::finished, this, &DomainServer::handleTokenRequestFinished); + } } // respond with a 200 code indicating that login is complete @@ -1224,6 +1263,65 @@ bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &u } } +bool DomainServer::isAuthenticatedRequest(HTTPConnection* connection, const QUrl& url) { + + const QByteArray HTTP_COOKIE_HEADER_KEY = "Cookie"; + const QString ADMIN_USERS_CONFIG_KEY = "admin-users"; + const QString ADMIN_ROLES_CONFIG_KEY = "admin-roles"; + + if (!_oauthProviderURL.isEmpty() + && (_argumentVariantMap.contains(ADMIN_USERS_CONFIG_KEY) || _argumentVariantMap.contains(ADMIN_ROLES_CONFIG_KEY))) { + QString cookieString = connection->requestHeaders().value(HTTP_COOKIE_HEADER_KEY); + + const QString COOKIE_UUID_REGEX_STRING = HIFI_SESSION_COOKIE_KEY + "=([\\d\\w-]+)($|;)"; + QRegExp cookieUUIDRegex(COOKIE_UUID_REGEX_STRING); + + QUuid cookieUUID; + if (cookieString.indexOf(cookieUUIDRegex) != -1) { + cookieUUID = cookieUUIDRegex.cap(1); + } + + if (!cookieUUID.isNull() && _cookieProfileJSONHash.contains(cookieUUID)) { + // pull the QJSONObject for the user with this cookie UUID + QJsonObject profileObject = _cookieProfileJSONHash.value(cookieUUID); + QString profileUsername = profileObject.value("username").toString(); + + if (_argumentVariantMap.value(ADMIN_USERS_CONFIG_KEY).toJsonValue().toArray().contains(profileUsername)) { + // this is an authenticated user + return true; + } else { + QString unauthenticatedRequest = "You do not have permission to access this domain-server."; + connection->respond(HTTPConnection::StatusCode401, unauthenticatedRequest.toUtf8()); + + // the user does not have allowed username or role, return 401 + return false; + } + } else { + // re-direct this user to OAuth page + + // generate a random state UUID to use + QUuid stateUUID = QUuid::createUuid(); + + // add it to the set so we can handle the callback from the OAuth provider + _webAuthenticationStateSet.insert(stateUUID); + + QUrl oauthRedirectURL = oauthAuthorizationURL(stateUUID); + + Headers redirectHeaders; + redirectHeaders.insert("Location", oauthRedirectURL.toEncoded()); + + connection->respond(HTTPConnection::StatusCode302, + QByteArray(), HTTPConnection::DefaultContentType, redirectHeaders); + + // we don't know about this user yet, so they are not yet authenticated + return false; + } + } else { + // we don't have an OAuth URL + admin roles/usernames, so all users are authenticated + return true; + } +} + const QString OAUTH_JSON_ACCESS_TOKEN_KEY = "access_token"; void DomainServer::handleTokenRequestFinished() { @@ -1231,20 +1329,11 @@ void DomainServer::handleTokenRequestFinished() { QUuid matchingSessionUUID = _networkReplyUUIDMap.take(networkReply); if (!matchingSessionUUID.isNull() && networkReply->error() == QNetworkReply::NoError) { - // pull the access token from the returned JSON and store it with the matching session UUID - QJsonDocument returnedJSON = QJsonDocument::fromJson(networkReply->readAll()); - QString accessToken = returnedJSON.object()[OAUTH_JSON_ACCESS_TOKEN_KEY].toString(); - - qDebug() << "Received access token for user with UUID" << uuidStringWithoutCurlyBraces(matchingSessionUUID); - - // fire off a request to get this user's identity so we can see if we will let them in - QUrl profileURL = _oauthProviderURL; - profileURL.setPath("/api/v1/users/profile"); - profileURL.setQuery(QString("%1=%2").arg(OAUTH_JSON_ACCESS_TOKEN_KEY, accessToken)); - - QNetworkReply* profileReply = NetworkAccessManager::getInstance().get(QNetworkRequest(profileURL)); - - qDebug() << "Requesting access token for user with session UUID" << uuidStringWithoutCurlyBraces(matchingSessionUUID); + + qDebug() << "Received access token for user with UUID" << uuidStringWithoutCurlyBraces(matchingSessionUUID) + << "-" << "requesting profile."; + + QNetworkReply* profileReply = profileRequestGivenTokenReply(networkReply); connect(profileReply, &QNetworkReply::finished, this, &DomainServer::handleProfileRequestFinished); @@ -1252,6 +1341,19 @@ void DomainServer::handleTokenRequestFinished() { } } +QNetworkReply* DomainServer::profileRequestGivenTokenReply(QNetworkReply* tokenReply) { + // pull the access token from the returned JSON and store it with the matching session UUID + QJsonDocument returnedJSON = QJsonDocument::fromJson(tokenReply->readAll()); + QString accessToken = returnedJSON.object()[OAUTH_JSON_ACCESS_TOKEN_KEY].toString(); + + // fire off a request to get this user's identity so we can see if we will let them in + QUrl profileURL = _oauthProviderURL; + profileURL.setPath("/api/v1/users/profile"); + profileURL.setQuery(QString("%1=%2").arg(OAUTH_JSON_ACCESS_TOKEN_KEY, accessToken)); + + return NetworkAccessManager::getInstance().get(QNetworkRequest(profileURL)); +} + void DomainServer::handleProfileRequestFinished() { QNetworkReply* networkReply = reinterpret_cast(sender()); QUuid matchingSessionUUID = _networkReplyUUIDMap.take(networkReply); @@ -1293,6 +1395,33 @@ void DomainServer::handleProfileRequestFinished() { } } +Headers DomainServer::setupCookieHeadersFromProfileReply(QNetworkReply* profileReply) { + Headers cookieHeaders; + + // create a UUID for this cookie + QUuid cookieUUID = QUuid::createUuid(); + + QJsonDocument profileDocument = QJsonDocument::fromJson(profileReply->readAll()); + + // add the profile to our in-memory data structure so we know who the user is when they send us their cookie + _cookieProfileJSONHash.insert(cookieUUID, profileDocument.object()["data"].toObject()["user"].toObject()); + + // setup expiry for cookie to 1 month from today + QDateTime cookieExpiry = QDateTime::currentDateTimeUtc().addMonths(1); + + QString cookieString = HIFI_SESSION_COOKIE_KEY + "=" + uuidStringWithoutCurlyBraces(cookieUUID.toString()); + cookieString += "; expires=" + cookieExpiry.toString("ddd, dd MMM yyyy HH:mm:ss") + " GMT"; + cookieString += "; domain=" + _hostname + "; path=/"; + + cookieHeaders.insert("Set-Cookie", cookieString.toUtf8()); + + // redirect the user back to the homepage so they can present their cookie and be authenticated + QString redirectString = "http://" + _hostname + ":" + QString::number(_httpManager.serverPort()); + cookieHeaders.insert("Location", redirectString.toUtf8()); + + return cookieHeaders; +} + void DomainServer::refreshStaticAssignmentAndAddToQueue(SharedAssignmentPointer& assignment) { QUuid oldUUID = assignment->getUUID(); assignment->resetUUID(); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index e15e277aaf..98e5b96f25 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -85,8 +85,12 @@ private: QUrl oauthRedirectURL(); QUrl oauthAuthorizationURL(const QUuid& stateUUID = QUuid::createUuid()); + bool isAuthenticatedRequest(HTTPConnection* connection, const QUrl& url); + void handleTokenRequestFinished(); + QNetworkReply* profileRequestGivenTokenReply(QNetworkReply* tokenReply); void handleProfileRequestFinished(); + Headers setupCookieHeadersFromProfileReply(QNetworkReply* profileReply); QJsonObject jsonForSocket(const HifiSockAddr& socket); QJsonObject jsonObjectForNode(const SharedNodePointer& node); @@ -110,6 +114,9 @@ private: QMap _networkReplyUUIDMap; QHash _sessionAuthenticationHash; + QSet _webAuthenticationStateSet; + QHash _cookieProfileJSONHash; + DomainServerSettingsManager _settingsManager; }; diff --git a/libraries/embedded-webserver/src/HTTPConnection.cpp b/libraries/embedded-webserver/src/HTTPConnection.cpp index beb107c4cf..7112a90825 100755 --- a/libraries/embedded-webserver/src/HTTPConnection.cpp +++ b/libraries/embedded-webserver/src/HTTPConnection.cpp @@ -21,6 +21,7 @@ const char* HTTPConnection::StatusCode200 = "200 OK"; const char* HTTPConnection::StatusCode301 = "301 Moved Permanently"; const char* HTTPConnection::StatusCode302 = "302 Found"; const char* HTTPConnection::StatusCode400 = "400 Bad Request"; +const char* HTTPConnection::StatusCode401 = "401 Unauthorized"; const char* HTTPConnection::StatusCode404 = "404 Not Found"; const char* HTTPConnection::DefaultContentType = "text/plain; charset=ISO-8859-1"; diff --git a/libraries/embedded-webserver/src/HTTPConnection.h b/libraries/embedded-webserver/src/HTTPConnection.h index e2352ed250..c981537c15 100644 --- a/libraries/embedded-webserver/src/HTTPConnection.h +++ b/libraries/embedded-webserver/src/HTTPConnection.h @@ -46,6 +46,7 @@ public: static const char* StatusCode301; static const char* StatusCode302; static const char* StatusCode400; + static const char* StatusCode401; static const char* StatusCode404; static const char* DefaultContentType; From 1903fff45c29f5200d37f5112896a084667fa7ea Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 17:03:09 -0700 Subject: [PATCH 05/41] hide remote socket closed error message in HTTPConnection --- libraries/embedded-webserver/src/HTTPConnection.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/embedded-webserver/src/HTTPConnection.cpp b/libraries/embedded-webserver/src/HTTPConnection.cpp index 7112a90825..82d3d7eba6 100755 --- a/libraries/embedded-webserver/src/HTTPConnection.cpp +++ b/libraries/embedded-webserver/src/HTTPConnection.cpp @@ -43,7 +43,8 @@ HTTPConnection::HTTPConnection (QTcpSocket* socket, HTTPManager* parentManager) HTTPConnection::~HTTPConnection() { // log the destruction - if (_socket->error() != QAbstractSocket::UnknownSocketError) { + if (_socket->error() != QAbstractSocket::UnknownSocketError + && _socket->error() != QAbstractSocket::RemoteHostClosedError) { qDebug() << _socket->errorString() << "-" << _socket->error(); } } From e75ed2c4fa8982468da9fd0d05897f40c5279013 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 17:23:57 -0700 Subject: [PATCH 06/41] add a class to hold web session data --- .../src/DomainServerWebSessionData.cpp | 26 ++++++++++++++++ .../src/DomainServerWebSessionData.h | 31 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 domain-server/src/DomainServerWebSessionData.cpp create mode 100644 domain-server/src/DomainServerWebSessionData.h diff --git a/domain-server/src/DomainServerWebSessionData.cpp b/domain-server/src/DomainServerWebSessionData.cpp new file mode 100644 index 0000000000..e59f255822 --- /dev/null +++ b/domain-server/src/DomainServerWebSessionData.cpp @@ -0,0 +1,26 @@ +// +// DomainServerWebSessionData.cpp +// domain-server/src +// +// Created by Stephen Birarda on 2014-07-21. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include + +#include "DomainServerWebSessionData.h" + +DomainServerWebSessionData::DomainServerWebSessionData(const QJsonDocument& profileDocument) : + _roles() +{ + _username = profileDocument.object()["user"].toObject()["username"].toString(); + + // pull each of the roles and throw them into our set + foreach(const QJsonValue& rolesValue, profileDocument.object()["user"].toObject()["roles"].toObject()) { + _roles.insert(rolesValue.toString()); + } +} \ No newline at end of file diff --git a/domain-server/src/DomainServerWebSessionData.h b/domain-server/src/DomainServerWebSessionData.h new file mode 100644 index 0000000000..80088c9362 --- /dev/null +++ b/domain-server/src/DomainServerWebSessionData.h @@ -0,0 +1,31 @@ +// +// DomainServerWebSessionData.h +// domain-server/src +// +// Created by Stephen Birarda on 2014-07-21. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_DomainServerWebSessionData_h +#define hifi_DomainServerWebSessionData_h + +#include +#include + +class DomainServerWebSessionData : public QObject { + Q_OBJECT +public: + DomainServerWebSessionData(const QJsonDocument& profileDocument); + + const QString& getUsername() const { return _username; } + const QSet& getRoles() const { return _roles; } + +private: + QString _username; + QSet _roles; +}; + +#endif // hifi_DomainServerWebSessionData_h \ No newline at end of file From f78a1f703374dcde09826d78b6a800d06d48fdd4 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 17:32:38 -0700 Subject: [PATCH 07/41] hook domain-server to user DomainServerWebSessionData class --- domain-server/src/DomainServer.cpp | 10 +++---- domain-server/src/DomainServer.h | 3 +- .../src/DomainServerWebSessionData.cpp | 28 ++++++++++++++++++- .../src/DomainServerWebSessionData.h | 5 ++++ 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index b0248d3b57..3c0a088869 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -43,7 +43,7 @@ DomainServer::DomainServer(int argc, char* argv[]) : _networkReplyUUIDMap(), _sessionAuthenticationHash(), _webAuthenticationStateSet(), - _cookieProfileJSONHash(), + _cookieSessionHash(), _settingsManager() { setOrganizationName("High Fidelity"); @@ -1281,10 +1281,10 @@ bool DomainServer::isAuthenticatedRequest(HTTPConnection* connection, const QUrl cookieUUID = cookieUUIDRegex.cap(1); } - if (!cookieUUID.isNull() && _cookieProfileJSONHash.contains(cookieUUID)) { + if (!cookieUUID.isNull() && _cookieSessionHash.contains(cookieUUID)) { // pull the QJSONObject for the user with this cookie UUID - QJsonObject profileObject = _cookieProfileJSONHash.value(cookieUUID); - QString profileUsername = profileObject.value("username").toString(); + DomainServerWebSessionData sessionData = _cookieSessionHash.value(cookieUUID); + QString profileUsername = sessionData.getUsername(); if (_argumentVariantMap.value(ADMIN_USERS_CONFIG_KEY).toJsonValue().toArray().contains(profileUsername)) { // this is an authenticated user @@ -1404,7 +1404,7 @@ Headers DomainServer::setupCookieHeadersFromProfileReply(QNetworkReply* profileR QJsonDocument profileDocument = QJsonDocument::fromJson(profileReply->readAll()); // add the profile to our in-memory data structure so we know who the user is when they send us their cookie - _cookieProfileJSONHash.insert(cookieUUID, profileDocument.object()["data"].toObject()["user"].toObject()); + _cookieSessionHash.insert(cookieUUID, DomainServerWebSessionData(profileDocument)); // setup expiry for cookie to 1 month from today QDateTime cookieExpiry = QDateTime::currentDateTimeUtc().addMonths(1); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index 98e5b96f25..f8daa9a529 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -25,6 +25,7 @@ #include #include "DomainServerSettingsManager.h" +#include "DomainServerWebSessionData.h" #include "WalletTransaction.h" #include "PendingAssignedNodeData.h" @@ -115,7 +116,7 @@ private: QHash _sessionAuthenticationHash; QSet _webAuthenticationStateSet; - QHash _cookieProfileJSONHash; + QHash _cookieSessionHash; DomainServerSettingsManager _settingsManager; }; diff --git a/domain-server/src/DomainServerWebSessionData.cpp b/domain-server/src/DomainServerWebSessionData.cpp index e59f255822..de73ca77dd 100644 --- a/domain-server/src/DomainServerWebSessionData.cpp +++ b/domain-server/src/DomainServerWebSessionData.cpp @@ -14,6 +14,13 @@ #include "DomainServerWebSessionData.h" +DomainServerWebSessionData::DomainServerWebSessionData() : + _username(), + _roles() +{ + +} + DomainServerWebSessionData::DomainServerWebSessionData(const QJsonDocument& profileDocument) : _roles() { @@ -23,4 +30,23 @@ DomainServerWebSessionData::DomainServerWebSessionData(const QJsonDocument& prof foreach(const QJsonValue& rolesValue, profileDocument.object()["user"].toObject()["roles"].toObject()) { _roles.insert(rolesValue.toString()); } -} \ No newline at end of file +} + +DomainServerWebSessionData::DomainServerWebSessionData(const DomainServerWebSessionData& otherSessionData) { + _username = otherSessionData._username; + _roles = otherSessionData._roles; +} + +DomainServerWebSessionData& DomainServerWebSessionData::operator=(const DomainServerWebSessionData& otherSessionData) { + DomainServerWebSessionData temp(otherSessionData); + swap(temp); + return *this; +} + +void DomainServerWebSessionData::swap(DomainServerWebSessionData& otherSessionData) { + using std::swap; + + swap(_username, otherSessionData._username); + swap(_roles, otherSessionData._roles); +} + diff --git a/domain-server/src/DomainServerWebSessionData.h b/domain-server/src/DomainServerWebSessionData.h index 80088c9362..cd2410cf66 100644 --- a/domain-server/src/DomainServerWebSessionData.h +++ b/domain-server/src/DomainServerWebSessionData.h @@ -18,12 +18,17 @@ class DomainServerWebSessionData : public QObject { Q_OBJECT public: + DomainServerWebSessionData(); DomainServerWebSessionData(const QJsonDocument& profileDocument); + DomainServerWebSessionData(const DomainServerWebSessionData& otherSessionData); + DomainServerWebSessionData& operator=(const DomainServerWebSessionData& otherSessionData); const QString& getUsername() const { return _username; } const QSet& getRoles() const { return _roles; } private: + void swap(DomainServerWebSessionData& otherSessionData); + QString _username; QSet _roles; }; From 8082e2f88b08c263cb4603099562b4249df7c7ae Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Jul 2014 17:41:22 -0700 Subject: [PATCH 08/41] allow a user to be let into domain-server based on role --- domain-server/src/DomainServer.cpp | 27 ++++++++++++++----- .../src/DomainServerWebSessionData.cpp | 9 ++++--- .../src/DomainServerWebSessionData.h | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 3c0a088869..52da966d46 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -1289,13 +1289,25 @@ bool DomainServer::isAuthenticatedRequest(HTTPConnection* connection, const QUrl if (_argumentVariantMap.value(ADMIN_USERS_CONFIG_KEY).toJsonValue().toArray().contains(profileUsername)) { // this is an authenticated user return true; - } else { - QString unauthenticatedRequest = "You do not have permission to access this domain-server."; - connection->respond(HTTPConnection::StatusCode401, unauthenticatedRequest.toUtf8()); - - // the user does not have allowed username or role, return 401 - return false; } + + // loop the roles of this user and see if they are in the admin-roles array + QJsonArray adminRolesArray = _argumentVariantMap.value(ADMIN_ROLES_CONFIG_KEY).toJsonValue().toArray(); + + if (!adminRolesArray.isEmpty()) { + foreach(const QString& userRole, sessionData.getRoles()) { + if (adminRolesArray.contains(userRole)) { + // this user has a role that allows them to administer the domain-server + return true; + } + } + } + + QString unauthenticatedRequest = "You do not have permission to access this domain-server."; + connection->respond(HTTPConnection::StatusCode401, unauthenticatedRequest.toUtf8()); + + // the user does not have allowed username or role, return 401 + return false; } else { // re-direct this user to OAuth page @@ -1402,9 +1414,10 @@ Headers DomainServer::setupCookieHeadersFromProfileReply(QNetworkReply* profileR QUuid cookieUUID = QUuid::createUuid(); QJsonDocument profileDocument = QJsonDocument::fromJson(profileReply->readAll()); + QJsonObject userObject = profileDocument.object()["data"].toObject()["user"].toObject(); // add the profile to our in-memory data structure so we know who the user is when they send us their cookie - _cookieSessionHash.insert(cookieUUID, DomainServerWebSessionData(profileDocument)); + _cookieSessionHash.insert(cookieUUID, DomainServerWebSessionData(userObject)); // setup expiry for cookie to 1 month from today QDateTime cookieExpiry = QDateTime::currentDateTimeUtc().addMonths(1); diff --git a/domain-server/src/DomainServerWebSessionData.cpp b/domain-server/src/DomainServerWebSessionData.cpp index de73ca77dd..b0c56cc59e 100644 --- a/domain-server/src/DomainServerWebSessionData.cpp +++ b/domain-server/src/DomainServerWebSessionData.cpp @@ -9,7 +9,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include +#include +#include #include #include "DomainServerWebSessionData.h" @@ -21,13 +22,13 @@ DomainServerWebSessionData::DomainServerWebSessionData() : } -DomainServerWebSessionData::DomainServerWebSessionData(const QJsonDocument& profileDocument) : +DomainServerWebSessionData::DomainServerWebSessionData(const QJsonObject& userObject) : _roles() { - _username = profileDocument.object()["user"].toObject()["username"].toString(); + _username = userObject["username"].toString(); // pull each of the roles and throw them into our set - foreach(const QJsonValue& rolesValue, profileDocument.object()["user"].toObject()["roles"].toObject()) { + foreach(const QJsonValue& rolesValue, userObject["roles"].toArray()) { _roles.insert(rolesValue.toString()); } } diff --git a/domain-server/src/DomainServerWebSessionData.h b/domain-server/src/DomainServerWebSessionData.h index cd2410cf66..15e4171b57 100644 --- a/domain-server/src/DomainServerWebSessionData.h +++ b/domain-server/src/DomainServerWebSessionData.h @@ -19,7 +19,7 @@ class DomainServerWebSessionData : public QObject { Q_OBJECT public: DomainServerWebSessionData(); - DomainServerWebSessionData(const QJsonDocument& profileDocument); + DomainServerWebSessionData(const QJsonObject& userObject); DomainServerWebSessionData(const DomainServerWebSessionData& otherSessionData); DomainServerWebSessionData& operator=(const DomainServerWebSessionData& otherSessionData); From 1a13fbb437b5d79fa66793d7170d8defc399b555 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 23 Jul 2014 10:54:55 -0700 Subject: [PATCH 09/41] add a setting section for voxel costs --- .../resources/web/settings/describe.json | 20 +++++++++++++++++++ .../resources/web/settings/index.shtml | 18 +++++++++++------ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/domain-server/resources/web/settings/describe.json b/domain-server/resources/web/settings/describe.json index 227b6bf0cd..97280bfd32 100644 --- a/domain-server/resources/web/settings/describe.json +++ b/domain-server/resources/web/settings/describe.json @@ -16,5 +16,25 @@ "default": false } } + }, + "voxels": { + "label": "Voxels", + "assignment-types": [3], + "settings": { + "per-voxel-cost": { + "label": "Per Voxel Cost", + "help": "Credit cost to change each voxel", + "placeholder": "0.00001", + "default": "0.00001", + "input_addon": "₵" + }, + "volume-cost": { + "label": "Volume Cost", + "help": "Credit cost to change each cubed meter of voxel space", + "placeholder": "0.0005", + "default": "0.0005", + "input_addon": "₵" + } + } } } \ No newline at end of file diff --git a/domain-server/resources/web/settings/index.shtml b/domain-server/resources/web/settings/index.shtml index 3bb669b32e..73e3cdbea2 100644 --- a/domain-server/resources/web/settings/index.shtml +++ b/domain-server/resources/web/settings/index.shtml @@ -16,16 +16,22 @@ <% var setting_id = group_key + "." + setting_key %>
- <% if(setting.type) %> - <% if (setting.type === "checkbox") { %> - <% var checked_box = (values[group_key] || {})[setting_key] || setting.default %> - > - <% } else { %> + <% if (setting.type === "checkbox") { %> + <% var checked_box = (values[group_key] || {})[setting_key] || setting.default %> + > + <% } else { %> + <% if (setting.input_addon) { %> +
+
<%- setting.input_addon %>
+ <% } %> + + <% if (setting.input_addon) { %> +
+ <% } %> <% } %> -

<%- setting.help %>

From ebf5379275a5376d23839a722b96bded3e267836 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 23 Jul 2014 11:13:12 -0700 Subject: [PATCH 10/41] default voxel costs to free, add a wallet ID for voxel change payments --- domain-server/resources/web/settings/describe.json | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/domain-server/resources/web/settings/describe.json b/domain-server/resources/web/settings/describe.json index 97280bfd32..0ab647734b 100644 --- a/domain-server/resources/web/settings/describe.json +++ b/domain-server/resources/web/settings/describe.json @@ -21,18 +21,24 @@ "label": "Voxels", "assignment-types": [3], "settings": { + "voxel-wallet": { + "label": "Destination Wallet ID", + "help": "Wallet to be paid for voxel changes", + "placeholder": "00000000-0000-0000-0000-000000000000", + "default": "" + }, "per-voxel-cost": { "label": "Per Voxel Cost", "help": "Credit cost to change each voxel", - "placeholder": "0.00001", - "default": "0.00001", + "placeholder": "0.0", + "default": "0.0", "input_addon": "₵" }, "volume-cost": { "label": "Volume Cost", "help": "Credit cost to change each cubed meter of voxel space", - "placeholder": "0.0005", - "default": "0.0005", + "placeholder": "0.0", + "default": "0.0", "input_addon": "₵" } } From 74d17a094f0a190c3aa080143e02c4f5ad6b0434 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 23 Jul 2014 11:47:56 -0700 Subject: [PATCH 11/41] tweaks to string default handling in DomainServerSettingsManager --- .../src/DomainServerSettingsManager.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/domain-server/src/DomainServerSettingsManager.cpp b/domain-server/src/DomainServerSettingsManager.cpp index f9996aa0e7..0520426ea8 100644 --- a/domain-server/src/DomainServerSettingsManager.cpp +++ b/domain-server/src/DomainServerSettingsManager.cpp @@ -145,7 +145,12 @@ void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJ // we don't continue if this key is not present in our descriptionObject if (descriptionObject.contains(key)) { if (rootValue.isString()) { - settingsVariant[key] = rootValue.toString(); + if (rootValue.toString().isEmpty()) { + // this is an empty value, clear it in settings variant so the default is sent + settingsVariant.remove(key); + } else { + settingsVariant[key] = rootValue.toString(); + } } else if (rootValue.isBool()) { settingsVariant[key] = rootValue.toBool(); } else if (rootValue.isObject()) { @@ -158,9 +163,16 @@ void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJ settingsVariant[key] = QVariantMap(); } + QVariantMap& thisMap = *reinterpret_cast(settingsVariant[key].data()); + recurseJSONObjectAndOverwriteSettings(rootValue.toObject(), - *reinterpret_cast(settingsVariant[key].data()), + thisMap, nextDescriptionObject[DESCRIPTION_SETTINGS_KEY].toObject()); + + if (thisMap.isEmpty()) { + // we've cleared all of the settings below this value, so remove this one too + settingsVariant.remove(key); + } } } } From 2e5dc2320d65d16df83afbca64717f7d34d5e767 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 24 Jul 2014 11:09:09 -0700 Subject: [PATCH 12/41] don't flip order on node socket, HifiSockAddr is handling --- domain-server/src/DomainServer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 52da966d46..4261b5033b 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -856,7 +856,7 @@ QJsonObject DomainServer::jsonForSocket(const HifiSockAddr& socket) { QJsonObject socketJSON; socketJSON["ip"] = socket.getAddress().toString(); - socketJSON["port"] = ntohs(socket.getPort()); + socketJSON["port"] = socket.getPort(); return socketJSON; } From 005a3c7c1239b156f7df2cd8e6d8efd107717f64 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 24 Jul 2014 17:17:57 -0700 Subject: [PATCH 13/41] persist and recall domain-server web sessions from ini settings --- domain-server/src/DomainServer.cpp | 28 ++++++++++++++++++- domain-server/src/DomainServer.h | 2 ++ .../src/DomainServerWebSessionData.cpp | 10 +++++++ .../src/DomainServerWebSessionData.h | 5 ++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 4261b5033b..e462e05aa6 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -50,6 +50,9 @@ DomainServer::DomainServer(int argc, char* argv[]) : setOrganizationDomain("highfidelity.io"); setApplicationName("domain-server"); QSettings::setDefaultFormat(QSettings::IniFormat); + + qRegisterMetaType("DomainServerWebSessionData"); + qRegisterMetaTypeStreamOperators("DomainServerWebSessionData"); _argumentVariantMap = HifiConfigVariantMap::mergeCLParametersWithJSONConfig(arguments()); @@ -59,6 +62,8 @@ DomainServer::DomainServer(int argc, char* argv[]) : qDebug() << "Setting up LimitedNodeList and assignments."; setupNodeListAndAssignments(); + + loadExistingSessionsFromSettings(); } } @@ -1407,6 +1412,9 @@ void DomainServer::handleProfileRequestFinished() { } } + +const QString DS_SETTINGS_SESSIONS_GROUP = "web-sessions"; + Headers DomainServer::setupCookieHeadersFromProfileReply(QNetworkReply* profileReply) { Headers cookieHeaders; @@ -1417,7 +1425,14 @@ Headers DomainServer::setupCookieHeadersFromProfileReply(QNetworkReply* profileR QJsonObject userObject = profileDocument.object()["data"].toObject()["user"].toObject(); // add the profile to our in-memory data structure so we know who the user is when they send us their cookie - _cookieSessionHash.insert(cookieUUID, DomainServerWebSessionData(userObject)); + DomainServerWebSessionData sessionData(userObject); + _cookieSessionHash.insert(cookieUUID, sessionData); + + // persist the cookie to settings file so we can get it back on DS relaunch + QSettings localSettings; + localSettings.beginGroup(DS_SETTINGS_SESSIONS_GROUP); + QVariant sessionVariant = QVariant::fromValue(sessionData); + localSettings.setValue(cookieUUID.toString(), QVariant::fromValue(sessionData)); // setup expiry for cookie to 1 month from today QDateTime cookieExpiry = QDateTime::currentDateTimeUtc().addMonths(1); @@ -1435,6 +1450,17 @@ Headers DomainServer::setupCookieHeadersFromProfileReply(QNetworkReply* profileR return cookieHeaders; } +void DomainServer::loadExistingSessionsFromSettings() { + // read data for existing web sessions into memory so existing sessions can be leveraged + QSettings domainServerSettings; + domainServerSettings.beginGroup(DS_SETTINGS_SESSIONS_GROUP); + + foreach(const QString& uuidKey, domainServerSettings.childKeys()) { + _cookieSessionHash.insert(QUuid(uuidKey), domainServerSettings.value(uuidKey).value()); + qDebug() << "Pulled web session from settings - cookie UUID is" << uuidKey; + } +} + void DomainServer::refreshStaticAssignmentAndAddToQueue(SharedAssignmentPointer& assignment) { QUuid oldUUID = assignment->getUUID(); assignment->resetUUID(); diff --git a/domain-server/src/DomainServer.h b/domain-server/src/DomainServer.h index f8daa9a529..666994c818 100644 --- a/domain-server/src/DomainServer.h +++ b/domain-server/src/DomainServer.h @@ -93,6 +93,8 @@ private: void handleProfileRequestFinished(); Headers setupCookieHeadersFromProfileReply(QNetworkReply* profileReply); + void loadExistingSessionsFromSettings(); + QJsonObject jsonForSocket(const HifiSockAddr& socket); QJsonObject jsonObjectForNode(const SharedNodePointer& node); diff --git a/domain-server/src/DomainServerWebSessionData.cpp b/domain-server/src/DomainServerWebSessionData.cpp index b0c56cc59e..ee32a17570 100644 --- a/domain-server/src/DomainServerWebSessionData.cpp +++ b/domain-server/src/DomainServerWebSessionData.cpp @@ -51,3 +51,13 @@ void DomainServerWebSessionData::swap(DomainServerWebSessionData& otherSessionDa swap(_roles, otherSessionData._roles); } +QDataStream& operator<<(QDataStream &out, const DomainServerWebSessionData& session) { + out << session._username << session._roles; + return out; +} + +QDataStream& operator>>(QDataStream &in, DomainServerWebSessionData& session) { + in >> session._username >> session._roles; + return in; +} + diff --git a/domain-server/src/DomainServerWebSessionData.h b/domain-server/src/DomainServerWebSessionData.h index 15e4171b57..450bc13a9b 100644 --- a/domain-server/src/DomainServerWebSessionData.h +++ b/domain-server/src/DomainServerWebSessionData.h @@ -26,6 +26,9 @@ public: const QString& getUsername() const { return _username; } const QSet& getRoles() const { return _roles; } + friend QDataStream& operator<<(QDataStream &out, const DomainServerWebSessionData& session); + friend QDataStream& operator>>(QDataStream &in, DomainServerWebSessionData& session); + private: void swap(DomainServerWebSessionData& otherSessionData); @@ -33,4 +36,6 @@ private: QSet _roles; }; +Q_DECLARE_METATYPE(DomainServerWebSessionData) + #endif // hifi_DomainServerWebSessionData_h \ No newline at end of file From 6334116070369afbffcd4ef4f88b17efe7bc7fdd Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 28 Jul 2014 12:07:19 -0700 Subject: [PATCH 14/41] 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 From dbf41c8a422b90aed2bfe1ec1194f3b452330943 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 28 Jul 2014 14:56:26 -0700 Subject: [PATCH 15/41] allow domain-server to serve settings publicly, fix retry in DomainHandler --- domain-server/src/DomainServer.cpp | 8 +++- .../src/DomainServerSettingsManager.cpp | 42 +++++++++++-------- .../src/DomainServerSettingsManager.h | 5 ++- libraries/networking/src/DomainHandler.cpp | 19 +++++---- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index e462e05aa6..2ea66c2ee3 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -940,6 +940,12 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url const QString UUID_REGEX_STRING = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"; + // allow sub-handlers to handle requests that do not require authentication + if (_settingsManager.handlePublicHTTPRequest(connection, url)) { + return true; + } + + // all requests below require a cookie to prove authentication so check that first if (!isAuthenticatedRequest(connection, url)) { // this is not an authenticated request // return true from the handler since it was handled with a 401 or re-direct to auth @@ -1186,7 +1192,7 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url } // didn't process the request, let our DomainServerSettingsManager or HTTPManager handle - return _settingsManager.handleHTTPRequest(connection, url); + return _settingsManager.handleAuthenticatedHTTPRequest(connection, url); } const QString HIFI_SESSION_COOKIE_KEY = "DS_WEB_SESSION_UUID"; diff --git a/domain-server/src/DomainServerSettingsManager.cpp b/domain-server/src/DomainServerSettingsManager.cpp index 0520426ea8..4ec88996a6 100644 --- a/domain-server/src/DomainServerSettingsManager.cpp +++ b/domain-server/src/DomainServerSettingsManager.cpp @@ -47,24 +47,8 @@ DomainServerSettingsManager::DomainServerSettingsManager() : const QString DESCRIPTION_SETTINGS_KEY = "settings"; const QString SETTING_DEFAULT_KEY = "default"; -bool DomainServerSettingsManager::handleHTTPRequest(HTTPConnection* connection, const QUrl &url) { - if (connection->requestOperation() == QNetworkAccessManager::PostOperation && url.path() == "/settings.json") { - // this is a POST operation to change one or more settings - QJsonDocument postedDocument = QJsonDocument::fromJson(connection->requestContent()); - QJsonObject postedObject = postedDocument.object(); - - // we recurse one level deep below each group for the appropriate setting - recurseJSONObjectAndOverwriteSettings(postedObject, _settingsMap, _descriptionObject); - - // store whatever the current _settingsMap is to file - persistToFile(); - - // return success to the caller - QString jsonSuccess = "{\"status\": \"success\"}"; - connection->respond(HTTPConnection::StatusCode200, jsonSuccess.toUtf8(), "application/json"); - - return true; - } else if (connection->requestOperation() == QNetworkAccessManager::GetOperation && url.path() == "/settings.json") { +bool DomainServerSettingsManager::handlePublicHTTPRequest(HTTPConnection* connection, const QUrl &url) { + if (connection->requestOperation() == QNetworkAccessManager::GetOperation && url.path() == "/settings.json") { // this is a GET operation for our settings // check if there is a query parameter for settings affecting a particular type of assignment @@ -135,6 +119,28 @@ bool DomainServerSettingsManager::handleHTTPRequest(HTTPConnection* connection, return false; } +bool DomainServerSettingsManager::handleAuthenticatedHTTPRequest(HTTPConnection *connection, const QUrl &url) { + if (connection->requestOperation() == QNetworkAccessManager::PostOperation && url.path() == "/settings.json") { + // this is a POST operation to change one or more settings + QJsonDocument postedDocument = QJsonDocument::fromJson(connection->requestContent()); + QJsonObject postedObject = postedDocument.object(); + + // we recurse one level deep below each group for the appropriate setting + recurseJSONObjectAndOverwriteSettings(postedObject, _settingsMap, _descriptionObject); + + // store whatever the current _settingsMap is to file + persistToFile(); + + // return success to the caller + QString jsonSuccess = "{\"status\": \"success\"}"; + connection->respond(HTTPConnection::StatusCode200, jsonSuccess.toUtf8(), "application/json"); + + return true; + } + + return false; +} + void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject, QVariantMap& settingsVariant, QJsonObject descriptionObject) { diff --git a/domain-server/src/DomainServerSettingsManager.h b/domain-server/src/DomainServerSettingsManager.h index 8b80cad280..26bfe57ab4 100644 --- a/domain-server/src/DomainServerSettingsManager.h +++ b/domain-server/src/DomainServerSettingsManager.h @@ -16,11 +16,12 @@ #include -class DomainServerSettingsManager : public QObject, HTTPRequestHandler { +class DomainServerSettingsManager : public QObject { Q_OBJECT public: DomainServerSettingsManager(); - bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url); + bool handlePublicHTTPRequest(HTTPConnection* connection, const QUrl& url); + bool handleAuthenticatedHTTPRequest(HTTPConnection* connection, const QUrl& url); QByteArray getJSONSettingsMap() const; private: diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index 286909279b..ce6a264b34 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -11,6 +11,7 @@ #include +#include "Assignment.h" #include "NodeList.h" #include "PacketHeaders.h" #include "UserActivityLogger.h" @@ -133,10 +134,9 @@ void DomainHandler::requestDomainSettings() const { 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; + settingsJSONURL.setPath("/settingz.json/"); + Assignment::Type assignmentType = Assignment::typeForNodeType(NodeList::getInstance()->getOwnerType()); + settingsJSONURL.setQuery(QString("type=%1").arg(assignmentType)); QNetworkReply* reply = NetworkAccessManager::getInstance().get(QNetworkRequest(settingsJSONURL)); connect(reply, &QNetworkReply::finished, this, &DomainHandler::settingsRequestFinished); @@ -147,11 +147,12 @@ const int MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS = 5; void DomainHandler::settingsRequestFinished() { QNetworkReply* settingsReply = reinterpret_cast(sender()); - if (settingsReply->error() == QNetworkReply::NoError) { + int replyCode = settingsReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (settingsReply->error() == QNetworkReply::NoError && replyCode != 301 && replyCode != 302) { // 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 @@ -165,9 +166,9 @@ void DomainHandler::settingsRequestFinished() { qDebug() << "Failed to retreive domain-server settings" << MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS << "times. Re-setting connection to domain."; clearSettings(); clearConnectionInfo(); - } - - requestDomainSettings(); + } else { + requestDomainSettings(); + } } } From 84b86c17b7d75a76d6afc1d4e6a03b7e537058b9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 28 Jul 2014 15:16:59 -0700 Subject: [PATCH 16/41] use common settings retreival code to block before run in AudioMixer --- assignment-client/src/audio/AudioMixer.cpp | 43 +++++++--------------- libraries/networking/src/DomainHandler.cpp | 29 +++++++++------ libraries/networking/src/DomainHandler.h | 3 ++ 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index ad4787b407..91ecee76e2 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -408,41 +408,24 @@ void AudioMixer::run() { nodeList->linkedDataCreateCallback = attachNewBufferToNode; - // setup a NetworkAccessManager to ask the domain-server for our settings - NetworkAccessManager& networkManager = NetworkAccessManager::getInstance(); + // wait until we have the domain-server settings, otherwise we bail + DomainHandler& domainHandler = nodeList->getDomainHandler(); - QUrl settingsJSONURL; - settingsJSONURL.setScheme("http"); - settingsJSONURL.setHost(nodeList->getDomainHandler().getHostname()); - settingsJSONURL.setPort(DOMAIN_SERVER_HTTP_PORT); - settingsJSONURL.setPath("/settings.json"); - settingsJSONURL.setQuery(QString("type=%1").arg(_type)); + qDebug() << "Waiting for domain settings from domain-server."; - QNetworkReply *reply = NULL; + // block until we get the settingsRequestComplete signal + QEventLoop loop; + loop.connect(&domainHandler, &DomainHandler::settingsRequestComplete, &loop, &QEventLoop::quit); + domainHandler.requestDomainSettings(); + loop.exec(); - int failedAttempts = 0; - const int MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS = 5; - - qDebug() << "Requesting settings for assignment from domain-server at" << settingsJSONURL.toString(); - - while (!reply || reply->error() != QNetworkReply::NoError) { - reply = networkManager.get(QNetworkRequest(settingsJSONURL)); - - QEventLoop loop; - QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - - loop.exec(); - - ++failedAttempts; - - if (failedAttempts == MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS) { - qDebug() << "Failed to get settings from domain-server. Bailing on assignment."; - setFinished(true); - return; - } + if (domainHandler.getSettingsObject().isEmpty()) { + qDebug() << "Failed to retreive settings object from domain-server. Bailing on assignment."; + setFinished(true); + return; } - QJsonObject settingsObject = QJsonDocument::fromJson(reply->readAll()).object(); + const QJsonObject& settingsObject = domainHandler.getSettingsObject(); // check the settings object to see if we have anything we can parse out const QString AUDIO_GROUP_KEY = "audio"; diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index ce6a264b34..0f37160512 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -128,18 +128,21 @@ void DomainHandler::setIsConnected(bool isConnected) { } 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("/settingz.json/"); - Assignment::Type assignmentType = Assignment::typeForNodeType(NodeList::getInstance()->getOwnerType()); - settingsJSONURL.setQuery(QString("type=%1").arg(assignmentType)); - - QNetworkReply* reply = NetworkAccessManager::getInstance().get(QNetworkRequest(settingsJSONURL)); - connect(reply, &QNetworkReply::finished, this, &DomainHandler::settingsRequestFinished); + if (_settingsObject.isEmpty()) { + // 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"); + Assignment::Type assignmentType = Assignment::typeForNodeType(NodeList::getInstance()->getOwnerType()); + settingsJSONURL.setQuery(QString("type=%1").arg(assignmentType)); + + qDebug() << "Requesting domain-server settings at" << settingsJSONURL.toString(); + + QNetworkReply* reply = NetworkAccessManager::getInstance().get(QNetworkRequest(settingsJSONURL)); + connect(reply, &QNetworkReply::finished, this, &DomainHandler::settingsRequestFinished); + } } const int MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS = 5; @@ -154,6 +157,7 @@ void DomainHandler::settingsRequestFinished() { _settingsObject = QJsonDocument::fromJson(settingsReply->readAll()).object(); qDebug() << "Received domain settings."; + emit settingsRequestComplete(true); // reset failed settings requests to 0, we got them _failedSettingsRequests = 0; @@ -166,6 +170,7 @@ void DomainHandler::settingsRequestFinished() { qDebug() << "Failed to retreive domain-server settings" << MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS << "times. Re-setting connection to domain."; clearSettings(); clearConnectionInfo(); + emit settingsRequestComplete(false); } else { requestDomainSettings(); } diff --git a/libraries/networking/src/DomainHandler.h b/libraries/networking/src/DomainHandler.h index 6761b8eb84..67edd64172 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -58,6 +58,7 @@ public: bool hasSettings() const { return !_settingsObject.isEmpty(); } void requestDomainSettings() const; + const QJsonObject& getSettingsObject() const { return _settingsObject; } void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket); @@ -68,6 +69,8 @@ signals: void hostnameChanged(const QString& hostname); void connectedToDomain(const QString& hostname); + void settingsRequestComplete(bool wasSuccessful); + private: void reset(); From ef5db5321d7051fdcc4375b5ec1c511c4fe7c65b Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 09:46:59 -0700 Subject: [PATCH 17/41] make voxel charges affect agents --- domain-server/resources/web/settings/describe.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain-server/resources/web/settings/describe.json b/domain-server/resources/web/settings/describe.json index 0ab647734b..beb02411c9 100644 --- a/domain-server/resources/web/settings/describe.json +++ b/domain-server/resources/web/settings/describe.json @@ -19,7 +19,7 @@ }, "voxels": { "label": "Voxels", - "assignment-types": [3], + "assignment-types": [2,3], "settings": { "voxel-wallet": { "label": "Destination Wallet ID", From 9a7267a10c33506cd39e6ebe4bb404e23fefb54f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 10:52:14 -0700 Subject: [PATCH 18/41] remove an unecessary reference to loop --- assignment-client/src/audio/AudioMixer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index 91ecee76e2..e2d9636118 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -415,7 +415,7 @@ void AudioMixer::run() { // block until we get the settingsRequestComplete signal QEventLoop loop; - loop.connect(&domainHandler, &DomainHandler::settingsRequestComplete, &loop, &QEventLoop::quit); + connect(&domainHandler, &DomainHandler::settingsRequestComplete, &loop, &QEventLoop::quit); domainHandler.requestDomainSettings(); loop.exec(); From 85b8449e83832d4d479da9f814c4a7b10c249b94 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 11:17:27 -0700 Subject: [PATCH 19/41] use two different signals for settings success and failure --- assignment-client/src/audio/AudioMixer.cpp | 3 ++- libraries/networking/src/DomainHandler.cpp | 4 ++-- libraries/networking/src/DomainHandler.h | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index e2d9636118..4c3d951716 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -415,7 +415,8 @@ void AudioMixer::run() { // block until we get the settingsRequestComplete signal QEventLoop loop; - connect(&domainHandler, &DomainHandler::settingsRequestComplete, &loop, &QEventLoop::quit); + connect(&domainHandler, &DomainHandler::settingsReceived, &loop, &QEventLoop::quit); + connect(&domainHandler, &DomainHandler::settingsReceiveFail, &loop, &QEventLoop::quit); domainHandler.requestDomainSettings(); loop.exec(); diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index 0f37160512..e316ea2cc5 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -157,7 +157,7 @@ void DomainHandler::settingsRequestFinished() { _settingsObject = QJsonDocument::fromJson(settingsReply->readAll()).object(); qDebug() << "Received domain settings."; - emit settingsRequestComplete(true); + emit settingsReceived(); // reset failed settings requests to 0, we got them _failedSettingsRequests = 0; @@ -170,7 +170,7 @@ void DomainHandler::settingsRequestFinished() { qDebug() << "Failed to retreive domain-server settings" << MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS << "times. Re-setting connection to domain."; clearSettings(); clearConnectionInfo(); - emit settingsRequestComplete(false); + emit settingsReceiveFail(); } else { requestDomainSettings(); } diff --git a/libraries/networking/src/DomainHandler.h b/libraries/networking/src/DomainHandler.h index 67edd64172..81745713e7 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -69,7 +69,8 @@ signals: void hostnameChanged(const QString& hostname); void connectedToDomain(const QString& hostname); - void settingsRequestComplete(bool wasSuccessful); + void settingsReceived(); + void settingsReceiveFail(); private: void reset(); From 39e6d7d31b99483a01e3d293d215bd3af4254b92 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 11:40:07 -0700 Subject: [PATCH 20/41] octree packet methods can take a cost --- libraries/octree/src/OctreeEditPacketSender.cpp | 9 ++++++--- libraries/octree/src/OctreeEditPacketSender.h | 6 +++--- libraries/voxels/src/VoxelsScriptingInterface.cpp | 1 - 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index 2ed8f6c2a0..5f6fb1274b 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -87,7 +87,8 @@ bool OctreeEditPacketSender::serversExist() const { // This method is called when the edit packet layer has determined that it has a fully formed packet destined for // a known nodeID. -void OctreeEditPacketSender::queuePacketToNode(const QUuid& nodeUUID, unsigned char* buffer, ssize_t length) { +void OctreeEditPacketSender::queuePacketToNode(const QUuid& nodeUUID, unsigned char* buffer, + ssize_t length, qint64 satoshiCost) { NodeList* nodeList = NodeList::getInstance(); foreach (const SharedNodePointer& node, nodeList->getNodeHash()) { @@ -157,7 +158,8 @@ void OctreeEditPacketSender::processPreServerExistsPackets() { } } -void OctreeEditPacketSender::queuePendingPacketToNodes(PacketType type, unsigned char* buffer, ssize_t length) { +void OctreeEditPacketSender::queuePendingPacketToNodes(PacketType type, unsigned char* buffer, + ssize_t length, qint64 satoshiCost) { // If we're asked to save messages while waiting for voxel servers to arrive, then do so... if (_maxPendingMessages > 0) { @@ -210,7 +212,8 @@ void OctreeEditPacketSender::queuePacketToNodes(unsigned char* buffer, ssize_t l // NOTE: codeColorBuffer - is JUST the octcode/color and does not contain the packet header! -void OctreeEditPacketSender::queueOctreeEditMessage(PacketType type, unsigned char* codeColorBuffer, ssize_t length) { +void OctreeEditPacketSender::queueOctreeEditMessage(PacketType type, unsigned char* codeColorBuffer, + ssize_t length, qint64 satoshiCost) { if (!_shouldSend) { return; // bail early diff --git a/libraries/octree/src/OctreeEditPacketSender.h b/libraries/octree/src/OctreeEditPacketSender.h index cdcfc21d4a..584f74fd39 100644 --- a/libraries/octree/src/OctreeEditPacketSender.h +++ b/libraries/octree/src/OctreeEditPacketSender.h @@ -39,7 +39,7 @@ public: /// Queues a single edit message. Will potentially send a pending multi-command packet. Determines which server /// node or nodes the packet should be sent to. Can be called even before servers are known, in which case up to /// MaxPendingMessages will be buffered and processed when servers are known. - void queueOctreeEditMessage(PacketType type, unsigned char* buffer, ssize_t length); + void queueOctreeEditMessage(PacketType type, unsigned char* buffer, ssize_t length, qint64 satoshiCost = 0); /// Releases all queued messages even if those messages haven't filled an MTU packet. This will move the packed message /// packets onto the send queue. If running in threaded mode, the caller does not need to do any further processing to @@ -100,8 +100,8 @@ public: protected: bool _shouldSend; - void queuePacketToNode(const QUuid& nodeID, unsigned char* buffer, ssize_t length); - void queuePendingPacketToNodes(PacketType type, unsigned char* buffer, ssize_t length); + void queuePacketToNode(const QUuid& nodeID, unsigned char* buffer, ssize_t length, qint64 satoshiCost = 0); + void queuePendingPacketToNodes(PacketType type, unsigned char* buffer, ssize_t length, qint64 satoshiCost = 0); void queuePacketToNodes(unsigned char* buffer, ssize_t length); void initializePacket(EditPacketBuffer& packetBuffer, PacketType type); void releaseQueuedPacket(EditPacketBuffer& packetBuffer); // releases specific queued packet diff --git a/libraries/voxels/src/VoxelsScriptingInterface.cpp b/libraries/voxels/src/VoxelsScriptingInterface.cpp index e877f99760..05be0fe350 100644 --- a/libraries/voxels/src/VoxelsScriptingInterface.cpp +++ b/libraries/voxels/src/VoxelsScriptingInterface.cpp @@ -71,7 +71,6 @@ void VoxelsScriptingInterface::setVoxel(float x, float y, float z, float scale, VoxelDetail addVoxelDetail = {x / (float)TREE_SCALE, y / (float)TREE_SCALE, z / (float)TREE_SCALE, scale / (float)TREE_SCALE, red, green, blue}; - // handle the local tree also... if (_tree) { if (_undoStack) { From 5fd7a11c6b41020168789b7c34d9b1d6aea658a9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 16:38:07 -0700 Subject: [PATCH 21/41] rename the voxel cost settings --- domain-server/resources/web/settings/describe.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/domain-server/resources/web/settings/describe.json b/domain-server/resources/web/settings/describe.json index beb02411c9..2cb558bf0c 100644 --- a/domain-server/resources/web/settings/describe.json +++ b/domain-server/resources/web/settings/describe.json @@ -27,15 +27,15 @@ "placeholder": "00000000-0000-0000-0000-000000000000", "default": "" }, - "per-voxel-cost": { + "per-voxel-credits": { "label": "Per Voxel Cost", "help": "Credit cost to change each voxel", "placeholder": "0.0", "default": "0.0", "input_addon": "₵" }, - "volume-cost": { - "label": "Volume Cost", + "per-meter-cubed-credits": { + "label": "Per Meter Cubed Cost", "help": "Credit cost to change each cubed meter of voxel space", "placeholder": "0.0", "default": "0.0", From 40e08d50088de709035fed7ee2cd586be4b17795 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 16:39:06 -0700 Subject: [PATCH 22/41] add voxel cost amounts to VoxelEditPacketSender --- .../voxels/src/VoxelEditPacketSender.cpp | 23 +++++++++++++++++++ libraries/voxels/src/VoxelEditPacketSender.h | 16 +++++++++++++ 2 files changed, 39 insertions(+) diff --git a/libraries/voxels/src/VoxelEditPacketSender.cpp b/libraries/voxels/src/VoxelEditPacketSender.cpp index 1832d5436e..b4de63b302 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.cpp +++ b/libraries/voxels/src/VoxelEditPacketSender.cpp @@ -142,3 +142,26 @@ void VoxelEditPacketSender::queueVoxelEditMessages(PacketType type, int numberOf } } } + +void VoxelEditPacketSender::updateVoxelCosts(const QJsonObject& domainSettingsObject) { + + // from the domain-handler, figure out the satoshi cost per voxel and per meter cubed + const QString VOXEL_SETTINGS_KEY = "voxels"; + const QString PER_VOXEL_COST_KEY = "per-voxel-credits"; + const QString PER_METER_CUBED_COST_KEY = "per-meter-cubed-credits"; + + if (!domainSettingsObject.isEmpty()) { + float perVoxelCredits = (float) domainSettingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_VOXEL_COST_KEY].toDouble(); + float perMeterCubedCredits = (float) domainSettingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_METER_CUBED_COST_KEY].toDouble(); + + qDebug() << "PV: " << perVoxelCredits << "PMC: " << perMeterCubedCredits; + } else { + qDebug() << "CALLED WITH EMPTY SETTINGS!"; + _satoshisPerVoxel = 0; + _satoshisPerMeterCubed = 0; + } +} + +qint64 VoxelEditPacketSender::satoshiCostForMessage(PacketType type, int numberOfDetails, VoxelDetail *details) { + return 0; +} diff --git a/libraries/voxels/src/VoxelEditPacketSender.h b/libraries/voxels/src/VoxelEditPacketSender.h index 3ef257de6c..33382d7faf 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.h +++ b/libraries/voxels/src/VoxelEditPacketSender.h @@ -15,6 +15,7 @@ #define hifi_VoxelEditPacketSender_h #include + #include "VoxelDetail.h" /// Utility for processing, packing, queueing and sending of outbound edit voxel messages. @@ -49,5 +50,20 @@ public: // My server type is the voxel server virtual char getMyNodeType() const { return NodeType::VoxelServer; } + + qint64 satoshiCostForMessage(PacketType type, int numberOfDetails, VoxelDetail* details); + + void setSatoshisPerVoxel(qint64 satoshisPerVoxel) { _satoshisPerVoxel = satoshisPerVoxel; } + qint64 getSatoshisPerVoxel() const { return _satoshisPerVoxel; } + + void setSatoshisPerMeterCubed(qint64 satoshisPerMeterCubed) { _satoshisPerMeterCubed = satoshisPerMeterCubed; } + qint64 getSatoshisPerMeterCubed() const { return _satoshisPerMeterCubed; } + +public slots: + void updateVoxelCosts(const QJsonObject& domainSettingsObject); + +private: + qint64 _satoshisPerVoxel; + qint64 _satoshisPerMeterCubed; }; #endif // hifi_VoxelEditPacketSender_h From 9dbe74b02f85bb70f6d747678fda898dbb079677 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 17:22:17 -0700 Subject: [PATCH 23/41] parse voxel costs to the DomainHandler object --- .../resources/web/settings/describe.json | 2 ++ .../src/DomainServerSettingsManager.cpp | 9 +++++++- libraries/networking/src/DomainHandler.cpp | 23 +++++++++++++++++++ libraries/networking/src/DomainHandler.h | 8 +++++++ .../networking/src/UserActivityLogger.cpp | 2 +- .../voxels/src/VoxelEditPacketSender.cpp | 19 --------------- libraries/voxels/src/VoxelEditPacketSender.h | 13 ----------- 7 files changed, 42 insertions(+), 34 deletions(-) diff --git a/domain-server/resources/web/settings/describe.json b/domain-server/resources/web/settings/describe.json index 2cb558bf0c..bf1a9cc23c 100644 --- a/domain-server/resources/web/settings/describe.json +++ b/domain-server/resources/web/settings/describe.json @@ -28,6 +28,7 @@ "default": "" }, "per-voxel-credits": { + "type": "double", "label": "Per Voxel Cost", "help": "Credit cost to change each voxel", "placeholder": "0.0", @@ -35,6 +36,7 @@ "input_addon": "₵" }, "per-meter-cubed-credits": { + "type": "double", "label": "Per Meter Cubed Cost", "help": "Credit cost to change each cubed meter of voxel space", "placeholder": "0.0", diff --git a/domain-server/src/DomainServerSettingsManager.cpp b/domain-server/src/DomainServerSettingsManager.cpp index 4ec88996a6..9c741b2a3b 100644 --- a/domain-server/src/DomainServerSettingsManager.cpp +++ b/domain-server/src/DomainServerSettingsManager.cpp @@ -141,6 +141,8 @@ bool DomainServerSettingsManager::handleAuthenticatedHTTPRequest(HTTPConnection return false; } +const QString SETTING_DESCRIPTION_TYPE_KEY = "type"; + void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject, QVariantMap& settingsVariant, QJsonObject descriptionObject) { @@ -155,7 +157,12 @@ void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJ // this is an empty value, clear it in settings variant so the default is sent settingsVariant.remove(key); } else { - settingsVariant[key] = rootValue.toString(); + if (descriptionObject[key].toObject().contains(SETTING_DESCRIPTION_TYPE_KEY)) { + // for now this means that this is a double, so set it as a double + settingsVariant[key] = rootValue.toString().toDouble(); + } else { + settingsVariant[key] = rootValue.toString(); + } } } else if (rootValue.isBool()) { settingsVariant[key] = rootValue.toBool(); diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index e316ea2cc5..a50f735542 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 #include "Assignment.h" @@ -159,6 +161,8 @@ void DomainHandler::settingsRequestFinished() { qDebug() << "Received domain settings."; emit settingsReceived(); + updateVoxelCosts(); + // reset failed settings requests to 0, we got them _failedSettingsRequests = 0; } else { @@ -177,6 +181,25 @@ void DomainHandler::settingsRequestFinished() { } } +void DomainHandler::updateVoxelCosts() { + + // from the domain-handler, figure out the satoshi cost per voxel and per meter cubed + const QString VOXEL_SETTINGS_KEY = "voxels"; + const QString PER_VOXEL_COST_KEY = "per-voxel-credits"; + const QString PER_METER_CUBED_COST_KEY = "per-meter-cubed-credits"; + + if (!_settingsObject.isEmpty()) { + float perVoxelCredits = (float) _settingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_VOXEL_COST_KEY].toDouble(); + float perMeterCubedCredits = (float) _settingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_METER_CUBED_COST_KEY].toDouble(); + + _satoshisPerVoxel = (qint64) floorf(perVoxelCredits * SATOSHIS_PER_CREDIT); + _satoshisPerMeterCubed = (qint64) floorf(perMeterCubedCredits * SATOSHIS_PER_CREDIT); + } else { + _satoshisPerVoxel = 0; + _satoshisPerMeterCubed = 0; + } +} + 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 81745713e7..7b7a80b49a 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -62,6 +62,9 @@ public: void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket); + qint64 getSatoshisPerVoxel() const { return _satoshisPerVoxel; } + qint64 getSatoshisPerMeterCubed() const { return _satoshisPerMeterCubed; } + private slots: void completedHostnameLookup(const QHostInfo& hostInfo); void settingsRequestFinished(); @@ -75,6 +78,8 @@ signals: private: void reset(); + void updateVoxelCosts(); + QUuid _uuid; QString _hostname; HifiSockAddr _sockAddr; @@ -83,6 +88,9 @@ private: QTimer* _handshakeTimer; QJsonObject _settingsObject; int _failedSettingsRequests; + + qint64 _satoshisPerVoxel; + qint64 _satoshisPerMeterCubed; }; #endif // hifi_DomainHandler_h diff --git a/libraries/networking/src/UserActivityLogger.cpp b/libraries/networking/src/UserActivityLogger.cpp index 90b9da07dc..e2d3434867 100644 --- a/libraries/networking/src/UserActivityLogger.cpp +++ b/libraries/networking/src/UserActivityLogger.cpp @@ -70,7 +70,7 @@ void UserActivityLogger::logAction(QString action, QJsonObject details, JSONCall } void UserActivityLogger::requestFinished(const QJsonObject& object) { - qDebug() << object; + // qDebug() << object; } void UserActivityLogger::requestError(QNetworkReply::NetworkError error,const QString& string) { diff --git a/libraries/voxels/src/VoxelEditPacketSender.cpp b/libraries/voxels/src/VoxelEditPacketSender.cpp index b4de63b302..40a0f853f6 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.cpp +++ b/libraries/voxels/src/VoxelEditPacketSender.cpp @@ -143,25 +143,6 @@ void VoxelEditPacketSender::queueVoxelEditMessages(PacketType type, int numberOf } } -void VoxelEditPacketSender::updateVoxelCosts(const QJsonObject& domainSettingsObject) { - - // from the domain-handler, figure out the satoshi cost per voxel and per meter cubed - const QString VOXEL_SETTINGS_KEY = "voxels"; - const QString PER_VOXEL_COST_KEY = "per-voxel-credits"; - const QString PER_METER_CUBED_COST_KEY = "per-meter-cubed-credits"; - - if (!domainSettingsObject.isEmpty()) { - float perVoxelCredits = (float) domainSettingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_VOXEL_COST_KEY].toDouble(); - float perMeterCubedCredits = (float) domainSettingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_METER_CUBED_COST_KEY].toDouble(); - - qDebug() << "PV: " << perVoxelCredits << "PMC: " << perMeterCubedCredits; - } else { - qDebug() << "CALLED WITH EMPTY SETTINGS!"; - _satoshisPerVoxel = 0; - _satoshisPerMeterCubed = 0; - } -} - qint64 VoxelEditPacketSender::satoshiCostForMessage(PacketType type, int numberOfDetails, VoxelDetail *details) { return 0; } diff --git a/libraries/voxels/src/VoxelEditPacketSender.h b/libraries/voxels/src/VoxelEditPacketSender.h index 33382d7faf..4fb8f718cc 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.h +++ b/libraries/voxels/src/VoxelEditPacketSender.h @@ -52,18 +52,5 @@ public: virtual char getMyNodeType() const { return NodeType::VoxelServer; } qint64 satoshiCostForMessage(PacketType type, int numberOfDetails, VoxelDetail* details); - - void setSatoshisPerVoxel(qint64 satoshisPerVoxel) { _satoshisPerVoxel = satoshisPerVoxel; } - qint64 getSatoshisPerVoxel() const { return _satoshisPerVoxel; } - - void setSatoshisPerMeterCubed(qint64 satoshisPerMeterCubed) { _satoshisPerMeterCubed = satoshisPerMeterCubed; } - qint64 getSatoshisPerMeterCubed() const { return _satoshisPerMeterCubed; } - -public slots: - void updateVoxelCosts(const QJsonObject& domainSettingsObject); - -private: - qint64 _satoshisPerVoxel; - qint64 _satoshisPerMeterCubed; }; #endif // hifi_VoxelEditPacketSender_h From 6a534a6ff1911df5e3ae495e914c2e4a639d3149 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 29 Jul 2014 17:31:12 -0700 Subject: [PATCH 24/41] send along satoshi costs for voxel additions from VoxelEditPacketSender --- .../octree/src/OctreeEditPacketSender.cpp | 2 +- libraries/octree/src/OctreeEditPacketSender.h | 2 +- .../voxels/src/VoxelEditPacketSender.cpp | 19 ++++++++++++++----- libraries/voxels/src/VoxelEditPacketSender.h | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index 5f6fb1274b..1d0c40d659 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -177,7 +177,7 @@ void OctreeEditPacketSender::queuePendingPacketToNodes(PacketType type, unsigned } } -void OctreeEditPacketSender::queuePacketToNodes(unsigned char* buffer, ssize_t length) { +void OctreeEditPacketSender::queuePacketToNodes(unsigned char* buffer, ssize_t length, qint64 satoshiCost) { if (!_shouldSend) { return; // bail early } diff --git a/libraries/octree/src/OctreeEditPacketSender.h b/libraries/octree/src/OctreeEditPacketSender.h index 584f74fd39..ad2526b866 100644 --- a/libraries/octree/src/OctreeEditPacketSender.h +++ b/libraries/octree/src/OctreeEditPacketSender.h @@ -102,7 +102,7 @@ protected: bool _shouldSend; void queuePacketToNode(const QUuid& nodeID, unsigned char* buffer, ssize_t length, qint64 satoshiCost = 0); void queuePendingPacketToNodes(PacketType type, unsigned char* buffer, ssize_t length, qint64 satoshiCost = 0); - void queuePacketToNodes(unsigned char* buffer, ssize_t length); + void queuePacketToNodes(unsigned char* buffer, ssize_t length, qint64 satoshiCost = 0); void initializePacket(EditPacketBuffer& packetBuffer, PacketType type); void releaseQueuedPacket(EditPacketBuffer& packetBuffer); // releases specific queued packet diff --git a/libraries/voxels/src/VoxelEditPacketSender.cpp b/libraries/voxels/src/VoxelEditPacketSender.cpp index 40a0f853f6..0913b10272 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.cpp +++ b/libraries/voxels/src/VoxelEditPacketSender.cpp @@ -117,9 +117,9 @@ void VoxelEditPacketSender::sendVoxelEditMessage(PacketType type, const VoxelDet // If we don't have voxel jurisdictions, then we will simply queue up these packets and wait till we have // jurisdictions for processing if (!voxelServersExist()) { - queuePendingPacketToNodes(type, bufferOut, sizeOut); + queuePendingPacketToNodes(type, bufferOut, sizeOut, satoshiCostForMessage(detail)); } else { - queuePacketToNodes(bufferOut, sizeOut); + queuePacketToNodes(bufferOut, sizeOut, satoshiCostForMessage(detail)); } // either way, clean up the created buffer @@ -138,11 +138,20 @@ void VoxelEditPacketSender::queueVoxelEditMessages(PacketType type, int numberOf int sizeOut = 0; if (encodeVoxelEditMessageDetails(type, 1, &details[i], &bufferOut[0], _maxPacketSize, sizeOut)) { - queueOctreeEditMessage(type, bufferOut, sizeOut); + queueOctreeEditMessage(type, bufferOut, sizeOut, satoshiCostForMessage(details[i])); } } } -qint64 VoxelEditPacketSender::satoshiCostForMessage(PacketType type, int numberOfDetails, VoxelDetail *details) { - return 0; +qint64 VoxelEditPacketSender::satoshiCostForMessage(const VoxelDetail& details) { + const DomainHandler& domainHandler = NodeList::getInstance()->getDomainHandler(); + + qint64 totalSatoshiCost = domainHandler.getSatoshisPerVoxel(); + + qint64 costPerMeterCubed = domainHandler.getSatoshisPerMeterCubed(); + float totalVolume = details.s * details.s * details.s; + + totalSatoshiCost += floorf(totalVolume * costPerMeterCubed); + + return costPerMeterCubed; } diff --git a/libraries/voxels/src/VoxelEditPacketSender.h b/libraries/voxels/src/VoxelEditPacketSender.h index 4fb8f718cc..e761812629 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.h +++ b/libraries/voxels/src/VoxelEditPacketSender.h @@ -51,6 +51,6 @@ public: // My server type is the voxel server virtual char getMyNodeType() const { return NodeType::VoxelServer; } - qint64 satoshiCostForMessage(PacketType type, int numberOfDetails, VoxelDetail* details); + qint64 satoshiCostForMessage(const VoxelDetail& details); }; #endif // hifi_VoxelEditPacketSender_h From f944c27cafac1a5b434fa4c07d32defecd7afb73 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 09:24:31 -0700 Subject: [PATCH 25/41] don't calculate satoshi cost if the costs are non-existent --- libraries/voxels/src/VoxelEditPacketSender.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libraries/voxels/src/VoxelEditPacketSender.cpp b/libraries/voxels/src/VoxelEditPacketSender.cpp index 0913b10272..87bba950c7 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.cpp +++ b/libraries/voxels/src/VoxelEditPacketSender.cpp @@ -149,9 +149,12 @@ qint64 VoxelEditPacketSender::satoshiCostForMessage(const VoxelDetail& details) qint64 totalSatoshiCost = domainHandler.getSatoshisPerVoxel(); qint64 costPerMeterCubed = domainHandler.getSatoshisPerMeterCubed(); - float totalVolume = details.s * details.s * details.s; - totalSatoshiCost += floorf(totalVolume * costPerMeterCubed); - - return costPerMeterCubed; + if (totalSatoshiCost == 0 && costPerMeterCubed == 0) { + float totalVolume = details.s * details.s * details.s; + + return totalSatoshiCost + floorf(totalVolume * costPerMeterCubed); + } else { + return 0; + } } From 2c8808e922ec88ec24aff1da5eda4314184552a9 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 09:45:33 -0700 Subject: [PATCH 26/41] repairs to cost calculations for voxels --- libraries/networking/src/DomainHandler.cpp | 3 ++- libraries/voxels/src/VoxelEditPacketSender.cpp | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index a50f735542..5f20bebd50 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -171,7 +171,8 @@ void DomainHandler::settingsRequestFinished() { 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."; + qDebug() << "Failed to retreive domain-server settings" << MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS + << "times. Re-setting connection to domain."; clearSettings(); clearConnectionInfo(); emit settingsReceiveFail(); diff --git a/libraries/voxels/src/VoxelEditPacketSender.cpp b/libraries/voxels/src/VoxelEditPacketSender.cpp index 87bba950c7..ee189f492d 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.cpp +++ b/libraries/voxels/src/VoxelEditPacketSender.cpp @@ -147,14 +147,14 @@ qint64 VoxelEditPacketSender::satoshiCostForMessage(const VoxelDetail& details) const DomainHandler& domainHandler = NodeList::getInstance()->getDomainHandler(); qint64 totalSatoshiCost = domainHandler.getSatoshisPerVoxel(); - qint64 costPerMeterCubed = domainHandler.getSatoshisPerMeterCubed(); if (totalSatoshiCost == 0 && costPerMeterCubed == 0) { - float totalVolume = details.s * details.s * details.s; - - return totalSatoshiCost + floorf(totalVolume * costPerMeterCubed); - } else { return 0; + } else { + float meterScale = details.s * TREE_SCALE; + float totalVolume = meterScale * meterScale * meterScale; + + return totalSatoshiCost + (qint64) floorf(totalVolume * costPerMeterCubed); } } From 66592466adfc6b885aac13bbca4add57f6c383dc Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 10:05:21 -0700 Subject: [PATCH 27/41] pipe satoshi cost down to queuePacketToNodes --- libraries/octree/src/EditPacketBuffer.cpp | 30 ++++++++++++++++ libraries/octree/src/EditPacketBuffer.h | 34 +++++++++++++++++++ .../octree/src/OctreeEditPacketSender.cpp | 18 ++++------ libraries/octree/src/OctreeEditPacketSender.h | 13 ++----- 4 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 libraries/octree/src/EditPacketBuffer.cpp create mode 100644 libraries/octree/src/EditPacketBuffer.h diff --git a/libraries/octree/src/EditPacketBuffer.cpp b/libraries/octree/src/EditPacketBuffer.cpp new file mode 100644 index 0000000000..d6e7bf0183 --- /dev/null +++ b/libraries/octree/src/EditPacketBuffer.cpp @@ -0,0 +1,30 @@ +// +// EditPacketBuffer.cpp +// libraries/octree/src +// +// Created by Stephen Birarda on 2014-07-30. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "EditPacketBuffer.h" + +EditPacketBuffer::EditPacketBuffer() : + _nodeUUID(), + _currentType(PacketTypeUnknown), + _currentSize(0), + _satoshiCost(0) +{ + +} + +EditPacketBuffer::EditPacketBuffer(PacketType type, unsigned char* buffer, ssize_t length, qint64 satoshiCost, QUuid nodeUUID) : + _nodeUUID(nodeUUID), + _currentType(type), + _currentSize(length), + _satoshiCost(satoshiCost) +{ + memcpy(_currentBuffer, buffer, length); +}; \ No newline at end of file diff --git a/libraries/octree/src/EditPacketBuffer.h b/libraries/octree/src/EditPacketBuffer.h new file mode 100644 index 0000000000..9b52cbc5e4 --- /dev/null +++ b/libraries/octree/src/EditPacketBuffer.h @@ -0,0 +1,34 @@ +// +// EditPacketBuffer.h +// libraries/octree/src +// +// Created by Stephen Birarda on 2014-07-30. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_EditPacketBuffer_h +#define hifi_EditPacketBuffer_h + +#include + +#include +#include + +/// Used for construction of edit packets +class EditPacketBuffer { +public: + EditPacketBuffer(); + EditPacketBuffer(PacketType type, unsigned char* codeColorBuffer, ssize_t length, + qint64 satoshiCost = 0, const QUuid nodeUUID = QUuid()); + + QUuid _nodeUUID; + PacketType _currentType; + unsigned char _currentBuffer[MAX_PACKET_SIZE]; + ssize_t _currentSize; + qint64 _satoshiCost; +}; + +#endif // hifi_EditPacketBuffer_h \ No newline at end of file diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index 1d0c40d659..b9891f0628 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -17,14 +17,6 @@ #include #include "OctreeEditPacketSender.h" -EditPacketBuffer::EditPacketBuffer(PacketType type, unsigned char* buffer, ssize_t length, QUuid nodeUUID) : - _nodeUUID(nodeUUID), - _currentType(type), - _currentSize(length) -{ - memcpy(_currentBuffer, buffer, length); -}; - const int OctreeEditPacketSender::DEFAULT_MAX_PENDING_MESSAGES = PacketSender::DEFAULT_PACKETS_PER_SECOND; @@ -136,7 +128,7 @@ void OctreeEditPacketSender::processPreServerExistsPackets() { _pendingPacketsLock.lock(); while (!_preServerSingleMessagePackets.empty()) { EditPacketBuffer* packet = _preServerSingleMessagePackets.front(); - queuePacketToNodes(&packet->_currentBuffer[0], packet->_currentSize); + queuePacketToNodes(&packet->_currentBuffer[0], packet->_currentSize, packet->_satoshiCost); delete packet; _preServerSingleMessagePackets.erase(_preServerSingleMessagePackets.begin()); } @@ -163,7 +155,7 @@ void OctreeEditPacketSender::queuePendingPacketToNodes(PacketType type, unsigned // If we're asked to save messages while waiting for voxel servers to arrive, then do so... if (_maxPendingMessages > 0) { - EditPacketBuffer* packet = new EditPacketBuffer(type, buffer, length); + EditPacketBuffer* packet = new EditPacketBuffer(type, buffer, length, satoshiCost); _pendingPacketsLock.lock(); _preServerSingleMessagePackets.push_back(packet); // if we've saved MORE than our max, then clear out the oldest packet... @@ -204,7 +196,7 @@ void OctreeEditPacketSender::queuePacketToNodes(unsigned char* buffer, ssize_t l isMyJurisdiction = (map.isMyJurisdiction(octCode, CHECK_NODE_ONLY) == JurisdictionMap::WITHIN); _serverJurisdictions->unlock(); if (isMyJurisdiction) { - queuePacketToNode(nodeUUID, buffer, length); + queuePacketToNode(nodeUUID, buffer, length, satoshiCost); } } } @@ -288,6 +280,7 @@ void OctreeEditPacketSender::queueOctreeEditMessage(PacketType type, unsigned ch memcpy(&packetBuffer._currentBuffer[packetBuffer._currentSize], codeColorBuffer, length); packetBuffer._currentSize += length; + packetBuffer._satoshiCost += satoshiCost; } } } @@ -309,7 +302,8 @@ void OctreeEditPacketSender::releaseQueuedMessages() { void OctreeEditPacketSender::releaseQueuedPacket(EditPacketBuffer& packetBuffer) { _releaseQueuedPacketMutex.lock(); if (packetBuffer._currentSize > 0 && packetBuffer._currentType != PacketTypeUnknown) { - queuePacketToNode(packetBuffer._nodeUUID, &packetBuffer._currentBuffer[0], packetBuffer._currentSize); + queuePacketToNode(packetBuffer._nodeUUID, &packetBuffer._currentBuffer[0], + packetBuffer._currentSize, packetBuffer._satoshiCost); packetBuffer._currentSize = 0; packetBuffer._currentType = PacketTypeUnknown; } diff --git a/libraries/octree/src/OctreeEditPacketSender.h b/libraries/octree/src/OctreeEditPacketSender.h index ad2526b866..d11aa55963 100644 --- a/libraries/octree/src/OctreeEditPacketSender.h +++ b/libraries/octree/src/OctreeEditPacketSender.h @@ -15,20 +15,11 @@ #include #include #include + +#include "EditPacketBuffer.h" #include "JurisdictionMap.h" #include "SentPacketHistory.h" -/// Used for construction of edit packets -class EditPacketBuffer { -public: - EditPacketBuffer() : _nodeUUID(), _currentType(PacketTypeUnknown), _currentSize(0) { } - EditPacketBuffer(PacketType type, unsigned char* codeColorBuffer, ssize_t length, const QUuid nodeUUID = QUuid()); - QUuid _nodeUUID; - PacketType _currentType; - unsigned char _currentBuffer[MAX_PACKET_SIZE]; - ssize_t _currentSize; -}; - /// Utility for processing, packing, queueing and sending of outbound edit messages. class OctreeEditPacketSender : public PacketSender { Q_OBJECT From 7a128e5f779dbde7f690fb48dd018b39d16bd673 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 10:14:51 -0700 Subject: [PATCH 28/41] add satoshi cost to voxel packet debug --- libraries/octree/src/OctreeEditPacketSender.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index b9891f0628..8c9bee2be0 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -94,7 +94,7 @@ void OctreeEditPacketSender::queuePacketToNode(const QUuid& nodeUUID, unsigned c unsigned char* sequenceAt = buffer + numBytesPacketHeader; quint16 sequence = _outgoingSequenceNumbers[nodeUUID]++; memcpy(sequenceAt, &sequence, sizeof(quint16)); - + // send packet QByteArray packet(reinterpret_cast(buffer), length); queuePacketForSending(node, packet); @@ -103,7 +103,7 @@ void OctreeEditPacketSender::queuePacketToNode(const QUuid& nodeUUID, unsigned c _sentPacketHistories[nodeUUID].packetSent(sequence, packet); // debugging output... - bool wantDebugging = false; + bool wantDebugging = true; if (wantDebugging) { int numBytesPacketHeader = numBytesForPacketHeader(reinterpret_cast(buffer)); unsigned short int sequence = (*((unsigned short int*)(buffer + numBytesPacketHeader))); @@ -113,6 +113,7 @@ void OctreeEditPacketSender::queuePacketToNode(const QUuid& nodeUUID, unsigned c qDebug() << "OctreeEditPacketSender::queuePacketToNode() queued " << buffer[0] << " - command to node bytes=" << length << + " satoshiCost=" << satoshiCost << " sequence=" << sequence << " transitTimeSoFar=" << transitTime << " usecs"; } From ab1be38fd5eba19ed91f098e17e25b2777764b4e Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 14:02:46 -0700 Subject: [PATCH 29/41] reset the cost of the packet back to 0 so it doesn't keep accumulating --- libraries/octree/src/OctreeEditPacketSender.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index 8c9bee2be0..634a0bb260 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -324,6 +324,9 @@ void OctreeEditPacketSender::initializePacket(EditPacketBuffer& packetBuffer, Pa packetBuffer._currentSize += sizeof(quint64); // nudge past timestamp packetBuffer._currentType = type; + + // reset cost for packet to 0 + packetBuffer._satoshiCost = 0; } bool OctreeEditPacketSender::process() { From eba92eb5173584d28ee11baddb6d33bc52075f79 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 17:21:04 -0700 Subject: [PATCH 30/41] have the OctreeEditPacketSender emit a signal when payment is required --- libraries/octree/src/OctreeEditPacketSender.cpp | 11 ++++++++++- libraries/octree/src/OctreeEditPacketSender.h | 14 +++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index 634a0bb260..86208c9fd2 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -26,7 +26,10 @@ OctreeEditPacketSender::OctreeEditPacketSender() : _maxPendingMessages(DEFAULT_MAX_PENDING_MESSAGES), _releaseQueuedMessagesPending(false), _serverJurisdictions(NULL), - _maxPacketSize(MAX_PACKET_SIZE) { + _maxPacketSize(MAX_PACKET_SIZE), + _destinationWalletUUID() +{ + } OctreeEditPacketSender::~OctreeEditPacketSender() { @@ -98,6 +101,12 @@ void OctreeEditPacketSender::queuePacketToNode(const QUuid& nodeUUID, unsigned c // send packet QByteArray packet(reinterpret_cast(buffer), length); queuePacketForSending(node, packet); + + if (hasDestinationWalletUUID() && satoshiCost > 0) { + // if we have a destination wallet UUID and a cost associated with this packet, signal that it + // needs to be sent + emit octreePaymentRequired(satoshiCost, nodeUUID, _destinationWalletUUID); + } // add packet to history _sentPacketHistories[nodeUUID].packetSent(sequence, packet); diff --git a/libraries/octree/src/OctreeEditPacketSender.h b/libraries/octree/src/OctreeEditPacketSender.h index d11aa55963..7b39a1d785 100644 --- a/libraries/octree/src/OctreeEditPacketSender.h +++ b/libraries/octree/src/OctreeEditPacketSender.h @@ -82,13 +82,19 @@ public: // you must override these... virtual char getMyNodeType() const = 0; virtual void adjustEditPacketForClockSkew(unsigned char* codeColorBuffer, ssize_t length, int clockSkew) { }; + + bool hasDestinationWalletUUID() const { return _destinationWalletUUID.isNull(); } + void setDestinationWalletUUID(const QUuid& destinationWalletUUID) { _destinationWalletUUID = destinationWalletUUID; } + const QUuid& getDestinationWalletUUID() { return _destinationWalletUUID; } + + void processNackPacket(const QByteArray& packet); public slots: void nodeKilled(SharedNodePointer node); -public: - void processNackPacket(const QByteArray& packet); - +signals: + void octreePaymentRequired(qint64 satoshiAmount, const QUuid& nodeUUID, const QUuid& destinationWalletUUID); + protected: bool _shouldSend; void queuePacketToNode(const QUuid& nodeID, unsigned char* buffer, ssize_t length, qint64 satoshiCost = 0); @@ -118,5 +124,7 @@ protected: // TODO: add locks for this and _pendingEditPackets QHash _sentPacketHistories; QHash _outgoingSequenceNumbers; + + QUuid _destinationWalletUUID; }; #endif // hifi_OctreeEditPacketSender_h From 363cef6d8a6890fb29be98657dbf1c7b794e63fb Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 17:33:37 -0700 Subject: [PATCH 31/41] move satoshi costs to VoxelEditPacketSender --- interface/src/Application.cpp | 31 +++++++++++++++++++ interface/src/Application.h | 2 ++ libraries/networking/src/DomainHandler.cpp | 23 +------------- libraries/networking/src/DomainHandler.h | 10 +----- .../voxels/src/VoxelEditPacketSender.cpp | 7 ++--- libraries/voxels/src/VoxelEditPacketSender.h | 7 +++++ 6 files changed, 44 insertions(+), 36 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9b54b84351..480f215d40 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3788,6 +3788,37 @@ void Application::uploadAttachment() { uploadModel(ATTACHMENT_MODEL); } +void Application::domainSettingsReceived(const QJsonObject& domainSettingsObject) { + // from the domain-handler, figure out the satoshi cost per voxel and per meter cubed + const QString VOXEL_SETTINGS_KEY = "voxels"; + const QString PER_VOXEL_COST_KEY = "per-voxel-credits"; + const QString PER_METER_CUBED_COST_KEY = "per-meter-cubed-credits"; + const QString VOXEL_WALLET_UUID = "voxel-wallet"; + + const QJsonObject& voxelObject = domainSettingsObject[VOXEL_SETTINGS_KEY].toObject(); + + qint64 satoshisPerVoxel = 0; + qint64 satoshisPerMeterCubed = 0; + QUuid voxelWalletUUID; + + if (domainSettingsObject.isEmpty()) { + float perVoxelCredits = (float) voxelObject[PER_VOXEL_COST_KEY].toDouble(); + float perMeterCubedCredits = (float) voxelObject[PER_METER_CUBED_COST_KEY].toDouble(); + + satoshisPerVoxel = (qint64) floorf(perVoxelCredits * SATOSHIS_PER_CREDIT); + satoshisPerMeterCubed = (qint64) floorf(perMeterCubedCredits * SATOSHIS_PER_CREDIT); + + voxelWalletUUID = QUuid(voxelObject[VOXEL_WALLET_UUID].toString()); + } + + qDebug() << "Voxel costs are" << satoshisPerVoxel << "per voxel and" << satoshisPerMeterCubed << "per meter cubed"; + qDebug() << "Destination wallet UUID for voxel payments is" << voxelWalletUUID; + + _voxelEditSender.setSatoshisPerVoxel(satoshisPerVoxel); + _voxelEditSender.setSatoshisPerMeterCubed(satoshisPerMeterCubed); + _voxelEditSender.setDestinationWalletUUID(voxelWalletUUID); +} + QString Application::getPreviousScriptLocation() { QString suggestedName; if (_previousScriptLocation.isEmpty()) { diff --git a/interface/src/Application.h b/interface/src/Application.h index a356b26725..54fb25839a 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -342,6 +342,8 @@ public slots: void uploadAttachment(); void bumpSettings() { ++_numChangedSettings; } + + void domainSettingsReceived(const QJsonObject& domainSettingsObject); private slots: void timer(); diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index 5f20bebd50..1bcf03f477 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -159,9 +159,7 @@ void DomainHandler::settingsRequestFinished() { _settingsObject = QJsonDocument::fromJson(settingsReply->readAll()).object(); qDebug() << "Received domain settings."; - emit settingsReceived(); - - updateVoxelCosts(); + emit settingsReceived(_settingsObject); // reset failed settings requests to 0, we got them _failedSettingsRequests = 0; @@ -182,25 +180,6 @@ void DomainHandler::settingsRequestFinished() { } } -void DomainHandler::updateVoxelCosts() { - - // from the domain-handler, figure out the satoshi cost per voxel and per meter cubed - const QString VOXEL_SETTINGS_KEY = "voxels"; - const QString PER_VOXEL_COST_KEY = "per-voxel-credits"; - const QString PER_METER_CUBED_COST_KEY = "per-meter-cubed-credits"; - - if (!_settingsObject.isEmpty()) { - float perVoxelCredits = (float) _settingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_VOXEL_COST_KEY].toDouble(); - float perMeterCubedCredits = (float) _settingsObject[VOXEL_SETTINGS_KEY].toObject()[PER_METER_CUBED_COST_KEY].toDouble(); - - _satoshisPerVoxel = (qint64) floorf(perVoxelCredits * SATOSHIS_PER_CREDIT); - _satoshisPerMeterCubed = (qint64) floorf(perMeterCubedCredits * SATOSHIS_PER_CREDIT); - } else { - _satoshisPerVoxel = 0; - _satoshisPerMeterCubed = 0; - } -} - 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 7b7a80b49a..a2b8e2e307 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -62,9 +62,6 @@ public: void parseDTLSRequirementPacket(const QByteArray& dtlsRequirementPacket); - qint64 getSatoshisPerVoxel() const { return _satoshisPerVoxel; } - qint64 getSatoshisPerMeterCubed() const { return _satoshisPerMeterCubed; } - private slots: void completedHostnameLookup(const QHostInfo& hostInfo); void settingsRequestFinished(); @@ -72,14 +69,12 @@ signals: void hostnameChanged(const QString& hostname); void connectedToDomain(const QString& hostname); - void settingsReceived(); + void settingsReceived(const QJsonObject& domainSettingsObject); void settingsReceiveFail(); private: void reset(); - void updateVoxelCosts(); - QUuid _uuid; QString _hostname; HifiSockAddr _sockAddr; @@ -88,9 +83,6 @@ private: QTimer* _handshakeTimer; QJsonObject _settingsObject; int _failedSettingsRequests; - - qint64 _satoshisPerVoxel; - qint64 _satoshisPerMeterCubed; }; #endif // hifi_DomainHandler_h diff --git a/libraries/voxels/src/VoxelEditPacketSender.cpp b/libraries/voxels/src/VoxelEditPacketSender.cpp index ee189f492d..3bb76d17fe 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.cpp +++ b/libraries/voxels/src/VoxelEditPacketSender.cpp @@ -146,15 +146,12 @@ void VoxelEditPacketSender::queueVoxelEditMessages(PacketType type, int numberOf qint64 VoxelEditPacketSender::satoshiCostForMessage(const VoxelDetail& details) { const DomainHandler& domainHandler = NodeList::getInstance()->getDomainHandler(); - qint64 totalSatoshiCost = domainHandler.getSatoshisPerVoxel(); - qint64 costPerMeterCubed = domainHandler.getSatoshisPerMeterCubed(); - - if (totalSatoshiCost == 0 && costPerMeterCubed == 0) { + if (_satoshisPerVoxel == 0 && _satoshisPerMeterCubed == 0) { return 0; } else { float meterScale = details.s * TREE_SCALE; float totalVolume = meterScale * meterScale * meterScale; - return totalSatoshiCost + (qint64) floorf(totalVolume * costPerMeterCubed); + return _satoshisPerVoxel + (qint64) floorf(totalVolume * _satoshisPerMeterCubed); } } diff --git a/libraries/voxels/src/VoxelEditPacketSender.h b/libraries/voxels/src/VoxelEditPacketSender.h index e761812629..560c585ed5 100644 --- a/libraries/voxels/src/VoxelEditPacketSender.h +++ b/libraries/voxels/src/VoxelEditPacketSender.h @@ -51,6 +51,13 @@ public: // My server type is the voxel server virtual char getMyNodeType() const { return NodeType::VoxelServer; } + void setSatoshisPerVoxel(qint64 satoshisPerVoxel) { _satoshisPerVoxel = satoshisPerVoxel; } + void setSatoshisPerMeterCubed(qint64 satoshisPerMeterCubed) { _satoshisPerMeterCubed = satoshisPerMeterCubed; } + qint64 satoshiCostForMessage(const VoxelDetail& details); + +private: + qint64 _satoshisPerVoxel; + qint64 _satoshisPerMeterCubed; }; #endif // hifi_VoxelEditPacketSender_h From 99daa062c017cdc691feae5310b686dd1e902855 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 17:35:33 -0700 Subject: [PATCH 32/41] trigger domainSettingsReceived slot when connected to domain --- interface/src/Application.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 480f215d40..be8e9c240e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -240,6 +240,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : connect(&nodeList->getDomainHandler(), SIGNAL(hostnameChanged(const QString&)), SLOT(domainChanged(const QString&))); connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain(const QString&)), SLOT(connectedToDomain(const QString&))); + connect(&nodeList->getDomainHandler(), &DomainHandler::settingsReceived, &Application::domainSettingsReceived); // update our location every 5 seconds in the data-server, assuming that we are authenticated with one const qint64 DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS = 5 * 1000; From 9c1dd7c4a4ce7b96fc6ebf01edf1f6a46a957873 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 17:35:49 -0700 Subject: [PATCH 33/41] fix call to connect in Application --- interface/src/Application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index be8e9c240e..d30cd63105 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -240,7 +240,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : connect(&nodeList->getDomainHandler(), SIGNAL(hostnameChanged(const QString&)), SLOT(domainChanged(const QString&))); connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain(const QString&)), SLOT(connectedToDomain(const QString&))); - connect(&nodeList->getDomainHandler(), &DomainHandler::settingsReceived, &Application::domainSettingsReceived); + connect(&nodeList->getDomainHandler(), &DomainHandler::settingsReceived, this, &Application::domainSettingsReceived); // update our location every 5 seconds in the data-server, assuming that we are authenticated with one const qint64 DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS = 5 * 1000; From db25cd9d2c6139674e9066f81e54725f6223a0d7 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 18:07:08 -0700 Subject: [PATCH 34/41] call PaymentManager when an OctreeEditPacketSender needs to pay --- interface/src/Application.cpp | 17 ++++++++++--- interface/src/PaymentManager.cpp | 24 ++++++++++++++++++ interface/src/PaymentManager.h | 25 +++++++++++++++++++ libraries/octree/src/OctreeEditPacketSender.h | 2 +- 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 interface/src/PaymentManager.cpp create mode 100644 interface/src/PaymentManager.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index d30cd63105..a099231654 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -69,6 +69,7 @@ #include "InterfaceVersion.h" #include "Menu.h" #include "ModelUploader.h" +#include "PaymentManager.h" #include "Util.h" #include "devices/MIDIManager.h" #include "devices/OculusManager.h" @@ -237,10 +238,17 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : connect(audioThread, SIGNAL(started()), &_audio, SLOT(start())); audioThread->start(); + + const DomainHandler& domainHandler = nodeList->getDomainHandler(); - connect(&nodeList->getDomainHandler(), SIGNAL(hostnameChanged(const QString&)), SLOT(domainChanged(const QString&))); - connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain(const QString&)), SLOT(connectedToDomain(const QString&))); - connect(&nodeList->getDomainHandler(), &DomainHandler::settingsReceived, this, &Application::domainSettingsReceived); + connect(&domainHandler, SIGNAL(hostnameChanged(const QString&)), SLOT(domainChanged(const QString&))); + connect(&domainHandler, SIGNAL(connectedToDomain(const QString&)), SLOT(connectedToDomain(const QString&))); + connect(&domainHandler, &DomainHandler::settingsReceived, this, &Application::domainSettingsReceived); + + // hookup VoxelEditSender to PaymentManager so we can pay for octree edits + const PaymentManager& paymentManager = PaymentManager::getInstance(); + connect(&_voxelEditSender, &VoxelEditPacketSender::octreePaymentRequired, + &paymentManager, &PaymentManager::sendSignedPayment); // update our location every 5 seconds in the data-server, assuming that we are authenticated with one const qint64 DATA_SERVER_LOCATION_CHANGE_UPDATE_MSECS = 5 * 1000; @@ -3790,6 +3798,7 @@ void Application::uploadAttachment() { } void Application::domainSettingsReceived(const QJsonObject& domainSettingsObject) { + // from the domain-handler, figure out the satoshi cost per voxel and per meter cubed const QString VOXEL_SETTINGS_KEY = "voxels"; const QString PER_VOXEL_COST_KEY = "per-voxel-credits"; @@ -3802,7 +3811,7 @@ void Application::domainSettingsReceived(const QJsonObject& domainSettingsObject qint64 satoshisPerMeterCubed = 0; QUuid voxelWalletUUID; - if (domainSettingsObject.isEmpty()) { + if (!domainSettingsObject.isEmpty()) { float perVoxelCredits = (float) voxelObject[PER_VOXEL_COST_KEY].toDouble(); float perMeterCubedCredits = (float) voxelObject[PER_METER_CUBED_COST_KEY].toDouble(); diff --git a/interface/src/PaymentManager.cpp b/interface/src/PaymentManager.cpp new file mode 100644 index 0000000000..f4d23fca30 --- /dev/null +++ b/interface/src/PaymentManager.cpp @@ -0,0 +1,24 @@ +// +// PaymentManager.cpp +// interface/src +// +// Created by Stephen Birarda on 2014-07-30. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include + +#include "PaymentManager.h" + +PaymentManager& PaymentManager::getInstance() { + static PaymentManager sharedInstance; + return sharedInstance; +} + +void PaymentManager::sendSignedPayment(qint64 satoshiAmount, const QUuid& nodeUUID, const QUuid& destinationWalletUUID) { + qDebug() << "Paying" << satoshiAmount << "satoshis to" << destinationWalletUUID << "via" << nodeUUID; +} \ No newline at end of file diff --git a/interface/src/PaymentManager.h b/interface/src/PaymentManager.h new file mode 100644 index 0000000000..67419a39a4 --- /dev/null +++ b/interface/src/PaymentManager.h @@ -0,0 +1,25 @@ +// +// PaymentManager.h +// interface/src +// +// Created by Stephen Birarda on 2014-07-30. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_PaymentManager_h +#define hifi_PaymentManager_h + +#include + +class PaymentManager : public QObject { + Q_OBJECT +public: + static PaymentManager& getInstance(); +public slots: + void sendSignedPayment(qint64 satoshiAmount, const QUuid& nodeUUID, const QUuid& destinationWalletUUID); +}; + +#endif // hifi_PaymentManager_h \ No newline at end of file diff --git a/libraries/octree/src/OctreeEditPacketSender.h b/libraries/octree/src/OctreeEditPacketSender.h index 7b39a1d785..a11c626003 100644 --- a/libraries/octree/src/OctreeEditPacketSender.h +++ b/libraries/octree/src/OctreeEditPacketSender.h @@ -83,7 +83,7 @@ public: virtual char getMyNodeType() const = 0; virtual void adjustEditPacketForClockSkew(unsigned char* codeColorBuffer, ssize_t length, int clockSkew) { }; - bool hasDestinationWalletUUID() const { return _destinationWalletUUID.isNull(); } + bool hasDestinationWalletUUID() const { return !_destinationWalletUUID.isNull(); } void setDestinationWalletUUID(const QUuid& destinationWalletUUID) { _destinationWalletUUID = destinationWalletUUID; } const QUuid& getDestinationWalletUUID() { return _destinationWalletUUID; } From ef58453fda85edb51eeb1db4929dd3f228e5b06f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 18:10:11 -0700 Subject: [PATCH 35/41] turn off verbose octree packet debug --- interface/src/PaymentManager.cpp | 2 ++ libraries/octree/src/OctreeEditPacketSender.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/interface/src/PaymentManager.cpp b/interface/src/PaymentManager.cpp index f4d23fca30..4baf11386b 100644 --- a/interface/src/PaymentManager.cpp +++ b/interface/src/PaymentManager.cpp @@ -21,4 +21,6 @@ PaymentManager& PaymentManager::getInstance() { void PaymentManager::sendSignedPayment(qint64 satoshiAmount, const QUuid& nodeUUID, const QUuid& destinationWalletUUID) { qDebug() << "Paying" << satoshiAmount << "satoshis to" << destinationWalletUUID << "via" << nodeUUID; + + } \ No newline at end of file diff --git a/libraries/octree/src/OctreeEditPacketSender.cpp b/libraries/octree/src/OctreeEditPacketSender.cpp index 86208c9fd2..543f47273c 100644 --- a/libraries/octree/src/OctreeEditPacketSender.cpp +++ b/libraries/octree/src/OctreeEditPacketSender.cpp @@ -112,7 +112,7 @@ void OctreeEditPacketSender::queuePacketToNode(const QUuid& nodeUUID, unsigned c _sentPacketHistories[nodeUUID].packetSent(sequence, packet); // debugging output... - bool wantDebugging = true; + bool wantDebugging = false; if (wantDebugging) { int numBytesPacketHeader = numBytesForPacketHeader(reinterpret_cast(buffer)); unsigned short int sequence = (*((unsigned short int*)(buffer + numBytesPacketHeader))); From 631977bac2c72b24662ea204c49e47a73087f5a2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 30 Jul 2014 18:15:20 -0700 Subject: [PATCH 36/41] setup a SignedTransaction from PaymentManager --- interface/src/PaymentManager.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/interface/src/PaymentManager.cpp b/interface/src/PaymentManager.cpp index 4baf11386b..b8af43ec67 100644 --- a/interface/src/PaymentManager.cpp +++ b/interface/src/PaymentManager.cpp @@ -9,9 +9,12 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include #include #include +#include "SignedWalletTransaction.h" + #include "PaymentManager.h" PaymentManager& PaymentManager::getInstance() { @@ -22,5 +25,11 @@ PaymentManager& PaymentManager::getInstance() { void PaymentManager::sendSignedPayment(qint64 satoshiAmount, const QUuid& nodeUUID, const QUuid& destinationWalletUUID) { qDebug() << "Paying" << satoshiAmount << "satoshis to" << destinationWalletUUID << "via" << nodeUUID; + // setup a signed wallet transaction + const qint64 DEFAULT_TRANSACTION_EXPIRY_SECONDS = 60; + qint64 currentTimestamp = QDateTime::currentDateTimeUtc().toTime_t(); + SignedWalletTransaction newTransaction(destinationWalletUUID, satoshiAmount, + currentTimestamp, DEFAULT_TRANSACTION_EXPIRY_SECONDS); + // send the signed transaction to the redeeming node } \ No newline at end of file From 350c75961801fe6a0132ed86e064ecc1c7a73f5c Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 31 Jul 2014 09:18:57 -0700 Subject: [PATCH 37/41] add a method to SignedWalletTransaction to return binary message --- interface/src/SignedWalletTransaction.cpp | 8 ++++++-- interface/src/SignedWalletTransaction.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/interface/src/SignedWalletTransaction.cpp b/interface/src/SignedWalletTransaction.cpp index d29207e4f5..8a0d4e6fdb 100644 --- a/interface/src/SignedWalletTransaction.cpp +++ b/interface/src/SignedWalletTransaction.cpp @@ -32,7 +32,7 @@ SignedWalletTransaction::SignedWalletTransaction(const QUuid& destinationUUID, q } -QByteArray SignedWalletTransaction::hexMessage() { +QByteArray SignedWalletTransaction::binaryMessage() { // build the message using the components of this transaction // UUID, source UUID, destination UUID, message timestamp, expiry delta, amount @@ -49,7 +49,11 @@ QByteArray SignedWalletTransaction::hexMessage() { messageBinary.append(reinterpret_cast(&_amount), sizeof(_amount)); - return messageBinary.toHex(); + return messageBinary; +} + +QByteArray SignedWalletTransaction::hexMessage() { + return binaryMessage().toHex(); } QByteArray SignedWalletTransaction::messageDigest() { diff --git a/interface/src/SignedWalletTransaction.h b/interface/src/SignedWalletTransaction.h index 3b13f73335..6bc66a535e 100644 --- a/interface/src/SignedWalletTransaction.h +++ b/interface/src/SignedWalletTransaction.h @@ -19,6 +19,7 @@ class SignedWalletTransaction : public WalletTransaction { public: SignedWalletTransaction(const QUuid& destinationUUID, qint64 amount, qint64 messageTimestamp, qint64 expiryDelta); + QByteArray binaryMessage(); QByteArray hexMessage(); QByteArray messageDigest(); QByteArray signedMessageDigest(); From ce4336894a1fed5172dda18071334fa200023b75 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 31 Jul 2014 09:26:18 -0700 Subject: [PATCH 38/41] actually send signed transaction from PaymentManager --- interface/src/PaymentManager.cpp | 15 ++++++++++++++- interface/src/SignedWalletTransaction.h | 2 ++ libraries/networking/src/PacketHeaders.h | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/interface/src/PaymentManager.cpp b/interface/src/PaymentManager.cpp index b8af43ec67..84f5e96fc9 100644 --- a/interface/src/PaymentManager.cpp +++ b/interface/src/PaymentManager.cpp @@ -13,6 +13,9 @@ #include #include +#include +#include + #include "SignedWalletTransaction.h" #include "PaymentManager.h" @@ -23,7 +26,6 @@ PaymentManager& PaymentManager::getInstance() { } void PaymentManager::sendSignedPayment(qint64 satoshiAmount, const QUuid& nodeUUID, const QUuid& destinationWalletUUID) { - qDebug() << "Paying" << satoshiAmount << "satoshis to" << destinationWalletUUID << "via" << nodeUUID; // setup a signed wallet transaction const qint64 DEFAULT_TRANSACTION_EXPIRY_SECONDS = 60; @@ -32,4 +34,15 @@ void PaymentManager::sendSignedPayment(qint64 satoshiAmount, const QUuid& nodeUU currentTimestamp, DEFAULT_TRANSACTION_EXPIRY_SECONDS); // send the signed transaction to the redeeming node + QByteArray transactionByteArray = byteArrayWithPopulatedHeader(PacketTypeSignedTransactionPayment); + + // append the binary message and the signed message digest + transactionByteArray.append(newTransaction.binaryMessage()); + transactionByteArray.append(newTransaction.signedMessageDigest()); + + qDebug() << "Paying" << satoshiAmount << "satoshis to" << destinationWalletUUID << "via" << nodeUUID; + + // use the NodeList to send that to the right node + NodeList* nodeList = NodeList::getInstance(); + nodeList->writeDatagram(transactionByteArray, nodeList->nodeWithUUID(nodeUUID)); } \ No newline at end of file diff --git a/interface/src/SignedWalletTransaction.h b/interface/src/SignedWalletTransaction.h index 6bc66a535e..b3b1620dac 100644 --- a/interface/src/SignedWalletTransaction.h +++ b/interface/src/SignedWalletTransaction.h @@ -14,6 +14,8 @@ #include +const int NUM_BYTES_SIGNED_TRANSACTION_MESSAGE = 72; + class SignedWalletTransaction : public WalletTransaction { Q_OBJECT public: diff --git a/libraries/networking/src/PacketHeaders.h b/libraries/networking/src/PacketHeaders.h index 83350a32d1..436c34e6eb 100644 --- a/libraries/networking/src/PacketHeaders.h +++ b/libraries/networking/src/PacketHeaders.h @@ -70,6 +70,7 @@ enum PacketType { PacketTypeVoxelEditNack, PacketTypeParticleEditNack, PacketTypeModelEditNack, + PacketTypeSignedTransactionPayment }; typedef char PacketVersion; From eadae8ed1ae224439e57586c69b7dcca8bbe8e2d Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 31 Jul 2014 10:05:26 -0700 Subject: [PATCH 39/41] allow unauthenticated redemptions of signed transactions from octree server --- assignment-client/src/octree/OctreeServer.cpp | 52 ++++++ assignment-client/src/octree/OctreeServer.h | 3 + interface/src/SignedWalletTransaction.h | 2 - libraries/networking/src/AccountManager.cpp | 155 ++++++++++-------- libraries/networking/src/AccountManager.h | 10 +- 5 files changed, 154 insertions(+), 68 deletions(-) diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index 551bed795d..42dc9a047d 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -9,11 +9,14 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include #include #include #include #include + +#include #include #include #include @@ -244,6 +247,10 @@ OctreeServer::OctreeServer(const QByteArray& packet) : _averageLoopTime.updateAverage(0); qDebug() << "Octree server starting... [" << this << "]"; + + // make sure the AccountManager has an Auth URL for payment redemptions + + AccountManager::getInstance().setAuthURL(DEFAULT_NODE_AUTH_URL); } OctreeServer::~OctreeServer() { @@ -857,6 +864,8 @@ void OctreeServer::readPendingDatagrams() { } } else if (packetType == PacketTypeJurisdictionRequest) { _jurisdictionSender->queueReceivedPacket(matchingNode, receivedPacket); + } else if (packetType == PacketTypeSignedTransactionPayment) { + handleSignedTransactionPayment(packetType, receivedPacket); } else if (_octreeInboundPacketProcessor && getOctree()->handlesEditPacketType(packetType)) { _octreeInboundPacketProcessor->queueReceivedPacket(matchingNode, receivedPacket); } else { @@ -1204,6 +1213,49 @@ QString OctreeServer::getStatusLink() { return result; } +void OctreeServer::handleSignedTransactionPayment(PacketType packetType, const QByteArray& datagram) { + // for now we're not verifying that this is actual payment for any octree edits + // just use the AccountManager to send it up to the data server and have it redeemed + AccountManager& accountManager = AccountManager::getInstance(); + + const int NUM_BYTES_SIGNED_TRANSACTION_BINARY_MESSAGE = 72; + const int NUM_BYTES_SIGNED_TRANSACTION_BINARY_SIGNATURE = 256; + + int numBytesPacketHeader = numBytesForPacketHeaderGivenPacketType(packetType); + + // pull out the transaction message in binary + QByteArray messageHex = datagram.mid(numBytesPacketHeader, NUM_BYTES_SIGNED_TRANSACTION_BINARY_MESSAGE).toHex(); + // pull out the binary signed message digest + QByteArray signatureHex = datagram.mid(numBytesPacketHeader + NUM_BYTES_SIGNED_TRANSACTION_BINARY_MESSAGE, + NUM_BYTES_SIGNED_TRANSACTION_BINARY_SIGNATURE).toHex(); + + // setup the QJSONObject we are posting + QJsonObject postObject; + + const QString TRANSACTION_OBJECT_MESSAGE_KEY = "message"; + const QString TRANSACTION_OBJECT_SIGNATURE_KEY = "signature"; + const QString POST_OBJECT_TRANSACTION_KEY = "transaction"; + + QJsonObject transactionObject; + transactionObject.insert(TRANSACTION_OBJECT_MESSAGE_KEY, QString(messageHex)); + transactionObject.insert(TRANSACTION_OBJECT_SIGNATURE_KEY, QString(signatureHex)); + + postObject.insert(POST_OBJECT_TRANSACTION_KEY, transactionObject); + + // setup our callback params + JSONCallbackParameters callbackParameters; + callbackParameters.jsonCallbackReceiver = this; + callbackParameters.jsonCallbackMethod = "handleSignedTransactionPaymentResponse"; + + accountManager.unauthenticatedRequest("/api/v1/transactions/redeem", QNetworkAccessManager::PostOperation, + callbackParameters, QJsonDocument(postObject).toJson()); + +} + +void OctreeServer::handleSignedTransactionPaymentResponse(const QJsonObject& jsonObject) { + qDebug() << "STP response:" << jsonObject; +} + void OctreeServer::sendStatsPacket() { // TODO: we have too many stats to fit in a single MTU... so for now, we break it into multiple JSON objects and // send them separately. What we really should do is change the NodeList::sendStatsToDomainServer() to handle the diff --git a/assignment-client/src/octree/OctreeServer.h b/assignment-client/src/octree/OctreeServer.h index 76b39c5771..d00af41d5e 100644 --- a/assignment-client/src/octree/OctreeServer.h +++ b/assignment-client/src/octree/OctreeServer.h @@ -137,6 +137,9 @@ protected: QString getConfiguration(); QString getStatusLink(); + void handleSignedTransactionPayment(PacketType packetType, const QByteArray& datagram); + void handleSignedTransactionPaymentResponse(const QJsonObject& jsonObject); + int _argc; const char** _argv; char** _parsedArgV; diff --git a/interface/src/SignedWalletTransaction.h b/interface/src/SignedWalletTransaction.h index b3b1620dac..6bc66a535e 100644 --- a/interface/src/SignedWalletTransaction.h +++ b/interface/src/SignedWalletTransaction.h @@ -14,8 +14,6 @@ #include -const int NUM_BYTES_SIGNED_TRANSACTION_MESSAGE = 72; - class SignedWalletTransaction : public WalletTransaction { Q_OBJECT public: diff --git a/libraries/networking/src/AccountManager.cpp b/libraries/networking/src/AccountManager.cpp index 563d735790..68af384007 100644 --- a/libraries/networking/src/AccountManager.cpp +++ b/libraries/networking/src/AccountManager.cpp @@ -15,15 +15,15 @@ #include #include #include +#include #include -#include #include "NodeList.h" #include "PacketHeaders.h" #include "AccountManager.h" -const bool VERBOSE_HTTP_REQUEST_DEBUGGING = false; +const bool VERBOSE_HTTP_REQUEST_DEBUGGING = true; AccountManager& AccountManager::getInstance() { static AccountManager sharedInstance; @@ -62,6 +62,8 @@ AccountManager::AccountManager() : qRegisterMetaType("QNetworkAccessManager::Operation"); qRegisterMetaType("JSONCallbackParameters"); + + qRegisterMetaType("QHttpMultiPart*"); connect(&_accountInfo, &DataServerAccountInfo::balanceChanged, this, &AccountManager::accountInfoBalanceChanged); } @@ -142,90 +144,113 @@ void AccountManager::authenticatedRequest(const QString& path, QNetworkAccessMan const JSONCallbackParameters& callbackParams, const QByteArray& dataByteArray, QHttpMultiPart* dataMultiPart) { + QMetaObject::invokeMethod(this, "invokedRequest", Q_ARG(const QString&, path), + Q_ARG(bool, true), Q_ARG(QNetworkAccessManager::Operation, operation), Q_ARG(const JSONCallbackParameters&, callbackParams), Q_ARG(const QByteArray&, dataByteArray), Q_ARG(QHttpMultiPart*, dataMultiPart)); } -void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager::Operation operation, +void AccountManager::unauthenticatedRequest(const QString& path, QNetworkAccessManager::Operation operation, + const JSONCallbackParameters& callbackParams, + const QByteArray& dataByteArray, + QHttpMultiPart* dataMultiPart) { + + QMetaObject::invokeMethod(this, "invokedRequest", + Q_ARG(const QString&, path), + Q_ARG(bool, false), + Q_ARG(QNetworkAccessManager::Operation, operation), + Q_ARG(const JSONCallbackParameters&, callbackParams), + Q_ARG(const QByteArray&, dataByteArray), + Q_ARG(QHttpMultiPart*, dataMultiPart)); +} + +void AccountManager::invokedRequest(const QString& path, + bool requiresAuthentication, + QNetworkAccessManager::Operation operation, const JSONCallbackParameters& callbackParams, const QByteArray& dataByteArray, QHttpMultiPart* dataMultiPart) { NetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); - if (hasValidAccessToken()) { - QNetworkRequest authenticatedRequest; - - QUrl requestURL = _authURL; - - if (path.startsWith("/")) { - requestURL.setPath(path); + QNetworkRequest networkRequest; + + QUrl requestURL = _authURL; + + if (path.startsWith("/")) { + requestURL.setPath(path); + } else { + requestURL.setPath("/" + path); + } + + if (requiresAuthentication) { + if (hasValidAccessToken()) { + requestURL.setQuery("access_token=" + _accountInfo.getAccessToken().token); } else { - requestURL.setPath("/" + path); + qDebug() << "No valid access token present. Bailing on authenticated invoked request."; + return; } - - requestURL.setQuery("access_token=" + _accountInfo.getAccessToken().token); - - authenticatedRequest.setUrl(requestURL); - - if (VERBOSE_HTTP_REQUEST_DEBUGGING) { - qDebug() << "Making an authenticated request to" << qPrintable(requestURL.toString()); - - if (!dataByteArray.isEmpty()) { - qDebug() << "The POST/PUT body -" << QString(dataByteArray); - } + } + + networkRequest.setUrl(requestURL); + + if (VERBOSE_HTTP_REQUEST_DEBUGGING) { + qDebug() << "Making an authenticated request to" << qPrintable(requestURL.toString()); + + if (!dataByteArray.isEmpty()) { + qDebug() << "The POST/PUT body -" << QString(dataByteArray); } - - QNetworkReply* networkReply = NULL; - - switch (operation) { - case QNetworkAccessManager::GetOperation: - networkReply = networkAccessManager.get(authenticatedRequest); - break; - case QNetworkAccessManager::PostOperation: - case QNetworkAccessManager::PutOperation: - if (dataMultiPart) { - if (operation == QNetworkAccessManager::PostOperation) { - networkReply = networkAccessManager.post(authenticatedRequest, dataMultiPart); - } else { - networkReply = networkAccessManager.put(authenticatedRequest, dataMultiPart); - } - dataMultiPart->setParent(networkReply); + } + + QNetworkReply* networkReply = NULL; + + switch (operation) { + case QNetworkAccessManager::GetOperation: + networkReply = networkAccessManager.get(networkRequest); + break; + case QNetworkAccessManager::PostOperation: + case QNetworkAccessManager::PutOperation: + if (dataMultiPart) { + if (operation == QNetworkAccessManager::PostOperation) { + networkReply = networkAccessManager.post(networkRequest, dataMultiPart); } else { - authenticatedRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); - if (operation == QNetworkAccessManager::PostOperation) { - networkReply = networkAccessManager.post(authenticatedRequest, dataByteArray); - } else { - networkReply = networkAccessManager.put(authenticatedRequest, dataByteArray); - } + networkReply = networkAccessManager.put(networkRequest, dataMultiPart); } - - break; - case QNetworkAccessManager::DeleteOperation: - networkReply = networkAccessManager.sendCustomRequest(authenticatedRequest, "DELETE"); - break; - default: - // other methods not yet handled - break; - } - - if (networkReply) { - if (!callbackParams.isEmpty()) { - // if we have information for a callback, insert the callbackParams into our local map - _pendingCallbackMap.insert(networkReply, callbackParams); - - if (callbackParams.updateReciever && !callbackParams.updateSlot.isEmpty()) { - callbackParams.updateReciever->connect(networkReply, SIGNAL(uploadProgress(qint64, qint64)), - callbackParams.updateSlot.toStdString().c_str()); + dataMultiPart->setParent(networkReply); + } else { + networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); + if (operation == QNetworkAccessManager::PostOperation) { + networkReply = networkAccessManager.post(networkRequest, dataByteArray); + } else { + networkReply = networkAccessManager.put(networkRequest, dataByteArray); } } - - // if we ended up firing of a request, hook up to it now - connect(networkReply, SIGNAL(finished()), SLOT(processReply())); + + break; + case QNetworkAccessManager::DeleteOperation: + networkReply = networkAccessManager.sendCustomRequest(networkRequest, "DELETE"); + break; + default: + // other methods not yet handled + break; + } + + if (networkReply) { + if (!callbackParams.isEmpty()) { + // if we have information for a callback, insert the callbackParams into our local map + _pendingCallbackMap.insert(networkReply, callbackParams); + + if (callbackParams.updateReciever && !callbackParams.updateSlot.isEmpty()) { + callbackParams.updateReciever->connect(networkReply, SIGNAL(uploadProgress(qint64, qint64)), + callbackParams.updateSlot.toStdString().c_str()); + } } + + // if we ended up firing of a request, hook up to it now + connect(networkReply, SIGNAL(finished()), SLOT(processReply())); } } diff --git a/libraries/networking/src/AccountManager.h b/libraries/networking/src/AccountManager.h index 49a39c1a22..64d62cd1c2 100644 --- a/libraries/networking/src/AccountManager.h +++ b/libraries/networking/src/AccountManager.h @@ -47,6 +47,12 @@ public: const JSONCallbackParameters& callbackParams = JSONCallbackParameters(), const QByteArray& dataByteArray = QByteArray(), QHttpMultiPart* dataMultiPart = NULL); + + void unauthenticatedRequest(const QString& path, + QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, + const JSONCallbackParameters& callbackParams = JSONCallbackParameters(), + const QByteArray& dataByteArray = QByteArray(), + QHttpMultiPart* dataMultiPart = NULL); const QUrl& getAuthURL() const { return _authURL; } void setAuthURL(const QUrl& authURL); @@ -88,7 +94,9 @@ private: void passSuccessToCallback(QNetworkReply* reply); void passErrorToCallback(QNetworkReply* reply); - Q_INVOKABLE void invokedRequest(const QString& path, QNetworkAccessManager::Operation operation, + Q_INVOKABLE void invokedRequest(const QString& path, + bool requiresAuthentication, + QNetworkAccessManager::Operation operation, const JSONCallbackParameters& callbackParams, const QByteArray& dataByteArray, QHttpMultiPart* dataMultiPart); From e1ba076a711cbe3dc1397f99054cdb2596d795fe Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 31 Jul 2014 11:03:12 -0700 Subject: [PATCH 40/41] complete redemption of signed transactions from octree server --- assignment-client/src/octree/OctreeServer.cpp | 4 +++- assignment-client/src/octree/OctreeServer.h | 3 ++- libraries/networking/src/AccountManager.cpp | 5 +++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/assignment-client/src/octree/OctreeServer.cpp b/assignment-client/src/octree/OctreeServer.cpp index 42dc9a047d..02fb26c09f 100644 --- a/assignment-client/src/octree/OctreeServer.cpp +++ b/assignment-client/src/octree/OctreeServer.cpp @@ -1253,7 +1253,9 @@ void OctreeServer::handleSignedTransactionPayment(PacketType packetType, const Q } void OctreeServer::handleSignedTransactionPaymentResponse(const QJsonObject& jsonObject) { - qDebug() << "STP response:" << jsonObject; + // pull the ID to debug the transaction + QString transactionIDString = jsonObject["data"].toObject()["transaction"].toObject()["id"].toString(); + qDebug() << "Redeemed transaction with ID" << transactionIDString << "successfully."; } void OctreeServer::sendStatsPacket() { diff --git a/assignment-client/src/octree/OctreeServer.h b/assignment-client/src/octree/OctreeServer.h index d00af41d5e..c904b3d86c 100644 --- a/assignment-client/src/octree/OctreeServer.h +++ b/assignment-client/src/octree/OctreeServer.h @@ -127,6 +127,8 @@ public slots: void nodeAdded(SharedNodePointer node); void nodeKilled(SharedNodePointer node); void sendStatsPacket(); + + void handleSignedTransactionPaymentResponse(const QJsonObject& jsonObject); protected: void parsePayload(); @@ -138,7 +140,6 @@ protected: QString getStatusLink(); void handleSignedTransactionPayment(PacketType packetType, const QByteArray& datagram); - void handleSignedTransactionPaymentResponse(const QJsonObject& jsonObject); int _argc; const char** _argv; diff --git a/libraries/networking/src/AccountManager.cpp b/libraries/networking/src/AccountManager.cpp index 68af384007..7fda9d74c9 100644 --- a/libraries/networking/src/AccountManager.cpp +++ b/libraries/networking/src/AccountManager.cpp @@ -23,7 +23,7 @@ #include "AccountManager.h" -const bool VERBOSE_HTTP_REQUEST_DEBUGGING = true; +const bool VERBOSE_HTTP_REQUEST_DEBUGGING = false; AccountManager& AccountManager::getInstance() { static AccountManager sharedInstance; @@ -198,7 +198,7 @@ void AccountManager::invokedRequest(const QString& path, networkRequest.setUrl(requestURL); if (VERBOSE_HTTP_REQUEST_DEBUGGING) { - qDebug() << "Making an authenticated request to" << qPrintable(requestURL.toString()); + qDebug() << "Making a request to" << qPrintable(requestURL.toString()); if (!dataByteArray.isEmpty()) { qDebug() << "The POST/PUT body -" << QString(dataByteArray); @@ -301,6 +301,7 @@ void AccountManager::passErrorToCallback(QNetworkReply* requestReply) { if (VERBOSE_HTTP_REQUEST_DEBUGGING) { qDebug() << "Received error response from data-server that has no matching callback."; qDebug() << "Error" << requestReply->error() << "-" << requestReply->errorString(); + qDebug() << requestReply->readAll(); } } } From 3c459d8460854b20b8c11ea6e7e61805c004ab8a Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 31 Jul 2014 11:12:41 -0700 Subject: [PATCH 41/41] fix include of tgmath for windows --- libraries/networking/src/DomainHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index 1bcf03f477..786726b745 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -9,7 +9,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include +#include #include