a little cleanup
Jeromy committed
Jan 9, 2015 at 04:13 UTC
0794d5b46a42717ec16a8578902aaff7752ffd4a
1 file changed
+25
-4
core/commands/ping.go
+25
-4
@@ -2,6 +2,7 @@ package commands
2
3
import (
4
"bytes"
5
+ "errors"
6
"fmt"
7
"io"
8
"time"
@@ -13,6 +14,8 @@ import (
14
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
15
)
16
17
+const kPingTimeout = 10 * time.Second
18
+
19
type PingResult struct {
20
Success bool
21
Time time.Duration
@@ -30,7 +33,7 @@ send pings, wait for pongs, and print out round-trip latency information.
33
`,
34
},
35
Arguments: []cmds.Argument{
33
- cmds.StringArg("peer-id", true, true, "ID of peer to ping"),
36
+ cmds.StringArg("count", false, true, "Number of pings to perform"),
37
},
38
Marshalers: cmds.MarshalerMap{
39
cmds.Text: func(res cmds.Response) (io.Reader, error) {
@@ -66,15 +69,32 @@ send pings, wait for pongs, and print out round-trip latency information.
69
return nil, err
70
}
71
72
+ // Must be online!
73
if !n.OnlineMode() {
74
return nil, errNotOnline
75
}
76
73
- peerID, err := peer.IDB58Decode("QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ")
77
+ if len(req.Arguments()) == 0 {
78
+ return nil, errors.New("no peer specified!")
79
+ }
80
+
81
+ // Set up number of pings
82
+ numPings := 10
83
+ val, found, err := req.Option("count").Int()
84
if err != nil {
85
return nil, err
86
}
77
- const kPingTimeout = 10 * time.Second
87
+ if found {
88
+ numPings = val
89
+ }
90
+
91
+ // One argument of input required, must be base58 encoded peerID
92
+ peerID, err := peer.IDB58Decode(req.Arguments()[0])
93
+ if err != nil {
94
+ return nil, err
95
+ }
96
+
97
+ // Make sure we can find the node in question
98
ctx, _ := context.WithTimeout(context.Background(), kPingTimeout)
99
p, err := n.Routing.FindPeer(ctx, peerID)
100
if err != nil {
@@ -85,11 +105,12 @@ send pings, wait for pongs, and print out round-trip latency information.
105
106
go func() {
107
defer close(outChan)
88
- for i := 0; i < 10; i++ {
108
+ for i := 0; i < numPings; i++ {
109
ctx, _ = context.WithTimeout(context.Background(), kPingTimeout)
110
before := time.Now()
111
err := n.Routing.Ping(ctx, p.ID)
112
if err != nil {
113
+ log.Errorf("Ping error: %s", err)
114
outChan <- &PingResult{}
115
break
116
}