diff --git a/src/identify.js b/src/identify.js index 12757b46..dadcc9eb 100644 --- a/src/identify.js +++ b/src/identify.js @@ -14,7 +14,7 @@ util.inherits(Identify, EventEmmiter) function Identify (swarm, peerSelf) { var self = this - swarm.registerHandle('/ipfs/identify/1.0.0', function (stream) { + swarm.registerHandler('/ipfs/identify/1.0.0', function (stream) { var identifyMsg = {} identifyMsg = {} identifyMsg.sender = exportPeer(peerSelf) @@ -30,8 +30,7 @@ function Identify (swarm, peerSelf) { }) stream.on('end', function () { - console.log(JSON.parse(answer)) - self.emit('thenews', answer) + self.emit('peer-update', answer) }) stream.end() @@ -40,7 +39,7 @@ function Identify (swarm, peerSelf) { // send back our stuff }) - swarm.on('connection', function (spdyConnection) { + swarm.on('connection-unknown', function (spdyConnection) { spdyConnection.request({ path: '/', method: 'GET' @@ -51,28 +50,27 @@ function Identify (swarm, peerSelf) { var msi = new Interactive() msi.handle(stream, function () { msi.select('/ipfs/identify/1.0.0', function (err, ds) { - if (err) { - return console.log('err') - } + if (err) { return console.log('err') } var identifyMsg = {} identifyMsg = {} identifyMsg.sender = exportPeer(peerSelf) // TODO (daviddias) populate with the way I see the other peer - // identifyMsg.receiver = stream.write(JSON.stringify(identifyMsg)) var answer = '' stream.on('data', function (chunk) { - answer += chunk.toString() + answer = answer + chunk.toString() }) stream.on('end', function () { - console.log(JSON.parse(answer)) - // TODO (daviddias), push to the connections list on swarm that we have a new known connection - self.emit('thenews', answer) + answer = JSON.parse(answer) + + swarm.connections[answer.sender.id] = spdyConnection + + self.emit('peer-update', answer) }) stream.end() diff --git a/src/swarm.js b/src/swarm.js index ff0b1fee..1053c378 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -42,7 +42,7 @@ function Swarm () { conn.start(3.1) - self.emit('connection', conn) + self.emit('connection-unknown', conn) // attach multistream handlers to incoming streams conn.on('stream', registerHandles) diff --git a/tests/swarm-test.js b/tests/swarm-test.js index 5e5d22bd..2519a4e7 100644 --- a/tests/swarm-test.js +++ b/tests/swarm-test.js @@ -11,7 +11,8 @@ var expect = Code.expect var multiaddr = require('multiaddr') var Id = require('ipfs-peer-id') var Peer = require('ipfs-peer') -var Swarm = require('../src/index.js') +var Swarm = require('../src/') +var Identify = require('../src/identify') var swarmA var swarmB @@ -75,7 +76,49 @@ experiment('BASE', function () { }) }) -experiment('IDENTIFY', function () {}) +experiment('IDENTIFY', function () { + test('Attach Identify, open a stream, see a peer update', function (done) { + var protocol = '/sparkles/3.3.3' + + var identifyA = new Identify(swarmA, peerA) + var identifyB = new Identify(swarmB, peerB) + + swarmB.registerHandler(protocol, function (stream) {}) + + swarmA.openStream(peerB, protocol, function (err, stream) { + expect(err).to.not.be.instanceof(Error) + }) + + identifyB.on('peer-update', function (answer) { + done() + }) + identifyA.on('peer-update', function (answer) {}) + }) + + test('Attach Identify, open a stream, reuse stream', function (done) { + var protocol = '/sparkles/3.3.3' + + var identifyA = new Identify(swarmA, peerA) + var identifyB = new Identify(swarmB, peerB) + + swarmA.registerHandler(protocol, function (stream) {}) + swarmB.registerHandler(protocol, function (stream) {}) + + swarmA.openStream(peerB, protocol, function (err, stream) { + expect(err).to.not.be.instanceof(Error) + }) + + identifyB.on('peer-update', function (answer) { + expect(Object.keys(swarmB.connections).length).to.equal(1) + swarmB.openStream(peerA, protocol, function (err, stream) { + expect(err).to.not.be.instanceof(Error) + expect(Object.keys(swarmB.connections).length).to.equal(1) + done() + }) + }) + identifyA.on('peer-update', function (answer) {}) + }) +}) experiment('HARDNESS', function () {}) @@ -91,7 +134,7 @@ function Counter (target, callback) { } } -function checkErr (err) { - console.log('err') - expect(err).to.be.instanceof(Error) -} +// function checkErr (err) { +// console.log('err') +// expect(err).to.be.instanceof(Error) +// }