Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
add hangup feature
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed May 28, 2016
1 parent 896fe7a commit bcc6690
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ dial uses the best transport (whatever works first, in the future we can have so
- `protocol`
- `callback`

### `swarm.hangUp(pi, callback)`

hangUp the muxedConn we have with the peer

- `pi` - peer info project
- `callback`

### `swarm.listen(callback)`

Start listening on all added transports that are available on the current `peerInfo`.
Expand Down
2 changes: 1 addition & 1 deletion src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module.exports = function dial (swarm) {

swarm.emit('peer-mux-established', pi)

muxedConn.on('close', () => {
muxedConn.once('close', () => {
delete swarm.muxedConns[pi.id.toB58String()]
swarm.emit('peer-mux-closed', pi)
})
Expand Down
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ function Swarm (peerInfo) {
}
}

this.hangUp = (peerInfo, callback) => {
const key = peerInfo.id.toB58String()
if (this.muxedConns[key]) {
const muxer = this.muxedConns[key].muxer
muxer.end()
muxer.on('close', () => {
delete this.muxedConns[key]
callback()
})
} else {
callback()
}
}

this.close = (callback) => {
Object.keys(this.muxedConns).forEach((key) => {
this.muxedConns[key].muxer.end()
Expand Down
22 changes: 21 additions & 1 deletion test/09-swarm-with-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,30 @@ describe('high level API - with everything mixed all together!', function () {
})
})

it('hangUp', (done) => {
let count = 0
const ready = () => ++count === 3 ? done() : null

swarmB.once('peer-mux-closed', (peerInfo) => {
expect(Object.keys(swarmA.muxedConns).length).to.equal(0)
ready()
})

swarmA.once('peer-mux-closed', (peerInfo) => {
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
ready()
})

swarmA.hangUp(peerB, (err) => {
expect(err).to.not.exist
ready()
})
})

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

0 comments on commit bcc6690

Please sign in to comment.