@cryptotaxi247 / kubo / commits / 1561291d6

improve UI of ping

Jeromy committed Jan 10, 2015 at 07:20 UTC 1561291d61adf14182314736ca0206562ebd7bb2
1 file changed +33 -16
core/commands/ping.go
+33 -16
@@ -7,11 +7,13 @@ import (
7 "time"
8
9 cmds "github.com/jbenet/go-ipfs/commands"
10 + config "github.com/jbenet/go-ipfs/config"
11 core "github.com/jbenet/go-ipfs/core"
12 peer "github.com/jbenet/go-ipfs/p2p/peer"
13 u "github.com/jbenet/go-ipfs/util"
14
15 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
16 + ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
17 )
18
19 const kPingTimeout = 10 * time.Second
@@ -56,7 +58,7 @@ Send pings to a peer using the routing system to discover its address
58 if len(obj.Text) > 0 {
59 buf = bytes.NewBufferString(obj.Text + "\n")
60 } else if obj.Success {
59 - fmt.Fprintf(buf, "Pong took %.2fms\n", obj.Time.Seconds()*1000)
61 + fmt.Fprintf(buf, "Pong received: time=%.2f ms\n", obj.Time.Seconds()*1000)
62 } else {
63 fmt.Fprintf(buf, "Pong failed\n")
64 }
@@ -80,11 +82,24 @@ Send pings to a peer using the routing system to discover its address
82 return nil, errNotOnline
83 }
84
83 - peerID, err := peer.IDB58Decode(req.Arguments()[0])
85 + bsp, err := config.ParseBootstrapPeer(req.Arguments()[0])
86 if err != nil {
87 return nil, err
88 }
89
90 + peerID, err := peer.IDB58Decode(bsp.PeerID)
91 + if err != nil {
92 + return nil, err
93 + }
94 +
95 + if len(bsp.Address) > 0 {
96 + addr, err := ma.NewMultiaddr(bsp.Address)
97 + if err != nil {
98 + return nil, err
99 + }
100 + n.Peerstore.AddAddress(peerID, addr)
101 + }
102 +
103 // Set up number of pings
104 numPings := 10
105 val, found, err := req.Option("count").Int()
@@ -107,26 +122,28 @@ Send pings to a peer using the routing system to discover its address
122 func pingPeer(n *core.IpfsNode, pid peer.ID, numPings int, outChan chan interface{}) {
123 defer close(outChan)
124
110 - // Make sure we can find the node in question
111 - outChan <- &PingResult{
112 - Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
113 - }
125 + if len(n.Peerstore.Addresses(pid)) == 0 {
126 + // Make sure we can find the node in question
127 + outChan <- &PingResult{
128 + Text: fmt.Sprintf("Looking up peer %s", pid.Pretty()),
129 + }
130
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
131 + // TODO: get master context passed in
132 + ctx, _ := context.WithTimeout(context.TODO(), kPingTimeout)
133 + p, err := n.Routing.FindPeer(ctx, pid)
134 + if err != nil {
135 + outChan <- &PingResult{Text: fmt.Sprintf("Peer lookup error: %s", err)}
136 + return
137 + }
138 + n.Peerstore.AddPeerInfo(p)
139 }
122 - n.Peerstore.AddPeerInfo(p)
140
124 - outChan <- &PingResult{Text: fmt.Sprintf("Peer found, starting pings.")}
141 + outChan <- &PingResult{Text: fmt.Sprintf("PING %s.", pid.Pretty())}
142
143 var total time.Duration
144 for i := 0; i < numPings; i++ {
128 - ctx, _ = context.WithTimeout(context.TODO(), kPingTimeout)
129 - took, err := n.Routing.Ping(ctx, p.ID)
145 + ctx, _ := context.WithTimeout(context.TODO(), kPingTimeout)
146 + took, err := n.Routing.Ping(ctx, pid)
147 if err != nil {
148 log.Errorf("Ping error: %s", err)
149 outChan <- &PingResult{Text: fmt.Sprintf("Ping error: %s", err)}