refactor: narrow dependencies of Routes

it doesn't need to know about the Router, just Ourself and Peers.
This commit is contained in:
Matthias Radestock committed 2014-12-24 19:40:18 +00:00
1 parent 2b2f47c031
commit b3be24248e
2 files changed
+14 -12

No files matched your search

+1 -1
View File
@@ -53,7 +53,7 @@ func (router *Router) Start() {
po, err := NewPcapO(router.Iface.Name)
checkFatal(err)
router.ConnectionMaker = StartConnectionMaker(router)
router.Routes = StartRoutes(router)
router.Routes = StartRoutes(router.Ourself, router.Peers)
router.UDPListener = router.listenUDP(Port, po)
router.listenTCP(Port)
router.sniff(pio)
+13 -11
View File
@@ -9,10 +9,11 @@ import (
type Routes struct {
sync.RWMutex
queryChan chan<- *Interaction
ourself *Peer
peers *Peers
unicast map[PeerName]PeerName
broadcast map[PeerName][]PeerName
router *Router
queryChan chan<- *Interaction
}
func (routes *Routes) Unicast(name PeerName) (PeerName, bool) {
@@ -47,15 +48,16 @@ func (routes *Routes) String() string {
return buf.String()
}
func StartRoutes(router *Router) *Routes {
func StartRoutes(ourself *Peer, peers *Peers) *Routes {
queryChan := make(chan *Interaction, ChannelSize)
state := &Routes{
router: router,
queryChan: queryChan,
ourself: ourself,
peers: peers,
unicast: make(map[PeerName]PeerName),
broadcast: make(map[PeerName][]PeerName)}
state.unicast[router.Ourself.Name] = UnknownPeerName
state.broadcast[router.Ourself.Name] = []PeerName{}
broadcast: make(map[PeerName][]PeerName),
queryChan: queryChan}
state.unicast[ourself.Name] = UnknownPeerName
state.broadcast[ourself.Name] = []PeerName{}
go state.queryLoop(queryChan)
return state
}
@@ -103,7 +105,7 @@ func (routes *Routes) queryLoop(queryChan <-chan *Interaction) {
// to exchange knowledge of MAC addresses, nor any constraints on
// the routes that we construct.
func (routes *Routes) calculateUnicast() map[PeerName]PeerName {
_, unicast := routes.router.Ourself.Routes(nil, true)
_, unicast := routes.ourself.Routes(nil, true)
return unicast
}
@@ -127,9 +129,9 @@ func (routes *Routes) calculateUnicast() map[PeerName]PeerName {
// where <= is the subset relationship on keys of the returned map.
func (routes *Routes) calculateBroadcast() map[PeerName][]PeerName {
broadcast := make(map[PeerName][]PeerName)
ourself := routes.router.Ourself
ourself := routes.ourself
routes.router.Peers.ForEach(func(name PeerName, peer *Peer) {
routes.peers.ForEach(func(name PeerName, peer *Peer) {
hops := []PeerName{}
if found, reached := peer.Routes(ourself, true); found {
ourself.ForEachConnection(func(remoteName PeerName, conn Connection) {