connection reuse with identify

This commit is contained in:
David Dias 2015-07-10 14:06:51 -07:00
parent 0576b18e16
commit b4cc1d5852
3 changed files with 60 additions and 19 deletions

View File

@ -14,7 +14,7 @@ util.inherits(Identify, EventEmmiter)
function Identify (swarm, peerSelf) { function Identify (swarm, peerSelf) {
var self = this var self = this
swarm.registerHandle('/ipfs/identify/1.0.0', function (stream) { swarm.registerHandler('/ipfs/identify/1.0.0', function (stream) {
var identifyMsg = {} var identifyMsg = {}
identifyMsg = {} identifyMsg = {}
identifyMsg.sender = exportPeer(peerSelf) identifyMsg.sender = exportPeer(peerSelf)
@ -30,8 +30,7 @@ function Identify (swarm, peerSelf) {
}) })
stream.on('end', function () { stream.on('end', function () {
console.log(JSON.parse(answer)) self.emit('peer-update', answer)
self.emit('thenews', answer)
}) })
stream.end() stream.end()
@ -40,7 +39,7 @@ function Identify (swarm, peerSelf) {
// send back our stuff // send back our stuff
}) })
swarm.on('connection', function (spdyConnection) { swarm.on('connection-unknown', function (spdyConnection) {
spdyConnection.request({ spdyConnection.request({
path: '/', path: '/',
method: 'GET' method: 'GET'
@ -51,28 +50,27 @@ function Identify (swarm, peerSelf) {
var msi = new Interactive() var msi = new Interactive()
msi.handle(stream, function () { msi.handle(stream, function () {
msi.select('/ipfs/identify/1.0.0', function (err, ds) { msi.select('/ipfs/identify/1.0.0', function (err, ds) {
if (err) { if (err) { return console.log('err') }
return console.log('err')
}
var identifyMsg = {} var identifyMsg = {}
identifyMsg = {} identifyMsg = {}
identifyMsg.sender = exportPeer(peerSelf) identifyMsg.sender = exportPeer(peerSelf)
// TODO (daviddias) populate with the way I see the other peer // TODO (daviddias) populate with the way I see the other peer
// identifyMsg.receiver =
stream.write(JSON.stringify(identifyMsg)) stream.write(JSON.stringify(identifyMsg))
var answer = '' var answer = ''
stream.on('data', function (chunk) { stream.on('data', function (chunk) {
answer += chunk.toString() answer = answer + chunk.toString()
}) })
stream.on('end', function () { stream.on('end', function () {
console.log(JSON.parse(answer)) answer = JSON.parse(answer)
// TODO (daviddias), push to the connections list on swarm that we have a new known connection
self.emit('thenews', answer) swarm.connections[answer.sender.id] = spdyConnection
self.emit('peer-update', answer)
}) })
stream.end() stream.end()

View File

@ -42,7 +42,7 @@ function Swarm () {
conn.start(3.1) conn.start(3.1)
self.emit('connection', conn) self.emit('connection-unknown', conn)
// attach multistream handlers to incoming streams // attach multistream handlers to incoming streams
conn.on('stream', registerHandles) conn.on('stream', registerHandles)

View File

@ -11,7 +11,8 @@ var expect = Code.expect
var multiaddr = require('multiaddr') var multiaddr = require('multiaddr')
var Id = require('ipfs-peer-id') var Id = require('ipfs-peer-id')
var Peer = require('ipfs-peer') var Peer = require('ipfs-peer')
var Swarm = require('../src/index.js') var Swarm = require('../src/')
var Identify = require('../src/identify')
var swarmA var swarmA
var swarmB 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 () {}) experiment('HARDNESS', function () {})
@ -91,7 +134,7 @@ function Counter (target, callback) {
} }
} }
function checkErr (err) { // function checkErr (err) {
console.log('err') // console.log('err')
expect(err).to.be.instanceof(Error) // expect(err).to.be.instanceof(Error)
} // }