Skip to content

Commit

Permalink
feat: new super simplified API
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Mar 27, 2017
1 parent 5d4d94e commit a6623c1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 124 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ class Node extends libp2p {

> Check if libp2p is started
#### `libp2p.ping(peer [, options], callback)`

> Ping a node in the network
#### `libp2p.peerBook`

> PeerBook instance of the node
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@
},
"homepage": "https://github.com/libp2p/js-libp2p",
"devDependencies": {
"aegir": "^11.0.0",
"aegir": "^11.0.1",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"pre-commit": "^1.2.2"
},
"dependencies": {
"libp2p-ping": "~0.3.2",
"libp2p-swarm": "~0.26.19",
"mafmt": "^2.1.6",
"multiaddr": "^2.2.2",
"multiaddr": "^2.2.3",
"peer-book": "~0.3.1",
"peer-id": "~0.8.4",
"peer-info": "~0.8.4"
Expand All @@ -55,4 +54,4 @@
"greenkeeperio-bot <[email protected]>",
"mayerwin <[email protected]>"
]
}
}
166 changes: 46 additions & 120 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const EE = require('events').EventEmitter
const EventEmitter = require('events').EventEmitter
const assert = require('assert')
const Ping = require('libp2p-ping')
const setImmediate = require('async/setImmediate')

exports = module.exports

const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
const IPFS_CODE = 421

class Node {
class Node extends EventEmitter {
constructor (_modules, _peerInfo, _peerBook, _options) {
super()
assert(_modules, 'requires modules to equip libp2p with features')
assert(_peerInfo, 'requires a PeerInfo instance')

Expand All @@ -26,8 +25,6 @@ class Node {
this.peerBook = _peerBook || new PeerBook()
this.isOnline = false

this.discovery = new EE()

this.swarm = new Swarm(this.peerInfo)

// Attach stream multiplexers
Expand Down Expand Up @@ -66,9 +63,7 @@ class Node {
let discoveries = this.modules.discovery
discoveries = Array.isArray(discoveries) ? discoveries : [discoveries]
discoveries.forEach((discovery) => {
discovery.on('peer', (peerInfo) => {
this.discovery.emit('peer', peerInfo)
})
discovery.on('peer', (peerInfo) => this.emit('peer', peerInfo))
})
}

Expand Down Expand Up @@ -142,88 +137,26 @@ class Node {
this.swarm.close(callback)
}

//
// Ping
//

// TODO
pingById (id, callback) {
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
callback(new Error('not implemented yet'))
}

// TODO
pingByMultiaddr (maddr, callback) {
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
callback(new Error('not implemented yet'))
isOn () {
return this.isOnline
}

pingByPeerInfo (peerInfo, callback) {
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
ping (peer, callback) {
assert(this.isOn, OFFLINE_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)
callback(null, new Ping(this.swarm, peerInfo))
}

//
// Dialing methods
//

// TODO
dialById (id, protocol, callback) {
// NOTE: dialById only works if a previous dial was made. This will
// change once we have PeerRouting

assert(this.isOnline, OFFLINE_ERROR_MESSAGE)

if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}

callback(new Error('not implemented yet'))
}

dialByMultiaddr (maddr, protocol, callback) {
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)

if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}

if (typeof maddr === 'string') {
maddr = multiaddr(maddr)
}

if (!mafmt.IPFS.matches(maddr.toString())) {
return callback(new Error('multiaddr not valid'))
}

const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
if (tuple[0] === IPFS_CODE) {
return true
}
})[0][1]

let peer
try {
peer = this.peerBook.getByB58String(ipfsIdB58String)
} catch (err) {
peer = new PeerInfo(PeerId.createFromB58String(ipfsIdB58String))
}

peer.multiaddr.add(maddr)
this.dialByPeerInfo(peer, protocol, callback)
}

dialByPeerInfo (peer, protocol, callback) {
dial (peer, protocol, callback) {
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)

if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}

this.swarm.dial(peer, protocol, (err, conn) => {
this.swarm.dial(peerInfo, protocol, (err, conn) => {
if (err) {
return callback(err)
}
Expand All @@ -232,59 +165,52 @@ class Node {
})
}

//
// Disconnecting (hangUp) methods
//

hangUpById (id, callback) {
// TODO
callback(new Error('not implemented yet'))
}

hangUpByMultiaddr (maddr, callback) {
hangUp (peer, callback) {
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)
const peerInfo = this._getPeerInfo(peer)

if (typeof maddr === 'string') {
maddr = multiaddr(maddr)
}

if (!mafmt.IPFS.matches(maddr.toString())) {
return callback(new Error('multiaddr not valid'))
}

const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
if (tuple[0] === IPFS_CODE) {
return true
}
})[0][1]

try {
const pi = this.peerBook.getByB58String(ipfsIdB58String)
this.hangUpByPeerInfo(pi, callback)
} catch (err) {
// already disconnected
callback()
}
}

hangUpByPeerInfo (peer, callback) {
assert(this.isOnline, OFFLINE_ERROR_MESSAGE)

this.peerBook.removeByB58String(peer.id.toB58String())
this.peerBook.removeByB58String(peerInfo.id.toB58String())
this.swarm.hangUp(peer, callback)
}

//
// Protocol multiplexing handling
//

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

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

/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
_getPeerInfo (peer) {
let p
switch (peer) {
case PeerInfo.isPeerInfo(peer): p = peer; break
case multiaddr.isMultiaddr(peer): {
const peerIdB58Str = multiaddr.getPeerId(peer)
try {
p = this.peerBook.getByB58String(peerIdB58Str)
} catch (err) {
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
}
p.multiaddr.add(peer)
} break
case PeerId.isPeerId(peer): {
const peerIdB58Str = peer.toB58String()
try {
p = this.peerBook.getByB58String(peerIdB58Str)
} catch (err) {
// TODO this is where PeerRouting comes into place
throw new Error('No knowledge about: ' + peerIdB58Str)
}
} break
default: throw new Error('Peer type not recognized')
}

return p
}
}

module.exports = Node

0 comments on commit a6623c1

Please sign in to comment.