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

+5 -8
View File
@@ -30,11 +30,8 @@ func (conn *mockChannelConnection) SendProtocolMsg(protocolMsg ProtocolMsg) {
} }
func (router *Router) AddTestChannelConnection(r *Router) { func (router *Router) AddTestChannelConnection(r *Router) {
fromName := router.Ourself.Peer.Name fromPeer := NewPeerFromSummary(router.Ourself.Peer.PeerSummary)
toName := r.Ourself.Peer.Name toPeer := NewPeerFromSummary(r.Ourself.Peer.PeerSummary)
fromPeer := NewPeer(fromName, "", router.Ourself.Peer.UID, 0)
toPeer := NewPeer(toName, "", r.Ourself.Peer.UID, 0)
r.Peers.FetchWithDefault(fromPeer) // Has side-effect of incrementing refcount r.Peers.FetchWithDefault(fromPeer) // Has side-effect of incrementing refcount
router.Peers.FetchWithDefault(toPeer) // router.Peers.FetchWithDefault(toPeer) //
@@ -70,13 +67,13 @@ func TestGossipTopology(t *testing.T) {
// the routers supplied as arguments, carrying across all UID and // the routers supplied as arguments, carrying across all UID and
// version information. // version information.
func (router *Router) tp(routers ...*Router) *Peer { func (router *Router) tp(routers ...*Router) *Peer {
peer := NewPeer(router.Ourself.Peer.Name, "", router.Ourself.Peer.UID, 0) peer := NewPeerFromSummary(router.Ourself.Peer.PeerSummary)
connections := make(map[PeerName]Connection) connections := make(map[PeerName]Connection)
for _, r := range routers { for _, r := range routers {
p := NewPeer(r.Ourself.Peer.Name, "", r.Ourself.Peer.UID, r.Ourself.Peer.version) p := NewPeerFromSummary(r.Ourself.Peer.PeerSummary)
connections[r.Ourself.Peer.Name] = newMockConnection(peer, p) connections[r.Ourself.Peer.Name] = newMockConnection(peer, p)
} }
peer.version = router.Ourself.Peer.version peer.Version = router.Ourself.Peer.Version
peer.connections = connections peer.connections = connections
return peer return peer
} }
+1 -1
View File
@@ -57,7 +57,7 @@ func (peers *Peers) MarshalJSON() ([]byte, error) {
connections = append(connections, conn) connections = append(connections, conn)
} }
} }
ps = append(ps, &p{peer.Name.String(), peer.NickName, peer.UID, peer.version, connections}) ps = append(ps, &p{peer.Name.String(), peer.NickName, peer.UID, peer.Version, connections})
}) })
return json.Marshal(ps) return json.Marshal(ps)
} }
+4 -4
View File
@@ -20,7 +20,7 @@ type LocalPeerAction func()
func NewLocalPeer(name PeerName, nickName string, router *Router) *LocalPeer { func NewLocalPeer(name PeerName, nickName string, router *Router) *LocalPeer {
actionChan := make(chan LocalPeerAction, ChannelSize) actionChan := make(chan LocalPeerAction, ChannelSize)
peer := &LocalPeer{ peer := &LocalPeer{
Peer: NewPeer(name, nickName, 0, 0), Peer: NewPeer(name, nickName, randomPeerUID(), 0),
router: router, router: router,
actionChan: actionChan, actionChan: actionChan,
} }
@@ -272,20 +272,20 @@ func (peer *LocalPeer) addConnection(conn Connection) {
peer.Lock() peer.Lock()
defer peer.Unlock() defer peer.Unlock()
peer.connections[conn.Remote().Name] = conn peer.connections[conn.Remote().Name] = conn
peer.version++ peer.Version++
} }
func (peer *LocalPeer) deleteConnection(conn Connection) { func (peer *LocalPeer) deleteConnection(conn Connection) {
peer.Lock() peer.Lock()
defer peer.Unlock() defer peer.Unlock()
delete(peer.connections, conn.Remote().Name) delete(peer.connections, conn.Remote().Name)
peer.version++ peer.Version++
} }
func (peer *LocalPeer) connectionEstablished(conn Connection) { func (peer *LocalPeer) connectionEstablished(conn Connection) {
peer.Lock() peer.Lock()
defer peer.Unlock() defer peer.Unlock()
peer.version++ peer.Version++
} }
func (peer *LocalPeer) connectionCount() int { func (peer *LocalPeer) connectionCount() int {
+5 -5
View File
@@ -12,7 +12,9 @@ import (
// Add to peers a connection from peers.ourself to p // Add to peers a connection from peers.ourself to p
func (peers *Peers) AddTestConnection(p *Peer) { func (peers *Peers) AddTestConnection(p *Peer) {
toPeer := NewPeer(p.Name, "", p.UID, 0) summary := p.PeerSummary
summary.Version = 0
toPeer := NewPeerFromSummary(summary)
peers.FetchWithDefault(toPeer) // Has side-effect of incrementing refcount peers.FetchWithDefault(toPeer) // Has side-effect of incrementing refcount
conn := newMockConnection(peers.ourself.Peer, toPeer) conn := newMockConnection(peers.ourself.Peer, toPeer)
peers.ourself.addConnection(conn) peers.ourself.addConnection(conn)
@@ -21,11 +23,9 @@ func (peers *Peers) AddTestConnection(p *Peer) {
// Add to peers a connection from p1 to p2 // Add to peers a connection from p1 to p2
func (peers *Peers) AddTestRemoteConnection(p1, p2 *Peer) { func (peers *Peers) AddTestRemoteConnection(p1, p2 *Peer) {
fromName := p1.Name fromPeer := NewPeerFromSummary(p1.PeerSummary)
fromPeer := NewPeer(fromName, "", p1.UID, 0)
fromPeer = peers.FetchWithDefault(fromPeer) fromPeer = peers.FetchWithDefault(fromPeer)
toName := p2.Name toPeer := NewPeerFromSummary(p2.PeerSummary)
toPeer := NewPeer(toName, "", p2.UID, 0)
toPeer = peers.FetchWithDefault(toPeer) toPeer = peers.FetchWithDefault(toPeer)
peers.ourself.addConnection(&RemoteConnection{fromPeer, toPeer, "", false, false}) peers.ourself.addConnection(&RemoteConnection{fromPeer, toPeer, "", false, false})
} }
+29 -14
View File
@@ -8,17 +8,25 @@ import (
type PeerUID uint64 type PeerUID uint64
func randomPeerUID() PeerUID {
return PeerUID(randUint64())
}
func ParsePeerUID(s string) (PeerUID, error) { func ParsePeerUID(s string) (PeerUID, error) {
uid, err := strconv.ParseUint(s, 10, 64) uid, err := strconv.ParseUint(s, 10, 64)
return PeerUID(uid), err return PeerUID(uid), err
} }
type PeerSummary struct {
NameByte []byte
NickName string
UID PeerUID
Version uint64
}
type Peer struct { type Peer struct {
Name PeerName Name PeerName
NameByte []byte PeerSummary
NickName string
UID PeerUID
version uint64
localRefCount uint64 // maintained by Peers localRefCount uint64 // maintained by Peers
connections map[PeerName]Connection connections map[PeerName]Connection
} }
@@ -26,24 +34,31 @@ type Peer struct {
type ConnectionSet map[Connection]struct{} type ConnectionSet map[Connection]struct{}
func NewPeer(name PeerName, nickName string, uid PeerUID, version uint64) *Peer { func NewPeer(name PeerName, nickName string, uid PeerUID, version uint64) *Peer {
if uid == 0 {
uid = PeerUID(randUint64())
}
return &Peer{ return &Peer{
Name: name, Name: name,
NameByte: name.Bin(), PeerSummary: PeerSummary{
NickName: nickName, NameByte: name.Bin(),
UID: uid, NickName: nickName,
version: version, UID: uid,
Version: version,
},
connections: make(map[PeerName]Connection)} 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 { func (peer *Peer) String() string {
return fmt.Sprint(peer.Name, "(", peer.NickName, ")") return fmt.Sprint(peer.Name, "(", peer.NickName, ")")
} }
func (peer *Peer) Info() string { 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 // Calculate the routing table from this peer to all peers reachable
+6 -18
View File
@@ -25,13 +25,6 @@ type NameCollisionError struct {
type PeerNameSet map[PeerName]struct{} type PeerNameSet map[PeerName]struct{}
type PeerSummary struct {
NameByte []byte
NickName string
UID PeerUID
Version uint64
}
type ConnectionSummary struct { type ConnectionSummary struct {
NameByte []byte NameByte []byte
RemoteTCPAddr string RemoteTCPAddr string
@@ -220,13 +213,12 @@ func (peers *Peers) decodeUpdate(update []byte) (newPeers map[PeerName]*Peer, de
err = decErr err = decErr
return return
} }
name := PeerNameFromBin(peerSummary.NameByte) newPeer := NewPeerFromSummary(peerSummary)
newPeer := NewPeer(name, peerSummary.NickName, peerSummary.UID, peerSummary.Version)
decodedUpdate = append(decodedUpdate, newPeer) decodedUpdate = append(decodedUpdate, newPeer)
decodedConns = append(decodedConns, connSummaries) decodedConns = append(decodedConns, connSummaries)
existingPeer, found := peers.table[name] existingPeer, found := peers.table[newPeer.Name]
if !found { if !found {
newPeers[name] = newPeer newPeers[newPeer.Name] = newPeer
} else if existingPeer.UID != newPeer.UID { } else if existingPeer.UID != newPeer.UID {
err = NameCollisionError{Name: newPeer.Name} err = NameCollisionError{Name: newPeer.Name}
return return
@@ -259,7 +251,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti
// guaranteed to find peer in the peers.table // guaranteed to find peer in the peers.table
peer := peers.table[name] peer := peers.table[name]
if peer != newPeer && if peer != newPeer &&
(peer == peers.ourself.Peer || peer.version >= newPeer.version) { (peer == peers.ourself.Peer || peer.Version >= newPeer.Version) {
// Nobody but us updates us. And if we know more about a // Nobody but us updates us. And if we know more about a
// peer than what's in the the update, we ignore the // peer than what's in the the update, we ignore the
// latter. // latter.
@@ -275,7 +267,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti
// for an update would be someone else calling // for an update would be someone else calling
// router.Peers.ApplyUpdate. But ApplyUpdate takes the Lock on // router.Peers.ApplyUpdate. But ApplyUpdate takes the Lock on
// the router.Peers, so there can be no race here. // the router.Peers, so there can be no race here.
peer.version = newPeer.version peer.Version = newPeer.Version
peer.connections = makeConnsMap(peer, connSummaries, peers.table) peer.connections = makeConnsMap(peer, connSummaries, peers.table)
newUpdate[name] = peer newUpdate[name] = peer
} }
@@ -283,11 +275,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti
} }
func (peer *Peer) Encode(enc *gob.Encoder) { func (peer *Peer) Encode(enc *gob.Encoder) {
checkFatal(enc.Encode(PeerSummary{ checkFatal(enc.Encode(peer.PeerSummary))
peer.NameByte,
peer.NickName,
peer.UID,
peer.version}))
connSummaries := []ConnectionSummary{} connSummaries := []ConnectionSummary{}
for _, conn := range peer.connections { for _, conn := range peer.connections {