Skip to content

Commit e54ebb6

Browse files
Merge pull request libp2p#54 from diasdavid/feat/listen
Feat/listen
2 parents 3f29ff5 + 9c8a8bb commit e54ebb6

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

README.md

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

105+
### `swarm.listen(callback)`
106+
107+
Start listening on all added transports that are available on the current `peerInfo`.
108+
105109
### `swarm.handle(protocol, handler)`
106110

107111
handle a new protocol.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@
7979
"Pau Ramon Revilla <[email protected]>",
8080
"Richard Littauer <[email protected]>"
8181
]
82-
}
82+
}

src/index.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ function Swarm (peerInfo) {
217217
msS.handle(conn)
218218
}
219219

220+
function availableTransports (pi) {
221+
const addrs = pi.multiaddrs
222+
return Object.keys(self.transports).filter((ts) => {
223+
// Only listen on transports we actually have addresses for
224+
return self.transports[ts].filter(addrs).length > 0
225+
})
226+
}
227+
220228
// higher level (public) API
221229
this.dial = (pi, protocol, callback) => {
222230
if (typeof protocol === 'function') {
@@ -279,7 +287,7 @@ function Swarm (peerInfo) {
279287
}
280288

281289
function attemptDial (pi, cb) {
282-
const tKeys = Object.keys(self.transports)
290+
const tKeys = availableTransports(pi)
283291
nextTransport(tKeys.shift())
284292

285293
function nextTransport (key) {
@@ -366,6 +374,14 @@ function Swarm (peerInfo) {
366374
}
367375
}
368376

377+
// Start listening on all available transports
378+
this.listen = (callback) => {
379+
parallel(availableTransports(peerInfo).map((ts) => (cb) => {
380+
// Listen on the given transport
381+
this.transport.listen(ts, {}, null, cb)
382+
}), callback)
383+
}
384+
369385
this.handle = (protocol, handler) => {
370386
this.protocols[protocol] = handler
371387
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ describe('high level API - with everything mixed all together!', function () {
6666

6767
parallel([
6868
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
69-
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
70-
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
69+
(cb) => swarmB.transport.listen('tcp', {}, null, cb)
70+
// (cb) => swarmC.transport.listen('tcp', {}, null, cb)
7171
], done)
7272
})
7373

@@ -86,12 +86,16 @@ describe('high level API - with everything mixed all together!', function () {
8686

8787
parallel([
8888
(cb) => swarmB.transport.listen('ws', {}, null, cb),
89-
(cb) => swarmC.transport.listen('ws', {}, null, cb),
89+
// (cb) => swarmC.transport.listen('ws', {}, null, cb),
9090
(cb) => swarmD.transport.listen('ws', {}, null, cb),
9191
(cb) => swarmE.transport.listen('ws', {}, null, cb)
9292
], done)
9393
})
9494

95+
it('listen automatically', (done) => {
96+
swarmC.listen(done)
97+
})
98+
9599
it('add spdy', () => {
96100
swarmA.connection.addStreamMuxer(spdy)
97101
swarmB.connection.addStreamMuxer(spdy)

0 commit comments

Comments
 (0)