3
3
const promisify = require ( 'promisify-es6' )
4
4
const debug = require ( 'debug' )
5
5
const OFFLINE_ERROR = require ( '../utils' ) . OFFLINE_ERROR
6
- const PeerId = require ( 'peer-id' )
7
- const PeerInfo = require ( 'peer-info' )
8
6
const pull = require ( 'pull-stream/pull' )
9
7
const Pushable = require ( 'pull-pushable' )
10
8
const ndjson = require ( 'pull-ndjson' )
9
+ const waterfall = require ( 'async/waterfall' )
11
10
12
11
const log = debug ( 'jsipfs:ping' )
13
12
log . error = debug ( 'jsipfs:ping:error' )
14
13
15
- function getPacket ( msg ) {
16
- // Default msg
17
- const basePacket = { Success : false , Time : 0 , Text : '' }
18
- return Object . assign ( { } , basePacket , msg )
19
- }
20
-
21
14
module . exports = function ping ( self ) {
22
15
return promisify ( ( peerId , count , cb ) => {
23
16
if ( ! self . isOnline ( ) ) {
@@ -31,49 +24,73 @@ module.exports = function ping (self) {
31
24
ndjson . serialize ( )
32
25
)
33
26
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
+ } )
43
35
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
+ }
50
45
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
+ }
54
62
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 ( ) } ` } ) )
66
72
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
69
79
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
+ } )
73
84
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 )
75
90
} )
76
91
77
- cb ( null , response )
92
+ p . start ( )
93
+
94
+ return cb ( )
78
95
} )
79
96
}
0 commit comments