@cryptotaxi247 / kubo / commits / 9fc0f86a3

cleanup from PR

Jeromy committed Jan 10, 2015 at 03:22 UTC 9fc0f86a3a88d38875cb5f5588730059011c3280
1 file changed +53 -55
core/commands/ping.go
+53 -55
@@ -7,6 +7,7 @@ import (
7 "time"
8
9 cmds "github.com/jbenet/go-ipfs/commands"
10 + core "github.com/jbenet/go-ipfs/core"
11 peer "github.com/jbenet/go-ipfs/p2p/peer"
12 u "github.com/jbenet/go-ipfs/util"
13
@@ -25,18 +26,18 @@ var PingCmd = &cmds.Command{
26 Helptext: cmds.HelpText{
27 Tagline: "send echo request packets to IPFS hosts",
28 Synopsis: `
28 -ipfs ping <peer.ID> - Send pings to a peer using the routing system to discover its address
29 -`,
29 +Send pings to a peer using the routing system to discover its address
30 + `,
31 ShortDescription: `
31 -ipfs ping is a tool to find a node (in the routing system),
32 -send pings, wait for pongs, and print out round-trip latency information.
33 -`,
32 + ipfs ping is a tool to find a node (in the routing system),
33 + send pings, wait for pongs, and print out round-trip latency information.
34 + `,
35 },
36 Arguments: []cmds.Argument{
37 cmds.StringArg("peer ID", true, true, "ID of peer to be pinged"),
38 },
39 Options: []cmds.Option{
39 - cmds.IntOption("count", "n"),
40 + cmds.IntOption("count", "n", "number of ping messages to send"),
41 },
42 Marshalers: cmds.MarshalerMap{
43 cmds.Text: func(res cmds.Response) (io.Reader, error) {
@@ -79,12 +80,11 @@ send pings, wait for pongs, and print out round-trip latency information.
80 return nil, errNotOnline
81 }
82
82 - if len(req.Arguments()) == 0 {
83 - return nil, cmds.ClientError("no peer specified!")
83 + peerID, err := peer.IDB58Decode(req.Arguments()[0])
84 + if err != nil {
85 + return nil, err
86 }
87
86 - outChan := make(chan interface{}, 5)
87 -
88 // Set up number of pings
89 numPings := 10
90 val, found, err := req.Option("count").Int()
@@ -95,54 +95,52 @@ send pings, wait for pongs, and print out round-trip latency information.
95 numPings = val
96 }
97
98 - // One argument of input required, must be base58 encoded peerID
99 - peerID, err := peer.IDB58Decode(req.Arguments()[0])
100 - if err != nil {
101 - return nil, err
102 - }
103 -
104 - go func() {
105 - defer close(outChan)
106 -
107 - // Make sure we can find the node in question
108 - outChan <- &PingResult{
109 - Text: fmt.Sprintf("Looking up peer %s", peerID.Pretty()),
110 - }
111 - ctx, _ := context.WithTimeout(context.Background(), kPingTimeout)
112 - p, err := n.Routing.FindPeer(ctx, peerID)
113 - n.Peerstore.AddPeerInfo(p)
114 - if err != nil {
115 - outChan <- &PingResult{Text: "Peer lookup error!"}
116 - outChan <- &PingResult{Text: err.Error()}
117 - return
118 - }
119 - outChan <- &PingResult{
120 - Text: fmt.Sprintf("Peer found, starting pings."),
121 - }
98 + outChan := make(chan interface{})
99
123 - var total time.Duration
124 - for i := 0; i < numPings; i++ {
125 - ctx, _ = context.WithTimeout(context.Background(), kPingTimeout)
126 - took, err := n.Routing.Ping(ctx, p.ID)
127 - if err != nil {
128 - log.Errorf("Ping error: %s", err)
129 - outChan <- &PingResult{}
130 - break
131 - }
132 - outChan <- &PingResult{
133 - Success: true,
134 - Time: took,
135 - }
136 - total += took
137 - time.Sleep(time.Second)
138 - }
139 - averagems := total.Seconds() * 1000 / float64(numPings)
140 - outChan <- &PingResult{
141 - Text: fmt.Sprintf("Average latency: %.2fms", averagems),
142 - }
143 - }()
100 + go pingPeer(n, peerID, numPings, outChan)
101
102 return outChan, nil
103 },
104 Type: PingResult{},
105 }
106 +
107 +func pingPeer(n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interface{}) {
108 + defer close(outChan)
109 +
110 + // Make sure we can find the node in question
111 + outChan <- &PingResult{
112 + Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
113 + }
114 +
115 + // TODO: get master context passed in
116 + ctx, _ := context.WithTimeout(context.TODO(), kPingTimeout)
117 + p, err := n.Routing.FindPeer(ctx, pid)
118 + if err != nil {
119 + outChan <- &PingResult{Text: fmt.Sprintf("Peer lookup error: %s", err)}
120 + return
121 + }
122 + n.Peerstore.AddPeerInfo(p)
123 +
124 + outChan <- &PingResult{Text: fmt.Sprintf("Peer found, starting pings.")}
125 +
126 + var total time.Duration
127 + for i := 0; i < numPings; i++ {
128 + ctx, _ = context.WithTimeout(context.TODO(), kPingTimeout)
129 + took, err := n.Routing.Ping(ctx, p.ID)
130 + if err != nil {
131 + log.Errorf("Ping error: %s", err)
132 + outChan <- &PingResult{Text: fmt.Sprintf("Ping error: %s", err)}
133 + break
134 + }
135 + outChan <- &PingResult{
136 + Success: true,
137 + Time: took,
138 + }
139 + total += took
140 + time.Sleep(time.Second)
141 + }
142 + averagems := total.Seconds() * 1000 / float64(numPings)
143 + outChan <- &PingResult{
144 + Text: fmt.Sprintf("Average latency: %.2fms", averagems),
145 + }
146 +}