Skip to content

Commit

Permalink
Merge pull request #5457 from overbool/feat/command/add-connection-di…
Browse files Browse the repository at this point in the history
…rection

feat(command): add connection direction
  • Loading branch information
Stebalien authored Sep 14, 2018
2 parents 041e771 + daf6fbd commit cbc6e69
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -79,14 +80,13 @@ 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
for _, c := range conns {
pid := c.RemotePeer()
addr := c.RemoteMultiaddr()

ci := connInfo{
Addr: addr.String(),
Peer: pid.Pretty(),
Expand All @@ -100,6 +100,11 @@ var swarmPeersCmd = &cmds.Command{
}
*/

if verbose || direction {
// set direction
ci.Direction = c.Stat().Direction
}

if verbose || latency {
lat := n.Peerstore.LatencyEWMA(pid)
if lat == 0 {
Expand Down Expand Up @@ -146,6 +151,11 @@ var swarmPeersCmd = &cmds.Command{
if info.Latency != "" {
fmt.Fprintf(buf, " %s", info.Latency)
}

if info.Direction != inet.DirUnknown {
fmt.Fprintf(buf, " %s", directionString(info.Direction))
}

fmt.Fprintln(buf)

for _, s := range info.Streams {
Expand All @@ -168,11 +178,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 {
Expand Down Expand Up @@ -203,6 +214,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 ""
}
}

var swarmAddrsCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "List known addresses. Useful for debugging.",
Expand Down

0 comments on commit cbc6e69

Please sign in to comment.