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

Commit 89c61e3

Browse files
committed
chore: refactor ping component and some cleanup
1 parent ca0438a commit 89c61e3

File tree

3 files changed

+63
-46
lines changed

3 files changed

+63
-46
lines changed

src/cli/commands/ping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const print = require('../utils').print
99
module.exports = {
1010
command: 'ping <peerId>',
1111

12-
describe: 'Measure the latency of a connection',
12+
description: 'Measure the latency of a connection',
1313

1414
builder: {
1515
count: {

src/core/components/ping.js

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33
const promisify = require('promisify-es6')
44
const debug = require('debug')
55
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
6-
const PeerId = require('peer-id')
7-
const PeerInfo = require('peer-info')
86
const pull = require('pull-stream/pull')
97
const Pushable = require('pull-pushable')
108
const ndjson = require('pull-ndjson')
9+
const waterfall = require('async/waterfall')
1110

1211
const log = debug('jsipfs:ping')
1312
log.error = debug('jsipfs:ping:error')
1413

15-
function getPacket (msg) {
16-
// Default msg
17-
const basePacket = {Success: false, Time: 0, Text: ''}
18-
return Object.assign({}, basePacket, msg)
19-
}
20-
2114
module.exports = function ping (self) {
2215
return promisify((peerId, count, cb) => {
2316
if (!self.isOnline()) {
@@ -31,49 +24,73 @@ module.exports = function ping (self) {
3124
ndjson.serialize()
3225
)
3326

34-
let peer
35-
try {
36-
peer = self._libp2pNode.peerBook.get(peerId)
37-
} catch (err) {
38-
// Conforming with go implemmentation, not sure if makes sense to log this
39-
// since we perform no `findPeer`
40-
source.push(getPacket({Success: true, Text: `Looking up peer ${peerId}`}))
41-
peer = new PeerInfo(PeerId.createFromB58String(peerId))
42-
}
27+
waterfall([
28+
getPeer.bind(null, self._libp2pNode, source, peerId),
29+
runPing.bind(null, self._libp2pNode, source, count)
30+
], (err) => {
31+
log.error(err)
32+
source.push(getPacket({Text: err.toString()}))
33+
return source.end(err)
34+
})
4335

44-
self._libp2pNode.ping(peer, (err, p) => {
45-
if (err) {
46-
log.error(err)
47-
source.push(getPacket({Text: err.toString()}))
48-
return source.end(err)
49-
}
36+
cb(null, response)
37+
})
38+
}
39+
40+
function getPacket (msg) {
41+
// Default msg
42+
const basePacket = {Success: false, Time: 0, Text: ''}
43+
return Object.assign({}, basePacket, msg)
44+
}
5045

51-
let packetCount = 0
52-
let totalTime = 0
53-
source.push(getPacket({Success: true, Text: `PING ${peerId}`}))
46+
function getPeer (libp2pNode, statusStream, peerId, cb) {
47+
let peer
48+
try {
49+
peer = libp2pNode.peerBook.get(peerId)
50+
console.log(peer)
51+
return cb(null, peer)
52+
} catch (err) {
53+
// Check if we have support for peerRouting
54+
if (!libp2pNode.peerRouting) {
55+
return cb(new Error('Peer not found in peer book and no peer routing mechanism enabled'))
56+
}
57+
// Share lookup status just as in the go implemmentation
58+
statusStream.push(getPacket({Success: true, Text: `Looking up peer ${peerId}`}))
59+
libp2pNode.peerRouting.findPeer(peerId, cb)
60+
}
61+
}
5462

55-
p.on('ping', (time) => {
56-
source.push(getPacket({ Success: true, Time: time }))
57-
totalTime += time
58-
packetCount++
59-
if (packetCount >= count) {
60-
const average = totalTime / count
61-
p.stop()
62-
source.push(getPacket({ Success: true, Text: `Average latency: ${average}ms` }))
63-
source.end()
64-
}
65-
})
63+
function runPing (libp2pNode, statusStream, count, peer, cb) {
64+
libp2pNode.ping(peer, (err, p) => {
65+
if (err) {
66+
return cb(err)
67+
}
68+
69+
let packetCount = 0
70+
let totalTime = 0
71+
statusStream.push(getPacket({Success: true, Text: `PING ${peer.id.toB58String()}`}))
6672

67-
p.on('error', (err) => {
68-
log.error(err)
73+
p.on('ping', (time) => {
74+
statusStream.push(getPacket({ Success: true, Time: time }))
75+
totalTime += time
76+
packetCount++
77+
if (packetCount >= count) {
78+
const average = totalTime / count
6979
p.stop()
70-
source.push(getPacket({Text: err.toString()}))
71-
source.end(err)
72-
})
80+
statusStream.push(getPacket({ Success: true, Text: `Average latency: ${average}ms` }))
81+
statusStream.end()
82+
}
83+
})
7384

74-
p.start()
85+
p.on('error', (err) => {
86+
log.error(err)
87+
p.stop()
88+
statusStream.push(getPacket({Text: err.toString()}))
89+
statusStream.end(err)
7590
})
7691

77-
cb(null, response)
92+
p.start()
93+
94+
return cb()
7895
})
7996
}

test/cli/ping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const config = {
2323
}
2424
}
2525

26-
describe.only('ping', function () {
26+
describe('ping', function () {
2727
this.timeout(60 * 1000)
2828
let ipfsdA
2929
let ipfsdB

0 commit comments

Comments
 (0)