2018-03-23 20:36:59 +00:00
|
|
|
package result
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2020-01-10 12:13:29 +00:00
|
|
|
// GetPeers payload for outputting peers in `getpeers` RPC call.
|
|
|
|
GetPeers struct {
|
|
|
|
Unconnected Peers `json:"unconnected"`
|
|
|
|
Connected Peers `json:"connected"`
|
|
|
|
Bad Peers `json:"bad"`
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 12:13:29 +00:00
|
|
|
// Peers represent a slice of peers.
|
|
|
|
Peers []Peer
|
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
// Peer represents the peer.
|
|
|
|
Peer struct {
|
|
|
|
Address string `json:"address"`
|
|
|
|
Port string `json:"port"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-01-10 12:13:29 +00:00
|
|
|
// NewGetPeers creates a new GetPeers structure.
|
|
|
|
func NewGetPeers() GetPeers {
|
|
|
|
return GetPeers{
|
2018-03-23 20:36:59 +00:00
|
|
|
Unconnected: []Peer{},
|
|
|
|
Connected: []Peer{},
|
|
|
|
Bad: []Peer{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 12:13:29 +00:00
|
|
|
// AddUnconnected adds a set of peers to the unconnected peers slice.
|
|
|
|
func (g *GetPeers) AddUnconnected(addrs []string) {
|
|
|
|
g.Unconnected.addPeers(addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddConnected adds a set of peers to the connected peers slice.
|
|
|
|
func (g *GetPeers) AddConnected(addrs []string) {
|
|
|
|
g.Connected.addPeers(addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBad adds a set of peers to the bad peers slice.
|
|
|
|
func (g *GetPeers) AddBad(addrs []string) {
|
|
|
|
g.Bad.addPeers(addrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPeers adds a set of peers to the given peer slice.
|
|
|
|
func (p *Peers) addPeers(addrs []string) {
|
|
|
|
for i := range addrs {
|
|
|
|
addressParts := strings.Split(addrs[i], ":")
|
|
|
|
peer := Peer{
|
|
|
|
Address: addressParts[0],
|
|
|
|
Port: addressParts[1],
|
|
|
|
}
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2020-01-10 12:13:29 +00:00
|
|
|
*p = append(*p, peer)
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
}
|