From a334059e85596bd65a0c0506e8c1cf63aa09782e Mon Sep 17 00:00:00 2001 From: David Wragg Date: Tue, 9 Jun 2015 06:30:33 +0100 Subject: [PATCH] Add NewPeerFromSummary To reduce the number of places that are explicit about which values go into a new Peer. --- router/gossip_test.go | 13 +++++-------- router/json.go | 2 +- router/local_peer.go | 8 ++++---- router/mocks_test.go | 10 +++++----- router/peer.go | 43 +++++++++++++++++++++++++++++-------------- router/peers.go | 24 ++++++------------------ 6 files changed, 50 insertions(+), 50 deletions(-) diff --git a/router/gossip_test.go b/router/gossip_test.go index 840c44c9..eba7a0c9 100644 --- a/router/gossip_test.go +++ b/router/gossip_test.go @@ -30,11 +30,8 @@ func (conn *mockChannelConnection) SendProtocolMsg(protocolMsg ProtocolMsg) { } func (router *Router) AddTestChannelConnection(r *Router) { - fromName := router.Ourself.Peer.Name - toName := r.Ourself.Peer.Name - - fromPeer := NewPeer(fromName, "", router.Ourself.Peer.UID, 0) - toPeer := NewPeer(toName, "", r.Ourself.Peer.UID, 0) + fromPeer := NewPeerFromSummary(router.Ourself.Peer.PeerSummary) + toPeer := NewPeerFromSummary(r.Ourself.Peer.PeerSummary) r.Peers.FetchWithDefault(fromPeer) // Has side-effect of incrementing refcount router.Peers.FetchWithDefault(toPeer) // @@ -70,13 +67,13 @@ func TestGossipTopology(t *testing.T) { // the routers supplied as arguments, carrying across all UID and // version information. 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) 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) } - peer.version = router.Ourself.Peer.version + peer.Version = router.Ourself.Peer.Version peer.connections = connections return peer } diff --git a/router/json.go b/router/json.go index aa93c63b..75c76320 100644 --- a/router/json.go +++ b/router/json.go @@ -57,7 +57,7 @@ func (peers *Peers) MarshalJSON() ([]byte, error) { 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) } diff --git a/router/local_peer.go b/router/local_peer.go index 4b8e2262..f41a60cb 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -20,7 +20,7 @@ type LocalPeerAction func() func NewLocalPeer(name PeerName, nickName string, router *Router) *LocalPeer { actionChan := make(chan LocalPeerAction, ChannelSize) peer := &LocalPeer{ - Peer: NewPeer(name, nickName, 0, 0), + Peer: NewPeer(name, nickName, randomPeerUID(), 0), router: router, actionChan: actionChan, } @@ -272,20 +272,20 @@ func (peer *LocalPeer) addConnection(conn Connection) { peer.Lock() defer peer.Unlock() peer.connections[conn.Remote().Name] = conn - peer.version++ + peer.Version++ } func (peer *LocalPeer) deleteConnection(conn Connection) { peer.Lock() defer peer.Unlock() delete(peer.connections, conn.Remote().Name) - peer.version++ + peer.Version++ } func (peer *LocalPeer) connectionEstablished(conn Connection) { peer.Lock() defer peer.Unlock() - peer.version++ + peer.Version++ } func (peer *LocalPeer) connectionCount() int { diff --git a/router/mocks_test.go b/router/mocks_test.go index 674eaa3a..c0308c13 100644 --- a/router/mocks_test.go +++ b/router/mocks_test.go @@ -12,7 +12,9 @@ import ( // Add to peers a connection from peers.ourself to p 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 conn := newMockConnection(peers.ourself.Peer, toPeer) peers.ourself.addConnection(conn) @@ -21,11 +23,9 @@ func (peers *Peers) AddTestConnection(p *Peer) { // Add to peers a connection from p1 to p2 func (peers *Peers) AddTestRemoteConnection(p1, p2 *Peer) { - fromName := p1.Name - fromPeer := NewPeer(fromName, "", p1.UID, 0) + fromPeer := NewPeerFromSummary(p1.PeerSummary) fromPeer = peers.FetchWithDefault(fromPeer) - toName := p2.Name - toPeer := NewPeer(toName, "", p2.UID, 0) + toPeer := NewPeerFromSummary(p2.PeerSummary) toPeer = peers.FetchWithDefault(toPeer) peers.ourself.addConnection(&RemoteConnection{fromPeer, toPeer, "", false, false}) } diff --git a/router/peer.go b/router/peer.go index 43f3f07d..7f79064e 100644 --- a/router/peer.go +++ b/router/peer.go @@ -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 diff --git a/router/peers.go b/router/peers.go index a8d73880..f7977da9 100644 --- a/router/peers.go +++ b/router/peers.go @@ -25,13 +25,6 @@ type NameCollisionError struct { type PeerNameSet map[PeerName]struct{} -type PeerSummary struct { - NameByte []byte - NickName string - UID PeerUID - Version uint64 -} - type ConnectionSummary struct { NameByte []byte RemoteTCPAddr string @@ -220,13 +213,12 @@ func (peers *Peers) decodeUpdate(update []byte) (newPeers map[PeerName]*Peer, de err = decErr return } - name := PeerNameFromBin(peerSummary.NameByte) - newPeer := NewPeer(name, peerSummary.NickName, peerSummary.UID, peerSummary.Version) + newPeer := NewPeerFromSummary(peerSummary) decodedUpdate = append(decodedUpdate, newPeer) decodedConns = append(decodedConns, connSummaries) - existingPeer, found := peers.table[name] + existingPeer, found := peers.table[newPeer.Name] if !found { - newPeers[name] = newPeer + newPeers[newPeer.Name] = newPeer } else if existingPeer.UID != newPeer.UID { err = NameCollisionError{Name: newPeer.Name} return @@ -259,7 +251,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti // guaranteed to find peer in the peers.table peer := peers.table[name] 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 // peer than what's in the the update, we ignore the // latter. @@ -275,7 +267,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti // for an update would be someone else calling // router.Peers.ApplyUpdate. But ApplyUpdate takes the Lock on // 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) newUpdate[name] = peer } @@ -283,11 +275,7 @@ func (peers *Peers) applyUpdate(decodedUpdate []*Peer, decodedConns [][]Connecti } func (peer *Peer) Encode(enc *gob.Encoder) { - checkFatal(enc.Encode(PeerSummary{ - peer.NameByte, - peer.NickName, - peer.UID, - peer.version})) + checkFatal(enc.Encode(peer.PeerSummary)) connSummaries := []ConnectionSummary{} for _, conn := range peer.connections {