Ohm-Management - Projektarbeit B-ME
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.

persistent_search.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ///--- Globals
  2. var parseDN = require('./dn').parse;
  3. var EntryChangeNotificationControl =
  4. require('./controls').EntryChangeNotificationControl;
  5. ///--- API
  6. // Cache used to store connected persistent search clients
  7. function PersistentSearch() {
  8. this.clientList = [];
  9. }
  10. PersistentSearch.prototype.addClient = function (req, res, callback) {
  11. if (typeof (req) !== 'object')
  12. throw new TypeError('req must be an object');
  13. if (typeof (res) !== 'object')
  14. throw new TypeError('res must be an object');
  15. if (callback && typeof (callback) !== 'function')
  16. throw new TypeError('callback must be a function');
  17. var log = req.log;
  18. var client = {};
  19. client.req = req;
  20. client.res = res;
  21. log.debug('%s storing client', req.logId);
  22. this.clientList.push(client);
  23. log.debug('%s stored client', req.logId);
  24. log.debug('%s total number of clients %s',
  25. req.logId, this.clientList.length);
  26. if (callback)
  27. callback(client);
  28. };
  29. PersistentSearch.prototype.removeClient = function (req, res, callback) {
  30. if (typeof (req) !== 'object')
  31. throw new TypeError('req must be an object');
  32. if (typeof (res) !== 'object')
  33. throw new TypeError('res must be an object');
  34. if (callback && typeof (callback) !== 'function')
  35. throw new TypeError('callback must be a function');
  36. var log = req.log;
  37. log.debug('%s removing client', req.logId);
  38. var client = {};
  39. client.req = req;
  40. client.res = res;
  41. // remove the client if it exists
  42. this.clientList.forEach(function (element, index, array) {
  43. if (element.req === client.req) {
  44. log.debug('%s removing client from list', req.logId);
  45. array.splice(index, 1);
  46. }
  47. });
  48. log.debug('%s number of persistent search clients %s',
  49. req.logId, this.clientList.length);
  50. if (callback)
  51. callback(client);
  52. };
  53. function getOperationType(requestType) {
  54. switch (requestType) {
  55. case 'AddRequest':
  56. case 'add':
  57. return 1;
  58. case 'DeleteRequest':
  59. case 'delete':
  60. return 2;
  61. case 'ModifyRequest':
  62. case 'modify':
  63. return 4;
  64. case 'ModifyDNRequest':
  65. case 'modrdn':
  66. return 8;
  67. default:
  68. throw new TypeError('requestType %s, is an invalid request type',
  69. requestType);
  70. }
  71. }
  72. function getEntryChangeNotificationControl(req, obj, callback) {
  73. // if we want to return a ECNC
  74. if (req.persistentSearch.value.returnECs) {
  75. var attrs = obj.attributes;
  76. var value = {};
  77. value.changeType = getOperationType(attrs.changetype);
  78. // if it's a modDN request, fill in the previous DN
  79. if (value.changeType === 8 && attrs.previousDN) {
  80. value.previousDN = attrs.previousDN;
  81. }
  82. value.changeNumber = attrs.changenumber;
  83. return new EntryChangeNotificationControl({ value: value });
  84. } else {
  85. return false;
  86. }
  87. }
  88. function checkChangeType(req, requestType) {
  89. return (req.persistentSearch.value.changeTypes &
  90. getOperationType(requestType));
  91. }
  92. ///--- Exports
  93. module.exports = {
  94. PersistentSearchCache: PersistentSearch,
  95. checkChangeType: checkChangeType,
  96. getEntryChangeNotificationControl: getEntryChangeNotificationControl
  97. };