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

Commit 293bcf9

Browse files
author
Melvin Philips
committed
feat: implement ipfs ping flags #928
1 parent 3bca165 commit 293bcf9

File tree

7 files changed

+112
-3
lines changed

7 files changed

+112
-3
lines changed

src/cli/commands/ping.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict'
2+
3+
const print = require('../utils').print
4+
5+
module.exports = {
6+
command: 'ping <peerId>',
7+
8+
describe: 'Measure the latency of a connection',
9+
10+
builder: {
11+
count: {
12+
alias: 'n',
13+
type: 'integer',
14+
default: 10
15+
}
16+
},
17+
18+
handler (argv) {
19+
const peerId = argv.peerId
20+
const count = argv.count || 10
21+
22+
print('PING ' + peerId)
23+
24+
let noOfTimes = 0
25+
let totalTime = 0
26+
27+
const pingCb = (err, p) => {
28+
if (err) {
29+
throw err
30+
}
31+
let time = p.Time
32+
totalTime = totalTime + time
33+
noOfTimes = noOfTimes + 1
34+
print('Pong received: time=' + time + ' ms')
35+
if (noOfTimes === count) {
36+
print('Average latency: ' + totalTime / count + 'ms')
37+
}
38+
}
39+
40+
for (let i = 0; i < count; i++) {
41+
argv.ipfs.ping(peerId, pingCb)
42+
}
43+
}
44+
}

src/core/components/ping.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4+
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
5+
const PeerId = require('peer-id')
6+
const PeerInfo = require('peer-info')
7+
var Readable = require('stream').Readable
48

59
module.exports = function ping (self) {
6-
return promisify((callback) => {
7-
callback(new Error('Not implemented'))
10+
return promisify((peerId, cb) => {
11+
if (!self.isOnline()) {
12+
return cb(new Error(OFFLINE_ERROR))
13+
}
14+
15+
var outputStream = new Readable()
16+
outputStream._read = function (size) {
17+
}
18+
19+
let peer
20+
try {
21+
peer = self._libp2pNode.peerBook.get(peerId)
22+
} catch (err) {
23+
peer = new PeerInfo(PeerId.createFromB58String(peerId))
24+
}
25+
26+
self._libp2pNode.ping(peer, (err, p) => {
27+
p.once('ping', (time) => {
28+
outputStream.push(JSON.stringify([{}, { Success: true, Time: time }, { Text: 'Average latency: ' + time + ' ms' }]))
29+
outputStream.push(null)
30+
p.stop()
31+
cb(err, outputStream)
32+
})
33+
})
834
})
935
}

src/http/api/resources/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports.version = require('./version')
44
exports.id = require('./id')
5+
exports.ping = require('./ping')
56
exports.bootstrap = require('./bootstrap')
67
exports.repo = require('./repo')
78
exports.object = require('./object')

src/http/api/resources/ping.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const boom = require('boom')
4+
5+
exports = module.exports
6+
7+
exports.get = (request, reply) => {
8+
const ipfs = request.server.app.ipfs
9+
const peerId = request.query.arg
10+
11+
ipfs.ping(peerId, (err, outputStream) => {
12+
if (err) {
13+
return reply(boom.badRequest(err))
14+
}
15+
16+
return reply(outputStream).type('application/json').header('x-chunked-output', '1')
17+
})
18+
}

src/http/api/routes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = (server) => {
88
require('./object')(server)
99
// require('./repo')(server)
1010
require('./config')(server)
11+
require('./ping')(server)
1112
require('./swarm')(server)
1213
require('./bitswap')(server)
1314
require('./file')(server)

src/http/api/routes/ping.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const resources = require('./../resources')
4+
5+
module.exports = (server) => {
6+
const api = server.select('API')
7+
8+
api.route({
9+
method: '*',
10+
path: '/api/v0/ping',
11+
config: {
12+
payload: {
13+
parse: false,
14+
output: 'stream'
15+
},
16+
handler: resources.ping.get
17+
}
18+
})
19+
}

test/cli/commands.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
const expect = require('chai').expect
55
const runOnAndOff = require('../utils/on-and-off')
66

7-
const commandCount = 60
7+
const commandCount = 61
88

99
describe('commands', () => runOnAndOff((thing) => {
1010
let ipfs

0 commit comments

Comments
 (0)