Skip to content

Commit a997237

Browse files
committed
Merge pull request libp2p#70 from diasdavid/feat/hangUp
add hangup feature
2 parents 896fe7a + bcc6690 commit a997237

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ dial uses the best transport (whatever works first, in the future we can have so
102102
- `protocol`
103103
- `callback`
104104

105+
### `swarm.hangUp(pi, callback)`
106+
107+
hangUp the muxedConn we have with the peer
108+
109+
- `pi` - peer info project
110+
- `callback`
111+
105112
### `swarm.listen(callback)`
106113

107114
Start listening on all added transports that are available on the current `peerInfo`.

src/dial.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module.exports = function dial (swarm) {
137137

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

140-
muxedConn.on('close', () => {
140+
muxedConn.once('close', () => {
141141
delete swarm.muxedConns[pi.id.toB58String()]
142142
swarm.emit('peer-mux-closed', pi)
143143
})

src/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ function Swarm (peerInfo) {
103103
}
104104
}
105105

106+
this.hangUp = (peerInfo, callback) => {
107+
const key = peerInfo.id.toB58String()
108+
if (this.muxedConns[key]) {
109+
const muxer = this.muxedConns[key].muxer
110+
muxer.end()
111+
muxer.on('close', () => {
112+
delete this.muxedConns[key]
113+
callback()
114+
})
115+
} else {
116+
callback()
117+
}
118+
}
119+
106120
this.close = (callback) => {
107121
Object.keys(this.muxedConns).forEach((key) => {
108122
this.muxedConns[key].muxer.end()

test/09-swarm-with-muxing.node.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,30 @@ describe('high level API - with everything mixed all together!', function () {
213213
})
214214
})
215215

216+
it('hangUp', (done) => {
217+
let count = 0
218+
const ready = () => ++count === 3 ? done() : null
219+
220+
swarmB.once('peer-mux-closed', (peerInfo) => {
221+
expect(Object.keys(swarmA.muxedConns).length).to.equal(0)
222+
ready()
223+
})
224+
225+
swarmA.once('peer-mux-closed', (peerInfo) => {
226+
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
227+
ready()
228+
})
229+
230+
swarmA.hangUp(peerB, (err) => {
231+
expect(err).to.not.exist
232+
ready()
233+
})
234+
})
235+
216236
it('close a muxer emits event', (done) => {
217237
parallel([
218238
(cb) => swarmC.close(cb),
219-
(cb) => swarmA.once('peer-mux-closed', () => cb())
239+
(cb) => swarmA.once('peer-mux-closed', (peerInfo) => cb())
220240
], done)
221241
})
222242
})

0 commit comments

Comments
 (0)