From 428b82f2281abfae3569927311b9b3e6f519d144 Mon Sep 17 00:00:00 2001 From: Overbool Date: Thu, 13 Sep 2018 23:22:54 +0800 Subject: [PATCH 1/2] feat(command): add connection direction License: MIT Signed-off-by: Overbool --- core/commands/swarm.go | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index ea2632fdb08..e768040046e 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -81,15 +81,14 @@ var swarmPeersCmd = &cmds.Command{ streams, _, _ := req.Option("streams").Bool() conns := n.PeerHost.Network().Conns() - var out connInfos for _, c := range conns { pid := c.RemotePeer() addr := c.RemoteMultiaddr() - ci := connInfo{ - Addr: addr.String(), - Peer: pid.Pretty(), + Addr: addr.String(), + Peer: pid.Pretty(), + Direction: inet.Direction(-1), } /* @@ -101,6 +100,9 @@ var swarmPeersCmd = &cmds.Command{ */ if verbose || latency { + // set direction + ci.Direction = c.Stat().Direction + lat := n.Peerstore.LatencyEWMA(pid) if lat == 0 { ci.Latency = "n/a" @@ -146,6 +148,11 @@ var swarmPeersCmd = &cmds.Command{ if info.Latency != "" { fmt.Fprintf(buf, " %s", info.Latency) } + + if info.Direction >= 0 { + fmt.Fprintf(buf, " %s", directionString(info.Direction)) + } + fmt.Fprintln(buf) for _, s := range info.Streams { @@ -168,11 +175,12 @@ type streamInfo struct { } type connInfo struct { - Addr string - Peer string - Latency string - Muxer string - Streams []streamInfo + Addr string + Peer string + Latency string + Muxer string + Direction inet.Direction + Streams []streamInfo } func (ci *connInfo) Less(i, j int) bool { @@ -203,6 +211,18 @@ func (ci connInfos) Swap(i, j int) { ci.Peers[i], ci.Peers[j] = ci.Peers[j], ci.Peers[i] } +// directionString transfers to string +func directionString(d inet.Direction) string { + switch d { + case inet.DirInbound: + return "inbound" + case inet.DirOutbound: + return "outbound" + default: + return "unknown" + } +} + var swarmAddrsCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "List known addresses. Useful for debugging.", From daf6fbd347e4f55456ad1e152dd1f5e1725e081d Mon Sep 17 00:00:00 2001 From: Overbool Date: Fri, 14 Sep 2018 08:46:28 +0800 Subject: [PATCH 2/2] feat(command): add direction flag for swarm peers command License: MIT Signed-off-by: Overbool --- core/commands/swarm.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/commands/swarm.go b/core/commands/swarm.go index e768040046e..fbbff7a4b37 100644 --- a/core/commands/swarm.go +++ b/core/commands/swarm.go @@ -62,6 +62,7 @@ var swarmPeersCmd = &cmds.Command{ cmdkit.BoolOption("verbose", "v", "display all extra information"), cmdkit.BoolOption("streams", "Also list information about open streams for each peer"), cmdkit.BoolOption("latency", "Also list information about latency to each peer"), + cmdkit.BoolOption("direction", "Also list information about the direction of connection"), }, Run: func(req cmds.Request, res cmds.Response) { @@ -79,6 +80,7 @@ var swarmPeersCmd = &cmds.Command{ verbose, _, _ := req.Option("verbose").Bool() latency, _, _ := req.Option("latency").Bool() streams, _, _ := req.Option("streams").Bool() + direction, _, _ := req.Option("direction").Bool() conns := n.PeerHost.Network().Conns() var out connInfos @@ -86,9 +88,8 @@ var swarmPeersCmd = &cmds.Command{ pid := c.RemotePeer() addr := c.RemoteMultiaddr() ci := connInfo{ - Addr: addr.String(), - Peer: pid.Pretty(), - Direction: inet.Direction(-1), + Addr: addr.String(), + Peer: pid.Pretty(), } /* @@ -99,10 +100,12 @@ var swarmPeersCmd = &cmds.Command{ } */ - if verbose || latency { + if verbose || direction { // set direction ci.Direction = c.Stat().Direction + } + if verbose || latency { lat := n.Peerstore.LatencyEWMA(pid) if lat == 0 { ci.Latency = "n/a" @@ -149,7 +152,7 @@ var swarmPeersCmd = &cmds.Command{ fmt.Fprintf(buf, " %s", info.Latency) } - if info.Direction >= 0 { + if info.Direction != inet.DirUnknown { fmt.Fprintf(buf, " %s", directionString(info.Direction)) } @@ -219,7 +222,7 @@ func directionString(d inet.Direction) string { case inet.DirOutbound: return "outbound" default: - return "unknown" + return "" } }