mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-25 13:56:35 +00:00
59 lines
977 B
Go
59 lines
977 B
Go
|
package result
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
// Peers payload for outputting peers in `getpeers` RPC call.
|
||
|
Peers struct {
|
||
|
Unconnected []Peer `json:"unconnected"`
|
||
|
Connected []Peer `json:"connected"`
|
||
|
Bad []Peer `json:"bad"`
|
||
|
}
|
||
|
|
||
|
// Peer represents the peer.
|
||
|
Peer struct {
|
||
|
Address string `json:"address"`
|
||
|
Port string `json:"port"`
|
||
|
}
|
||
|
)
|
||
|
|
||
|
// NewPeers creates a new Peers struct.
|
||
|
func NewPeers() Peers {
|
||
|
return Peers{
|
||
|
Unconnected: []Peer{},
|
||
|
Connected: []Peer{},
|
||
|
Bad: []Peer{},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// AddPeer adds a peer to the given peer type slice.
|
||
|
func (p *Peers) AddPeer(peerType string, addr string) {
|
||
|
addressParts := strings.Split(addr, ":")
|
||
|
peer := Peer{
|
||
|
Address: addressParts[0],
|
||
|
Port: addressParts[1],
|
||
|
}
|
||
|
|
||
|
switch peerType {
|
||
|
case "unconnected":
|
||
|
p.Unconnected = append(
|
||
|
p.Unconnected,
|
||
|
peer,
|
||
|
)
|
||
|
|
||
|
case "connected":
|
||
|
p.Connected = append(
|
||
|
p.Connected,
|
||
|
peer,
|
||
|
)
|
||
|
|
||
|
case "bad":
|
||
|
p.Bad = append(
|
||
|
p.Bad,
|
||
|
peer,
|
||
|
)
|
||
|
}
|
||
|
}
|