2023-03-17 10:37:27 +01:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
stateDisconnected = iota
|
|
|
|
stateConnected
|
|
|
|
stateConnecting
|
2023-03-29 10:39:54 +02:00
|
|
|
stateDisconnecting
|
2023-03-17 10:37:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type notifier struct {
|
|
|
|
serverStateLock sync.Mutex
|
|
|
|
listenersLock sync.Mutex
|
2023-04-03 16:59:13 +02:00
|
|
|
listener Listener
|
2023-03-17 10:37:27 +01:00
|
|
|
currentClientState bool
|
|
|
|
lastNotification int
|
2023-08-04 14:14:08 +02:00
|
|
|
lastNumberOfPeers int
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newNotifier() *notifier {
|
2023-04-03 16:59:13 +02:00
|
|
|
return ¬ifier{}
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:59:13 +02:00
|
|
|
func (n *notifier) setListener(listener Listener) {
|
2023-03-17 10:37:27 +01:00
|
|
|
n.listenersLock.Lock()
|
|
|
|
defer n.listenersLock.Unlock()
|
|
|
|
|
|
|
|
n.serverStateLock.Lock()
|
2023-04-03 16:59:13 +02:00
|
|
|
n.notifyListener(listener, n.lastNotification)
|
2023-08-04 14:14:08 +02:00
|
|
|
listener.OnPeersListChanged(n.lastNumberOfPeers)
|
2023-03-17 10:37:27 +01:00
|
|
|
n.serverStateLock.Unlock()
|
2023-04-03 16:59:13 +02:00
|
|
|
|
|
|
|
n.listener = listener
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:59:13 +02:00
|
|
|
func (n *notifier) removeListener() {
|
2023-03-17 10:37:27 +01:00
|
|
|
n.listenersLock.Lock()
|
|
|
|
defer n.listenersLock.Unlock()
|
2023-04-03 16:59:13 +02:00
|
|
|
n.listener = nil
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) updateServerStates(mgmState bool, signalState bool) {
|
|
|
|
n.serverStateLock.Lock()
|
|
|
|
defer n.serverStateLock.Unlock()
|
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
calculatedState := n.calculateState(mgmState, signalState)
|
2023-03-17 10:37:27 +01:00
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
if !n.isServerStateChanged(calculatedState) {
|
2023-03-17 10:37:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
n.lastNotification = calculatedState
|
2023-03-29 10:39:54 +02:00
|
|
|
|
2023-04-03 16:59:13 +02:00
|
|
|
n.notify(n.lastNotification)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) clientStart() {
|
|
|
|
n.serverStateLock.Lock()
|
|
|
|
defer n.serverStateLock.Unlock()
|
|
|
|
n.currentClientState = true
|
2023-04-10 18:22:25 +02:00
|
|
|
n.lastNotification = stateConnected
|
2023-04-03 16:59:13 +02:00
|
|
|
n.notify(n.lastNotification)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) clientStop() {
|
|
|
|
n.serverStateLock.Lock()
|
|
|
|
defer n.serverStateLock.Unlock()
|
|
|
|
n.currentClientState = false
|
2023-04-10 18:22:25 +02:00
|
|
|
n.lastNotification = stateDisconnected
|
2023-04-03 16:59:13 +02:00
|
|
|
n.notify(n.lastNotification)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 10:39:54 +02:00
|
|
|
func (n *notifier) clientTearDown() {
|
|
|
|
n.serverStateLock.Lock()
|
|
|
|
defer n.serverStateLock.Unlock()
|
|
|
|
n.currentClientState = false
|
|
|
|
n.lastNotification = stateDisconnecting
|
2023-04-03 16:59:13 +02:00
|
|
|
n.notify(n.lastNotification)
|
2023-03-29 10:39:54 +02:00
|
|
|
}
|
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
func (n *notifier) isServerStateChanged(newState int) bool {
|
|
|
|
return n.lastNotification != newState
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:59:13 +02:00
|
|
|
func (n *notifier) notify(state int) {
|
2023-03-17 10:37:27 +01:00
|
|
|
n.listenersLock.Lock()
|
|
|
|
defer n.listenersLock.Unlock()
|
2023-04-03 16:59:13 +02:00
|
|
|
if n.listener == nil {
|
|
|
|
return
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2023-04-03 16:59:13 +02:00
|
|
|
n.notifyListener(n.listener, state)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) notifyListener(l Listener, state int) {
|
2023-04-03 16:59:13 +02:00
|
|
|
go func() {
|
|
|
|
switch state {
|
|
|
|
case stateDisconnected:
|
|
|
|
l.OnDisconnected()
|
|
|
|
case stateConnected:
|
|
|
|
l.OnConnected()
|
|
|
|
case stateConnecting:
|
|
|
|
l.OnConnecting()
|
|
|
|
case stateDisconnecting:
|
|
|
|
l.OnDisconnecting()
|
|
|
|
}
|
|
|
|
}()
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
func (n *notifier) calculateState(managementConn, signalConn bool) int {
|
|
|
|
if managementConn && signalConn {
|
2023-03-17 10:37:27 +01:00
|
|
|
return stateConnected
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
if !managementConn && !signalConn {
|
2023-03-17 10:37:27 +01:00
|
|
|
return stateDisconnected
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:22:25 +02:00
|
|
|
if n.lastNotification == stateDisconnecting {
|
|
|
|
return stateDisconnecting
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
return stateConnecting
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) peerListChanged(numOfPeers int) {
|
2023-08-04 14:14:08 +02:00
|
|
|
n.lastNumberOfPeers = numOfPeers
|
2023-03-17 10:37:27 +01:00
|
|
|
n.listenersLock.Lock()
|
|
|
|
defer n.listenersLock.Unlock()
|
2023-04-03 16:59:13 +02:00
|
|
|
if n.listener == nil {
|
|
|
|
return
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2023-04-03 16:59:13 +02:00
|
|
|
n.listener.OnPeersListChanged(numOfPeers)
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2023-03-24 18:51:35 +01:00
|
|
|
|
|
|
|
func (n *notifier) localAddressChanged(fqdn, address string) {
|
|
|
|
n.listenersLock.Lock()
|
|
|
|
defer n.listenersLock.Unlock()
|
2023-04-03 16:59:13 +02:00
|
|
|
if n.listener == nil {
|
|
|
|
return
|
2023-03-24 18:51:35 +01:00
|
|
|
}
|
2023-04-03 16:59:13 +02:00
|
|
|
n.listener.OnAddressChanged(fqdn, address)
|
2023-03-24 18:51:35 +01:00
|
|
|
}
|