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

Add swarm connect, peers and localAddrs #151

Merged
merged 1 commit into from
Apr 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"debug": "^2.2.0",
"fs-blob-store": "^5.2.1",
"hapi": "^13.3.0",
"ipfs-api": "github:ipfs/js-ipfs-api#f3e2d42",
"ipfs-api": "^3.0.1",
"ipfs-blocks": "^0.1.0",
"ipfs-data-importing": "^0.3.3",
"ipfs-merkle-dag": "^0.4.0",
Expand Down
1 change: 0 additions & 1 deletion src/cli/commands/swarm/addrs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')
Expand Down
18 changes: 15 additions & 3 deletions src/cli/commands/swarm/addrs/local.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const Command = require('ronin').Command
const utils = require('../../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: '',
desc: 'List local addresses',

options: {},

Expand All @@ -15,7 +14,20 @@ module.exports = Command.extend({
if (err) {
throw err
}
// TODO

if (!utils.isDaemonOn()) {
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
}

ipfs.swarm.localAddrs((err, res) => {
if (err) {
throw err
}

res.Strings.forEach((addr) => {
console.log(addr)
})
})
})
}
})
26 changes: 20 additions & 6 deletions src/cli/commands/swarm/connect.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')
const log = debug('cli:swarm')
log.error = debug('cli:swarm:error')

module.exports = Command.extend({
desc: '',
desc: 'Open connection to a given address',

options: {},

run: () => {
run: (address) => {
if (!address) {
throw new Error("Argument 'address' is required")
}

utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
// TODO

if (!utils.isDaemonOn()) {
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
}

ipfs.swarm.connect(address, (err, res) => {
if (err) {
throw err
}

console.log(res.Strings[0])
})
})
}
})
2 changes: 1 addition & 1 deletion src/cli/commands/swarm/disconnect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
// const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')
Expand Down
18 changes: 15 additions & 3 deletions src/cli/commands/swarm/peers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const Command = require('ronin').Command
const utils = require('../../utils')
const bs58 = require('bs58')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

module.exports = Command.extend({
desc: '',
desc: 'List peers with open connections',

options: {},

Expand All @@ -15,7 +14,20 @@ module.exports = Command.extend({
if (err) {
throw err
}
// TODO

if (!utils.isDaemonOn()) {
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
}

ipfs.swarm.peers((err, res) => {
if (err) {
throw err
}

res.Strings.forEach((addr) => {
console.log(addr)
})
})
})
}
})
54 changes: 49 additions & 5 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function IPFS (repo) {
const dagS = new DAGService(blockS)
var peerInfo
var libp2pNode
var peers = {}

this.load = (callback) => {
repo.exists((err, exists) => {
Expand Down Expand Up @@ -308,6 +309,8 @@ function IPFS (repo) {
}
}

const OFFLINE_ERROR = new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')

this.libp2p = {
start: (callback) => {
libp2pNode = new libp2p.Node(peerInfo)
Expand All @@ -324,13 +327,54 @@ function IPFS (repo) {
},
swarm: {
peers: (callback) => {
callback(null, [])
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}

callback(null, peers)
},
// all the addrs we know
addrs: notImpl,
localAddrs: notImpl,
connect: notImpl,
disconnect: notImpl,
addrs: (callback) => {
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}

notImpl()
},
localAddrs: (callback) => {
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}

callback(null, peerInfo.multiaddrs)
},
connect: (ma, callback) => {
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}

const idStr = ma.toString().match(/\/ipfs\/(.*)/)
if (!idStr) {
return callback(new Error('invalid multiaddr'))
}
const id = peerId.createFromB58String(idStr[1])
const peer = new PeerInfo(id)

ma = ma.toString().replace(/\/ipfs\/(.*)/, '') // FIXME remove this when multiaddr supports ipfs

peer.multiaddr.add(multiaddr(ma))
peers[peer.id.toB58String()] = peer
libp2pNode.swarm.dial(peer, (err) => {
callback(err, id)
})
},
disconnect: (callback) => {
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}

notImpl()
},
filters: notImpl // TODO
},
routing: {},
Expand Down
1 change: 1 addition & 0 deletions src/http-api/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ exports.repo = require('./repo')
exports.object = require('./object')
exports.config = require('./config')
exports.block = require('./block')
exports.swarm = require('./swarm')
83 changes: 83 additions & 0 deletions src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict'

const ipfs = require('./../index.js').ipfs
const debug = require('debug')
const log = debug('http-api:block')
log.error = debug('http-api:block:error')

exports = module.exports

// common pre request handler that parses the args and returns `addr` which is assigned to `request.pre.args`
exports.parseAddrs = (request, reply) => {
if (!request.query.arg) {
return reply("Argument 'addr' is required").code(400).takeover()
}

return reply({
addr: request.query.arg
})
}

exports.peers = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
ipfs.libp2p.swarm.peers((err, peers) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

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

exports.localAddrs = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
ipfs.libp2p.swarm.localAddrs((err, addrs) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

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

exports.connect = {
// 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

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

return reply({
Strings: [`connect ${res.toB58String()} success`]
})
})
}
}
1 change: 1 addition & 0 deletions src/http-api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = (server) => {
require('./object')(server)
// require('./repo')(server)
require('./config')(server)
require('./swarm')(server)
}
31 changes: 21 additions & 10 deletions src/http-api/routes/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ module.exports = (server) => {

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

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

api.route({
method: 'GET',
path: '/api/v0/swarm/addrs/local',
Expand All @@ -23,17 +31,20 @@ module.exports = (server) => {
method: 'GET',
path: '/api/v0/swarm/connect',
config: {
handler: resources.swarm.connect
pre: [
{ method: resources.swarm.connect.parseArgs, assign: 'args' }
],
handler: resources.swarm.connect.handler
}
})

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

// TODO
// api.route({
Expand Down
Loading