diff --git a/src/swarm.js b/src/swarm.js index 3579b8a8..d381e33f 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -35,7 +35,11 @@ function Swarm () { self.port = port } - tcp.createServer(function (socket) { + self.listener = tcp.createServer(function (socket) { + socket.on('error', function (err) { + // self.emit('error', err) + }) + socket.on('close', function () {}) var ms = new Select() ms.handle(socket) ms.addHandler('/spdy/3.1.0', function (ds) { @@ -52,11 +56,18 @@ function Swarm () { // attach multistream handlers to incoming streams conn.on('stream', registerHandles) + conn.on('error', function (err) { + // self.emit('error', err) + }) + conn.on('close', function () { + }) // IDENTIFY DOES THAT FOR US // conn.on('close', function () { delete self.connections[conn.peerId] }) }) }).listen(self.port, ready) + self.listener.on('error', function (err) { self.emit('error', err) }) + } // interface @@ -72,6 +83,7 @@ function Swarm () { var tmp = tcp.connect(multiaddr.toOptions(), function () { socket = tmp + socket.on('error', function (err) { self.emit('error', err) }) next() }) @@ -104,6 +116,7 @@ function Swarm () { self.connections[peer.id.toB58String()] = conn conn.on('close', function () { delete self.connections[peer.id.toB58String()] }) + conn.on('error', function (err) { self.emit('error', err) }) createStream(peer, protocol, cb) }) @@ -136,7 +149,7 @@ function Swarm () { log.info('Registered handler for protocol:', protocol) } - self.close = function (cb) { + self.closeConns = function (cb) { var keys = Object.keys(self.connections) var number = keys.length if (number === 0) { cb() } @@ -148,6 +161,10 @@ function Swarm () { }) } + self.closeListener = function (cb) { + self.listener.close(cb) + } + function registerHandles (spdyStream) { log.info('Preparing stream to handle the registered protocols') var msH = new Select() diff --git a/tests/swarm-test.js b/tests/swarm-test.js index 80711091..66cbf655 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -23,24 +23,34 @@ beforeEach(function (done) { swarmB = new Swarm() var c = new Counter(2, done) - swarmA.listen(4000, function () { + swarmA.listen(8100, function () { peerA = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmA.port)]) c.hit() }) - swarmB.listen(4001, function () { + swarmB.listen(8101, function () { peerB = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmB.port)]) c.hit() }) }) -afterEach(function (done) { - var c = new Counter(2, done) - swarmA.close(function () { +afterEach({ timeout: 5000 }, function (done) { + var c = new Counter(4, done) + swarmA.closeConns(function () { c.hit() + swarmA.closeListener(function () { + console.log('AAA CLOSE') + c.hit() + }) }) - swarmB.close(function () { + + swarmB.closeConns(function () { + console.log('bb') c.hit() + swarmB.closeListener(function () { + console.log('BBB CLOSE') + c.hit() + }) }) }) @@ -58,6 +68,20 @@ experiment('BASE', function () { c.hit() }) }) + + test('Reuse stream (from dialer)', {timeout: false}, function (done) { + var protocol = '/sparkles/3.3.3' + var c = new Counter(2, done) + + swarmB.registerHandle(protocol, function (stream) { + c.hit() + }) + + swarmA.openStream(peerB, protocol, function (err, stream) { + expect(err).to.not.be.instanceof(Error) + c.hit() + }) + }) }) experiment('IDENTIFY', function () {})