Added a verbose option for swarm peers.
This is used like 'ipfs swarm peers -v'. It prints out the normal peers information, but also the latency of each peer License: MIT Signed-off-by: Chris Sasarak <chris.sasarak@gmail.com>
Christopher Sasarak committed
May 15, 2016 at 13:11 UTC
762aded60e7b003bc54c121f13876e34c0842bba
1 file changed
+30
-1
core/commands/swarm.go
+30
-1
@@ -7,6 +7,7 @@ import (
7
"io"
8
"path"
9
"sort"
10
+ "time"
11
12
cmds "github.com/ipfs/go-ipfs/commands"
13
iaddr "github.com/ipfs/go-ipfs/thirdparty/ipfsaddr"
@@ -15,6 +16,7 @@ import (
16
17
mafilter "gx/ipfs/QmUaRHbB7pUwj5mS9BS4CMvBiW48MpaH2wbGxeWfFhhHxK/multiaddr-filter"
18
ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
19
+ context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
20
)
21
22
type stringList struct {
@@ -49,8 +51,15 @@ var swarmPeersCmd = &cmds.Command{
51
ShortDescription: `
52
'ipfs swarm peers' lists the set of peers this node is connected to.
53
`,
54
+ },
55
+ Options: []cmds.Option{
56
+ cmds.BoolOption("verbose", "v",
57
+ `Also display latency along with peer information in the following form:
58
+ <peer address> <latency>
59
+ `),
60
},
61
Run: func(req cmds.Request, res cmds.Response) {
62
+ timeout := time.Duration(5) * time.Second
63
64
log.Debug("ipfs swarm peers")
65
n, err := req.InvocContext().GetNode()
@@ -64,12 +73,32 @@ var swarmPeersCmd = &cmds.Command{
73
return
74
}
75
76
+ verbose, _, _ := req.Option("verbose").Bool()
77
conns := n.PeerHost.Network().Conns()
78
addrs := make([]string, len(conns))
79
+ pingService := n.Ping
80
+
81
for i, c := range conns {
82
pid := c.RemotePeer()
83
addr := c.RemoteMultiaddr()
72
- addrs[i] = fmt.Sprintf("%s/ipfs/%s", addr, pid.Pretty())
84
+
85
+ if verbose {
86
+ pContext, cancelFn := context.WithTimeout(req.Context(), timeout)
87
+ defer cancelFn()
88
+
89
+ ch, _ := pingService.Ping(pContext, pid)
90
+ duration := <-ch
91
+ var durStr string
92
+
93
+ if pContext.Err() != nil {
94
+ durStr = pContext.Err().Error()
95
+ } else {
96
+ durStr = duration.String()
97
+ }
98
+ addrs[i] = fmt.Sprintf("%s/ipfs/%s %s", addr, pid.Pretty(), durStr)
99
+ } else {
100
+ addrs[i] = fmt.Sprintf("%s/ipfs/%s", addr, pid.Pretty())
101
+ }
102
}
103
104
sort.Sort(sort.StringSlice(addrs))