Source: impl/interest-filter-table.js

  1. /**
  2. * Copyright (C) 2016-2018 Regents of the University of California.
  3. * @author: Jeff Thompson <jefft0@remap.ucla.edu>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * A copy of the GNU Lesser General Public License is in the file COPYING.
  18. */
  19. /** @ignore */
  20. var LOG = require('../log.js').Log.LOG;
  21. /**
  22. * An InterestFilterTable is an internal class to hold a list of entries with
  23. * an interest Filter and its OnInterestCallback.
  24. * @constructor
  25. */
  26. var InterestFilterTable = function InterestFilterTable()
  27. {
  28. this.table_ = []; // of Entry
  29. };
  30. exports.InterestFilterTable = InterestFilterTable;
  31. /**
  32. * InterestFilterTable.Entry holds an interestFilterId, an InterestFilter and
  33. * the OnInterestCallback with its related Face.
  34. * Create a new Entry with the given values.
  35. * @param {number} interestFilterId The ID from getNextEntryId().
  36. * @param {InterestFilter} filter The InterestFilter for this entry.
  37. * @param {function} onInterest The callback to call.
  38. * @param {Face} face The face on which was called registerPrefix or
  39. * setInterestFilter which is passed to the onInterest callback.
  40. * @constructor
  41. */
  42. InterestFilterTable.Entry = function InterestFilterTableEntry
  43. (interestFilterId, filter, onInterest, face)
  44. {
  45. this.interestFilterId_ = interestFilterId;
  46. this.filter_ = filter;
  47. this.onInterest_ = onInterest;
  48. this.face_ = face;
  49. };
  50. /**
  51. * Get the interestFilterId given to the constructor.
  52. * @return {number} The interestFilterId.
  53. */
  54. InterestFilterTable.Entry.prototype.getInterestFilterId = function()
  55. {
  56. return this.interestFilterId_;
  57. };
  58. /**
  59. * Get the InterestFilter given to the constructor.
  60. * @return {InterestFilter} The InterestFilter.
  61. */
  62. InterestFilterTable.Entry.prototype.getFilter = function()
  63. {
  64. return this.filter_;
  65. };
  66. /**
  67. * Get the onInterest callback given to the constructor.
  68. * @return {function} The onInterest callback.
  69. */
  70. InterestFilterTable.Entry.prototype.getOnInterest = function()
  71. {
  72. return this.onInterest_;
  73. };
  74. /**
  75. * Get the Face given to the constructor.
  76. * @return {Face} The Face.
  77. */
  78. InterestFilterTable.Entry.prototype.getFace = function()
  79. {
  80. return this.face_;
  81. };
  82. /**
  83. * Add a new entry to the table.
  84. * @param {number} interestFilterId The ID from Node.getNextEntryId().
  85. * @param {InterestFilter} filter The InterestFilter for this entry.
  86. * @param {function} onInterest The callback to call.
  87. * @param {Face} face The face on which was called registerPrefix or
  88. * setInterestFilter which is passed to the onInterest callback.
  89. */
  90. InterestFilterTable.prototype.setInterestFilter = function
  91. (interestFilterId, filter, onInterest, face)
  92. {
  93. this.table_.push(new InterestFilterTable.Entry
  94. (interestFilterId, filter, onInterest, face));
  95. };
  96. /**
  97. * Find all entries from the interest filter table where the interest conforms
  98. * to the entry's filter, and add to the matchedFilters list.
  99. * @param {Interest} interest The interest which may match the filter in
  100. * multiple entries.
  101. * @param {Array<InterestFilterTable.Entry>} matchedFilters Add each matching
  102. * InterestFilterTable.Entry from the interest filter table. The caller should
  103. * pass in an empty array.
  104. */
  105. InterestFilterTable.prototype.getMatchedFilters = function
  106. (interest, matchedFilters)
  107. {
  108. for (var i = 0; i < this.table_.length; ++i) {
  109. var entry = this.table_[i];
  110. if (entry.getFilter().doesMatch(interest.getName()))
  111. matchedFilters.push(entry);
  112. }
  113. };
  114. /**
  115. * Remove the interest filter entry which has the interestFilterId from the
  116. * interest filter table. This does not affect another interest filter with a
  117. * different interestFilterId, even if it has the same prefix name. If there is
  118. * no entry with the interestFilterId, do nothing.
  119. * @param {number} interestFilterId The ID returned from setInterestFilter.
  120. */
  121. InterestFilterTable.prototype.unsetInterestFilter = function(interestFilterId)
  122. {
  123. // Go backwards through the list so we can erase entries.
  124. // Remove all entries even though interestFilterId should be unique.
  125. var count = 0;
  126. for (var i = this.table_.length - 1; i >= 0; --i) {
  127. if (this.table_[i].getInterestFilterId() == interestFilterId) {
  128. ++count;
  129. this.table_.splice(i, 1);
  130. }
  131. }
  132. if (count === 0)
  133. if (LOG > 0) console.log
  134. ("unsetInterestFilter: Didn't find interestFilterId " + interestFilterId);
  135. };