2018-10-19 17:37:34 +02:00
|
|
|
/* eslint-disable no-console */
|
2017-07-07 18:43:15 +01:00
|
|
|
'use strict'
|
|
|
|
|
2018-06-28 10:06:25 +02:00
|
|
|
const libp2p = require('../../')
|
2017-07-07 18:43:15 +01:00
|
|
|
const TCP = require('libp2p-tcp')
|
|
|
|
const SPDY = require('libp2p-spdy')
|
|
|
|
const PeerInfo = require('peer-info')
|
|
|
|
const waterfall = require('async/waterfall')
|
|
|
|
const parallel = require('async/parallel')
|
|
|
|
const series = require('async/series')
|
|
|
|
const pull = require('pull-stream')
|
2018-06-28 10:06:25 +02:00
|
|
|
const defaultsDeep = require('@nodeutils/defaults-deep')
|
2017-07-07 18:43:15 +01:00
|
|
|
|
|
|
|
class MyBundle extends libp2p {
|
2018-06-28 10:06:25 +02:00
|
|
|
constructor (_options) {
|
|
|
|
const defaults = {
|
|
|
|
modules: {
|
|
|
|
transport: [ TCP ],
|
|
|
|
streamMuxer: [ SPDY ]
|
2017-07-07 18:43:15 +01:00
|
|
|
}
|
|
|
|
}
|
2018-06-28 10:06:25 +02:00
|
|
|
|
|
|
|
super(defaultsDeep(_options, defaults))
|
2017-07-07 18:43:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNode (callback) {
|
|
|
|
let node
|
|
|
|
|
|
|
|
waterfall([
|
|
|
|
(cb) => PeerInfo.create(cb),
|
|
|
|
(peerInfo, cb) => {
|
|
|
|
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
2018-06-28 10:06:25 +02:00
|
|
|
node = new MyBundle({
|
|
|
|
peerInfo
|
|
|
|
})
|
2017-07-07 18:43:15 +01:00
|
|
|
node.start(cb)
|
|
|
|
}
|
|
|
|
], (err) => callback(err, node))
|
|
|
|
}
|
|
|
|
|
|
|
|
parallel([
|
|
|
|
(cb) => createNode(cb),
|
|
|
|
(cb) => createNode(cb)
|
|
|
|
], (err, nodes) => {
|
|
|
|
if (err) { throw err }
|
|
|
|
|
|
|
|
const node1 = nodes[0]
|
|
|
|
const node2 = nodes[1]
|
|
|
|
|
|
|
|
node1.handle('/node-1', (protocol, conn) => {
|
|
|
|
pull(
|
|
|
|
conn,
|
|
|
|
pull.map((v) => v.toString()),
|
|
|
|
pull.log()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
node2.handle('/node-2', (protocol, conn) => {
|
|
|
|
pull(
|
|
|
|
conn,
|
|
|
|
pull.map((v) => v.toString()),
|
|
|
|
pull.log()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
series([
|
2018-06-28 10:06:25 +02:00
|
|
|
(cb) => node1.dialProtocol(node2.peerInfo, '/node-2', (err, conn) => {
|
2017-07-07 18:43:15 +01:00
|
|
|
if (err) { throw err }
|
|
|
|
pull(pull.values(['from 1 to 2']), conn)
|
|
|
|
cb()
|
|
|
|
}),
|
2018-06-28 10:06:25 +02:00
|
|
|
(cb) => node2.dialProtocol(node1.peerInfo, '/node-1', (err, conn) => {
|
2017-07-07 18:43:15 +01:00
|
|
|
if (err) { throw err }
|
|
|
|
pull(pull.values(['from 2 to 1']), conn)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
], (err) => {
|
|
|
|
if (err) { throw err }
|
|
|
|
console.log('Addresses by which both peers are connected')
|
|
|
|
node1.peerBook
|
2017-10-26 04:51:36 -07:00
|
|
|
.getAllArray()
|
|
|
|
.forEach((peer) => console.log('node 1 to node 2:', peer.isConnected().toString()))
|
2017-07-07 18:43:15 +01:00
|
|
|
node2.peerBook
|
2017-10-26 04:51:36 -07:00
|
|
|
.getAllArray()
|
|
|
|
.forEach((peer) => console.log('node 2 to node 1:', peer.isConnected().toString()))
|
2017-07-07 18:43:15 +01:00
|
|
|
})
|
|
|
|
})
|