You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BoshAddressChoice.js 1.7KB

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