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
|
|
|
currentServerState bool
|
|
|
|
currentClientState bool
|
|
|
|
lastNotification int
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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()
|
|
|
|
|
|
|
|
var newState bool
|
|
|
|
if mgmState && signalState {
|
|
|
|
newState = true
|
|
|
|
} else {
|
|
|
|
newState = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !n.isServerStateChanged(newState) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.currentServerState = newState
|
|
|
|
|
2023-03-29 10:39:54 +02:00
|
|
|
if n.lastNotification == stateDisconnecting {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.lastNotification = n.calculateState(newState, n.currentClientState)
|
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
|
|
|
|
n.lastNotification = n.calculateState(n.currentServerState, true)
|
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
|
|
|
|
n.lastNotification = n.calculateState(n.currentServerState, false)
|
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-03-17 10:37:27 +01:00
|
|
|
func (n *notifier) isServerStateChanged(newState bool) bool {
|
|
|
|
return n.currentServerState != newState
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) calculateState(serverState bool, clientState bool) int {
|
|
|
|
if serverState && clientState {
|
|
|
|
return stateConnected
|
|
|
|
}
|
|
|
|
|
|
|
|
if !clientState {
|
|
|
|
return stateDisconnected
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateConnecting
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) peerListChanged(numOfPeers int) {
|
|
|
|
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
|
|
|
}
|