UNPKG

ndn-js-contrib

Version:

Reusable 'Classes' for Named Data Networking: NameTree, PIT, FIB, ContentStore, Interfaces, and Transports

1 lines 2.16 kB
function FibEntry(a,b){return this.prefix="string"==typeof a?new ndn.Name(a):a,this.nextHops=function(){function a(){if(b&&b.length>0){var d;d="undefined"!=typeof b[0].faceID?b.shift():{faceID:b.shift()};var e=binarySearch(c,d,"faceID");return 0>e&&c.splice(~e,0,d),a()}return c}var c=[];return a()}(),this}function FIB(a){return this.nameTree=a,this}var binarySearch=require("./../Utility/binarySearch.js"),ndn,debug={};debug.debug=require("debug")("FIB"),FibEntry.type="FibEntry",FibEntry.prototype.getNextHops=function(a){var b;if(void 0!==a){var c={faceID:a},d=binarySearch(this.nextHops,c,"faceID");b=d>=0?this.nextHops.slice(0,d).concat(this.nextHops.slice(d+1)):this.nextHops}else b=this.nextHops;return b},FibEntry.prototype.removeNextHop=function(a){var b=binarySearch(this.nextHops,a,"faceID");return 0>b?this:(this.nextHops.splice(b,1),this)},FibEntry.prototype.addNextHop=function(a){var b=binarySearch(this.nextHops,a,"faceID");return 0>b?(this.nextHops.splice(~b,0,a),this):(this.nextHops.splice(b,1,a),this)},FIB.installNDN=function(a){return ndn=a,this},FIB.prototype.lookup=function(a){a="string"==typeof a?new ndn.Name(a):a;var b=this.nameTree.lookup(a),c=b.fibEntry;return c?c:b.fibEntry=new FibEntry({prefix:a,nextHops:[]})},FIB.prototype.findAllFibEntries=function(a){var b=this.nameTree.findAllMatches(a,function(a){return a.fibEntry&&a.fibEntry.nextHops.length>0?!0:!1}),c={hasNext:b.hasNext,next:function(){if(b.hasNext){var a=b.next();return this.hasNext=b.hasNext?!0:!1,a.fibEntry}return null}};return c},FIB.prototype.findAllNextHops=function(a,b){a="string"==typeof a?new ndn.Name(a):a;for(var c=0,d=this.findAllFibEntries(a);d.hasNext;)for(var e=d.next(),f=e.getNextHops(b),g=0;g<f.length;g++)c|=1<<f[g].faceID;return c},FIB.prototype.addEntry=function(a,b){var c;c="number"==typeof b||"object"==typeof b&&b.faceID?new FibEntry(a,[b]):new FibEntry(a,b);var d=this.nameTree.lookup(c.prefix);if(d.fibEntry){for(var e=0;e<c.nextHops.length;e++){var f=binarySearch(d.fibEntry.nextHops,c.nextHops[e],"faceID");0>f&&d.fibEntry.nextHops.splice(~f,0,c.nextHops[e])}return this}return d.fibEntry=c,this},FIB.Entry=FibEntry,module.exports=FIB;