Skip to content

Commit 1bfd672

Browse files
committed
docs(examples): protocols and stream muxing 1, 2 and 3
1 parent 7df0074 commit 1bfd672

File tree

4 files changed

+432
-2
lines changed

4 files changed

+432
-2
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict'
2+
3+
const libp2p = require('libp2p')
4+
const TCP = require('libp2p-tcp')
5+
const PeerInfo = require('peer-info')
6+
const waterfall = require('async/waterfall')
7+
const parallel = require('async/parallel')
8+
const pull = require('pull-stream')
9+
10+
class MyBundle extends libp2p {
11+
constructor (peerInfo) {
12+
const modules = {
13+
transport: [new TCP()]
14+
}
15+
super(modules, peerInfo)
16+
}
17+
}
18+
19+
function createNode (callback) {
20+
let node
21+
22+
waterfall([
23+
(cb) => PeerInfo.create(cb),
24+
(peerInfo, cb) => {
25+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
26+
node = new MyBundle(peerInfo)
27+
node.start(cb)
28+
}
29+
], (err) => callback(err, node))
30+
}
31+
32+
parallel([
33+
(cb) => createNode(cb),
34+
(cb) => createNode(cb)
35+
], (err, nodes) => {
36+
if (err) { throw err }
37+
38+
const node1 = nodes[0]
39+
const node2 = nodes[1]
40+
41+
// exact matching
42+
node2.handle('/your-protocol', (protocol, conn) => {
43+
pull(
44+
conn,
45+
pull.map((v) => v.toString()),
46+
pull.log()
47+
)
48+
})
49+
50+
// semver matching
51+
/*
52+
node2.handle('/another-protocol/1.0.1', (protocol, conn) => {
53+
pull(
54+
conn,
55+
pull.map((v) => v.toString()),
56+
pull.log()
57+
)
58+
})
59+
*/
60+
61+
// custom func matching
62+
/*
63+
node2.handle('/custom-match-func', (protocol, conn) => {
64+
pull(
65+
conn,
66+
pull.map((v) => v.toString()),
67+
pull.log()
68+
)
69+
}, (myProtocol, requestedProtocol, callback) => {
70+
if (myProtocol.indexOf(requestedProtocol)) {
71+
callback(null, true)
72+
} else {
73+
callback(null, false)
74+
}
75+
})
76+
*/
77+
78+
node1.dial(node2.peerInfo, '/your-protocol', (err, conn) => {
79+
if (err) { throw err }
80+
pull(pull.values(['my own protocol, wow!']), conn)
81+
})
82+
83+
/*
84+
node1.dial(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
85+
if (err) { throw err }
86+
pull(pull.values(['semver me please']), conn)
87+
})
88+
*/
89+
90+
/*
91+
node1.dial(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
92+
if (err) { throw err }
93+
pull(pull.values(['do I fall into your criteria?']), conn)
94+
})
95+
*/
96+
})
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict'
2+
3+
const libp2p = require('libp2p')
4+
const TCP = require('libp2p-tcp')
5+
const SPDY = require('libp2p-spdy')
6+
const PeerInfo = require('peer-info')
7+
const waterfall = require('async/waterfall')
8+
const parallel = require('async/parallel')
9+
const series = require('async/series')
10+
const pull = require('pull-stream')
11+
12+
class MyBundle extends libp2p {
13+
constructor (peerInfo) {
14+
const modules = {
15+
transport: [new TCP()],
16+
connection: {
17+
muxer: [SPDY]
18+
}
19+
}
20+
super(modules, peerInfo)
21+
}
22+
}
23+
24+
function createNode (callback) {
25+
let node
26+
27+
waterfall([
28+
(cb) => PeerInfo.create(cb),
29+
(peerInfo, cb) => {
30+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
31+
node = new MyBundle(peerInfo)
32+
node.start(cb)
33+
}
34+
], (err) => callback(err, node))
35+
}
36+
37+
parallel([
38+
(cb) => createNode(cb),
39+
(cb) => createNode(cb)
40+
], (err, nodes) => {
41+
if (err) { throw err }
42+
43+
const node1 = nodes[0]
44+
const node2 = nodes[1]
45+
46+
node2.handle('/a', (protocol, conn) => {
47+
pull(
48+
conn,
49+
pull.map((v) => v.toString()),
50+
pull.log()
51+
)
52+
})
53+
54+
node2.handle('/b', (protocol, conn) => {
55+
pull(
56+
conn,
57+
pull.map((v) => v.toString()),
58+
pull.log()
59+
)
60+
})
61+
62+
series([
63+
(cb) => node1.dial(node2.peerInfo, '/a', (err, conn) => {
64+
if (err) { throw err }
65+
pull(pull.values(['protocol (a)']), conn)
66+
cb()
67+
}),
68+
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
69+
if (err) { throw err }
70+
pull(pull.values(['protocol (b)']), conn)
71+
cb()
72+
}),
73+
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
74+
if (err) { throw err }
75+
pull(pull.values(['another conn on protocol (b)']), conn)
76+
cb()
77+
})
78+
])
79+
})
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict'
2+
3+
const libp2p = require('libp2p')
4+
const TCP = require('libp2p-tcp')
5+
const SPDY = require('libp2p-spdy')
6+
const PeerInfo = require('peer-info')
7+
const waterfall = require('async/waterfall')
8+
const parallel = require('async/parallel')
9+
const series = require('async/series')
10+
const pull = require('pull-stream')
11+
12+
class MyBundle extends libp2p {
13+
constructor (peerInfo) {
14+
const modules = {
15+
transport: [new TCP()],
16+
connection: {
17+
muxer: [SPDY]
18+
}
19+
}
20+
super(modules, peerInfo)
21+
}
22+
}
23+
24+
function createNode (callback) {
25+
let node
26+
27+
waterfall([
28+
(cb) => PeerInfo.create(cb),
29+
(peerInfo, cb) => {
30+
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
31+
node = new MyBundle(peerInfo)
32+
node.start(cb)
33+
}
34+
], (err) => callback(err, node))
35+
}
36+
37+
parallel([
38+
(cb) => createNode(cb),
39+
(cb) => createNode(cb)
40+
], (err, nodes) => {
41+
if (err) { throw err }
42+
43+
const node1 = nodes[0]
44+
const node2 = nodes[1]
45+
46+
node1.handle('/node-1', (protocol, conn) => {
47+
pull(
48+
conn,
49+
pull.map((v) => v.toString()),
50+
pull.log()
51+
)
52+
})
53+
54+
node2.handle('/node-2', (protocol, conn) => {
55+
pull(
56+
conn,
57+
pull.map((v) => v.toString()),
58+
pull.log()
59+
)
60+
})
61+
62+
series([
63+
(cb) => node1.dial(node2.peerInfo, '/node-2', (err, conn) => {
64+
if (err) { throw err }
65+
pull(pull.values(['from 1 to 2']), conn)
66+
cb()
67+
}),
68+
(cb) => node2.dial(node1.peerInfo, '/node-1', (err, conn) => {
69+
if (err) { throw err }
70+
pull(pull.values(['from 2 to 1']), conn)
71+
cb()
72+
})
73+
], (err) => {
74+
if (err) { throw err }
75+
console.log('Addresses by which both peers are connected')
76+
node1.peerBook
77+
.getAllArray()
78+
.forEach((peer) => console.log('node 1 to node 2:', peer.isConnected().toString()))
79+
node2.peerBook
80+
.getAllArray()
81+
.forEach((peer) => console.log('node 2 to node 1:', peer.isConnected().toString()))
82+
})
83+
})

0 commit comments

Comments
 (0)