Skip to content

Commit

Permalink
feat(command): add direction flag for swarm peers command
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Sep 14, 2018
1 parent 428b82f commit fff878d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions core/commands/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
"strings"

cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
repo "github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/core/commands/e"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/fsrepo"

peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
"gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
mafilter "gx/ipfs/QmSMZwvs3n4GBikZ7hKzT17c3bk65FmyZo2JqtJ16swqCv/multiaddr-filter"
cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
config "gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config"
"gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
"gx/ipfs/QmYVqYJTVjetcf1guieEgWpK1PZtHPytP624vKzTF1P3r2/go-ipfs-config"
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr"
inet "gx/ipfs/QmZNJyx9GGCX4GeuHnLB8fxaxMLs4MjTjHokxfQcCd6Nve/go-libp2p-net"
pstore "gx/ipfs/Qmda4cPRvSRyox3SqgJN6DfSZGU5TtHufPTp9uXjFj71X6/go-libp2p-peerstore"
swarm "gx/ipfs/QmeDpqUwwdye8ABKVMPXKuWwPVURFdqTqssbTUB39E2Nwd/go-libp2p-swarm"
"gx/ipfs/QmeDpqUwwdye8ABKVMPXKuWwPVURFdqTqssbTUB39E2Nwd/go-libp2p-swarm"
iaddr "gx/ipfs/QmePSRaGafvmURQwQkHPDBJsaGwKXC1WpBBHVCQxdr8FPn/go-ipfs-addr"
)

Expand Down 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,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
Expand All @@ -88,7 +90,6 @@ var swarmPeersCmd = &cmds.Command{
ci := connInfo{
Addr: addr.String(),
Peer: pid.Pretty(),
Direction: inet.Direction(-1),
}

/*
Expand All @@ -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"
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -219,7 +222,7 @@ func directionString(d inet.Direction) string {
case inet.DirOutbound:
return "outbound"
default:
return "unknown"
return ""
}
}

Expand Down

0 comments on commit fff878d

Please sign in to comment.