Clean up naming of commands and states
This commit is contained in:
1 parent
6aaa57606a
commit
527ddfca0d
1 file changed
+13
-13
+13
-13
@@ -20,9 +20,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CMUnconnected ConnectionState = iota
|
CSUnconnected ConnectionState = iota
|
||||||
CMAttempting = iota
|
CSAttempting = iota
|
||||||
CMEstablishedState = iota
|
CSEstablished = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
func StartConnectionMaker(router *Router) *ConnectionMaker {
|
func StartConnectionMaker(router *Router) *ConnectionMaker {
|
||||||
@@ -100,13 +100,13 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
case query.code == CMEstablished:
|
case query.code == CMEstablished:
|
||||||
target := cm.targets[query.address]
|
target := cm.targets[query.address]
|
||||||
if target != nil {
|
if target != nil {
|
||||||
target.state = CMEstablishedState
|
target.state = CSEstablished
|
||||||
} else {
|
} else {
|
||||||
log.Fatal("Connection established to unknown address", query.address)
|
log.Fatal("Connection established to unknown address", query.address)
|
||||||
}
|
}
|
||||||
case query.code == CMConnFailed:
|
case query.code == CMConnFailed:
|
||||||
target := cm.targets[query.address]
|
target := cm.targets[query.address]
|
||||||
target.state = CMUnconnected
|
target.state = CSUnconnected
|
||||||
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
||||||
maybeTick()
|
maybeTick()
|
||||||
default:
|
default:
|
||||||
@@ -114,12 +114,12 @@ func (cm *ConnectionMaker) queryLoop(queryChan <-chan *ConnectionMakerInteractio
|
|||||||
}
|
}
|
||||||
case now := <-tick:
|
case now := <-tick:
|
||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
if target.state == CMUnconnected && now.After(target.tryAfter) {
|
if target.state == CSUnconnected && now.After(target.tryAfter) {
|
||||||
if _, found := cm.router.Ourself.ConnectionOn(address); found {
|
if _, found := cm.router.Ourself.ConnectionOn(address); found {
|
||||||
target.state = CMEstablished
|
target.state = CSEstablished
|
||||||
} else {
|
} else {
|
||||||
target.attemptCount += 1
|
target.attemptCount += 1
|
||||||
target.state = CMAttempting
|
target.state = CSAttempting
|
||||||
go cm.attemptConnection(address, target.acceptAnyPeer)
|
go cm.attemptConnection(address, target.acceptAnyPeer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,7 +134,7 @@ func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
|
|||||||
target := cm.targets[address]
|
target := cm.targets[address]
|
||||||
if target == nil {
|
if target == nil {
|
||||||
target = &Target{
|
target = &Target{
|
||||||
state: CMUnconnected,
|
state: CSUnconnected,
|
||||||
acceptAnyPeer: acceptAnyPeer,
|
acceptAnyPeer: acceptAnyPeer,
|
||||||
}
|
}
|
||||||
cm.targets[address] = target
|
cm.targets[address] = target
|
||||||
@@ -144,20 +144,20 @@ func (cm *ConnectionMaker) addToTargets(acceptAnyPeer bool, address string) {
|
|||||||
func (cm *ConnectionMaker) resetTarget(address string) {
|
func (cm *ConnectionMaker) resetTarget(address string) {
|
||||||
target := cm.targets[address]
|
target := cm.targets[address]
|
||||||
// If we were in the middle of a connection attempt then back-off again
|
// If we were in the middle of a connection attempt then back-off again
|
||||||
if target.state == CMAttempting {
|
if target.state == CSAttempting {
|
||||||
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
target.tryAfter, target.tryInterval = tryAfter(target.tryInterval)
|
||||||
} else {
|
} else {
|
||||||
target.tryAfter, target.tryInterval = tryImmediately()
|
target.tryAfter, target.tryInterval = tryImmediately()
|
||||||
}
|
}
|
||||||
target.state = CMUnconnected
|
target.state = CSUnconnected
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConnectionMaker) status() string {
|
func (cm *ConnectionMaker) status() string {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
if target.state == CMEstablishedState {
|
if target.state == CSEstablished {
|
||||||
buf.WriteString(fmt.Sprintf("%s connected\n", address))
|
buf.WriteString(fmt.Sprintf("%s connected\n", address))
|
||||||
} else if target.state == CMAttempting {
|
} else if target.state == CSAttempting {
|
||||||
buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter))
|
buf.WriteString(fmt.Sprintf("%s (%v attempts, trying since %v)\n", address, target.attemptCount, target.tryAfter))
|
||||||
} else {
|
} else {
|
||||||
buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter))
|
buf.WriteString(fmt.Sprintf("%s (%v attempts, next at %v)\n", address, target.attemptCount, target.tryAfter))
|
||||||
|
|||||||
Reference in new issue
Block a user