refactor: make tie breaking part of the Connection interface

and thus eliminate all knowledge of LocalConnection from the
LocalPeer.handle* methods
This commit is contained in:
Matthias Radestock committed 2015-01-21 21:44:22 +00:00
1 parent 99525ebf85
commit 8af2a1c25d
2 files changed
+36 -22

No files matched your search

+9 -3
View File
@@ -15,6 +15,7 @@ import (
type Connection interface {
Local() *Peer
Remote() *Peer
BreakTie(Connection) ConnectionTieBreak
RemoteTCPAddr() string
Established() bool
Shutdown(error)
@@ -80,6 +81,10 @@ func (conn *RemoteConnection) Remote() *Peer {
return conn.remote
}
func (conn *RemoteConnection) BreakTie(Connection) ConnectionTieBreak {
return TieBreakTied
}
func (conn *RemoteConnection) RemoteTCPAddr() string {
return conn.remoteTCPAddr
}
@@ -124,12 +129,13 @@ func (conn *LocalConnection) Start(acceptNewPeer bool) {
go conn.run(queryChan, acceptNewPeer)
}
func (conn *LocalConnection) BreakTie(dupConn *LocalConnection) ConnectionTieBreak {
func (conn *LocalConnection) BreakTie(dupConn Connection) ConnectionTieBreak {
dupConnLocal := dupConn.(*LocalConnection)
// conn.uid is used as the tie breaker here, in the knowledge that
// both sides will make the same decision.
if conn.uid < dupConn.uid {
if conn.uid < dupConnLocal.uid {
return TieBreakWon
} else if dupConn.uid < conn.uid {
} else if dupConnLocal.uid < conn.uid {
return TieBreakLost
} else {
return TieBreakTied