Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using swarm example #3

Merged
merged 2 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/level1-swarm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Using libp2p-swarm
==================


37 changes: 37 additions & 0 deletions examples/level1-swarm/node-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')

// set up

var mh = multiaddr('/ip4/127.0.0.1/tcp/8010')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)

// create a libp2p node

var node = new Libp2p(sw)

node.swarm.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8010}, function () {
// Ready to receive incoming connections

sw.addStreamMuxer('spdy', Spdy, {})

// dial to another node

var mh2 = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p2 = new Peer(Id.create(), [mh2])

node.swarm.dial(p2, {}, '/sparkles/1.0.0', function (err, conn) {
if (err) {
return console.error(err)
}

console.log('-> connection is ready')
process.stdin.pipe(conn).pipe(process.stdout)
})
})
29 changes: 29 additions & 0 deletions examples/level1-swarm/node-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var Swarm = require('libp2p-swarm')
var tcp = require('libp2p-tcp')
var multiaddr = require('multiaddr')
var Id = require('peer-id')
var Spdy = require('libp2p-spdy')
var Libp2p = require('../../src')
var Peer = require('peer-info')

// set up

var mh = multiaddr('/ip4/127.0.0.1/tcp/8020')
var p = new Peer(Id.create(), [])
var sw = new Swarm(p)

sw.addTransport('tcp', tcp, {multiaddr: mh}, {}, {port: 8020}, function () {
// Ready to receive incoming connections

sw.addStreamMuxer('spdy', Spdy, {})

// create a libp2p node

var node = new Libp2p(sw)

// handle/mount a protocol

node.swarm.handleProtocol('/sparkles/1.0.0', function (conn) {
process.stdin.pipe(conn).pipe(process.stdout)
})
})
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
"devDependencies": {
"code": "^1.5.0",
"lab": "^5.17.0",
"libp2p-spdy": "^0.1.0",
"libp2p-swarm": "^0.5.5",
"libp2p-tcp": "^0.1.2",
"multiaddr": "^1.1.1",
"peer-id": "^0.4.0",
"peer-info": "^0.4.0",
"pre-commit": "^1.1.1",
"standard": "^5.3.1"
}
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

exports = module.exports = Libp2p

function Libp2p (swarm, peerRouting, recordStore) {
Expand Down