2017-04-10 18:54:12 +02:00
|
|
|
package meshviewer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
2017-04-16 15:12:23 +02:00
|
|
|
type nodeBuilder func(*runtime.Nodes) interface{}
|
2017-04-10 18:54:12 +02:00
|
|
|
|
2017-04-16 15:12:23 +02:00
|
|
|
var nodeFormats = map[int]nodeBuilder{
|
|
|
|
1: BuildNodesV1,
|
|
|
|
2: BuildNodesV2,
|
2017-04-10 18:54:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start all services to manage Nodes
|
|
|
|
func Start(config *runtime.Config, nodes *runtime.Nodes) {
|
|
|
|
go worker(config, nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Periodically saves the cached DB to json file
|
|
|
|
func worker(config *runtime.Config, nodes *runtime.Nodes) {
|
|
|
|
c := time.Tick(config.Nodes.SaveInterval.Duration)
|
|
|
|
|
|
|
|
for range c {
|
|
|
|
saveMeshviewer(config, nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveMeshviewer(config *runtime.Config, nodes *runtime.Nodes) {
|
|
|
|
// Locking foo
|
|
|
|
nodes.RLock()
|
|
|
|
defer nodes.RUnlock()
|
|
|
|
if path := config.Meshviewer.NodesPath; path != "" {
|
|
|
|
version := config.Meshviewer.Version
|
2017-04-16 15:12:23 +02:00
|
|
|
builder := nodeFormats[version]
|
|
|
|
|
|
|
|
if builder != nil {
|
|
|
|
runtime.SaveJSON(builder(nodes), path)
|
|
|
|
} else {
|
2017-04-10 18:54:12 +02:00
|
|
|
log.Panicf("invalid nodes version: %d", version)
|
|
|
|
}
|
2017-04-16 15:12:23 +02:00
|
|
|
|
2017-04-10 18:54:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if path := config.Meshviewer.GraphPath; path != "" {
|
|
|
|
runtime.SaveJSON(BuildGraph(nodes), path)
|
|
|
|
}
|
|
|
|
}
|