Merge pull request #3083 from birarda/master

initial pass at settings in the domain-server passed down to audio-mixer
This commit is contained in:
Philip Rosedale 2014-06-27 09:26:01 -07:00
commit e47fb5750b
14 changed files with 497 additions and 68 deletions

View file

@ -213,8 +213,6 @@ void Agent::run() {
loop.exec(); loop.exec();
// let the AvatarData and ResourceCache classes use our QNetworkAccessManager // let the AvatarData and ResourceCache classes use our QNetworkAccessManager
AvatarData::setNetworkAccessManager(networkManager); AvatarData::setNetworkAccessManager(networkManager);
ResourceCache::setNetworkAccessManager(networkManager); ResourceCache::setNetworkAccessManager(networkManager);

View file

@ -38,6 +38,9 @@
#include <QtCore/QJsonObject> #include <QtCore/QJsonObject>
#include <QtCore/QJsonValue> #include <QtCore/QJsonValue>
#include <QtCore/QTimer> #include <QtCore/QTimer>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <Logging.h> #include <Logging.h>
#include <NodeList.h> #include <NodeList.h>
@ -478,40 +481,81 @@ void AudioMixer::run() {
nodeList->linkedDataCreateCallback = attachNewBufferToNode; nodeList->linkedDataCreateCallback = attachNewBufferToNode;
// check the payload to see if we have any unattenuated zones // setup a QNetworkAccessManager to ask the domain-server for our settings
const QString UNATTENUATED_ZONE_REGEX_STRING = "--unattenuated-zone ([\\d.,-]+)"; QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
QRegExp unattenuatedZoneMatch(UNATTENUATED_ZONE_REGEX_STRING);
if (unattenuatedZoneMatch.indexIn(_payload) != -1) { QUrl settingsJSONURL;
QString unattenuatedZoneString = unattenuatedZoneMatch.cap(1); settingsJSONURL.setScheme("http");
QStringList zoneStringList = unattenuatedZoneString.split(','); settingsJSONURL.setHost(nodeList->getDomainHandler().getHostname());
settingsJSONURL.setPort(DOMAIN_SERVER_HTTP_PORT);
settingsJSONURL.setPath("/settings.json");
settingsJSONURL.setQuery(QString("type=%1").arg(_type));
glm::vec3 sourceCorner(zoneStringList[0].toFloat(), zoneStringList[1].toFloat(), zoneStringList[2].toFloat()); QNetworkReply *reply = NULL;
glm::vec3 sourceDimensions(zoneStringList[3].toFloat(), zoneStringList[4].toFloat(), zoneStringList[5].toFloat());
glm::vec3 listenerCorner(zoneStringList[6].toFloat(), zoneStringList[7].toFloat(), zoneStringList[8].toFloat()); int failedAttempts = 0;
glm::vec3 listenerDimensions(zoneStringList[9].toFloat(), zoneStringList[10].toFloat(), zoneStringList[11].toFloat()); const int MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS = 5;
_sourceUnattenuatedZone = new AABox(sourceCorner, sourceDimensions); qDebug() << "Requesting settings for assignment from domain-server at" << settingsJSONURL.toString();
_listenerUnattenuatedZone = new AABox(listenerCorner, listenerDimensions);
glm::vec3 sourceCenter = _sourceUnattenuatedZone->calcCenter(); while (!reply || reply->error() != QNetworkReply::NoError) {
glm::vec3 destinationCenter = _listenerUnattenuatedZone->calcCenter(); reply = networkManager->get(QNetworkRequest(settingsJSONURL));
qDebug() << "There is an unattenuated zone with source center at" QEventLoop loop;
<< QString("%1, %2, %3").arg(sourceCenter.x).arg(sourceCenter.y).arg(sourceCenter.z); QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
qDebug() << "Buffers inside this zone will not be attenuated inside a box with center at"
<< QString("%1, %2, %3").arg(destinationCenter.x).arg(destinationCenter.y).arg(destinationCenter.z); loop.exec();
++failedAttempts;
if (failedAttempts == MAX_SETTINGS_REQUEST_FAILED_ATTEMPTS) {
qDebug() << "Failed to get settings from domain-server. Bailing on assignment.";
setFinished(true);
return;
}
} }
// check the payload to see if we have asked for dynamicJitterBuffer support QJsonObject settingsObject = QJsonDocument::fromJson(reply->readAll()).object();
const QString DYNAMIC_JITTER_BUFFER_REGEX_STRING = "--dynamicJitterBuffer";
QRegExp dynamicJitterBufferMatch(DYNAMIC_JITTER_BUFFER_REGEX_STRING); // check the settings object to see if we have anything we can parse out
if (dynamicJitterBufferMatch.indexIn(_payload) != -1) { const QString AUDIO_GROUP_KEY = "audio";
qDebug() << "Enable dynamic jitter buffers.";
_useDynamicJitterBuffers = true; if (settingsObject.contains(AUDIO_GROUP_KEY)) {
} else { QJsonObject audioGroupObject = settingsObject[AUDIO_GROUP_KEY].toObject();
qDebug() << "Dynamic jitter buffers disabled, using old behavior.";
const QString UNATTENUATED_ZONE_KEY = "unattenuated-zone";
QString unattenuatedZoneString = audioGroupObject[UNATTENUATED_ZONE_KEY].toString();
if (!unattenuatedZoneString.isEmpty()) {
QStringList zoneStringList = unattenuatedZoneString.split(',');
glm::vec3 sourceCorner(zoneStringList[0].toFloat(), zoneStringList[1].toFloat(), zoneStringList[2].toFloat());
glm::vec3 sourceDimensions(zoneStringList[3].toFloat(), zoneStringList[4].toFloat(), zoneStringList[5].toFloat());
glm::vec3 listenerCorner(zoneStringList[6].toFloat(), zoneStringList[7].toFloat(), zoneStringList[8].toFloat());
glm::vec3 listenerDimensions(zoneStringList[9].toFloat(), zoneStringList[10].toFloat(), zoneStringList[11].toFloat());
_sourceUnattenuatedZone = new AABox(sourceCorner, sourceDimensions);
_listenerUnattenuatedZone = new AABox(listenerCorner, listenerDimensions);
glm::vec3 sourceCenter = _sourceUnattenuatedZone->calcCenter();
glm::vec3 destinationCenter = _listenerUnattenuatedZone->calcCenter();
qDebug() << "There is an unattenuated zone with source center at"
<< QString("%1, %2, %3").arg(sourceCenter.x).arg(sourceCenter.y).arg(sourceCenter.z);
qDebug() << "Buffers inside this zone will not be attenuated inside a box with center at"
<< QString("%1, %2, %3").arg(destinationCenter.x).arg(destinationCenter.y).arg(destinationCenter.z);
}
// check the payload to see if we have asked for dynamicJitterBuffer support
const QString DYNAMIC_JITTER_BUFFER_JSON_KEY = "dynamic-jitter-buffer";
bool shouldUseDynamicJitterBuffers = audioGroupObject[DYNAMIC_JITTER_BUFFER_JSON_KEY].toBool();
if (shouldUseDynamicJitterBuffers) {
qDebug() << "Enable dynamic jitter buffers.";
_useDynamicJitterBuffers = true;
} else {
qDebug() << "Dynamic jitter buffers disabled, using old behavior.";
}
} }
int nextFrame = 0; int nextFrame = 0;

View file

@ -1,8 +1,8 @@
#nodes-lead { #nodes-lead, #settings-lead {
color: #66CCCC; color: #66CCCC;
} }
#nodes-lead .lead-line { #nodes-lead .lead-line, #settings-lead .lead-line {
background-color: #66CCCC; background-color: #66CCCC;
} }

