|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('debug') |
| 4 | +const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR |
| 5 | +const PeerId = require('peer-id') |
| 6 | +const pull = require('pull-stream') |
| 7 | +const Pushable = require('pull-pushable') |
| 8 | +const waterfall = require('async/waterfall') |
| 9 | + |
| 10 | +const log = debug('jsipfs:pingPullStream') |
| 11 | +log.error = debug('jsipfs:pingPullStream:error') |
| 12 | + |
| 13 | +module.exports = function pingPullStream (self) { |
| 14 | + return (peerId, opts) => { |
| 15 | + if (!self.isOnline()) { |
| 16 | + return pull.error(new Error(OFFLINE_ERROR)) |
| 17 | + } |
| 18 | + |
| 19 | + opts = Object.assign({ count: 10 }, opts) |
| 20 | + |
| 21 | + const source = Pushable() |
| 22 | + |
| 23 | + waterfall([ |
| 24 | + (cb) => getPeer(self._libp2pNode, source, peerId, cb), |
| 25 | + (peer, cb) => runPing(self._libp2pNode, source, opts.count, peer, cb) |
| 26 | + ], (err) => { |
| 27 | + if (err) { |
| 28 | + log.error(err) |
| 29 | + source.push(getPacket({ success: false, text: err.toString() })) |
| 30 | + source.end(err) |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + return source |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function getPacket (msg) { |
| 39 | + // Default msg |
| 40 | + const basePacket = { success: true, time: 0, text: '' } |
| 41 | + return Object.assign(basePacket, msg) |
| 42 | +} |
| 43 | + |
| 44 | +function getPeer (libp2pNode, statusStream, peerId, cb) { |
| 45 | + let peer |
| 46 | + |
| 47 | + try { |
| 48 | + peer = libp2pNode.peerBook.get(peerId) |
| 49 | + } catch (err) { |
| 50 | + log('Peer not found in peer book, trying peer routing') |
| 51 | + // Share lookup status just as in the go implemmentation |
| 52 | + statusStream.push(getPacket({ text: `Looking up peer ${peerId}` })) |
| 53 | + |
| 54 | + // Try to use peerRouting |
| 55 | + try { |
| 56 | + peerId = PeerId.createFromB58String(peerId) |
| 57 | + } catch (err) { |
| 58 | + return cb(Object.assign(err, { |
| 59 | + message: `failed to parse peer address '${peerId}': input isn't valid multihash` |
| 60 | + })) |
| 61 | + } |
| 62 | + |
| 63 | + return libp2pNode.peerRouting.findPeer(peerId, cb) |
| 64 | + } |
| 65 | + |
| 66 | + cb(null, peer) |
| 67 | +} |
| 68 | + |
| 69 | +function runPing (libp2pNode, statusStream, count, peer, cb) { |
| 70 | + libp2pNode.ping(peer, (err, p) => { |
| 71 | + if (err) { |
| 72 | + return cb(err) |
| 73 | + } |
| 74 | + |
| 75 | + log('Got peer', peer) |
| 76 | + |
| 77 | + let packetCount = 0 |
| 78 | + let totalTime = 0 |
| 79 | + statusStream.push(getPacket({ text: `PING ${peer.id.toB58String()}` })) |
| 80 | + |
| 81 | + p.on('ping', (time) => { |
| 82 | + statusStream.push(getPacket({ time: time })) |
| 83 | + totalTime += time |
| 84 | + packetCount++ |
| 85 | + if (packetCount >= count) { |
| 86 | + const average = totalTime / count |
| 87 | + p.stop() |
| 88 | + statusStream.push(getPacket({ text: `Average latency: ${average}ms` })) |
| 89 | + statusStream.end() |
| 90 | + } |
| 91 | + }) |
| 92 | + |
| 93 | + p.on('error', (err) => { |
| 94 | + log.error(err) |
| 95 | + p.stop() |
| 96 | + statusStream.push(getPacket({ success: false, text: err.toString() })) |
| 97 | + statusStream.end(err) |
| 98 | + }) |
| 99 | + |
| 100 | + p.start() |
| 101 | + |
| 102 | + return cb() |
| 103 | + }) |
| 104 | +} |
0 commit comments