From 21102d3615f2db07082c7803e6acac253019df30 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Sun, 12 Apr 2015 10:46:46 +0100 Subject: [PATCH] refactor: extract router configuration the param list of NewRouter was getting a bit long --- router/gossip_test.go | 2 +- router/router.go | 31 ++++++++++++++++++++++--------- weaver/main.go | 14 ++++++++++++-- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/router/gossip_test.go b/router/gossip_test.go index 371c77c4..e70977b8 100644 --- a/router/gossip_test.go +++ b/router/gossip_test.go @@ -17,7 +17,7 @@ type mockChannelConnection struct { // Construct a "passive" Router, i.e. without any goroutines, except // for Routes and GossipSenders. func NewTestRouter(name PeerName) *Router { - router := NewRouter(nil, name, "", nil, 10, 1024, nil, Port) + router := NewRouter(RouterConfig{Name: name}) // need to create a dummy channel otherwise tests hang on nil // channels when the Router invoked ConnectionMaker.Refresh router.ConnectionMaker.actionChan = make(chan ConnectionMakerAction, ChannelSize) diff --git a/router/router.go b/router/router.go index 4e20dd57..0197b008 100644 --- a/router/router.go +++ b/router/router.go @@ -17,6 +17,19 @@ const macMaxAge = 10 * time.Minute // [1] // [1] should be greater than typical ARP cache expiries, i.e. > 3/2 * // /proc/sys/net/ipv4_neigh/*/base_reachable_time_ms on Linux +type LogFrameFunc func(string, []byte, *layers.Ethernet) + +type RouterConfig struct { + Port int + Iface *net.Interface + Name PeerName + NickName string + Password []byte + ConnLimit int + BufSz int + LogFrame LogFrameFunc +} + type Router struct { Port int Iface *net.Interface @@ -31,7 +44,7 @@ type Router struct { Password []byte ConnLimit int BufSz int - LogFrame func(string, []byte, *layers.Ethernet) + LogFrame LogFrameFunc } type PacketSource interface { @@ -47,15 +60,15 @@ type PacketSourceSink interface { PacketSink } -func NewRouter(iface *net.Interface, name PeerName, nickName string, password []byte, connLimit int, bufSz int, logFrame func(string, []byte, *layers.Ethernet), port int) *Router { +func NewRouter(config RouterConfig) *Router { router := &Router{ - Iface: iface, + Port: config.Port, + Iface: config.Iface, GossipChannels: make(map[uint32]*GossipChannel), - Password: password, - ConnLimit: connLimit, - BufSz: bufSz, - LogFrame: logFrame, - Port: port} + Password: config.Password, + ConnLimit: config.ConnLimit, + BufSz: config.BufSz, + LogFrame: config.LogFrame} onMacExpiry := func(mac net.HardwareAddr, peer *Peer) { log.Println("Expired MAC", mac, "at", peer.FullName()) } @@ -63,7 +76,7 @@ func NewRouter(iface *net.Interface, name PeerName, nickName string, password [] router.Macs.Delete(peer) log.Println("Removed unreachable peer", peer.FullName()) } - router.Ourself = NewLocalPeer(name, nickName, router) + router.Ourself = NewLocalPeer(config.Name, config.NickName, router) router.Macs = NewMacCache(macMaxAge, onMacExpiry) router.Peers = NewPeers(router.Ourself.Peer, onPeerGC) router.Peers.FetchWithDefault(router.Ourself.Peer) diff --git a/weaver/main.go b/weaver/main.go index 87c8d129..a872faae 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -128,7 +128,17 @@ func main() { defer profile.Start(&p).Stop() } - router := weave.NewRouter(iface, ourName, nickName, pwSlice, connLimit, bufSz*1024*1024, logFrameFunc(debug), port) + router := weave.NewRouter( + weave.RouterConfig{ + Port: port, + Iface: iface, + Name: ourName, + NickName: nickName, + Password: pwSlice, + ConnLimit: connLimit, + BufSz: bufSz * 1024 * 1024, + LogFrame: logFrameFunc(debug)}) + log.Println("Our name is", router.Ourself.FullName()) router.Start() initiateConnections(router, peers) @@ -138,7 +148,7 @@ func main() { handleSignals(router) } -func logFrameFunc(debug bool) func(string, []byte, *layers.Ethernet) { +func logFrameFunc(debug bool) weave.LogFrameFunc { if !debug { return func(prefix string, frame []byte, eth *layers.Ethernet) {} }