From 945a5cf7fee6ba14d91edfb311b10fbb3eb8ac81 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 24 Dec 2014 19:53:11 +0000 Subject: [PATCH] refactor: reduce dependencies of ConnectionMaker it doesn't need to know about Router, just Ourself and Peers --- router/connection_maker.go | 17 +++++++++-------- router/router.go | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/router/connection_maker.go b/router/connection_maker.go index ae15ea25..6733f95d 100644 --- a/router/connection_maker.go +++ b/router/connection_maker.go @@ -22,7 +22,8 @@ const ( ) type ConnectionMaker struct { - router *Router + ourself *Peer + peers *Peers targets map[string]*Target cmdLineAddress map[string]bool queryChan chan<- *ConnectionMakerInteraction @@ -40,10 +41,11 @@ type ConnectionMakerInteraction struct { address string } -func StartConnectionMaker(router *Router) *ConnectionMaker { +func StartConnectionMaker(ourself *Peer, peers *Peers) *ConnectionMaker { queryChan := make(chan *ConnectionMakerInteraction, ChannelSize) state := &ConnectionMaker{ - router: router, + ourself: ourself, + peers: peers, queryChan: queryChan, cmdLineAddress: make(map[string]bool), targets: make(map[string]*Target)} @@ -110,13 +112,12 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio } func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration { - ourself := cm.router.Ourself validTarget := make(map[string]bool) // copy the set of things we are connected to, so we can access them without locking our_connected_peers := make(map[PeerName]bool) our_connected_targets := make(map[string]bool) - ourself.ForEachConnection(func(peer PeerName, conn Connection) { + cm.ourself.ForEachConnection(func(peer PeerName, conn Connection) { our_connected_peers[peer] = true our_connected_targets[conn.RemoteTCPAddr()] = true }) @@ -135,9 +136,9 @@ func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration { // Add targets for peers that someone else is connected to, but we // aren't - cm.router.Peers.ForEach(func(name PeerName, peer *Peer) { + cm.peers.ForEach(func(name PeerName, peer *Peer) { peer.ForEachConnection(func(otherPeer PeerName, conn Connection) { - if otherPeer == ourself.Name || our_connected_peers[otherPeer] { + if otherPeer == cm.ourself.Name || our_connected_peers[otherPeer] { return } address := conn.RemoteTCPAddr() @@ -198,7 +199,7 @@ func (cm *ConnectionMaker) status() string { func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) { log.Println("Attempting connection to", address) - if err := cm.router.Ourself.CreateConnection(address, acceptNewPeer); err != nil { + if err := cm.ourself.CreateConnection(address, acceptNewPeer); err != nil { log.Println(err) cm.ConnectionTerminated(address) } diff --git a/router/router.go b/router/router.go index daa236bc..c968c270 100644 --- a/router/router.go +++ b/router/router.go @@ -52,7 +52,7 @@ func (router *Router) Start() { checkFatal(err) po, err := NewPcapO(router.Iface.Name) checkFatal(err) - router.ConnectionMaker = StartConnectionMaker(router) + router.ConnectionMaker = StartConnectionMaker(router.Ourself, router.Peers) router.Routes = StartRoutes(router.Ourself, router.Peers) router.UDPListener = router.listenUDP(Port, po) router.listenTCP(Port)