Skip to content

Commit

Permalink
Merge pull request libp2p#46 from diasdavid/feat/improve-identify
Browse files Browse the repository at this point in the history
Freeze handling conns till identify is finished on the incomming multiplexed streams
  • Loading branch information
daviddias committed May 4, 2016
2 parents 02dd32e + 0143ab6 commit b453bd4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,25 @@ function Swarm (peerInfo) {
var peerIdForConn

muxedConn.on('stream', (conn) => {
if (peerIdForConn) {
conn.peerId = peerIdForConn
function gotId () {
if (peerIdForConn) {
conn.peerId = peerIdForConn
connHandler(conn)
} else {
setTimeout(gotId, 100)
}
}

if (this.identify) {
return gotId()
}

connHandler(conn)
})

// if identify is enabled, attempt to do it for muxer reuse
if (this.identify) {
console.log('exec identify')
identify.exec(conn, muxedConn, peerInfo, (err, pi) => {
if (err) {
return console.log('Identify exec failed', err)
Expand Down
16 changes: 1 addition & 15 deletions test/09-swarm-with-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ describe('high level API - with everything mixed all together!', function () {

it('dial from tcp+ws to tcp+ws', (done) => {
swarmC.handle('/mamao/1.0.0', (conn) => {
expect(conn.peerId).to.exist
conn.pipe(conn)
})

Expand All @@ -204,21 +205,6 @@ describe('high level API - with everything mixed all together!', function () {
})
})

it('again, so that identify had time', (done) => {
swarmC.handle('/mamao/1.0.0', (conn) => {
expect(conn.peerId).to.exist
conn.pipe(conn)
})

swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => {
expect(err).to.not.exist
expect(conn.peerId).to.exist
conn.end()
conn.on('data', () => {}) // let it flow.. let it flooooow
conn.on('end', done)
})
})

it('close a muxer emits event', (done) => {
swarmC.close(() => {})
swarmA.once('peer-mux-closed', (peerInfo) => {
Expand Down

0 comments on commit b453bd4

Please sign in to comment.