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

Commit

Permalink
feat(swarm): make interface-ipfs-core compliant
Browse files Browse the repository at this point in the history
Fixes #439
  • Loading branch information
dignifiedquire committed Sep 15, 2016
1 parent d3f98fe commit ef729bb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 37 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"lodash.has": "^4.5.2",
"lodash.set": "^4.3.2",
"lodash.sortby": "^4.7.0",
"lodash.values": "^4.3.0",
"mafmt": "^2.1.2",
"map-limit": "0.0.1",
"multiaddr": "^2.0.3",
Expand Down
23 changes: 13 additions & 10 deletions src/core/components/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const multiaddr = require('multiaddr')
const promisify = require('promisify-es6')
const flatMap = require('lodash.flatmap')
const values = require('lodash.values')

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

Expand All @@ -13,25 +14,32 @@ module.exports = function swarm (self) {
return callback(OFFLINE_ERROR)
}

const mas = collectAddrs(self._libp2pNode.peerBook)
const peers = self._libp2pNode.peerBook.getAll()
const mas = flatMap(Object.keys(peers), (id) => {
return peers[id].multiaddrs
})

callback(null, mas)
}),

// all the addrs we know
addrs: promisify((callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
}

const mas = collectAddrs(self._libp2pNode.peerBook)
callback(null, mas)
const peers = values(self._libp2pNode.peerBook.getAll())
callback(null, peers)
}),

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)
Expand All @@ -43,6 +51,7 @@ module.exports = function swarm (self) {

self._libp2pNode.dialByMultiaddr(maddr, callback)
}),

disconnect: promisify((maddr, callback) => {
if (!self.isOnline()) {
return callback(OFFLINE_ERROR)
Expand All @@ -54,16 +63,10 @@ module.exports = function swarm (self) {

self._libp2pNode.hangUpByMultiaddr(maddr, callback)
}),

filters: promisify((callback) => {
// TODO
throw new Error('Not implemented')
})
}
}

function collectAddrs (book) {
const peers = book.getAll()
return flatMap(Object.keys(peers), (id) => {
return peers[id].multiaddrs
})
}
64 changes: 56 additions & 8 deletions src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,28 @@ 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.swarm.peers((err, peers) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.peers((err, peers) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

return reply({
Strings: peers.map((addr) => addr.toString())
})
})
}
}

exports.addrs = {
handler: (request, reply) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.addrs((err, peers) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -36,19 +55,22 @@ exports.peers = {
}).code(500)
}

const addrs = {}
peers.forEach((peer) => {
addrs[peer.id.toB58String()] = peer.multiaddrs.map((addr) => addr.toString())
})

return reply({
Strings: Object.keys(peers)
.map((key) =>
`${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`)
Addrs: addrs
})
})
}
}

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.swarm.localAddrs((err, addrs) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.localAddrs((err, addrs) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -71,8 +93,9 @@ exports.connect = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const addr = request.pre.args.addr
const ipfs = request.server.app.ipfs

request.server.app.ipfs.swarm.connect(addr, (err) => {
ipfs.swarm.connect(addr, (err) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -87,3 +110,28 @@ exports.connect = {
})
}
}

exports.disconnect = {
// uses common parseAddr method that returns a `addr`
parseArgs: exports.parseAddrs,

// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const addr = request.pre.args.addr
const ipfs = request.server.app.ipfs

ipfs.swarm.disconnect(addr, (err) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

return reply({
Strings: [`disconnect ${addr} success`]
})
})
}
}
31 changes: 17 additions & 14 deletions src/http-api/routes/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ module.exports = (server) => {
}
})

// api.route({
// method: '*',
// path: '/api/v0/swarm/addrs',
// config: {
// handler: resources.swarm.addrs.handler
// }
// })
api.route({
method: '*',
path: '/api/v0/swarm/addrs',
config: {
handler: resources.swarm.addrs.handler
}
})

api.route({
method: '*',
Expand All @@ -40,13 +40,16 @@ module.exports = (server) => {
}
})

// api.route({
// method: '*',
// path: '/api/v0/swarm/disconnect',
// config: {
// handler: resources.swarm.disconnect
// }
// })
api.route({
method: '*',
path: '/api/v0/swarm/disconnect',
config: {
pre: [
{ method: resources.swarm.disconnect.parseArgs, assign: 'args' }
],
handler: resources.swarm.disconnect.handler
}
})

// TODO
// api.route({
Expand Down
7 changes: 2 additions & 5 deletions test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

'use strict'

/*
const test = require('interface-ipfs-core')
const FactoryClient = require('./../../utils/factory-http')

Expand All @@ -17,7 +16,5 @@ const common = {
fc.dismantle(callback)
}
}
*/
// TODO
// Needs: https://github.com/ipfs/js-libp2p-ipfs/pull/16
// test.swarm(common)

test.swarm(common)

0 comments on commit ef729bb

Please sign in to comment.