Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
style(core): cleanup libp2p commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Sep 14, 2016
1 parent c346c54 commit 3acf9c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -81,6 +81,7 @@
"joi": "^9.0.4",
"libp2p-ipfs": "^0.14.1",
"libp2p-ipfs-browser": "^0.15.1",
"lodash.flatmap": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.has": "^4.5.2",
"lodash.set": "^4.3.2",
Expand Down
55 changes: 25 additions & 30 deletions src/core/ipfs/libp2p.js
Expand Up @@ -3,6 +3,7 @@
const multiaddr = require('multiaddr')
const Libp2pNode = require('libp2p-ipfs').Node
const promisify = require('promisify-es6')
const flatMap = require('lodash.flatmap')

const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR

Expand All @@ -11,21 +12,27 @@ module.exports = function libp2p (self) {

return {
start: (callback) => {
self._libp2pNode = new Libp2pNode(self._peerInfo)
self._libp2pNode.start(() => {
// TODO connect to bootstrap nodes, it will get us more addrs
self._libp2pNode.peerInfo.multiaddrs.forEach((ma) => {
const node = self._libp2pNode = new Libp2pNode(self._peerInfo)
node.start((err) => {
if (err) {
return callback(err)
}

// TODO: connect to bootstrap nodes, it will
// get us more addrs
node.peerInfo.multiaddrs.forEach((ma) => {
console.log('Swarm listening on', ma.toString())
})
callback()
})

self._libp2pNode.discovery.on('peer', (peerInfo) => {
self._libp2pNode.peerBook.put(peerInfo)
self._libp2pNode.dialByPeerInfo(peerInfo, () => {})
node.discovery.on('peer', (peerInfo) => {
node.peerBook.put(peerInfo)
node.dialByPeerInfo(peerInfo, () => {})
})
self._libp2pNode.swarm.on('peer-mux-established', (peerInfo) => {
self._libp2pNode.peerBook.put(peerInfo)

node.swarm.on('peer-mux-established', (peerInfo) => {
node.peerBook.put(peerInfo)
})
},
stop: (callback) => {
Expand All @@ -37,35 +44,16 @@ module.exports = function libp2p (self) {
return callback(OFFLINE_ERROR)
}

const peers = self._libp2pNode.peerBook.getAll()
const mas = []
Object
.keys(peers)
.forEach((b58Id) => {
peers[b58Id].multiaddrs.forEach((ma) => {
// TODO this should only print the addr we are using
mas.push(ma)
})
})

const mas = collectAddrs(self._libp2pNode.peerBook)
callback(null, mas)
}),
// all the addrs we know
addrs: promisify((callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}
const peers = self._libp2pNode.peerBook.getAll()
const mas = []
Object
.keys(peers)
.forEach((b58Id) => {
peers[b58Id].multiaddrs.forEach((ma) => {
// TODO this should only print the addr we are using
mas.push(ma)
})
})

const mas = collectAddrs(self._libp2pNode.peerBook)
callback(null, mas)
}),
localAddrs: promisify((callback) => {
Expand Down Expand Up @@ -109,3 +97,10 @@ module.exports = function libp2p (self) {
}
}
}

function collectAddrs (book) {
const peers = book.getAll()
return flatMap(Object.keys(peers), (id) => {
return peers[id].multiaddrs
})
}

0 comments on commit 3acf9c0

Please sign in to comment.