-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathping.go
63 lines (56 loc) · 1.8 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package commands
import (
"fmt"
"io"
"time"
peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
"github.com/filecoin-project/go-filecoin/api"
)
var pingCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Send echo request packets to p2p network members",
ShortDescription: `
'ping' is a tool to test sending data to other nodes. It finds nodes
via the routing system, sends pings, waits for pongs, and prints out round-
trip latency information.
`,
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("peer ID", true, true, "ID of peer to be pinged.").EnableStdin(),
},
Options: []cmdkit.Option{
cmdkit.UintOption("count", "n", "Number of ping messages to send.").WithDefault(10),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
peerID, err := peer.IDB58Decode(req.Arguments[0])
if err != nil {
return fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments[0], err)
}
numPings, _ := req.Options["count"].(uint)
ch, err := GetAPI(env).Ping().Ping(req.Context, peerID, numPings, time.Second)
if err != nil {
return err
}
for p := range ch {
if err := re.Emit(p); err != nil {
return err
}
}
return nil
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, p *api.PingResult) error {
if len(p.Text) > 0 {
fmt.Fprintln(w, p.Text) // nolint: errcheck
} else if p.Success {
fmt.Fprintf(w, "Pong received: time=%.2f ms\n", p.Time.Seconds()*1000) // nolint: errcheck
} else {
fmt.Fprintf(w, "Pong failed\n") // nolint: errcheck
}
return nil
}),
},
Type: api.PingResult{},
}