View file

@ -18,7 +18,20 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
</tbody> <script id="nodes-template" type="text/template">
<% _.each(nodes, function(node, node_index){ %>
<tr>
<td><%- node.type %></td>
<td><a href="stats/?uuid=<%- node.uuid %>"><%- node.uuid %></a></td>
<td><%- node.pool %></td>
<td><%- node.public.ip %><span class='port'><%- node.public.port %></span></td>
<td><%- node.local.ip %><span class='port'><%- node.local.port %></span></td>
<td><%- ((Date.now() - node.wake_timestamp) / 1000).toLocaleString() %></td>
<td><%- (typeof node.pending_credits == 'number' ? node.pending_credits.toLocaleString() : 'N/A') %></td>
<td><span class='glyphicon glyphicon-remove' data-uuid="<%- node.uuid %>"></span></td>
</tr>
<% }); %>
</script>
</table> </table>
<div id="queued-lead" class="table-lead"><h3>Queued Assignments</h3><div class="lead-line"></div></div> <div id="queued-lead" class="table-lead"><h3>Queued Assignments</h3><div class="lead-line"></div></div>
@ -31,8 +44,18 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<script id="queued-template" type="text/template">
<% _.each(queued, function(assignment, uuid){ %>
<tr>
<td><%- assignment.type %></td>
<td><%- uuid %></td>
<td><%- assignment.pool %></td>
</tr>
<% }); %>
</script>
</tbody> </tbody>
</table> </table>
<!--#include file="footer.html"--> <!--#include file="footer.html"-->
<script src='js/tables.js'></script> <script src='js/tables.js'></script>
<script src='js/underscore-1.5.0.min.js'></script>
<!--#include file="page-end.html"--> <!--#include file="page-end.html"-->

