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

Commit

Permalink
Merge pull request #490 from ipfs/refactor
Browse files Browse the repository at this point in the history
refactor: bubble swarm up and make a module just for it, preprepare libp2p to be used directly from libp2p instance, take off cat alias, make ping into its own module
  • Loading branch information
daviddias authored Sep 14, 2016
2 parents c346c54 + efe2f76 commit 0e99029
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 140 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"gulp": "^3.9.1",
"idb-plus-blob-store": "^1.1.2",
"idb-pull-blob-store": "^0.4.0",
"interface-ipfs-core": "^0.14.5",
"interface-ipfs-core": "^0.14.6",
"left-pad": "^1.1.1",
"lodash": "^4.15.0",
"ncp": "^2.0.0",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions src/core/components/libp2p.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const Libp2pNode = require('libp2p-ipfs').Node
const promisify = require('promisify-es6')

module.exports = function libp2p (self) {
// TODO Just expose libp2p API directly, this start stop wrapping doesn't make that much sense anymore :)
return {
start: promisify((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) => {
console.log('Swarm listening on', ma.toString())
})
callback()
})

self._libp2pNode.discovery.on('peer', (peerInfo) => {
self._libp2pNode.peerBook.put(peerInfo)
self._libp2pNode.dialByPeerInfo(peerInfo, () => {})
})
self._libp2pNode.swarm.on('peer-mux-established', (peerInfo) => {
self._libp2pNode.peerBook.put(peerInfo)
})
}),
stop: promisify((callback) => {
self._libp2pNode.stop(callback)
})
}
}
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions src/core/components/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

const promisify = require('promisify-es6')

module.exports = function ping (self) {
return promisify((callback) => {
callback(new Error('Not implemented'))
})
}
File renamed without changes.
80 changes: 80 additions & 0 deletions src/core/components/swarm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict'

const multiaddr = require('multiaddr')
const promisify = require('promisify-es6')

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

module.exports = function swarm (self) {
return {
peers: 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)
})
})

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)
})
})

callback(null, mas)
}),
localAddrs: promisify((callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}

callback(null, self._libp2pNode.peerInfo.multiaddrs)
}),
connect: promisify((maddr, callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}

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

self._libp2pNode.dialByMultiaddr(maddr, callback)
}),
disconnect: promisify((maddr, callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}

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

self._libp2pNode.hangUpByMultiaddr(maddr, callback)
}),
filters: promisify((callback) => {
// TODO
throw new Error('Not implemented')
})
}
}
File renamed without changes.
42 changes: 24 additions & 18 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ const PeerBook = require('peer-book')

const defaultRepo = require('./default-repo')

const goOnline = require('./ipfs/go-online')
const goOffline = require('./ipfs/go-offline')
const isOnline = require('./ipfs/is-online')
const load = require('./ipfs/load')
const version = require('./ipfs/version')
const id = require('./ipfs/id')
const repo = require('./ipfs/repo')
const init = require('./ipfs/init')
const bootstrap = require('./ipfs/bootstrap')
const config = require('./ipfs/config')
const block = require('./ipfs/block')
const object = require('./ipfs/object')
const libp2p = require('./ipfs/libp2p')
const files = require('./ipfs/files')
const bitswap = require('./ipfs/bitswap')
const goOnline = require('./components/go-online')
const goOffline = require('./components/go-offline')
const isOnline = require('./components/is-online')
const load = require('./components/load')
const version = require('./components/version')
const id = require('./components/id')
const repo = require('./components/repo')
const init = require('./components/init')
const bootstrap = require('./components/bootstrap')
const config = require('./components/config')
const block = require('./components/block')
const object = require('./components/object')
const libp2p = require('./components/libp2p')
const swarm = require('./components/swarm')
const ping = require('./components/ping')
const files = require('./components/files')
const bitswap = require('./components/bitswap')

exports = module.exports = IPFS

Expand All @@ -35,6 +37,7 @@ function IPFS (repoInstance) {
repoInstance = defaultRepo(repoInstance)
}

// IPFS Core Internals
this._repo = repoInstance
this._peerInfoBook = new PeerBook()
this._peerInfo = null
Expand All @@ -43,21 +46,24 @@ function IPFS (repoInstance) {
this._blockS = new BlockService(this._repo)
this._dagS = new DAGService(this._blockS)

// IPFS Core exposed components
// for booting up a node
this.goOnline = goOnline(this)
this.goOffline = goOffline(this)
this.isOnline = isOnline(this)
this.load = load(this)
this.init = init(this)
// interface-ipfs-core defined API
this.version = version(this)
this.id = id(this)
this.repo = repo(this)
this.init = init(this)
this.bootstrap = bootstrap(this)
this.config = config(this)
this.block = block(this)
this.object = object(this)
this.libp2p = libp2p(this)
this.swarm = this.libp2p.swarm // for interface-ipfs-core sake
this.swarm = swarm(this)
this.files = files(this)
this.cat = files(this).cat // Alias for js-ipfs-api cat
this.bitswap = bitswap(this)
this.ping = ping(this)
}
111 changes: 0 additions & 111 deletions src/core/ipfs/libp2p.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports.parseAddrs = (request, reply) => {
exports.peers = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
request.server.app.ipfs.libp2p.swarm.peers((err, peers) => {
request.server.app.ipfs.swarm.peers((err, peers) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -48,7 +48,7 @@ exports.peers = {
exports.localAddrs = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
request.server.app.ipfs.libp2p.swarm.localAddrs((err, addrs) => {
request.server.app.ipfs.swarm.localAddrs((err, addrs) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -72,7 +72,7 @@ exports.connect = {
handler: (request, reply) => {
const addr = request.pre.args.addr

request.server.app.ipfs.libp2p.swarm.connect(addr, (err) => {
request.server.app.ipfs.swarm.connect(addr, (err) => {
if (err) {
log.error(err)
return reply({
Expand Down
Loading

0 comments on commit 0e99029

Please sign in to comment.