refactor: extract Peer.ForEachConnectedPeer helper

This commit is contained in:
Matthias Radestock committed 2015-05-11 21:06:07 +01:00
1 parent c5c93d3a77
commit 953449c215
2 files changed
+22 -25

No files matched your search

+20 -11
View File
@@ -73,15 +73,10 @@ func (peer *Peer) Routes(stopAt *Peer, establishedAndSymmetric bool) (bool, map[
if curPeer == stopAt {
return true, routes
}
for remoteName, conn := range curPeer.connections {
if establishedAndSymmetric && !conn.Established() {
continue
}
if _, found := routes[remoteName]; found {
continue
}
if remoteConn, found := conn.Remote().connections[curPeer.Name]; !establishedAndSymmetric || (found && remoteConn.Established()) {
nextWorklist = append(nextWorklist, conn.Remote())
curPeer.ForEachConnectedPeer(establishedAndSymmetric, routes,
func(remotePeer *Peer) {
nextWorklist = append(nextWorklist, remotePeer)
remoteName := remotePeer.Name
// We now know how to get to remoteName: the same
// way we get to curPeer. Except, if curPeer is
// the starting peer in which case we know we can
@@ -91,9 +86,23 @@ func (peer *Peer) Routes(stopAt *Peer, establishedAndSymmetric bool) (bool, map[
} else {
routes[remoteName] = routes[curPeer.Name]
}
}
}
})
}
}
return false, routes
}
func (peer *Peer) ForEachConnectedPeer(establishedAndSymmetric bool, exclude map[PeerName]PeerName, f func(*Peer)) {
for remoteName, conn := range peer.connections {
if establishedAndSymmetric && !conn.Established() {
continue
}
if _, found := exclude[remoteName]; found {
continue
}
remotePeer := conn.Remote()
if remoteConn, found := remotePeer.connections[peer.Name]; !establishedAndSymmetric || (found && remoteConn.Established()) {
f(remotePeer)
}
}
}
+2 -14
View File
@@ -222,20 +222,8 @@ func (routes *Routes) calculateBroadcast(establishedAndSymmetric bool) map[PeerN
for _, peer := range routes.peers.table {
hops := []PeerName{}
if found, reached := peer.Routes(routes.ourself.Peer, establishedAndSymmetric); found {
// This is rather similar to the inner loop on
// peer.Routes(...); the main difference is in the
// locking.
for remoteName, conn := range routes.ourself.connections {
if establishedAndSymmetric && !conn.Established() {
continue
}
if _, found := reached[remoteName]; found {
continue
}
if remoteConn, found := conn.Remote().connections[routes.ourself.Name]; !establishedAndSymmetric || (found && remoteConn.Established()) {
hops = append(hops, remoteName)
}
}
routes.ourself.ForEachConnectedPeer(establishedAndSymmetric, reached,
func(remotePeer *Peer) { hops = append(hops, remotePeer.Name) })
}
broadcast[peer.Name] = hops
}