refactor: use better 'switch'

This commit is contained in:
Matthias Radestock committed 2014-09-22 22:34:46 +01:00
1 parent f11f84fe17
commit d5e797f695
4 files changed
+19 -19

No files matched your search

+5 -5
View File
@@ -182,14 +182,14 @@ func (conn *LocalConnection) queryLoop(queryChan <-chan *ConnectionInteraction,
if !ok { if !ok {
break break
} }
switch { switch query.code {
case query.code == CShutdown: case CShutdown:
terminate = true terminate = true
case query.code == CSetEstablished: case CSetEstablished:
err = conn.handleSetEstablished() err = conn.handleSetEstablished()
case query.code == CSetRemoteUDPAddr: case CSetRemoteUDPAddr:
err = conn.handleSetRemoteUDPAddr(query.payload.(*net.UDPAddr)) err = conn.handleSetRemoteUDPAddr(query.payload.(*net.UDPAddr))
case query.code == CSendTCP: case CSendTCP:
err = conn.handleSendTCP(query.payload.([]byte)) err = conn.handleSendTCP(query.payload.([]byte))
} }
} }
+6 -6
View File
@@ -16,8 +16,8 @@ const (
const ( const (
CMInitiate = iota CMInitiate = iota
CMStatus = iota
CMTerminated = iota CMTerminated = iota
CMStatus = iota
) )
func StartConnectionMaker(router *Router) *ConnectionMaker { func StartConnectionMaker(router *Router) *ConnectionMaker {
@@ -64,20 +64,20 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
if !ok { if !ok {
return return
} }
switch { switch query.code {
case query.code == CMInitiate: case CMInitiate:
cm.cmdLineAddress[NormalisePeerAddr(query.address)] = true cm.cmdLineAddress[NormalisePeerAddr(query.address)] = true
cm.checkStateAndAttemptConnections() cm.checkStateAndAttemptConnections()
maybeTick() maybeTick()
case query.code == CMStatus: case CMTerminated:
query.resultChan <- cm.status()
case query.code == CMTerminated:
if target, found := cm.targets[query.address]; found { if target, found := cm.targets[query.address]; found {
target.attempting = false target.attempting = false
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval) target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
} }
cm.checkStateAndAttemptConnections() cm.checkStateAndAttemptConnections()
maybeTick() maybeTick()
case CMStatus:
query.resultChan <- cm.status()
default: default:
log.Fatal("Unexpected connection maker query:", query) log.Fatal("Unexpected connection maker query:", query)
} }
+5 -5
View File
@@ -259,15 +259,15 @@ func (peer *Peer) queryLoop(queryChan <-chan *PeerInteraction) {
if !ok { if !ok {
return return
} }
switch { switch query.code {
case query.code == PAddConnection: case PAddConnection:
peer.handleAddConnection(query.payload.(*LocalConnection)) peer.handleAddConnection(query.payload.(*LocalConnection))
case query.code == PDeleteConnection: case PDeleteConnection:
peer.handleDeleteConnection(query.payload.(*LocalConnection)) peer.handleDeleteConnection(query.payload.(*LocalConnection))
query.resultChan <- nil query.resultChan <- nil
case query.code == PConnectionEstablished: case PConnectionEstablished:
peer.handleConnectionEstablished(query.payload.(*LocalConnection)) peer.handleConnectionEstablished(query.payload.(*LocalConnection))
case query.code == PBroadcastTCP: case PBroadcastTCP:
peer.handleBroadcastTCP(query.payload.([]byte)) peer.handleBroadcastTCP(query.payload.([]byte))
} }
} }
+3 -3
View File
@@ -81,15 +81,15 @@ func (topo *Topology) queryLoop(queryChan <-chan *Interaction) {
if !ok { if !ok {
return return
} }
switch { switch query.code {
case query.code == TRebuildRoutes: case TRebuildRoutes:
unicast := topo.buildUnicastRoutes() unicast := topo.buildUnicastRoutes()
broadcast := topo.buildBroadcastRoutes() broadcast := topo.buildBroadcastRoutes()
topo.Lock() topo.Lock()
topo.unicast = unicast topo.unicast = unicast
topo.broadcast = broadcast topo.broadcast = broadcast
topo.Unlock() topo.Unlock()
case query.code == TFetchAll: case TFetchAll:
query.resultChan <- topo.router.Peers.EncodeAllPeers() query.resultChan <- topo.router.Peers.EncodeAllPeers()
default: default:
log.Fatal("Unexpected topology query:", query) log.Fatal("Unexpected topology query:", query)