@cryptotaxi247 / kubo / commits / 428b82f22

feat(command): add connection direction

License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>

Overbool committed Sep 13, 2018 at 23:22 UTC 428b82f2281abfae3569927311b9b3e6f519d144
1 file changed +29 -9
core/commands/swarm.go
+29 -9
@@ -81,15 +81,14 @@ var swarmPeersCmd = &cmds.Command{
81 streams, _, _ := req.Option("streams").Bool()
82
83 conns := n.PeerHost.Network().Conns()
84 -
84 var out connInfos
85 for _, c := range conns {
86 pid := c.RemotePeer()
87 addr := c.RemoteMultiaddr()
89 -
88 ci := connInfo{
91 - Addr: addr.String(),
92 - Peer: pid.Pretty(),
89 + Addr: addr.String(),
90 + Peer: pid.Pretty(),
91 + Direction: inet.Direction(-1),
92 }
93
94 /*
@@ -101,6 +100,9 @@ var swarmPeersCmd = &cmds.Command{
100 */
101
102 if verbose || latency {
103 + // set direction
104 + ci.Direction = c.Stat().Direction
105 +
106 lat := n.Peerstore.LatencyEWMA(pid)
107 if lat == 0 {
108 ci.Latency = "n/a"
@@ -146,6 +148,11 @@ var swarmPeersCmd = &cmds.Command{
148 if info.Latency != "" {
149 fmt.Fprintf(buf, " %s", info.Latency)
150 }
151 +
152 + if info.Direction >= 0 {
153 + fmt.Fprintf(buf, " %s", directionString(info.Direction))
154 + }
155 +
156 fmt.Fprintln(buf)
157
158 for _, s := range info.Streams {
@@ -168,11 +175,12 @@ type streamInfo struct {
175 }
176
177 type connInfo struct {
171 - Addr string
172 - Peer string
173 - Latency string
174 - Muxer string
175 - Streams []streamInfo
178 + Addr string
179 + Peer string
180 + Latency string
181 + Muxer string
182 + Direction inet.Direction
183 + Streams []streamInfo
184 }
185
186 func (ci *connInfo) Less(i, j int) bool {
@@ -203,6 +211,18 @@ func (ci connInfos) Swap(i, j int) {
211 ci.Peers[i], ci.Peers[j] = ci.Peers[j], ci.Peers[i]
212 }
213
214 +// directionString transfers to string
215 +func directionString(d inet.Direction) string {
216 + switch d {
217 + case inet.DirInbound:
218 + return "inbound"
219 + case inet.DirOutbound:
220 + return "outbound"
221 + default:
222 + return "unknown"
223 + }
224 +}
225 +
226 var swarmAddrsCmd = &cmds.Command{
227 Helptext: cmdkit.HelpText{
228 Tagline: "List known addresses. Useful for debugging.",