refactor: reduce dependencies of ConnectionMaker

it doesn't need to know about Router, just Ourself and Peers
This commit is contained in:
Matthias Radestock committed 2014-12-24 19:53:11 +00:00
1 parent 71cd83bf20
commit 945a5cf7fe
2 files changed
+10 -9

No files matched your search

+9 -8
View File
@@ -22,7 +22,8 @@ const (
) )
type ConnectionMaker struct { type ConnectionMaker struct {
router *Router ourself *Peer
peers *Peers
targets map[string]*Target targets map[string]*Target
cmdLineAddress map[string]bool cmdLineAddress map[string]bool
queryChan chan<- *ConnectionMakerInteraction queryChan chan<- *ConnectionMakerInteraction
@@ -40,10 +41,11 @@ type ConnectionMakerInteraction struct {
address string address string
} }
func StartConnectionMaker(router *Router) *ConnectionMaker { func StartConnectionMaker(ourself *Peer, peers *Peers) *ConnectionMaker {
queryChan := make(chan *ConnectionMakerInteraction, ChannelSize) queryChan := make(chan *ConnectionMakerInteraction, ChannelSize)
state := &ConnectionMaker{ state := &ConnectionMaker{
router: router, ourself: ourself,
peers: peers,
queryChan: queryChan, queryChan: queryChan,
cmdLineAddress: make(map[string]bool), cmdLineAddress: make(map[string]bool),
targets: make(map[string]*Target)} targets: make(map[string]*Target)}
@@ -110,13 +112,12 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
} }
func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration { func (cm *ConnectionMaker) checkStateAndAttemptConnections() time.Duration {
ourself := cm.router.Ourself
validTarget := make(map[string]bool) validTarget := make(map[string]bool)
// copy the set of things we are connected to, so we can access them without locking // 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_peers := make(map[PeerName]bool)
our_connected_targets := make(map[string]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_peers[peer] = true
our_connected_targets[conn.RemoteTCPAddr()] = 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 // Add targets for peers that someone else is connected to, but we
// aren't // 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) { 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 return
} }
address := conn.RemoteTCPAddr() address := conn.RemoteTCPAddr()
@@ -198,7 +199,7 @@ func (cm *ConnectionMaker) status() string {
func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) { func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) {
log.Println("Attempting connection to", address) 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) log.Println(err)
cm.ConnectionTerminated(address) cm.ConnectionTerminated(address)
} }
+1 -1
View File
@@ -52,7 +52,7 @@ func (router *Router) Start() {
checkFatal(err) checkFatal(err)
po, err := NewPcapO(router.Iface.Name) po, err := NewPcapO(router.Iface.Name)
checkFatal(err) checkFatal(err)
router.ConnectionMaker = StartConnectionMaker(router) router.ConnectionMaker = StartConnectionMaker(router.Ourself, router.Peers)
router.Routes = StartRoutes(router.Ourself, router.Peers) router.Routes = StartRoutes(router.Ourself, router.Peers)
router.UDPListener = router.listenUDP(Port, po) router.UDPListener = router.listenUDP(Port, po)
router.listenTCP(Port) router.listenTCP(Port)