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

Commit

Permalink
feat: add .listen method
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 10, 2016
1 parent 3f29ff5 commit 9c8a8bb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ dial uses the best transport (whatever works first, in the future we can have so
- `protocol`
- `callback`

### `swarm.listen(callback)`

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

### `swarm.handle(protocol, handler)`

handle a new protocol.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@
"Pau Ramon Revilla <[email protected]>",
"Richard Littauer <[email protected]>"
]
}
}
18 changes: 17 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ function Swarm (peerInfo) {
msS.handle(conn)
}

function availableTransports (pi) {
const addrs = pi.multiaddrs
return Object.keys(self.transports).filter((ts) => {
// Only listen on transports we actually have addresses for
return self.transports[ts].filter(addrs).length > 0
})
}

// higher level (public) API
this.dial = (pi, protocol, callback) => {
if (typeof protocol === 'function') {
Expand Down Expand Up @@ -279,7 +287,7 @@ function Swarm (peerInfo) {
}

function attemptDial (pi, cb) {
const tKeys = Object.keys(self.transports)
const tKeys = availableTransports(pi)
nextTransport(tKeys.shift())

function nextTransport (key) {
Expand Down Expand Up @@ -366,6 +374,14 @@ function Swarm (peerInfo) {
}
}

// Start listening on all available transports
this.listen = (callback) => {
parallel(availableTransports(peerInfo).map((ts) => (cb) => {
// Listen on the given transport
this.transport.listen(ts, {}, null, cb)
}), callback)
}

this.handle = (protocol, handler) => {
this.protocols[protocol] = handler
}
Expand Down
10 changes: 7 additions & 3 deletions test/09-swarm-with-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ describe('high level API - with everything mixed all together!', function () {

parallel([
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
(cb) => swarmB.transport.listen('tcp', {}, null, cb)
// (cb) => swarmC.transport.listen('tcp', {}, null, cb)
], done)
})

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

parallel([
(cb) => swarmB.transport.listen('ws', {}, null, cb),
(cb) => swarmC.transport.listen('ws', {}, null, cb),
// (cb) => swarmC.transport.listen('ws', {}, null, cb),
(cb) => swarmD.transport.listen('ws', {}, null, cb),
(cb) => swarmE.transport.listen('ws', {}, null, cb)
], done)
})

it('listen automatically', (done) => {
swarmC.listen(done)
})

it('add spdy', () => {
swarmA.connection.addStreamMuxer(spdy)
swarmB.connection.addStreamMuxer(spdy)
Expand Down

0 comments on commit 9c8a8bb

Please sign in to comment.