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

Commit

Permalink
feat(bitswap.wantlist) add peer parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed May 15, 2018
1 parent 81907be commit 41a0d49
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/cli/commands/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ module.exports = {
},

handler (argv) {
// TODO: handle argv.peer
argv.ipfs.bitswap.wantlist((err, res) => {
argv.ipfs.bitswap.wantlist(argv.peer, (err, res) => {
if (err) {
throw err
}
Expand Down
16 changes: 14 additions & 2 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@ const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
const promisify = require('promisify-es6')
const setImmediate = require('async/setImmediate')
const Big = require('big.js')
const PeerId = require('peer-id')

function formatWantlist (list) {
return Array.from(list).map((e) => e[1])
}

module.exports = function bitswap (self) {
return {
wantlist: promisify((callback) => {
wantlist: promisify((peerId, callback) => {
if (!callback) {
callback = peerId
peerId = undefined
}

if (!self.isOnline()) {
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
}

let list = self._bitswap.getWantlist()
let list
if (peerId) {
peerId = PeerId.createFromB58String(peerId)
list = self._bitswap.wantlistForPeer(peerId)
} else {
list = self._bitswap.getWantlist()
}
list = formatWantlist(list)
callback(null, list)
}),
Expand Down
57 changes: 32 additions & 25 deletions src/http/api/resources/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,48 @@ const parseKey = require('./block').parseKey

exports = module.exports

exports.wantlist = (request, reply) => {
request.server.app.ipfs.bitswap.wantlist((err, list) => {
if (err) {
exports.wantlist = {
handler: (request, reply) => {
const peerId = request.query.arg
let list
try {
list = request.server.app.ipfs.bitswap.wantlist(peerId)
list = list.map((entry) => entry.cid.toBaseEncodedString())
} catch (err) {
return reply(boom.badRequest(err))
}
list = list.map((entry) => entry.cid.toBaseEncodedString())

reply({
Keys: list
})
})
}
}

exports.stat = (request, reply) => {
const ipfs = request.server.app.ipfs
exports.stat = {
handler: (request, reply) => {
const ipfs = request.server.app.ipfs

ipfs.bitswap.stat((err, stats) => {
if (err) {
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}
ipfs.bitswap.stat((err, stats) => {
if (err) {
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

reply({
ProvideBufLen: stats.provideBufLen,
BlocksReceived: stats.blocksReceived,
Wantlist: stats.wantlist,
Peers: stats.peers,
DupBlksReceived: stats.dupBlksReceived,
DupDataReceived: stats.dupDataReceived,
DataReceived: stats.dataReceived,
BlocksSent: stats.blocksSent,
DataSent: stats.dataSent
reply({
ProvideBufLen: stats.provideBufLen,
BlocksReceived: stats.blocksReceived,
Wantlist: stats.wantlist,
Peers: stats.peers,
DupBlksReceived: stats.dupBlksReceived,
DupDataReceived: stats.dupDataReceived,
DataReceived: stats.dataReceived,
BlocksSent: stats.blocksSent,
DataSent: stats.dataSent
})
})
})
}
}

exports.unwant = {
Expand Down
4 changes: 2 additions & 2 deletions src/http/api/routes/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module.exports = (server) => {
method: '*',
path: '/api/v0/bitswap/wantlist',
config: {
handler: resources.bitswap.wantlist
handler: resources.bitswap.wantlist.handler
}
})

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

Expand Down
2 changes: 1 addition & 1 deletion src/http/api/routes/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = (server) => {
method: '*',
path: '/api/v0/stats/bitswap',
config: {
handler: resources.stats.bitswap
handler: resources.stats.bitswap.handler
}
})

Expand Down

0 comments on commit 41a0d49

Please sign in to comment.