add stream listings as swarm peers flag
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Nov 3, 2016 at 21:50 UTC
4a029c737415930cb0297f797ca97317cfd313de
2 files changed
+97
-15
core/commands/swarm.go
+95
-13
@@ -54,9 +54,9 @@ var swarmPeersCmd = &cmds.Command{
54
`,
55
},
56
Options: []cmds.Option{
57
- cmds.BoolOption("verbose", "v",
58
- "Also display latency along with peer information in the following form: "+
59
- "<peer address> <latency>"),
57
+ cmds.BoolOption("verbose", "v", "display all extra information"),
58
+ cmds.BoolOption("streams", "Also list information about open streams for each peer"),
59
+ cmds.BoolOption("latency", "Also list information about latency to each peer"),
60
},
61
Run: func(req cmds.Request, res cmds.Response) {
62
@@ -73,27 +73,109 @@ var swarmPeersCmd = &cmds.Command{
73
}
74
75
verbose, _, _ := req.Option("verbose").Bool()
76
+ latency, _, _ := req.Option("latency").Bool()
77
+ streams, _, _ := req.Option("streams").Bool()
78
+
79
conns := n.PeerHost.Network().Conns()
77
- addrs := make([]string, len(conns))
80
79
- for i, c := range conns {
81
+ var out connInfos
82
+ for _, c := range conns {
83
pid := c.RemotePeer()
84
addr := c.RemoteMultiaddr()
85
83
- if verbose {
84
- addrs[i] = fmt.Sprintf("%s/ipfs/%s %s", addr, pid.Pretty(), n.Peerstore.LatencyEWMA(pid))
85
- } else {
86
- addrs[i] = fmt.Sprintf("%s/ipfs/%s", addr, pid.Pretty())
86
+ ci := connInfo{
87
+ Addr: addr.String(),
88
+ Peer: pid.Pretty(),
89
+ }
90
+
91
+ if verbose || latency {
92
+ ci.Latency = n.Peerstore.LatencyEWMA(pid).String()
93
}
94
+ if verbose || streams {
95
+ strs, err := c.GetStreams()
96
+ if err != nil {
97
+ res.SetError(err, cmds.ErrNormal)
98
+ return
99
+ }
100
+
101
+ for _, s := range strs {
102
+ ci.Streams = append(ci.Streams, streamInfo{Protocol: string(s.Protocol())})
103
+ }
104
+ }
105
+ sort.Sort(&ci)
106
+ out.Peers = append(out.Peers, ci)
107
}
108
90
- sort.Sort(sort.StringSlice(addrs))
91
- res.SetOutput(&stringList{addrs})
109
+ sort.Sort(&out)
110
+ res.SetOutput(&out)
111
},
112
Marshalers: cmds.MarshalerMap{
94
- cmds.Text: stringListMarshaler,
113
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
114
+ ci, ok := res.Output().(*connInfos)
115
+ if !ok {
116
+ return nil, fmt.Errorf("expected output type to be connInfos")
117
+ }
118
+
119
+ buf := new(bytes.Buffer)
120
+ for _, info := range ci.Peers {
121
+ fmt.Fprintf(buf, "%s/ipfs/%s", info.Addr, info.Peer)
122
+ if info.Latency != "" {
123
+ fmt.Fprintf(buf, " %s", info.Latency)
124
+ }
125
+ fmt.Fprintln(buf)
126
+
127
+ for _, s := range info.Streams {
128
+ if s.Protocol == "" {
129
+ s.Protocol = "<no protocol name>"
130
+ }
131
+
132
+ fmt.Fprintf(buf, " %s\n", s.Protocol)
133
+ }
134
+ }
135
+
136
+ return buf, nil
137
+ },
138
},
96
- Type: stringList{},
139
+ Type: connInfos{},
140
+}
141
+
142
+type streamInfo struct {
143
+ Protocol string
144
+}
145
+
146
+type connInfo struct {
147
+ Addr string
148
+ Peer string
149
+ Latency string
150
+ Streams []streamInfo
151
+}
152
+
153
+func (ci *connInfo) Less(i, j int) bool {
154
+ return ci.Streams[i].Protocol < ci.Streams[j].Protocol
155
+}
156
+
157
+func (ci *connInfo) Len() int {
158
+ return len(ci.Streams)
159
+}
160
+
161
+func (ci *connInfo) Swap(i, j int) {
162
+ ci.Streams[i], ci.Streams[j] = ci.Streams[j], ci.Streams[i]
163
+}
164
+
165
+type connInfos struct {
166
+ Peers []connInfo
167
+}
168
+
169
+func (ci connInfos) Less(i, j int) bool {
170
+ return ci.Peers[i].Addr < ci.Peers[j].Addr
171
+}
172
+
173
+func (ci connInfos) Len() int {
174
+ return len(ci.Peers)
175
+}
176
+
177
+func (ci connInfos) Swap(i, j int) {
178
+ ci.Peers[i], ci.Peers[j] = ci.Peers[j], ci.Peers[i]
179
}
180
181
var swarmAddrsCmd = &cmds.Command{
package.json
+2
-2
@@ -52,9 +52,9 @@
52
"version": "0.1.0"
53
},
54
{
55
- "hash": "QmQF3y5N5RcMaGUbSunmbnjhaFHP7uGDismvngkn8HJ1Xa",
55
+ "hash": "QmU6FqHBr1W5ajRrj4cr2SfGq1w8gq2LpS42JWRysEb3BK",
56
"name": "iptb",
57
- "version": "1.1.1"
57
+ "version": "1.1.2"
58
},
59
{
60
"hash": "QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J",