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 #101 from ipfs/feature/swarm
Browse files Browse the repository at this point in the history
WIP feature/swarm
  • Loading branch information
daviddias committed Apr 18, 2016
2 parents 6f2d879 + f37d97a commit 509b4dd
Show file tree
Hide file tree
Showing 21 changed files with 706 additions and 24 deletions.
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
20 changes: 20 additions & 0 deletions src/cli/commands/swarm/addrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Command = require('ronin').Command
const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

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

options: {},

run: () => {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
// TODO
})
}
})
33 changes: 33 additions & 0 deletions src/cli/commands/swarm/addrs/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Command = require('ronin').Command
const utils = require('../../../utils')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

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

options: {},

run: () => {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

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)
})
})
})
}
})
35 changes: 35 additions & 0 deletions src/cli/commands/swarm/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Command = require('ronin').Command
const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:swarm')
log.error = debug('cli:swarm:error')

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

options: {},

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

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

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])
})
})
}
})
21 changes: 21 additions & 0 deletions src/cli/commands/swarm/disconnect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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: '',

options: {},

run: () => {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}
// TODO
})
}
})
33 changes: 33 additions & 0 deletions src/cli/commands/swarm/peers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Command = require('ronin').Command
const utils = require('../../utils')
const debug = require('debug')
const log = debug('cli:object')
log.error = debug('cli:object:error')

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

options: {},

run: () => {
utils.getIPFS((err, ipfs) => {
if (err) {
throw err
}

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)
})
})
})
}
})
59 changes: 54 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 peerInfoBook = {}

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 @@ -323,11 +326,57 @@ function IPFS (repo) {
libp2pNode.swarm.close(callback)
},
swarm: {
peers: () => {},
addrs: notImpl,
connect: notImpl,
disconnect: notImpl,
filters: notImpl
peers: (callback) => {
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}

callback(null, peerInfoBook)
},
// all the addrs we know
addrs: (callback) => {
if (!libp2pNode) {
return callback(OFFLINE_ERROR)
}
// TODO
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))
peerInfoBook[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: {},
records: {},
Expand Down
11 changes: 9 additions & 2 deletions src/http-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ log.error = debug('api:error')

exports = module.exports

exports.start = (callback) => {
const ipfs = exports.ipfs = new IPFS()
exports.start = (repo, callback) => {
if (typeof repo === 'function') {
callback = repo
repo = undefined
}

const ipfs = exports.ipfs = new IPFS(repo)
ipfs.load(() => {
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
try {
Expand Down Expand Up @@ -67,7 +72,9 @@ exports.start = (callback) => {
exports.stop = (callback) => {
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
fs.unlinkSync(repoPath + '/api')
console.log('->', 'going to stop libp2p')
exports.ipfs.libp2p.stop(() => {
console.log('->', 'going to stop api server')
exports.server.stop(callback)
})
}
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)
}
Loading

0 comments on commit 509b4dd

Please sign in to comment.