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

Commit 509b4dd

Browse files
committed
Merge pull request #101 from ipfs/feature/swarm
WIP feature/swarm
2 parents 6f2d879 + f37d97a commit 509b4dd

File tree

21 files changed

+706
-24
lines changed

21 files changed

+706
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"debug": "^2.2.0",
8181
"fs-blob-store": "^5.2.1",
8282
"hapi": "^13.3.0",
83-
"ipfs-api": "github:ipfs/js-ipfs-api#f3e2d42",
83+
"ipfs-api": "^3.0.1",
8484
"ipfs-blocks": "^0.1.0",
8585
"ipfs-data-importing": "^0.3.3",
8686
"ipfs-merkle-dag": "^0.4.0",

src/cli/commands/swarm/addrs.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const Command = require('ronin').Command
2+
const utils = require('../../utils')
3+
const debug = require('debug')
4+
const log = debug('cli:object')
5+
log.error = debug('cli:object:error')
6+
7+
module.exports = Command.extend({
8+
desc: '',
9+
10+
options: {},
11+
12+
run: () => {
13+
utils.getIPFS((err, ipfs) => {
14+
if (err) {
15+
throw err
16+
}
17+
// TODO
18+
})
19+
}
20+
})

src/cli/commands/swarm/addrs/local.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Command = require('ronin').Command
2+
const utils = require('../../../utils')
3+
const debug = require('debug')
4+
const log = debug('cli:object')
5+
log.error = debug('cli:object:error')
6+
7+
module.exports = Command.extend({
8+
desc: 'List local addresses',
9+
10+
options: {},
11+
12+
run: () => {
13+
utils.getIPFS((err, ipfs) => {
14+
if (err) {
15+
throw err
16+
}
17+
18+
if (!utils.isDaemonOn()) {
19+
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
20+
}
21+
22+
ipfs.swarm.localAddrs((err, res) => {
23+
if (err) {
24+
throw err
25+
}
26+
27+
res.Strings.forEach((addr) => {
28+
console.log(addr)
29+
})
30+
})
31+
})
32+
}
33+
})

src/cli/commands/swarm/connect.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const Command = require('ronin').Command
2+
const utils = require('../../utils')
3+
const debug = require('debug')
4+
const log = debug('cli:swarm')
5+
log.error = debug('cli:swarm:error')
6+
7+
module.exports = Command.extend({
8+
desc: 'Open connection to a given address',
9+
10+
options: {},
11+
12+
run: (address) => {
13+
if (!address) {
14+
throw new Error("Argument 'address' is required")
15+
}
16+
17+
utils.getIPFS((err, ipfs) => {
18+
if (err) {
19+
throw err
20+
}
21+
22+
if (!utils.isDaemonOn()) {
23+
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
24+
}
25+
26+
ipfs.swarm.connect(address, (err, res) => {
27+
if (err) {
28+
throw err
29+
}
30+
31+
console.log(res.Strings[0])
32+
})
33+
})
34+
}
35+
})

src/cli/commands/swarm/disconnect.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Command = require('ronin').Command
2+
const utils = require('../../utils')
3+
// const bs58 = require('bs58')
4+
const debug = require('debug')
5+
const log = debug('cli:object')
6+
log.error = debug('cli:object:error')
7+
8+
module.exports = Command.extend({
9+
desc: '',
10+
11+
options: {},
12+
13+
run: () => {
14+
utils.getIPFS((err, ipfs) => {
15+
if (err) {
16+
throw err
17+
}
18+
// TODO
19+
})
20+
}
21+
})

src/cli/commands/swarm/peers.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Command = require('ronin').Command
2+
const utils = require('../../utils')
3+
const debug = require('debug')
4+
const log = debug('cli:object')
5+
log.error = debug('cli:object:error')
6+
7+
module.exports = Command.extend({
8+
desc: 'List peers with open connections',
9+
10+
options: {},
11+
12+
run: () => {
13+
utils.getIPFS((err, ipfs) => {
14+
if (err) {
15+
throw err
16+
}
17+
18+
if (!utils.isDaemonOn()) {
19+
throw new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
20+
}
21+
22+
ipfs.swarm.peers((err, res) => {
23+
if (err) {
24+
throw err
25+
}
26+
27+
res.Strings.forEach((addr) => {
28+
console.log(addr)
29+
})
30+
})
31+
})
32+
}
33+
})

