Skip to content

Commit

Permalink
Merge pull request libp2p#59 from diasdavid/update/multistream
Browse files Browse the repository at this point in the history
WIP add new version of multistream
  • Loading branch information
daviddias committed May 18, 2016
2 parents 9d958c3 + a224b0b commit 7789f5d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"ip-address": "^5.8.0",
"lodash.contains": "^2.4.3",
"multiaddr": "^2.0.0",
"multistream-select": "^0.6.5",
"multistream-select": "^0.9.0",
"peer-id": "^0.6.6",
"peer-info": "^0.6.2",
"protocol-buffers-stream": "^1.3.1",
Expand All @@ -79,4 +79,4 @@
"Richard Littauer <[email protected]>",
"dignifiedquire <[email protected]>"
]
}
}
11 changes: 7 additions & 4 deletions src/default-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ const multistream = require('multistream-select')

// incomming connection handler
module.exports = function connHandler (protocols, conn) {
var msS = new multistream.Select()

const ms = new multistream.Listener()
Object.keys(protocols).forEach((protocol) => {
if (!protocol) {
return
}

msS.addHandler(protocol, protocols[protocol])
ms.addHandler(protocol, protocols[protocol])
})

msS.handle(conn)
ms.handle(conn, (err) => {
if (err) {
return // the multistream handshake failed
}
})
}
18 changes: 12 additions & 6 deletions src/dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ module.exports = function dial (swarm) {
nextMuxer(muxers.shift())

function nextMuxer (key) {
var msI = new multistream.Interactive()
msI.handle(conn, function () {
msI.select(key, (err, conn) => {
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
if (err) {
return callback(new Error('multistream not supported'))
}
ms.select(key, (err, conn) => {
if (err) {
if (muxers.length === 0) {
cb(new Error('could not upgrade to stream muxing'))
Expand Down Expand Up @@ -144,9 +147,12 @@ module.exports = function dial (swarm) {
}

function protocolHandshake (conn, protocol, cb) {
var msI = new multistream.Interactive()
msI.handle(conn, function () {
msI.select(protocol, (err, conn) => {
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
if (err) {
return callback(err)
}
ms.select(protocol, (err, conn) => {
if (err) {
return callback(err)
}
Expand Down
10 changes: 7 additions & 3 deletions src/identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ exports.exec = (rawConn, muxer, peerInfo, callback) => {

const conn = muxer.newStream()

var msI = new multistream.Interactive()
msI.handle(conn, () => {
msI.select(exports.multicodec, (err, ds) => {
const ms = new multistream.Dialer()
ms.handle(conn, (err) => {
if (err) {
return callback(err)
}

ms.select(exports.multicodec, (err, ds) => {
if (err) {
return callback(err)
}
Expand Down

0 comments on commit 7789f5d

Please sign in to comment.