26
domain-server/resources/web/js/form2js.min.js vendored Executable file
View file

@ -0,0 +1,26 @@
/**
* Copyright (c) 2010 Maxim Vasiliev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Maxim Vasiliev
* Date: 09.09.2010
* Time: 19:02:33
*/
(function(e,t){if(typeof define==="function"&&define.amd){define(t)}else{e.form2js=t()}})(this,function(){"use strict";function e(e,r,i,s,o,u){u=u?true:false;if(typeof i=="undefined"||i==null)i=true;if(typeof r=="undefined"||r==null)r=".";if(arguments.length<5)o=false;e=typeof e=="string"?document.getElementById(e):e;var a=[],f,l=0;if(e.constructor==Array||typeof NodeList!="undefined"&&e.constructor==NodeList){while(f=e[l++]){a=a.concat(n(f,s,o,u))}}else{a=n(e,s,o,u)}return t(a,i,r)}function t(e,t,n){var r={},i={},s,o,u,a,f,l,c,h,p,d,v,m,g;for(s=0;s<e.length;s++){f=e[s].value;if(t&&(f===""||f===null))continue;m=e[s].name;g=m.split(n);l=[];c=r;h="";for(o=0;o<g.length;o++){v=g[o].split("][");if(v.length>1){for(u=0;u<v.length;u++){if(u==0){v[u]=v[u]+"]"}else if(u==v.length-1){v[u]="["+v[u]}else{v[u]="["+v[u]+"]"}d=v[u].match(/([a-z_]+)?\[([a-z_][a-z0-9_]+?)\]/i);if(d){for(a=1;a<d.length;a++){if(d[a])l.push(d[a])}}else{l.push(v[u])}}}else l=l.concat(v)}for(o=0;o<l.length;o++){v=l[o];if(v.indexOf("[]")>-1&&o==l.length-1){p=v.substr(0,v.indexOf("["));h+=p;if(!c[p])c[p]=[];c[p].push(f)}else if(v.indexOf("[")>-1){p=v.substr(0,v.indexOf("["));d=v.replace(/(^([a-z_]+)?\[)|(\]$)/gi,"");h+="_"+p+"_"+d;if(!i[h])i[h]={};if(p!=""&&!c[p])c[p]=[];if(o==l.length-1){if(p==""){c.push(f);i[h][d]=c[c.length-1]}else{c[p].push(f);i[h][d]=c[p][c[p].length-1]}}else{if(!i[h][d]){if(/^[0-9a-z_]+\[?/i.test(l[o+1]))c[p].push({});else c[p].push([]);i[h][d]=c[p][c[p].length-1]}}c=i[h][d]}else{h+=v;if(o<l.length-1){if(!c[v])c[v]={};c=c[v]}else{c[v]=f}}}}return r}function n(e,t,n,s){var o=i(e,t,n,s);return o.length>0?o:r(e,t,n,s)}function r(e,t,n,r){var s=[],o=e.firstChild;while(o){s=s.concat(i(o,t,n,r));o=o.nextSibling}return s}function i(e,t,n,i){if(e.disabled&&!i)return[];var u,a,f,l=s(e,n);u=t&&t(e);if(u&&u.name){f=[u]}else if(l!=""&&e.nodeName.match(/INPUT|TEXTAREA/i)){a=o(e,i);if(null===a){f=[]}else{f=[{name:l,value:a}]}}else if(l!=""&&e.nodeName.match(/SELECT/i)){a=o(e,i);f=[{name:l.replace(/\[\]$/,""),value:a}]}else{f=r(e,t,n,i)}return f}function s(e,t){if(e.name&&e.name!="")return e.name;else if(t&&e.id&&e.id!="")return e.id;else return""}function o(e,t){if(e.disabled&&!t)return null;switch(e.nodeName){case"INPUT":case"TEXTAREA":switch(e.type.toLowerCase()){case"radio":if(e.checked&&e.value==="false")return false;case"checkbox":if(e.checked&&e.value==="true")return true;if(!e.checked&&e.value==="true")return false;if(e.checked)return e.value;break;case"button":case"reset":case"submit":case"image":return"";break;default:return e.value;break}break;case"SELECT":return u(e);break;default:break}return null}function u(e){var t=e.multiple,n=[],r,i,s;if(!t)return e.value;for(r=e.getElementsByTagName("option"),i=0,s=r.length;i<s;i++){if(r[i].selected)n.push(r[i].value)}return n}return e})

View file

@ -0,0 +1,72 @@
var Settings = {};
$(document).ready(function(){
var source = $('#settings-template').html();
Settings.template = _.template(source);
reloadSettings();
});
function reloadSettings() {
$.getJSON('/settings.json', function(data){
$('#settings').html(Settings.template(data));
});
}
var SETTINGS_ERROR_MESSAGE = "There was a problem saving domain settings. Please try again!";
$('#settings').on('click', 'button', function(e){
// disable any inputs not changed
$("input:not([data-changed])").each(function(){
$(this).prop('disabled', true);
});
// grab a JSON representation of the form via form2js
var formJSON = form2js('settings-form', ".", false, cleanupFormValues, true);
// re-enable all inputs
$("input").each(function(){
$(this).prop('disabled', false);
});
// POST the form JSON to the domain-server settings.json endpoint so the settings are saved
$.ajax('/settings.json', {
data: JSON.stringify(formJSON),
contentType: 'application/json',
type: 'POST'
}).done(function(data){
if (data.status == "success") {
showAlertMessage("Domain settings saved.", true);
} else {
showAlertMessage(SETTINGS_ERROR_MESSAGE, false);
}
reloadSettings();
}).fail(function(){
showAlertMessage(SETTINGS_ERROR_MESSAGE, false);
reloadSettings();
});
return false;
});
$('#settings').on('change', 'input', function(){
// this input was changed, add the changed data attribute to it
$(this).attr('data-changed', true);
});
function cleanupFormValues(node) {
if (node.type && node.type === 'checkbox') {
return { name: node.id, value: node.checked ? true : false };
} else {
return false;
}
}
function showAlertMessage(message, isSuccess) {
var alertBox = $('.alert');
alertBox.attr('class', 'alert');
alertBox.addClass(isSuccess ? 'alert-success' : 'alert-danger');
alertBox.html(message);
alertBox.fadeIn();
}

View file

@ -1,4 +1,8 @@
$(document).ready(function(){ $(document).ready(function(){
// setup the underscore templates
var nodeTemplate = _.template($('#nodes-template').html());
var queuedTemplate = _.template($('#queued-template').html());
// setup a function to grab the assignments // setup a function to grab the assignments
function getNodesAndAssignments() { function getNodesAndAssignments() {
$.getJSON("nodes.json", function(json){ $.getJSON("nodes.json", function(json){
@ -29,40 +33,11 @@ $(document).ready(function(){
} }
}); });
nodesTableBody = ""; $('#nodes-table tbody').html(nodeTemplate(json));
$.each(json.nodes, function(index, data) {
nodesTableBody += "<tr>";
nodesTableBody += "<td>" + data.type + "</td>";
nodesTableBody += "<td><a href='stats/?uuid=" + data.uuid + "'>" + data.uuid + "</a></td>";
nodesTableBody += "<td>" + (data.pool ? data.pool : "") + "</td>";
nodesTableBody += "<td>" + data.public.ip + "<span class='port'>:" + data.public.port + "</span></td>";
nodesTableBody += "<td>" + data.local.ip + "<span class='port'>:" + data.local.port + "</span></td>";
var uptimeSeconds = (Date.now() - data.wake_timestamp) / 1000;
nodesTableBody += "<td>" + uptimeSeconds.toLocaleString() + "</td>";
nodesTableBody += "<td>" + (typeof data.pending_credits == 'number' ? data.pending_credits.toLocaleString() : 'N/A') + "</td>";
nodesTableBody += "<td><span class='glyphicon glyphicon-remove' data-uuid=" + data.uuid + "></span></td>";
nodesTableBody += "</tr>";
});
$('#nodes-table tbody').html(nodesTableBody);
}); });
$.getJSON("assignments.json", function(json){ $.getJSON("assignments.json", function(json){
queuedTableBody = ""; $('#assignments-table tbody').html(queuedTemplate(json));
$.each(json.queued, function (uuid, data) {
queuedTableBody += "<tr>";
queuedTableBody += "<td>" + data.type + "</td>";
queuedTableBody += "<td>" + uuid + "</td>";
queuedTableBody += "<td>" + (data.pool ? data.pool : "") + "</td>";
queuedTableBody += "</tr>";
});
$('#assignments-table tbody').html(queuedTableBody);
}); });
} }

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,20 @@
{
"audio": {
"label": "Audio",
"assignment-types": [0],
"settings": {
"unattenuated-zone": {
"label": "Unattenuated Zone",
"help": "Boxes for source and listener (corner x, corner y, corner z, size x, size y, size z, corner x, corner y, corner z, size x, size y, size z)",
"placeholder": "no zone",
"default": ""
},
"dynamic-jitter-buffer": {
"type": "checkbox",
"label": "Dynamic Jitter Buffers",
"help": "Dynamically buffer client audio based on perceived jitter in packet receipt timing",
"default": false
}
}
}
}

View file

@ -0,0 +1,46 @@
<!--#include virtual="header.html"-->
<div id="settings-lead" class="table-lead"><h3>Settings</h3><div class="lead-line"></div></div>
<div style="clear: both;"></div>
<div class="alert" style="display:none;"></div>
<form class="form-horizontal" id="settings-form" role="form">
<script id="settings-template" type="text/template">
<% _.each(descriptions, function(group, group_key){ %>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><%- group.label %></h3>
</div>
<div class="panel-body">
<% _.each(group.settings, function(setting, setting_key){ %>
<div class="form-group">
<% var setting_id = group_key + "." + setting_key %>
<label for="<%- setting_id %>" class="col-sm-2 control-label"><%- setting.label %></label>
<div class="col-sm-10">
<% if(setting.type) %>
<% if (setting.type === "checkbox") { %>
<% var checked_box = (values[group_key] || {})[setting_key] || setting.default %>
<input type="checkbox" id="<%- setting_id %>" <%- checked_box ? "checked" : "" %>>
<% } else { %>
<input type="text" class="form-control" id="<%- setting_id %>"
placeholder="<%- setting.placeholder %>"
value="<%- (values[group_key] || {})[setting_key] %>">
<% } %>
</div>
<p class="help-block col-sm-offset-2 col-sm-10"><%- setting.help %></p>
</div>
<% }); %>
</div>
</div>
<% }); %>
<button type="submit" class="btn btn-default">Save</button>
</script>
<div id="settings"></div>
</form>
<!--#include virtual="footer.html"-->
<script src='/js/settings.js'></script>
<script src='/js/form2js.min.js'></script>
<script src='/js/underscore-1.5.0.min.js'></script>
<!--#include virtual="page-end.html"-->

View file

@ -41,7 +41,8 @@ DomainServer::DomainServer(int argc, char* argv[]) :
_oauthClientID(), _oauthClientID(),
_hostname(), _hostname(),
_networkReplyUUIDMap(), _networkReplyUUIDMap(),
_sessionAuthenticationHash() _sessionAuthenticationHash(),
_settingsManager()
{ {
setOrganizationName("High Fidelity"); setOrganizationName("High Fidelity");
setOrganizationDomain("highfidelity.io"); setOrganizationDomain("highfidelity.io");
@ -1162,8 +1163,8 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url
} }
} }
// didn't process the request, let the HTTPManager try and handle // didn't process the request, let our DomainServerSettingsManager or HTTPManager handle
return false; return _settingsManager.handleHTTPRequest(connection, url);
} }
bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &url) { bool DomainServer::handleHTTPSRequest(HTTPSConnection* connection, const QUrl &url) {

View file

@ -24,6 +24,7 @@
#include <HTTPSConnection.h> #include <HTTPSConnection.h>
#include <LimitedNodeList.h> #include <LimitedNodeList.h>
#include "DomainServerSettingsManager.h"
#include "WalletTransaction.h" #include "WalletTransaction.h"
#include "PendingAssignedNodeData.h" #include "PendingAssignedNodeData.h"
@ -110,6 +111,8 @@ private:
QString _hostname; QString _hostname;
QMap<QNetworkReply*, QUuid> _networkReplyUUIDMap; QMap<QNetworkReply*, QUuid> _networkReplyUUIDMap;
QHash<QUuid, bool> _sessionAuthenticationHash; QHash<QUuid, bool> _sessionAuthenticationHash;
DomainServerSettingsManager _settingsManager;
}; };
#endif // hifi_DomainServer_h #endif // hifi_DomainServer_h

