Skip to content

Commit

Permalink
docs: update peer and content routing examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Jun 27, 2018
1 parent 6f7baac commit e3a4c01
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 34 deletions.
35 changes: 23 additions & 12 deletions examples/peer-and-content-routing/1.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
'use strict'

const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const KadDHT = require('libp2p-kad-dht')

const defaultsDeep = require('@nodeutils/defaults-deep')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')

class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Mplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
// we add the DHT module that will enable Peer and Content Routing
dht: KadDHT
},
// we add the DHT module that will enable Peer and Content Routing
DHT: KadDHT
config: {
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
dht: true
}
}
}
super(modules, peerInfo)

super(defaultsDeep(_options, defaults))
}
}

Expand All @@ -32,7 +41,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))
Expand Down
35 changes: 23 additions & 12 deletions examples/peer-and-content-routing/2.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
'use strict'

const libp2p = require('libp2p')
const libp2p = require('../../')
const TCP = require('libp2p-tcp')
const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const CID = require('cids')
const KadDHT = require('libp2p-kad-dht')

const defaultsDeep = require('@nodeutils/defaults-deep')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')

class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Mplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
// we add the DHT module that will enable Peer and Content Routing
dht: KadDHT
},
// we add the DHT module that will enable Peer and Content Routing
DHT: KadDHT
config: {
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
dht: true
}
}
}
super(modules, peerInfo)

super(defaultsDeep(_options, defaults))
}
}

Expand All @@ -33,7 +42,9 @@ function createNode (callback) {
(cb) => PeerInfo.create(cb),
(peerInfo, cb) => {
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
node = new MyBundle(peerInfo)
node = new MyBundle({
peerInfo
})
node.start(cb)
}
], (err) => callback(err, node))
Expand Down
30 changes: 20 additions & 10 deletions examples/peer-and-content-routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,27 @@ First, let's update our bundle to support Peer Routing and Content Routing.

```JavaScript
class MyBundle extends libp2p {
constructor (peerInfo) {
const modules = {
transport: [new TCP()],
connection: {
muxer: [Mplex],
crypto: [SECIO]
constructor (_options) {
const defaults = {
modules: {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
// we add the DHT module that will enable Peer and Content Routing
dht: KadDHT
},
// we add the DHT module that will enable Peer and Content Routing
DHT: KadDHT
config: {
dht: {
kBucketSize: 20
},
EXPERIMENTAL: {
// dht must be enabled
dht: true
}
}
}
super(modules, peerInfo)

super(defaultsDeep(_options, defaults))
}
}
```
Expand All @@ -44,7 +54,7 @@ parallel([
], (err) => {
if (err) { throw err }

//
//
node1.peerRouting.findPeer(node3.peerInfo.id, (err, peer) => {
if (err) { throw err }

Expand Down

0 comments on commit e3a4c01

Please sign in to comment.