mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00

also directly return values instead of assigning them first. Signed-off-by: Julian Strobl <jmastr@mailbox.org>
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package monitor
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/cometbft/cometbft/libs/log"
|
|
"github.com/planetmint/planetmint-go/config"
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
)
|
|
|
|
type MQTTMonitorClientI interface {
|
|
AddParticipant(address string, lastSeenTS int64) (err error)
|
|
SelectPoPParticipantsOutOfActiveActors() (challenger string, challengee string, err error)
|
|
GetActiveActorCount() (count uint64)
|
|
Start() (err error)
|
|
}
|
|
|
|
var monitorMutex sync.RWMutex
|
|
var mqttLogger log.Logger
|
|
var mqttMonitorInstance MQTTMonitorClientI
|
|
|
|
func SetMqttMonitorInstance(monitorInstance MQTTMonitorClientI) {
|
|
monitorMutex.Lock()
|
|
defer monitorMutex.Unlock()
|
|
mqttMonitorInstance = monitorInstance
|
|
}
|
|
|
|
func GetMqttMonitorInstance() (monitorInstance MQTTMonitorClientI) {
|
|
monitorMutex.Lock()
|
|
defer monitorMutex.Unlock()
|
|
return mqttMonitorInstance
|
|
}
|
|
|
|
func LazyMqttMonitorLoader(logger log.Logger, homeDir string) {
|
|
monitorMutex.RLock()
|
|
tmpInstance := mqttMonitorInstance
|
|
monitorMutex.RUnlock()
|
|
if tmpInstance != nil {
|
|
return
|
|
}
|
|
if logger != nil {
|
|
mqttLogger = logger
|
|
}
|
|
if homeDir == "" {
|
|
homeDir = "./"
|
|
}
|
|
aciveActorsDB, err := leveldb.OpenFile(homeDir+"activeActors.db", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
SetMqttMonitorInstance(NewMqttMonitorService(aciveActorsDB, *config.GetConfig()))
|
|
}
|
|
|
|
func SelectPoPParticipantsOutOfActiveActors() (challenger string, challengee string, err error) {
|
|
monitorMutex.RLock()
|
|
defer monitorMutex.RUnlock()
|
|
return mqttMonitorInstance.SelectPoPParticipantsOutOfActiveActors()
|
|
}
|
|
|
|
func Start() (err error) {
|
|
err = mqttMonitorInstance.Start()
|
|
return
|
|
}
|
|
|
|
func AddParticipant(address string, lastSeenTS int64) (err error) {
|
|
monitorMutex.RLock()
|
|
defer monitorMutex.RUnlock()
|
|
return mqttMonitorInstance.AddParticipant(address, lastSeenTS)
|
|
}
|
|
|
|
func GetActiveActorCount() (count uint64) {
|
|
monitorMutex.RLock()
|
|
defer monitorMutex.RUnlock()
|
|
return mqttMonitorInstance.GetActiveActorCount()
|
|
}
|
|
|
|
func Log(msg string) {
|
|
if mqttLogger == nil {
|
|
return
|
|
}
|
|
mqttLogger.Info("[app] [monitor] " + msg)
|
|
}
|