Implements choosing from a list of possible BOSH addresses.

This commit is contained in:
Boris Grozev 2015-10-20 10:41:15 -05:00
parent 554808549e
commit 23ff99db6e
2 changed files with 51 additions and 2 deletions

9
app.js
View File

@ -46,7 +46,7 @@ function init() {
}
/**
* If we have HTTP endpoint for getting confgi.json configured we're going to
* If we have an HTTP endpoint for getting config.json configured we're going to
* read it and override properties from config.js and interfaceConfig.js.
* If there is no endpoint we'll just continue with initialization.
* Keep in mind that if the endpoint has been configured and we fail to obtain
@ -54,9 +54,11 @@ function init() {
* will be displayed to the user.
*/
function obtainConfigAndInit() {
var roomName = APP.UI.getRoomNode();
if (config.configLocation) {
APP.configFetch.obtainConfig(
config.configLocation, APP.UI.getRoomNode(),
config.configLocation, roomName,
// Get config result callback
function(success, error) {
if (success) {
@ -71,6 +73,9 @@ function obtainConfigAndInit() {
}
});
} else {
require("./modules/config/BoshAddressChoice").chooseAddress(
config, roomName);
init();
}
}

View File

@ -0,0 +1,44 @@
var jssha = require('jssha');
module.exports = {
/**
* Looks for a list of possible BOSH addresses in 'config.boshList' and
* sets the value of 'config.bosh' based on that list and 'roomName'.
* @param config the configuration object.
* @param roomName the name of the room/conference.
*/
chooseAddress: function(config, roomName) {
if (!roomName || !config.boshList || !Array.isArray(config.boshList) ||
!config.boshList.length) {
return;
}
// This implements the actual choice of an entry in the list based on
// roomName. Please consider the implications for existing deployments
// before introducing changes.
var hash = (new jssha(roomName, 'TEXT')).getHash('SHA-1', 'HEX');
var n = parseInt("0x"+hash.substr(-6));
var idx = n % config.boshList.length;
var attemptFirstAddress;
config.bosh = config.boshList[idx];
console.log('Setting config.bosh to ' + config.bosh +
' (idx=' + idx + ')');
if (config.boshAttemptFirstList &&
Array.isArray(config.boshAttemptFirstList) &&
config.boshAttemptFirstList.length > 0) {
idx = n % config.boshAttemptFirstList.length;
attemptFirstAddress = config.boshAttemptFirstList[idx];
if (attemptFirstAddress != config.bosh) {
config.boshAttemptFirst = attemptFirstAddress;
console.log('Setting config.boshAttemptFirst=' +
attemptFirstAddress + ' (idx=' + idx + ')');
} else {
console.log('Not setting boshAttemptFirst, address matches.');
}
}
}
};