Add NewPeerFromSummary

To reduce the number of places that are explicit about which values
go into a new Peer.
This commit is contained in:
David Wragg committed 2015-06-12 16:12:59 +01:00
1 parent a9c710211b
commit a334059e85
6 files changed
+50 -50

No files matched your search

+29 -14
View File
@@ -8,17 +8,25 @@ import (
type PeerUID uint64
func randomPeerUID() PeerUID {
return PeerUID(randUint64())
}
func ParsePeerUID(s string) (PeerUID, error) {
uid, err := strconv.ParseUint(s, 10, 64)
return PeerUID(uid), err
}
type PeerSummary struct {
NameByte []byte
NickName string
UID PeerUID
Version uint64
}
type Peer struct {
Name PeerName
NameByte []byte
NickName string
UID PeerUID
version uint64
Name PeerName
PeerSummary
localRefCount uint64 // maintained by Peers
connections map[PeerName]Connection
}
@@ -26,24 +34,31 @@ type Peer struct {
type ConnectionSet map[Connection]struct{}
func NewPeer(name PeerName, nickName string, uid PeerUID, version uint64) *Peer {
if uid == 0 {
uid = PeerUID(randUint64())
}
return &Peer{
Name: name,
NameByte: name.Bin(),
NickName: nickName,
UID: uid,
version: version,
Name: name,
PeerSummary: PeerSummary{
NameByte: name.Bin(),
NickName: nickName,
UID: uid,
Version: version,
},
connections: make(map[PeerName]Connection)}
}
func NewPeerFromSummary(summary PeerSummary) *Peer {
return &Peer{
Name: PeerNameFromBin(summary.NameByte),
PeerSummary: summary,
connections: make(map[PeerName]Connection),
}
}
func (peer *Peer) String() string {
return fmt.Sprint(peer.Name, "(", peer.NickName, ")")
}
func (peer *Peer) Info() string {
return fmt.Sprint(peer.String(), " (v", peer.version, ") (UID ", peer.UID, ")")
return fmt.Sprint(peer.String(), " (v", peer.Version, ") (UID ", peer.UID, ")")
}
// Calculate the routing table from this peer to all peers reachable