UNPKG

ndn-js-contrib

Version:

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

1 lines 1.49 kB
var ndn,Face,debug={},ndn=require("ndn-js"),TlvDecoder=require("ndn-lib/js/encoding/tlv/tlv-decoder.js").TlvDecoder,Tlv=require("ndn-js/js/encoding/tlv/tlv.js").Tlv;debug.debug=require("debug")("Interfaces");var Interfaces=function(a){return this.subject=a,this.transports={},Face=ndn.Face,this.Faces=[],this};Interfaces.installNDN=function(a){return ndn=a,this},Interfaces.prototype.transports={},Interfaces.prototype.installTransport=function(a){return this.transports[a.prototype.name]=a,a.Listener&&a.Listener(this),this},Interfaces.prototype.newFace=function(a,b,c,d){var e=this;if(this.transports[a]){var f=new this.transports[a](b),g=new ndn.Face(f,f.connectionInfo);this.Faces.push(g),g.faceID=this.Faces.length-1;var h;return h="WebSocketTransport"===a?new this.transports[a].ConnectionInfo(b.host,b.port):g.connectionInfo||f.connectionInfo||new this.transports[a].ConnectionInfo(f.socket),d&&(g.onclose=d),g.transport.connect(h,g,function(){g.onReceivedElement=function(a){var b=new TlvDecoder(a);b.peekType(Tlv.Interest,a.length)?e.subject.handleInterest(a,this.faceID):b.peekType(Tlv.Data,a.length)&&e.subject.handleData(a,this.faceID)},g.send=function(a){this.transport.send(a)},c&&c(g.faceID)},function(){d&&d(g.faceID)}),g.faceID}return-1},Interfaces.prototype.closeFace=function(){},Interfaces.prototype.dispatch=function(a,b,c){if(b)for(var d=0;d<b.toString(2).length;d++)b&1<<d&&(this.Faces[d]&&this.Faces[d].transport.send(a),c&&c(d));return this},module.exports=Interfaces;