forked from aergoio/aergo
/
getpeers.go
171 lines (139 loc) · 3.35 KB
/
getpeers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package cmd
import (
"bytes"
"context"
"os"
"sort"
"strings"
"github.com/aergoio/aergo/cmd/aergocli/util"
"github.com/aergoio/aergo/types"
"github.com/spf13/cobra"
)
var getpeersCmd = &cobra.Command{
Use: "getpeers",
Short: "Get Peer list",
Run: execGetPeers,
}
var nohidden bool
var showself bool
var sortFlag string
var detailed int
const (
sortAddr = "addr"
sortID = "id"
sortHeight = "height"
sortDefault = "no"
)
const (
DetailShort int = -1
DetailDefault = 0
DetailLong = 1
)
func init() {
rootCmd.AddCommand(getpeersCmd)
getpeersCmd.Flags().BoolVar(&nohidden, "nohidden", false, "exclude hidden peers")
getpeersCmd.Flags().BoolVar(&showself, "self", false, "show self peer info")
getpeersCmd.Flags().StringVar(&sortFlag, "sort", "no", "sort peers by address, id or other")
getpeersCmd.Flags().IntVar(&detailed, "detail", 0, "detail level")
}
func execGetPeers(cmd *cobra.Command, args []string) {
sorter := GetSorter(cmd, sortFlag)
msg, err := client.GetPeers(context.Background(), &types.PeersParams{NoHidden: nohidden, ShowSelf: showself})
if err != nil {
cmd.Printf("Failed to get peer from server: %s\n", err.Error())
return
}
// address and peerid should be encoded, respectively
sorter.Sort(msg.Peers)
if detailed == 0 {
cmd.Println(util.PeerListToString(msg))
} else if detailed > 0 {
// TODO show long fields
cmd.Println(util.LongPeerListToString(msg))
} else {
cmd.Println(util.ShortPeerListToString(msg))
}
}
func Must(a0 string, _ error) string {
return a0
}
func GetSorter(cmd *cobra.Command, flag string) peerSorter {
switch flag {
case sortAddr:
return addrSorter{}
case sortID:
return idSorter{}
case sortHeight:
return heightSorter{}
case sortDefault:
return noSorter{}
default:
cmd.Println("Invalid sort type", flag)
os.Exit(1)
return noSorter{}
}
}
type peerSorter interface {
Sort([]*types.Peer)
}
type addrSorter struct{}
func (addrSorter) Sort(peerArr []*types.Peer) {
sort.Sort(byAddr(peerArr))
}
type idSorter struct{}
func (idSorter) Sort(peerArr []*types.Peer) {
sort.Sort(byID(peerArr))
}
type heightSorter struct{}
func (heightSorter) Sort(peerArr []*types.Peer) {
sort.Sort(byHeight(peerArr))
}
type noSorter struct{}
func (noSorter) Sort(peerArr []*types.Peer) {
// Do nothing. no sort
}
type byAddr []*types.Peer
func (s byAddr) Len() int {
return len(s)
}
func (s byAddr) Less(i, j int) bool {
result := strings.Compare(s[i].Address.Address, s[j].Address.Address)
if result == 0 {
result = int(s[i].Address.Port) - int(s[j].Address.Port)
}
return result < 0
}
func (s byAddr) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type byID []*types.Peer
func (s byID) Len() int {
return len(s)
}
func (s byID) Less(i, j int) bool {
result := bytes.Compare(s[i].Address.PeerID, s[j].Address.PeerID)
return result < 0
}
func (s byID) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type byHeight []*types.Peer
func (s byHeight) Len() int {
return len(s)
}
func (s byHeight) Less(i, j int) bool {
if s[i].Bestblock.BlockNo < s[j].Bestblock.BlockNo {
return true
} else if s[i].Bestblock.BlockNo > s[j].Bestblock.BlockNo {
return false
} else {
return bytes.Compare(s[i].Bestblock.GetBlockHash(), s[j].Bestblock.GetBlockHash()) < 0
}
}
func (s byHeight) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}