提交 428b82f2 作者: Overbool

feat(command): add connection direction

License: MIT
Signed-off-by: 's avatarOverbool <overbool.xu@gmail.com>
上级 cf963724
...@@ -81,15 +81,14 @@ var swarmPeersCmd = &cmds.Command{ ...@@ -81,15 +81,14 @@ var swarmPeersCmd = &cmds.Command{
streams, _, _ := req.Option("streams").Bool() streams, _, _ := req.Option("streams").Bool()
conns := n.PeerHost.Network().Conns() conns := n.PeerHost.Network().Conns()
var out connInfos var out connInfos
for _, c := range conns { for _, c := range conns {
pid := c.RemotePeer() pid := c.RemotePeer()
addr := c.RemoteMultiaddr() addr := c.RemoteMultiaddr()
ci := connInfo{ ci := connInfo{
Addr: addr.String(), Addr: addr.String(),
Peer: pid.Pretty(), Peer: pid.Pretty(),
Direction: inet.Direction(-1),
} }
/* /*
...@@ -101,6 +100,9 @@ var swarmPeersCmd = &cmds.Command{ ...@@ -101,6 +100,9 @@ var swarmPeersCmd = &cmds.Command{
*/ */
if verbose || latency { if verbose || latency {
// set direction
ci.Direction = c.Stat().Direction
lat := n.Peerstore.LatencyEWMA(pid) lat := n.Peerstore.LatencyEWMA(pid)
if lat == 0 { if lat == 0 {
ci.Latency = "n/a" ci.Latency = "n/a"
...@@ -146,6 +148,11 @@ var swarmPeersCmd = &cmds.Command{ ...@@ -146,6 +148,11 @@ var swarmPeersCmd = &cmds.Command{
if info.Latency != "" { if info.Latency != "" {
fmt.Fprintf(buf, " %s", info.Latency) fmt.Fprintf(buf, " %s", info.Latency)
} }
if info.Direction >= 0 {
fmt.Fprintf(buf, " %s", directionString(info.Direction))
}
fmt.Fprintln(buf) fmt.Fprintln(buf)
for _, s := range info.Streams { for _, s := range info.Streams {
...@@ -168,11 +175,12 @@ type streamInfo struct { ...@@ -168,11 +175,12 @@ type streamInfo struct {
} }
type connInfo struct { type connInfo struct {
Addr string Addr string
Peer string Peer string
Latency string Latency string
Muxer string Muxer string
Streams []streamInfo Direction inet.Direction
Streams []streamInfo
} }
func (ci *connInfo) Less(i, j int) bool { func (ci *connInfo) Less(i, j int) bool {
...@@ -203,6 +211,18 @@ func (ci connInfos) Swap(i, j int) { ...@@ -203,6 +211,18 @@ func (ci connInfos) Swap(i, j int) {
ci.Peers[i], ci.Peers[j] = ci.Peers[j], ci.Peers[i] 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{ var swarmAddrsCmd = &cmds.Command{
Helptext: cmdkit.HelpText{ Helptext: cmdkit.HelpText{
Tagline: "List known addresses. Useful for debugging.", Tagline: "List known addresses. Useful for debugging.",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论