Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
feat: muxer selection
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 24, 2017
1 parent 222efa4 commit 0be223a
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 4 deletions.
68 changes: 68 additions & 0 deletions examples/transfer-file/dialer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict'
/* eslint-disable no-console */

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('../../src')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const async = require('async')
let idListener

async.parallel([
(callback) => {
PeerId.createFromJSON(require('./peer-id-dialer'), (err, idDialer) => {
if (err) {
throw err
}
callback(null, idDialer)
})
},
(callback) => {
PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
if (err) {
throw err
}
callback(null, idListener)
})
}
], (err, ids) => {
if (err) throw err
const peerDialer = new PeerInfo(ids[0])
peerDialer.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
const nodeDialer = new Node(peerDialer)

const peerListener = new PeerInfo(ids[1])
idListener = ids[1]
peerListener.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/10333'))
nodeDialer.start((err) => {
if (err) {
throw err
}

console.log('Dialer ready, listening on:')

peerListener.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
})

nodeDialer.dialByPeerInfo(peerListener, '/echo/1.0.0', (err, conn) => {
if (err) {
throw err
}
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')

pull(
pull.values(['hey']),
conn,
pull.through(console.log),
pull.collect((err, data) => {
if (err) {
throw err
}
console.log('received echo:', data.toString())
})
)
})
})
})
51 changes: 51 additions & 0 deletions examples/transfer-file/listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict'
/* eslint-disable no-console */

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const Node = require('../../src')
const multiaddr = require('multiaddr')
const pull = require('pull-stream')

PeerId.createFromJSON(require('./peer-id-listener'), (err, idListener) => {
if (err) {
throw err
}
const peerListener = new PeerInfo(idListener)
peerListener.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/10333'))
const nodeListener = new Node(peerListener)

nodeListener.start((err) => {
if (err) {
throw err
}

nodeListener.swarm.on('peer-mux-established', (peerInfo) => {
console.log(peerInfo.id.toB58String())
})

nodeListener.handle('/echo/1.0.0', (protocol, conn) => {
pull(
conn,
conn
)
})

nodeListener.handle('/hello/1.0.0', (protocol, conn) => {
pull(
pull.values(['hello', 'world']),
conn
)
setTimeout(() => {
console.log('Printing my peerbook')
Object
.keys(nodeListener.peerBook.getAll())
.forEach(console.log)
}, 500)
})
console.log('Listener ready, listening on:')
peerListener.multiaddrs.forEach((ma) => {
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
})
})
})
5 changes: 5 additions & 0 deletions examples/transfer-file/peer-id-dialer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "Qma3GsJmB47xYuyahPZPSadh1avvxfyYQwk8R3UnFrQ6aP",
"privKey": "CAASpwkwggSjAgEAAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAECggEAZnrCJ6IYiLyyRdr9SbKXCNDb4YByGYPEi/HT1aHgIJfFE1PSMjxcdytxfyjP4JJpVtPjiT9JFVU2ddoYu5qJN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J2qki6fDs0HKwq4kojAXI1Md8Th0eobNjsy21fEEJT7uKMJdovI/SErI=",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCaNSDOjPz6T8HZsf7LDpxiQRiN2OjeyIHUS05p8QWOr3EFUCFsC31R4moihE5HN+FxNalUyyFZU//yjf1pdnlMJqrVByJSMa+y2y4x2FucpoCAO97Tx+iWzwlZ2UXEUXM1Y81mhPbeWXy+wP2xElTgIER0Tsn/thoA0SD2u9wJuVvM7dB7cBcHYmqV6JH+KWCedRTum6O1BssqP/4Lbm2+rkrbZ4+oVRoU2DRLoFhKqwqLtylrbuj4XOI3XykMXV5+uQXz1JzubNOB9lsc6K+eRC+w8hhhDuFMgzkZ4qomCnx3uhO67KaICd8yqqBa6PJ/+fBM5Xk4hjyR40bwcf41AgMBAAE="
}
5 changes: 5 additions & 0 deletions examples/transfer-file/peer-id-listener.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "QmcrQZ6RJdpYuGvZqD5QEHAv6qX4BrQLJLQPQUrTrzdcgm",
"privKey": "CAASqAkwggSkAgEAAoIBAQDLZZcGcbe4urMBVlcHgN0fpBymY+xcr14ewvamG70QZODJ1h9sljlExZ7byLiqRB3SjGbfpZ1FweznwNxWtWpjHkQjTVXeoM4EEgDSNO/Cg7KNlU0EJvgPJXeEPycAZX9qASbVJ6EECQ40VR/7+SuSqsdL1hrmG1phpIju+D64gLyWpw9WEALfzMpH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAECggEAQj0obPnVyjxLFZFnsFLgMHDCv9Fk5V5bOYtmxfvcm50us6ye+T8HEYWGUa9RrGmYiLweuJD34gLgwyzE1RwptHPj3tdNsr4NubefOtXwixlWq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qjJ/9U98fVM7rvOraakrhcf9gRwuspguJQnSO9cLj6",
"pubKey": "CAASpgIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDLZZcGcbe4urMBVlcHgN0fpBymY+xcr14ewvamG70QZODJ1h9sljlExZ7byLiqRB3SjGbfpZ1FweznwNxWtWpjHkQjTVXeoM4EEgDSNO/Cg7KNlU0EJvgPJXeEPycAZX9qASbVJ6EECQ40VR/7+SuSqsdL1hrmG1phpIju+D64gLyWpw9WEALfzMpH5I/KvdYDW3N4g6zOD2mZNp5y1gHeXINHWzMF596O72/6cxwyiXV1eJ000k1NVnUyrPjXtqWdVLRk5IU1LFpoQoXZU5X1hKj1a2qt/lZfH5eOrF/ramHcwhrYYw1txf8JHXWO/bbNnyemTHAvutZpTNrsWATfAgMBAAE="
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"main": "src/index.js",
"scripts": {
"test": "aegir-test node",
"test:muxer:spdy": "aegir-test node LIBP2P_MUXER=spdy",
"test:muxer:multiplex": "aegir-test node LIBP2P_MUXER=multiplex",
"test:muxer:both": "aegir-test node LIBP2P_MUXER=spdy, multiplex",
"lint": "aegir-lint",
"release": "aegir-release node",
"release-minor": "aegir-release node --type minor",
Expand Down Expand Up @@ -43,6 +46,7 @@
"dependencies": {
"libp2p": "^0.3.1",
"libp2p-mdns": "^0.5.2",
"libp2p-multiplex": "^0.3.2",
"libp2p-secio": "^0.6.5",
"libp2p-spdy": "^0.10.3",
"libp2p-swarm": "^0.26.10",
Expand All @@ -66,4 +70,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"varunagarwal315 <varunagarwal315@gmail.com>"
]
}
}
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const WebRTCStar = require('libp2p-webrtc-star')
const MulticastDNS = require('libp2p-mdns')
const WS = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
const multiplex = require('libp2p-multiplex')
const secio = require('libp2p-secio')
const libp2p = require('libp2p')

Expand All @@ -21,9 +22,16 @@ class Node extends libp2p {
webRTCStar
],
connection: {
muxer: [
spdy
],
muxer: process.env.LIBP2P_MUXER ? (() => {
const muxerPrefs = process.env.LIBP2P_MUXER
return muxerPrefs.split(',').map((pref) => {
switch (pref) {
case 'spdy': return spdy
case 'multiplex': return multiplex
default: throw new Error(pref + ' muxer not available')
}
})
})() : [spdy],
crypto: [
secio
]
Expand Down

0 comments on commit 0be223a

Please sign in to comment.