From d5e797f695639014c2df1dc1cc651d77f9306733 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 22 Sep 2014 22:34:46 +0100 Subject: [PATCH] refactor: use better 'switch' --- connection.go | 10 +++++----- connection_maker.go | 12 ++++++------ peer.go | 10 +++++----- topology.go | 6 +++--- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/connection.go b/connection.go index 014183b3..61ced2b0 100644 --- a/connection.go +++ b/connection.go @@ -182,14 +182,14 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction, if !ok { break } - switch { - case query.code == CShutdown: + switch query.code { + case CShutdown: terminate = true - case query.code == CSetEstablished: + case CSetEstablished: err = conn.handleSetEstablished() - case query.code == CSetRemoteUDPAddr: + case CSetRemoteUDPAddr: err = conn.handleSetRemoteUDPAddr(query.payload.(*net.UDPAddr)) - case query.code == CSendTCP: + case CSendTCP: err = conn.handleSendTCP(query.payload.([]byte)) } } diff --git a/connection_maker.go b/connection_maker.go index a9a53f22..e52c413e 100644 --- a/connection_maker.go +++ b/connection_maker.go @@ -16,8 +16,8 @@ const ( const ( CMInitiate = iota - CMStatus = iota CMTerminated = iota + CMStatus = iota ) func StartConnectionMaker(router *Router) *ConnectionMaker { @@ -64,20 +64,20 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio if !ok { return } - switch { - case query.code == CMInitiate: + switch query.code { + case CMInitiate: cm.cmdLineAddress[NormalisePeerAddr(query.address)] = true cm.checkStateAndAttemptConnections() maybeTick() - case query.code == CMStatus: - query.resultChan <- cm.status() - case query.code == CMTerminated: + case CMTerminated: if target, found := cm.targets[query.address]; found { target.attempting = false target.tryAfter, target.tryInterval = tryAfter(target.tryInterval) } cm.checkStateAndAttemptConnections() maybeTick() + case CMStatus: + query.resultChan <- cm.status() default: log.Fatal("Unexpected connection maker query:", query) } diff --git a/peer.go b/peer.go index b62e8b27..80dc3d2a 100644 --- a/peer.go +++ b/peer.go @@ -259,15 +259,15 @@ func (peer *Peer) queryLoop(queryChan <-chan *PeerInteraction) { if !ok { return } - switch { - case query.code == PAddConnection: + switch query.code { + case PAddConnection: peer.handleAddConnection(query.payload.(*LocalConnection)) - case query.code == PDeleteConnection: + case PDeleteConnection: peer.handleDeleteConnection(query.payload.(*LocalConnection)) query.resultChan <- nil - case query.code == PConnectionEstablished: + case PConnectionEstablished: peer.handleConnectionEstablished(query.payload.(*LocalConnection)) - case query.code == PBroadcastTCP: + case PBroadcastTCP: peer.handleBroadcastTCP(query.payload.([]byte)) } } diff --git a/topology.go b/topology.go index c0db270c..fe3a9323 100644 --- a/topology.go +++ b/topology.go @@ -81,15 +81,15 @@ func (topo *Topology) queryLoop(queryChan <-chan *Interaction) { if !ok { return } - switch { - case query.code == TRebuildRoutes: + switch query.code { + case TRebuildRoutes: unicast := topo.buildUnicastRoutes() broadcast := topo.buildBroadcastRoutes() topo.Lock() topo.unicast = unicast topo.broadcast = broadcast topo.Unlock() - case query.code == TFetchAll: + case TFetchAll: query.resultChan <- topo.router.Peers.EncodeAllPeers() default: log.Fatal("Unexpected topology query:", query)