src/core/index.js

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function IPFS (repo) {
2929
const dagS = new DAGService(blockS)
3030
var peerInfo
3131
var libp2pNode
32+
var peerInfoBook = {}
3233

3334
this.load = (callback) => {
3435
repo.exists((err, exists) => {
@@ -308,6 +309,8 @@ function IPFS (repo) {
308309
}
309310
}
310311

312+
const OFFLINE_ERROR = new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
313+
311314
this.libp2p = {
312315
start: (callback) => {
313316
libp2pNode = new libp2p.Node(peerInfo)
@@ -323,11 +326,57 @@ function IPFS (repo) {
323326
libp2pNode.swarm.close(callback)
324327
},
325328
swarm: {
326-
peers: () => {},
327-
addrs: notImpl,
328-
connect: notImpl,
329-
disconnect: notImpl,
330-
filters: notImpl
329+
peers: (callback) => {
330+
if (!libp2pNode) {
331+
return callback(OFFLINE_ERROR)
332+
}
333+
334+
callback(null, peerInfoBook)
335+
},
336+
// all the addrs we know
337+
addrs: (callback) => {
338+
if (!libp2pNode) {
339+
return callback(OFFLINE_ERROR)
340+
}
341+
// TODO
342+
notImpl()
343+
},
344+
localAddrs: (callback) => {
345+
if (!libp2pNode) {
346+
return callback(OFFLINE_ERROR)
347+
}
348+
349+
callback(null, peerInfo.multiaddrs)
350+
},
351+
connect: (ma, callback) => {
352+
if (!libp2pNode) {
353+
return callback(OFFLINE_ERROR)
354+
}
355+
356+
const idStr = ma.toString().match(/\/ipfs\/(.*)/)
357+
if (!idStr) {
358+
return callback(new Error('invalid multiaddr'))
359+
}
360+
const id = peerId.createFromB58String(idStr[1])
361+
const peer = new PeerInfo(id)
362+
363+
ma = ma.toString().replace(/\/ipfs\/(.*)/, '') // FIXME remove this when multiaddr supports ipfs
364+
365+
peer.multiaddr.add(multiaddr(ma))
366+
peerInfoBook[peer.id.toB58String()] = peer
367+
368+
libp2pNode.swarm.dial(peer, (err) => {
369+
callback(err, id)
370+
})
371+
},
372+
disconnect: (callback) => {
373+
if (!libp2pNode) {
374+
return callback(OFFLINE_ERROR)
375+
}
376+
377+
notImpl()
378+
},
379+
filters: notImpl // TODO
331380
},
332381
routing: {},
333382
records: {},

src/http-api/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ log.error = debug('api:error')
1010

1111
exports = module.exports
1212

13-
exports.start = (callback) => {
14-
const ipfs = exports.ipfs = new IPFS()
13+
exports.start = (repo, callback) => {
14+
if (typeof repo === 'function') {
15+
callback = repo
16+
repo = undefined
17+
}
18+
19+
const ipfs = exports.ipfs = new IPFS(repo)
1520
ipfs.load(() => {
1621
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
1722
try {
@@ -67,7 +72,9 @@ exports.start = (callback) => {
6772
exports.stop = (callback) => {
6873
const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs'
6974
fs.unlinkSync(repoPath + '/api')
75+
console.log('->', 'going to stop libp2p')
7076
exports.ipfs.libp2p.stop(() => {
77+
console.log('->', 'going to stop api server')
7178
exports.server.stop(callback)
7279
})
7380
}

src/http-api/resources/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ exports.repo = require('./repo')
55
exports.object = require('./object')
66
exports.config = require('./config')
77
exports.block = require('./block')
8+
exports.swarm = require('./swarm')

src/http-api/resources/swarm.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict'
2+
3+
const ipfs = require('./../index.js').ipfs
4+
const debug = require('debug')
5+
const log = debug('http-api:block')
6+
log.error = debug('http-api:block:error')
7+
8+
exports = module.exports
9+
10+
// common pre request handler that parses the args and returns `addr` which is assigned to `request.pre.args`
11+
exports.parseAddrs = (request, reply) => {
12+
if (!request.query.arg) {
13+
return reply("Argument 'addr' is required").code(400).takeover()
14+
}
15+
16+
return reply({
17+
addr: request.query.arg
18+
})
19+
}
20+
21+
exports.peers = {
22+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
23+
handler: (request, reply) => {
24+
ipfs.libp2p.swarm.peers((err, peers) => {
25+
if (err) {
26+
log.error(err)
27+
return reply({
28+
Message: err.toString(),
29+
Code: 0
30+
}).code(500)
31+
}
32+
33+
return reply({
34+
Strings: Object.keys(peers)
35+
.map((key) =>
36+
`${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`)
37+
})
38+
})
39+
}
40+
}
41+
42+
exports.localAddrs = {
43+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
44+
handler: (request, reply) => {
45+
ipfs.libp2p.swarm.localAddrs((err, addrs) => {
46+
if (err) {
47+
log.error(err)
48+
return reply({
49+
Message: err.toString(),
50+
Code: 0
51+
}).code(500)
52+
}
53+
54+
return reply({
55+
Strings: addrs.map((addr) => addr.toString())
56+
})
57+
})
58+
}
59+
}
60+
61+
exports.connect = {
62+
// uses common parseAddr method that returns a `addr`
63+
parseArgs: exports.parseAddrs,
64+
65+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
66+
handler: (request, reply) => {
67+
const addr = request.pre.args.addr
68+
69+
ipfs.libp2p.swarm.connect(addr, (err, res) => {
70+
if (err) {
71+
log.error(err)
72+
return reply({
73+
Message: err.toString(),
74+
Code: 0
75+
}).code(500)
76+
}
77+
78+
return reply({
79+
Strings: [`connect ${res.toB58String()} success`]
80+
})
81+
})
82+
}
83+
}

src/http-api/routes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ module.exports = (server) => {
66
require('./object')(server)
77
// require('./repo')(server)
88
require('./config')(server)
9+
require('./swarm')(server)
910
}

0 commit comments

Comments
 (0)