33const promisify = require ( 'promisify-es6' )
44const debug = require ( 'debug' )
55const OFFLINE_ERROR = require ( '../utils' ) . OFFLINE_ERROR
6- const PeerId = require ( 'peer-id' )
7- const PeerInfo = require ( 'peer-info' )
86const pull = require ( 'pull-stream/pull' )
97const Pushable = require ( 'pull-pushable' )
108const ndjson = require ( 'pull-ndjson' )
9+ const waterfall = require ( 'async/waterfall' )
1110
1211const log = debug ( 'jsipfs:ping' )
1312log . 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-
2114module . 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}
0 commit comments