Browse Source

Implements choosing from a list of possible BOSH addresses.

master
Boris Grozev 10 years ago
parent
commit
23ff99db6e
2 changed files with 51 additions and 2 deletions
  1. 7
    2
      app.js
  2. 44
    0
      modules/config/BoshAddressChoice.js

+ 7
- 2
app.js View File

@@ -46,7 +46,7 @@ function init() {
46 46
 }
47 47
 
48 48
 /**
49
- * If we have HTTP endpoint for getting confgi.json configured we're going to
49
+ * If we have an HTTP endpoint for getting config.json configured we're going to
50 50
  * read it and override properties from config.js and interfaceConfig.js.
51 51
  * If there is no endpoint we'll just continue with initialization.
52 52
  * Keep in mind that if the endpoint has been configured and we fail to obtain
@@ -54,9 +54,11 @@ function init() {
54 54
  * will be displayed to the user.
55 55
  */
56 56
 function obtainConfigAndInit() {
57
+    var roomName = APP.UI.getRoomNode();
58
+
57 59
     if (config.configLocation) {
58 60
         APP.configFetch.obtainConfig(
59
-            config.configLocation, APP.UI.getRoomNode(),
61
+            config.configLocation, roomName,
60 62
             // Get config result callback
61 63
             function(success, error) {
62 64
                 if (success) {
@@ -71,6 +73,9 @@ function obtainConfigAndInit() {
71 73
                 }
72 74
             });
73 75
     } else {
76
+        require("./modules/config/BoshAddressChoice").chooseAddress(
77
+            config, roomName);
78
+
74 79
         init();
75 80
     }
76 81
 }

+ 44
- 0
modules/config/BoshAddressChoice.js View File

@@ -0,0 +1,44 @@
1
+var jssha = require('jssha');
2
+
3
+module.exports = {
4
+    /**
5
+     * Looks for a list of possible BOSH addresses in 'config.boshList' and
6
+     * sets the value of 'config.bosh' based on that list and 'roomName'.
7
+     * @param config the configuration object.
8
+     * @param roomName the name of the room/conference.
9
+     */
10
+    chooseAddress: function(config, roomName) {
11
+        if (!roomName || !config.boshList || !Array.isArray(config.boshList) ||
12
+            !config.boshList.length) {
13
+            return;
14
+        }
15
+
16
+        // This implements the actual choice of an entry in the list based on
17
+        // roomName. Please consider the implications for existing deployments
18
+        // before introducing changes.
19
+        var hash = (new jssha(roomName, 'TEXT')).getHash('SHA-1', 'HEX');
20
+        var n = parseInt("0x"+hash.substr(-6));
21
+        var idx = n % config.boshList.length;
22
+        var attemptFirstAddress;
23
+
24
+        config.bosh = config.boshList[idx];
25
+        console.log('Setting config.bosh to ' + config.bosh +
26
+            ' (idx=' + idx + ')');
27
+
28
+        if (config.boshAttemptFirstList &&
29
+            Array.isArray(config.boshAttemptFirstList) &&
30
+            config.boshAttemptFirstList.length > 0) {
31
+
32
+            idx = n % config.boshAttemptFirstList.length;
33
+            attemptFirstAddress = config.boshAttemptFirstList[idx];
34
+
35
+            if (attemptFirstAddress != config.bosh) {
36
+                config.boshAttemptFirst = attemptFirstAddress;
37
+                console.log('Setting config.boshAttemptFirst=' +
38
+                    attemptFirstAddress + ' (idx=' + idx + ')');
39
+            } else {
40
+                console.log('Not setting boshAttemptFirst, address matches.');
41
+            }
42
+        }
43
+    }
44
+};

Loading…
Cancel
Save