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

Update to libp2p-switch #161

Merged
merged 2 commits into from
Feb 7, 2018
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
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"node": ">=6.0.0",
"npm": ">=3.0.0"
},
"pre-commit": [
"pre-push": [
"lint",
"test"
],
Expand All @@ -39,7 +39,7 @@
"dependencies": {
"async": "^2.6.0",
"libp2p-ping": "~0.6.0",
"libp2p-swarm": "~0.35.1",
"libp2p-switch": "~0.36.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this reset versioning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good question - I was thinking along the lines of once the name of the module changes, it might be worth resetting the version, but thinking some more I realize that there is no benefit in doing that. nvm.

"mafmt": "^3.0.2",
"multiaddr": "^3.0.2",
"peer-book": "~0.5.4",
Expand All @@ -53,13 +53,13 @@
"dirty-chai": "^2.0.1",
"electron-webrtc": "~0.3.0",
"libp2p-circuit": "~0.1.4",
"libp2p-kad-dht": "~0.6.0",
"libp2p-mdns": "~0.9.1",
"libp2p-kad-dht": "~0.6.3",
"libp2p-mdns": "~0.9.2",
"libp2p-multiplex": "~0.5.1",
"libp2p-railing": "~0.7.1",
"libp2p-secio": "~0.9.1",
"libp2p-spdy": "~0.11.0",
"libp2p-tcp": "~0.11.2",
"libp2p-tcp": "~0.11.5",
"libp2p-webrtc-star": "~0.13.3",
"libp2p-websockets": "~0.10.4",
"libp2p-websocket-star": "~0.7.2",
Expand Down
38 changes: 19 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const each = require('async/each')
const series = require('async/series')

const Ping = require('libp2p-ping')
const Swarm = require('libp2p-swarm')
const Switch = require('libp2p-switch')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
Expand All @@ -31,28 +31,28 @@ class Node extends EventEmitter {

this._isStarted = false

this.swarm = new Swarm(this.peerInfo, this.peerBook)
this.switch = new Switch(this.peerInfo, this.peerBook)

// Attach stream multiplexers
if (this.modules.connection && this.modules.connection.muxer) {
let muxers = this.modules.connection.muxer
muxers = Array.isArray(muxers) ? muxers : [muxers]
muxers.forEach((muxer) => this.swarm.connection.addStreamMuxer(muxer))
muxers.forEach((muxer) => this.switch.connection.addStreamMuxer(muxer))

// If muxer exists, we can use Identify
this.swarm.connection.reuse()
this.switch.connection.reuse()

// If muxer exists, we can use Relay for listening/dialing
this.swarm.connection.enableCircuitRelay(_options.relay)
this.switch.connection.enableCircuitRelay(_options.relay)

// Received incommind dial and muxer upgrade happened,
// reuse this muxed connection
this.swarm.on('peer-mux-established', (peerInfo) => {
this.switch.on('peer-mux-established', (peerInfo) => {
this.emit('peer:connect', peerInfo)
this.peerBook.put(peerInfo)
})

this.swarm.on('peer-mux-closed', (peerInfo) => {
this.switch.on('peer-mux-closed', (peerInfo) => {
this.emit('peer:disconnect', peerInfo)
})
}
Expand All @@ -62,7 +62,7 @@ class Node extends EventEmitter {
let cryptos = this.modules.connection.crypto
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
cryptos.forEach((crypto) => {
this.swarm.connection.crypto(crypto.tag, crypto.encrypt)
this.switch.connection.crypto(crypto.tag, crypto.encrypt)
})
}

Expand All @@ -77,11 +77,11 @@ class Node extends EventEmitter {
}

// Mount default protocols
Ping.mount(this.swarm)
Ping.mount(this.switch)

// dht provided components (peerRouting, contentRouting, dht)
if (_modules.DHT) {
this._dht = new this.modules.DHT(this.swarm, {
this._dht = new this.modules.DHT(this.switch, {
kBucketSize: 20,
datastore: _options.DHT && _options.DHT.datastore
})
Expand Down Expand Up @@ -167,7 +167,7 @@ class Node extends EventEmitter {
const multiaddrs = this.peerInfo.multiaddrs.toArray()
transports.forEach((transport) => {
if (transport.filter(multiaddrs).length > 0) {
this.swarm.transport.add(
this.switch.transport.add(
transport.tag || transport.constructor.name, transport)
} else if (transport.constructor &&
transport.constructor.name === 'WebSockets') {
Expand All @@ -178,11 +178,11 @@ class Node extends EventEmitter {
})

series([
(cb) => this.swarm.listen(cb),
(cb) => this.switch.start(cb),
(cb) => {
if (ws) {
// always add dialing on websockets
this.swarm.transport.add(ws.tag || ws.constructor.name, ws)
this.switch.transport.add(ws.tag || ws.constructor.name, ws)
}

// all transports need to be setup before discover starts
Expand Down Expand Up @@ -237,7 +237,7 @@ class Node extends EventEmitter {
}
cb()
},
(cb) => this.swarm.close(cb),
(cb) => this.switch.stop(cb),
(cb) => {
this.emit('stop')
cb()
Expand All @@ -259,7 +259,7 @@ class Node extends EventEmitter {
return callback(err)
}

callback(null, new Ping(this.swarm, peerInfo))
callback(null, new Ping(this.switch, peerInfo))
})
}

Expand All @@ -276,7 +276,7 @@ class Node extends EventEmitter {
return callback(err)
}

this.swarm.dial(peerInfo, protocol, (err, conn) => {
this.switch.dial(peerInfo, protocol, (err, conn) => {
if (err) {
return callback(err)
}
Expand All @@ -294,16 +294,16 @@ class Node extends EventEmitter {
return callback(err)
}

this.swarm.hangUp(peerInfo, callback)
this.switch.hangUp(peerInfo, callback)
})
}

handle (protocol, handlerFunc, matchFunc) {
this.swarm.handle(protocol, handlerFunc, matchFunc)
this.switch.handle(protocol, handlerFunc, matchFunc)
}

unhandle (protocol) {
this.swarm.unhandle(protocol)
this.switch.unhandle(protocol)
}

/*
Expand Down
2 changes: 1 addition & 1 deletion test/circuit-relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('circuit relay', function () {
node.start((err) => {
expect(err).to.not.exist()

handlerSpies.push(sinon.spy(node.swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle'))
handlerSpies.push(sinon.spy(node.switch.transports[Circuit.tag].listeners[0].hopHandler, 'handle'))
cb(node)
})
})
Expand Down
6 changes: 3 additions & 3 deletions test/stream-muxing.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ describe('stream muxing', () => {
(cb) => setup(cb),
(cb) => {
// it will just 'warm up a conn'
expect(Object.keys(nodeA.swarm.muxers)).to.have.length(1)
expect(Object.keys(nodeB.swarm.muxers)).to.have.length(1)
expect(Object.keys(nodeA.switch.muxers)).to.have.length(1)
expect(Object.keys(nodeB.switch.muxers)).to.have.length(1)

nodeA.dial(nodeB.peerInfo, (err) => {
expect(err).to.not.exist()
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
cb()
})
},
Expand Down
16 changes: 8 additions & 8 deletions test/transports.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('transports', () => {
function check () {
const peers = nodeA.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
done()
}
})
Expand Down Expand Up @@ -153,7 +153,7 @@ describe('transports', () => {
const peers = nodeA.peerBook.getAll()
expect(err).to.not.exist()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
done()
}
})
Expand Down Expand Up @@ -280,7 +280,7 @@ describe('transports', () => {
function check () {
const peers = node1.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0)
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
done()
}
})
Expand All @@ -291,8 +291,8 @@ describe('transports', () => {

function check () {
if (++counter === 3) {
expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1)
expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1)
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
done()
}
}
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('transports', () => {
function check () {
const peers = node1.peerBook.getAll()
expect(Object.keys(peers)).to.have.length(1)
expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0)
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
done()
}
})
Expand All @@ -400,8 +400,8 @@ describe('transports', () => {

function check () {
if (++counter === 3) {
expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1)
expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1)
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
done()
}
}
Expand Down
Loading