View file

@ -0,0 +1,179 @@
//
// DomainServerSettingsManager.cpp
// domain-server/src
//
// Created by Stephen Birarda on 2014-06-24.
// 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 <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtCore/QUrl>
#include <QtCore/QUrlQuery>
#include <Assignment.h>
#include <HTTPConnection.h>
#include "DomainServerSettingsManager.h"
const QString SETTINGS_DESCRIPTION_RELATIVE_PATH = "/resources/web/settings/describe.json";
const QString SETTINGS_CONFIG_FILE_RELATIVE_PATH = "/resources/config.json";
DomainServerSettingsManager::DomainServerSettingsManager() :
_descriptionObject(),
_settingsMap()
{
// load the description object from the settings description
QFile descriptionFile(QCoreApplication::applicationDirPath() + SETTINGS_DESCRIPTION_RELATIVE_PATH);
descriptionFile.open(QIODevice::ReadOnly);
_descriptionObject = QJsonDocument::fromJson(descriptionFile.readAll()).object();
// load the existing config file to get the current values
QFile configFile(QCoreApplication::applicationDirPath() + SETTINGS_CONFIG_FILE_RELATIVE_PATH);
configFile.open(QIODevice::ReadOnly);
_settingsMap = QJsonDocument::fromJson(configFile.readAll()).toVariant().toMap();
}
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") {
// this is a GET operation for our settings
// check if there is a query parameter for settings affecting a particular type of assignment
const QString SETTINGS_TYPE_QUERY_KEY = "type";
QUrlQuery settingsQuery(url);
QString typeValue = settingsQuery.queryItemValue(SETTINGS_TYPE_QUERY_KEY);
QJsonObject responseObject;
if (typeValue.isEmpty()) {
// combine the description object and our current settings map
responseObject["descriptions"] = _descriptionObject;
responseObject["values"] = QJsonDocument::fromVariant(_settingsMap).object();
} else {
// convert the string type value to a QJsonValue
QJsonValue queryType = QJsonValue(typeValue.toInt());
const QString AFFECTED_TYPES_JSON_KEY = "assignment-types";
// enumerate the groups in the description object to find which settings to pass
foreach(const QString& group, _descriptionObject.keys()) {
QJsonObject groupObject = _descriptionObject[group].toObject();
QJsonObject groupSettingsObject = groupObject[DESCRIPTION_SETTINGS_KEY].toObject();
QJsonObject groupResponseObject;
foreach(const QString& settingKey, groupSettingsObject.keys()) {
QJsonObject settingObject = groupSettingsObject[settingKey].toObject();
QJsonArray affectedTypesArray = settingObject[AFFECTED_TYPES_JSON_KEY].toArray();
if (affectedTypesArray.isEmpty()) {
affectedTypesArray = groupObject[AFFECTED_TYPES_JSON_KEY].toArray();
}
if (affectedTypesArray.contains(queryType)) {
// this is a setting we should include in the responseObject
// we need to check if the settings map has a value for this setting
QVariant variantValue;
QVariant settingsMapGroupValue = _settingsMap.value(group);
if (!settingsMapGroupValue.isNull()) {
variantValue = settingsMapGroupValue.toMap().value(settingKey);
}
if (variantValue.isNull()) {
// no value for this setting, pass the default
groupResponseObject[settingKey] = settingObject[SETTING_DEFAULT_KEY];
} else {
groupResponseObject[settingKey] = QJsonValue::fromVariant(variantValue);
}
}
}
if (!groupResponseObject.isEmpty()) {
// set this group's object to the constructed object
responseObject[group] = groupResponseObject;
}
}
}
connection->respond(HTTPConnection::StatusCode200, QJsonDocument(responseObject).toJson(), "application/json");
return true;
}
return false;
}
void DomainServerSettingsManager::recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject,
QVariantMap& settingsVariant,
QJsonObject descriptionObject) {
foreach(const QString& key, postedObject.keys()) {
QJsonValue rootValue = postedObject[key];
// we don't continue if this key is not present in our descriptionObject
if (descriptionObject.contains(key)) {
if (rootValue.isString()) {
settingsVariant[key] = rootValue.toString();
} else if (rootValue.isBool()) {
settingsVariant[key] = rootValue.toBool();
} else if (rootValue.isObject()) {
// there's a JSON Object to explore, so attempt to recurse into it
QJsonObject nextDescriptionObject = descriptionObject[key].toObject();
if (nextDescriptionObject.contains(DESCRIPTION_SETTINGS_KEY)) {
if (!settingsVariant.contains(key)) {
// we don't have a map below this key yet, so set it up now
settingsVariant[key] = QVariantMap();
}
recurseJSONObjectAndOverwriteSettings(rootValue.toObject(),
*reinterpret_cast<QVariantMap*>(settingsVariant[key].data()),
nextDescriptionObject[DESCRIPTION_SETTINGS_KEY].toObject());
}
}
}
}
}
QByteArray DomainServerSettingsManager::getJSONSettingsMap() const {
return QJsonDocument::fromVariant(_settingsMap).toJson();
}
void DomainServerSettingsManager::persistToFile() {
QFile settingsFile(QCoreApplication::applicationDirPath() + SETTINGS_CONFIG_FILE_RELATIVE_PATH);
if (settingsFile.open(QIODevice::WriteOnly)) {
settingsFile.write(getJSONSettingsMap());
} else {
qCritical("Could not write to JSON settings file. Unable to persist settings.");
}
}

View file

@ -0,0 +1,35 @@
//
// DomainServerSettingsManager.h
// domain-server/src
//
// Created by Stephen Birarda on 2014-06-24.
// 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_DomainServerSettingsManager_h
#define hifi_DomainServerSettingsManager_h
#include <QtCore/QJsonDocument>
#include <HTTPManager.h>
class DomainServerSettingsManager : public QObject, HTTPRequestHandler {
Q_OBJECT
public:
DomainServerSettingsManager();
bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url);
QByteArray getJSONSettingsMap() const;
private:
void recurseJSONObjectAndOverwriteSettings(const QJsonObject& postedObject, QVariantMap& settingsVariant,
QJsonObject descriptionObject);
void persistToFile();
QJsonObject _descriptionObject;
QVariantMap _settingsMap;
};
#endif // hifi_DomainServerSettingsManager_h