UNPKG

ndn-js-contrib

Version:

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

1 lines 1.97 kB
var ElementReader=require("ndn-js/js/encoding/element-reader.js").ElementReader,Transport=require("ndn-js/js/transport/transport.js").Transport,net=require("net"),debug={};debug.debug=require("debug")("TCPServerTransport");var TCPServerTransport=function(a){var b=this;return"string"==typeof a?this.socket=net.connect(a.split(":")[2]||8484,a.split("://")[1].split(":")[0]||"localhost",function(){b.connectedHost=a.split("://")[1]||"localhost",b.connectedPort=a.split(":")[2]||8484,b.sock_ready=!0}):(this.socket=a,this.connectedHost=null,this.connectedPort=null),this};TCPServerTransport.prototype=new Transport,TCPServerTransport.prototype.name="TCPServerTransport",TCPServerTransport.ConnectionInfo=function(a){Transport.ConnectionInfo.call(this),this.socket=a},TCPServerTransport.ConnectionInfo.prototype=new Transport.ConnectionInfo,TCPServerTransport.ConnectionInfo.prototype.name="TCPServerTransport.ConnectionInfo",TCPServerTransport.ConnectionInfo.prototype.getSocket=function(){return this.socket},TCPServerTransport.defineListener=function(a,b){b=b||8484,this.Listener=function(a){this.server=net.createServer(function(b){b.on("end",function(){}),a.newFace("TCPServerTransport",b,function(b){a.Faces[b].transport.connect({},a.Faces[b],function(){},function(){})},function(){})}),this.server.listen(b,function(){})}},TCPServerTransport.prototype.connect=function(a,b,c){this.elementReader=new ElementReader(b);var d=this;b.readyStatus=2,this.socket.on("data",function(a){if("object"==typeof a){var b=new Buffer(a);try{d.elementReader.onReceivedData(b)}catch(c){return}}}),this.socket.on("error",function(){}),this.socket.on("close",function(){d.socket=null,b.closeByTransport()}),this.socket.on("connection",function(){c()}),c(),this.connectedHost=111,this.connectedPort=111},TCPServerTransport.prototype.send=function(a){try{this.socket.write(a)}catch(b){}},TCPServerTransport.prototype.close=function(){this.socket.end()},module.exports=TCPServerTransport;