mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-07-02 02:42:30 +00:00

* feat: add QueryActiveTrustAnchorCount --------- Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package monitor
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"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 mqttMonitorInstance MQTTMonitorClientI
|
|
|
|
func SetMqttMonitorInstance(monitorInstance MQTTMonitorClientI) {
|
|
monitorMutex.Lock()
|
|
mqttMonitorInstance = monitorInstance
|
|
monitorMutex.Unlock()
|
|
}
|
|
|
|
func LazyMqttMonitorLoader(homeDir string) {
|
|
monitorMutex.RLock()
|
|
tmpInstance := mqttMonitorInstance
|
|
monitorMutex.RUnlock()
|
|
if tmpInstance != nil {
|
|
return
|
|
}
|
|
if homeDir == "" {
|
|
homeDir = "./"
|
|
}
|
|
aciveActorsDB, err := leveldb.OpenFile(homeDir+"activeActors.db", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
SetMqttMonitorInstance(NewMqttMonitorService(aciveActorsDB, *config.GetConfig()))
|
|
err = mqttMonitorInstance.Start()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func SelectPoPParticipantsOutOfActiveActors() (challenger string, challengee string, err error) {
|
|
monitorMutex.RLock()
|
|
challenger, challengee, err = mqttMonitorInstance.SelectPoPParticipantsOutOfActiveActors()
|
|
monitorMutex.RUnlock()
|
|
return
|
|
}
|
|
|
|
func Start() (err error) {
|
|
err = mqttMonitorInstance.Start()
|
|
return
|
|
}
|
|
|
|
func AddParticipant(address string, lastSeenTS int64) (err error) {
|
|
monitorMutex.RLock()
|
|
err = mqttMonitorInstance.AddParticipant(address, lastSeenTS)
|
|
monitorMutex.RUnlock()
|
|
return
|
|
}
|
|
|
|
func GetActiveActorCount() (count uint64) {
|
|
monitorMutex.RLock()
|
|
defer monitorMutex.RUnlock()
|
|
return mqttMonitorInstance.GetActiveActorCount()
|
|
}
|