Skip to content

Commit

Permalink
feat: dialProtocol and small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Feb 7, 2018
1 parent cd43863 commit 6651401
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 112 deletions.
20 changes: 20 additions & 0 deletions src/content-routing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

module.exports = (node) => {
return {
findProviders: (key, timeout, callback) => {
if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.findProviders(key, timeout, callback)
},
provide: (key, callback) => {
if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.provide(key, callback)
}
}
}
27 changes: 27 additions & 0 deletions src/dht.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

module.exports = (node) => {
return {
put: (key, value, callback) => {
if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.put(key, value, callback)
},
get: (key, callback) => {
if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.get(key, callback)
},
getMany (key, nVals, callback) {
if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.getMany(key, nVals, callback)
}
}
}
48 changes: 48 additions & 0 deletions src/get-peer-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'

const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')

module.exports = (node) => {
/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
return function (peer, callback) {
let p
// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
p = peer
// Multiaddr instance or Multiaddr String
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
if (typeof peer === 'string') {
peer = multiaddr(peer)
}

const peerIdB58Str = peer.getPeerId()
if (!peerIdB58Str) {
throw new Error(`peer multiaddr instance or string must include peerId`)
}

try {
p = node.peerBook.get(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddrs.add(peer)

// PeerId
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
p = node.peerBook.get(peerIdB58Str)
} catch (err) {
return node.peerRouting.findPeer(peer, callback)
}
} else {
return setImmediate(() => callback(new Error('peer type not recognized')))
}

setImmediate(() => callback(null, p))
}
}
147 changes: 35 additions & 112 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const setImmediate = require('async/setImmediate')
const each = require('async/each')
const series = require('async/series')

const Ping = require('libp2p-ping')
const Switch = require('libp2p-switch')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const multiaddr = require('multiaddr')
const Switch = require('libp2p-switch')
const Ping = require('libp2p-ping')

const peerRouting = require('./peer-routing')
const contentRouting = require('./content-routing')
const dht = require('./dht')
const getPeerInfo = require('./get-peer-info')

exports = module.exports

Expand Down Expand Up @@ -76,9 +78,6 @@ class Node extends EventEmitter {
})
}

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

// dht provided components (peerRouting, contentRouting, dht)
if (_modules.DHT) {
this._dht = new this.modules.DHT(this.switch, {
Expand All @@ -87,56 +86,14 @@ class Node extends EventEmitter {
})
}

this.peerRouting = {
findPeer: (id, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}

this._dht.findPeer(id, callback)
}
}
this.peerRouting = peerRouting(this)
this.contentRouting = contentRouting(this)
this.dht = dht(this)

this.contentRouting = {
findProviders: (key, timeout, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}

this._dht.findProviders(key, timeout, callback)
},
provide: (key, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}
this._getPeerInfo = getPeerInfo(this)

this._dht.provide(key, callback)
}
}

this.dht = {
put: (key, value, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}

this._dht.put(key, value, callback)
},
get: (key, callback) => {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}

this._dht.get(key, callback)
},
getMany (key, nVals, callback) {
if (!this._dht) {
return callback(new Error('DHT is not available'))
}

this._dht.getMany(key, nVals, callback)
}
}
// Mount default protocols
Ping.mount(this.switch)
}

/*
Expand Down Expand Up @@ -252,18 +209,22 @@ class Node extends EventEmitter {
return this._isStarted
}

ping (peer, callback) {
dial (peer, callback) {
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)

this._getPeerInfo(peer, (err, peerInfo) => {
if (err) {
return callback(err)
}
if (err) { return callback(err) }

callback(null, new Ping(this.switch, peerInfo))
this.switch.dial(peerInfo, (err, conn) => {
if (err) { return callback(err) }

this.peerBook.put(peerInfo)
callback(null, conn)
})
})
}

dial (peer, protocol, callback) {
dialProtocol (peer, protocol, callback) {
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)

if (typeof protocol === 'function') {
Expand All @@ -272,14 +233,10 @@ class Node extends EventEmitter {
}

this._getPeerInfo(peer, (err, peerInfo) => {
if (err) {
return callback(err)
}
if (err) { return callback(err) }

this.switch.dial(peerInfo, protocol, (err, conn) => {
if (err) {
return callback(err)
}
if (err) { return callback(err) }
this.peerBook.put(peerInfo)
callback(null, conn)
})
Expand All @@ -290,62 +247,28 @@ class Node extends EventEmitter {
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)

this._getPeerInfo(peer, (err, peerInfo) => {
if (err) {
return callback(err)
}
if (err) { return callback(err) }

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

ping (peer, callback) {
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }

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

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

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

/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
_getPeerInfo (peer, callback) {
let p
// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
p = peer
// Multiaddr instance or Multiaddr String
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
if (typeof peer === 'string') {
peer = multiaddr(peer)
}

const peerIdB58Str = peer.getPeerId()
if (!peerIdB58Str) {
throw new Error(`peer multiaddr instance or string must include peerId`)
}

try {
p = this.peerBook.get(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddrs.add(peer)

// PeerId
} else if (PeerId.isPeerId(peer)) {
const peerIdB58Str = peer.toB58String()
try {
p = this.peerBook.get(peerIdB58Str)
} catch (err) {
return this.peerRouting.findPeer(peer, callback)
}
} else {
return setImmediate(() => callback(new Error('peer type not recognized')))
}

setImmediate(() => callback(null, p))
}
}

module.exports = Node
13 changes: 13 additions & 0 deletions src/peer-routing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

module.exports = (node) => {
return {
findPeer: (id, callback) => {
if (!node._dht) {
return callback(new Error('DHT is not available'))
}

node._dht.findPeer(id, callback)
}
}
}

0 comments on commit 6651401

Please sign in to comment.