Skip to content

Commit

Permalink
Merge pull request #556 from rade/do_not_duplicate_router_config
Browse files Browse the repository at this point in the history
LGTM
  • Loading branch information
bboreham committed Apr 14, 2015
2 parents 9b29d2e + 798bfeb commit d334b90
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 29 deletions.
5 changes: 3 additions & 2 deletions router/local_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ func (peer *LocalPeer) broadcastPeerUpdate(peers ...*Peer) {
}

func (peer *LocalPeer) checkConnectionLimit() error {
if 0 != peer.router.ConnLimit && peer.ConnectionCount() >= peer.router.ConnLimit {
return fmt.Errorf("Connection limit reached (%v)", peer.router.ConnLimit)
limit := peer.router.ConnLimit
if 0 != limit && peer.ConnectionCount() >= limit {
return fmt.Errorf("Connection limit reached (%v)", limit)
}
return nil
}
22 changes: 4 additions & 18 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ 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
RouterConfig
Ourself *LocalPeer
Macs *MacCache
Peers *Peers
Expand All @@ -41,10 +38,6 @@ type Router struct {
GossipChannels map[uint32]*GossipChannel
TopologyGossip Gossip
UDPListener *net.UDPConn
Password []byte
ConnLimit int
BufSz int
LogFrame LogFrameFunc
}

type PacketSource interface {
Expand All @@ -60,23 +53,16 @@ type PacketSourceSink interface {
PacketSink
}

func NewRouter(config RouterConfig) *Router {
router := &Router{
Port: config.Port,
Iface: config.Iface,
GossipChannels: make(map[uint32]*GossipChannel),
Password: config.Password,
ConnLimit: config.ConnLimit,
BufSz: config.BufSz,
LogFrame: config.LogFrame}
func NewRouter(config RouterConfig, name PeerName, nickName string) *Router {
router := &Router{RouterConfig: config, GossipChannels: make(map[uint32]*GossipChannel)}
onMacExpiry := func(mac net.HardwareAddr, peer *Peer) {
log.Println("Expired MAC", mac, "at", peer.FullName())
}
onPeerGC := func(peer *Peer) {
router.Macs.Delete(peer)
log.Println("Removed unreachable peer", peer.FullName())
}
router.Ourself = NewLocalPeer(config.Name, config.NickName, router)
router.Ourself = NewLocalPeer(name, nickName, router)
router.Macs = NewMacCache(macMaxAge, onMacExpiry)
router.Peers = NewPeers(router.Ourself.Peer, onPeerGC)
router.Peers.FetchWithDefault(router.Ourself.Peer)
Expand Down
18 changes: 9 additions & 9 deletions weaver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
justVersion bool
ifaceName string
routerName string
nickName string
password string
wait int
debug bool
Expand All @@ -52,7 +53,7 @@ func main() {
flag.IntVar(&config.Port, "port", weave.Port, "router port")
flag.StringVar(&ifaceName, "iface", "", "name of interface to capture/inject from (disabled if blank)")
flag.StringVar(&routerName, "name", "", "name of router (defaults to MAC of interface)")
flag.StringVar(&config.NickName, "nickname", "", "nickname of peer (defaults to hostname)")
flag.StringVar(&nickName, "nickname", "", "nickname of peer (defaults to hostname)")
flag.StringVar(&password, "password", "", "network password")
flag.IntVar(&wait, "wait", 0, "number of seconds to wait for interface to be created and come up (0 = don't wait)")
flag.BoolVar(&debug, "debug", false, "enable debug logging")
Expand Down Expand Up @@ -86,19 +87,18 @@ func main() {
}
routerName = config.Iface.HardwareAddr.String()
}
name, err := weave.PeerNameFromUserInput(routerName)
if err != nil {
log.Fatal(err)
}

if config.NickName == "" {
config.NickName, err = os.Hostname()
if nickName == "" {
nickName, err = os.Hostname()
if err != nil {
log.Fatal(err)
}
}

config.Name, err = weave.PeerNameFromUserInput(routerName)
if err != nil {
log.Fatal(err)
}

if password == "" {
password = os.Getenv("WEAVE_PASSWORD")
}
Expand All @@ -119,7 +119,7 @@ func main() {
config.BufSz = bufSzMB * 1024 * 1024
config.LogFrame = logFrameFunc(debug)

router := weave.NewRouter(config)
router := weave.NewRouter(config, name, nickName)
log.Println("Our name is", router.Ourself.FullName())
router.Start()
initiateConnections(router, peers)
Expand Down

0 comments on commit d334b90

Please sign in to comment.