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

Commit

Permalink
Feat/support new mss (#124)
Browse files Browse the repository at this point in the history
* chore: updaet deps

* feat: support new mss

* docs: update readme
  • Loading branch information
daviddias authored Nov 3, 2016
1 parent 77188b2 commit c199bfc
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 31 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,13 @@ Hang up the muxed connection we have with the peer.

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

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

Handle a new protocol.

- `protocol`
- `handler` - function called when we receive a dial on `protocol. Signature must be `function (conn) {}`
- `handlerFunc` - function called when we receive a dial on `protocol. Signature must be `function (protocol, conn) {}`

This comment has been minimized.

Copy link
@parkan

parkan Nov 11, 2016

ouch, this was a backwards incompatible change! should be a MAJOR semver bump?

- `matchFunc` - matchFunc for multistream-select

### `swarm.unhandle(protocol)`

Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ gulp.task('test:browser:before', (done) => {
createListenerB(ready)
sigS = sigServer.start(15555, ready)

function echo (conn) {
function echo (protocol, conn) {
pull(conn, conn)
}
})
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"chai": "^3.5.0",
"gulp": "^3.9.1",
"libp2p-multiplex": "^0.2.1",
"libp2p-secio": "^0.6.0",
"libp2p-secio": "^0.6.2",
"libp2p-spdy": "^0.10.0",
"libp2p-tcp": "^0.9.1",
"libp2p-webrtc-star": "^0.5.0",
Expand All @@ -60,7 +60,7 @@
"libp2p-identify": "^0.3.0",
"lodash.includes": "^4.3.0",
"multiaddr": "^2.0.3",
"multistream-select": "^0.12.0",
"multistream-select": "^0.13.0",
"peer-id": "^0.8.0",
"peer-info": "^0.8.0",
"protocol-buffers": "^3.1.6"
Expand Down
6 changes: 3 additions & 3 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function connection (swarm) {
swarm.muxers[muxer.multicodec] = muxer

// for listening
swarm.handle(muxer.multicodec, (conn) => {
swarm.handle(muxer.multicodec, (protocol, conn) => {
const muxedConn = muxer.listener(conn)

muxedConn.on('stream', (conn) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ module.exports = function connection (swarm) {

reuse () {
swarm.identify = true
swarm.handle(identify.multicodec, (conn) => {
swarm.handle(identify.multicodec, (protocol, conn) => {
identify.listener(conn, swarm._peerInfo)
})
},
Expand All @@ -82,7 +82,7 @@ module.exports = function connection (swarm) {
}

swarm.unhandle(swarm.crypto.tag)
swarm.handle(tag, (conn) => {
swarm.handle(tag, (protocol, conn) => {
const id = swarm._peerInfo.id
const secure = encrypt(id, id.privKey, conn)

Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ function Swarm (peerInfo) {
}), callback)
}

this.handle = (protocol, handler) => {
this.protocols[protocol] = handler
this.handle = (protocol, handlerFunc, matchFunc) => {
this.protocols[protocol] = {
handlerFunc: handlerFunc,
matchFunc: matchFunc
}
}

this.handle(this.crypto.tag, (conn) => {
this.handle(this.crypto.tag, (protocol, conn) => {
const id = this._peerInfo.id
const wrapped = this.crypto.encrypt(id, id.privKey, conn)
return protocolMuxer(this.protocols, wrapped)
Expand Down
2 changes: 1 addition & 1 deletion src/protocol-muxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function protocolMuxer (protocols, conn) {
return
}

ms.addHandler(protocol, protocols[protocol])
ms.addHandler(protocol, protocols[protocol].handlerFunc, protocols[protocol].matchFunc)
})

ms.handle(conn, (err) => {
Expand Down
4 changes: 2 additions & 2 deletions test/04-muxing-multiplex.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe.skip('stream muxing with multiplex (on TCP)', () => {
})

it('handle + dial on protocol', (done) => {
swarmB.handle('/abacaxi/1.0.0', (conn) => {
swarmB.handle('/abacaxi/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -98,7 +98,7 @@ describe.skip('stream muxing with multiplex (on TCP)', () => {
})

it('dial on protocol, reuse warmed conn', (done) => {
swarmA.handle('/papaia/1.0.0', (conn) => {
swarmA.handle('/papaia/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand Down
20 changes: 13 additions & 7 deletions test/05-muxing-spdy.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('stream muxing with spdy (on TCP)', () => {
})

it('handle + dial on protocol', (done) => {
swarmB.handle('/abacaxi/1.0.0', (conn) => {
swarmB.handle('/abacaxi/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -101,7 +101,7 @@ describe('stream muxing with spdy (on TCP)', () => {
})

it('dial on protocol, reuse warmed conn', (done) => {
swarmA.handle('/papaia/1.0.0', (conn) => {
swarmA.handle('/papaia/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -128,7 +128,7 @@ describe('stream muxing with spdy (on TCP)', () => {
})

it('with Identify, do getPeerInfo', (done) => {
swarmA.handle('/banana/1.0.0', (conn) => {
swarmA.handle('/banana/1.0.0', (protocol, conn) => {
conn.getPeerInfo((err, peerInfoC) => {
expect(err).to.not.exist
expect(peerInfoC.id.toB58String()).to.equal(peerC.id.toB58String())
Expand Down Expand Up @@ -160,8 +160,11 @@ describe('stream muxing with spdy (on TCP)', () => {
let count = 0
const destroyed = () => ++count === 2 ? done() : null

swarmD.handle('/banana/1.0.0', (conn) => {
pull(conn, pull.onEnd(destroyed))
swarmD.handle('/banana/1.0.0', (protocol, conn) => {
pull(
conn,
pull.onEnd(destroyed)
)
})

swarmA.dial(peerD, '/banana/1.0.0', (err, conn) => {
Expand Down Expand Up @@ -210,8 +213,11 @@ describe('stream muxing with spdy (on TCP)', () => {
let count = 0
const destroyed = () => ++count === 2 ? close() : null

swarmE.handle('/avocado/1.0.0', (conn) => {
pull(conn, pull.onEnd(destroyed))
swarmE.handle('/avocado/1.0.0', (protocol, conn) => {
pull(
conn,
pull.onEnd(destroyed)
)
})

swarmF.dial(peerE, '/avocado/1.0.0', (err, conn) => {
Expand Down
4 changes: 2 additions & 2 deletions test/06-conn-upgrade-secio.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('secio conn upgrade (on TCP)', () => {
})

it('handle + dial on protocol', (done) => {
swarmB.handle('/abacaxi/1.0.0', (conn) => {
swarmB.handle('/abacaxi/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -95,7 +95,7 @@ describe('secio conn upgrade (on TCP)', () => {
})

it('dial on protocol, reuse warmed conn', (done) => {
swarmA.handle('/papaia/1.0.0', (conn) => {
swarmA.handle('/papaia/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand Down
6 changes: 3 additions & 3 deletions test/08-swarm-without-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ describe('high level API - 1st without stream multiplexing (on TCP)', () => {
})

it('handle a protocol', (done) => {
swarmB.handle('/bananas/1.0.0', (conn) => {
swarmB.handle('/bananas/1.0.0', (protocol, conn) => {
pull(conn, conn)
})
expect(Object.keys(swarmB.protocols).length).to.equal(2)
done()
})

it('dial on protocol', (done) => {
swarmB.handle('/pineapple/1.0.0', (conn) => {
swarmB.handle('/pineapple/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -68,7 +68,7 @@ describe('high level API - 1st without stream multiplexing (on TCP)', () => {
})

it('dial on protocol (returned conn)', (done) => {
swarmB.handle('/apples/1.0.0', (conn) => {
swarmB.handle('/apples/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand Down
8 changes: 4 additions & 4 deletions test/09-swarm-with-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('high level API - with everything mixed all together!', () => {
})

it('dial from tcp to tcp+ws, on protocol', (done) => {
swarmB.handle('/anona/1.0.0', (conn) => {
swarmB.handle('/anona/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -169,7 +169,7 @@ describe('high level API - with everything mixed all together!', () => {
})

it('dial from ws to ws', (done) => {
swarmE.handle('/abacaxi/1.0.0', (conn) => {
swarmE.handle('/abacaxi/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -192,7 +192,7 @@ describe('high level API - with everything mixed all together!', () => {
})

it('dial from tcp to tcp+ws (returned conn)', (done) => {
swarmB.handle('/grapes/1.0.0', (conn) => {
swarmB.handle('/grapes/1.0.0', (protocol, conn) => {
pull(conn, conn)
})

Expand All @@ -209,7 +209,7 @@ describe('high level API - with everything mixed all together!', () => {
})

it('dial from tcp+ws to tcp+ws', (done) => {
swarmC.handle('/mamao/1.0.0', (conn) => {
swarmC.handle('/mamao/1.0.0', (protocol, conn) => {
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist
expect(peerInfo).to.exist
Expand Down
2 changes: 1 addition & 1 deletion test/browser-03-swarm-with-muxing-plus-webrtc-star.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('high level API (swarm with spdy + webrtc-star)', () => {
})

it('handle proto', () => {
swarm2.handle('/echo/1.0.0', (conn) => {
swarm2.handle('/echo/1.0.0', (protocol, conn) => {
pull(conn, conn)
})
})
Expand Down

0 comments on commit c199bfc

Please sign in to comment.