freifunkmanager/runtime/nodes.go

99 lines
2.3 KiB
Go
Raw Normal View History

package runtime
import (
2017-05-12 21:32:10 +02:00
"sync"
2018-06-30 01:45:51 +02:00
log "github.com/sirupsen/logrus"
runtimeYanic "github.com/FreifunkBremen/yanic/runtime"
2018-06-30 01:45:51 +02:00
"github.com/genofire/golang-lib/file"
"github.com/FreifunkBremen/freifunkmanager/ssh"
)
type Nodes struct {
List map[string]*Node `json:"nodes"`
Current map[string]*Node `json:"-"`
Statistics *runtimeYanic.GlobalStats `json:"-"`
ssh *ssh.Manager
statePath string
iface string
notifyNodeFunc []func(*Node, bool)
notifyStatsFunc []func(*runtimeYanic.GlobalStats)
2017-05-12 21:32:10 +02:00
sync.Mutex
}
2017-05-07 03:37:30 +02:00
func NewNodes(path string, iface string, mgmt *ssh.Manager) *Nodes {
nodes := &Nodes{
List: make(map[string]*Node),
Current: make(map[string]*Node),
2017-05-07 03:37:30 +02:00
ssh: mgmt,
statePath: path,
iface: iface,
}
2018-06-30 01:45:51 +02:00
file.ReadJSON(path, nodes)
2017-05-07 03:37:30 +02:00
return nodes
}
func (nodes *Nodes) AddNotifyNode(f func(*Node, bool)) {
nodes.notifyNodeFunc = append(nodes.notifyNodeFunc, f)
}
func (nodes *Nodes) notifyNode(node *Node, system bool) {
for _, f := range nodes.notifyNodeFunc {
f(node, system)
}
2017-05-08 19:13:29 +02:00
}
func (nodes *Nodes) AddNotifyStats(f func(stats *runtimeYanic.GlobalStats)) {
nodes.notifyStatsFunc = append(nodes.notifyStatsFunc, f)
2017-05-08 19:13:29 +02:00
}
func (nodes *Nodes) notifyStats(stats *runtimeYanic.GlobalStats) {
nodes.Statistics = stats
for _, f := range nodes.notifyStatsFunc {
f(stats)
2017-05-08 19:13:29 +02:00
}
2017-05-07 03:37:30 +02:00
}
func (nodes *Nodes) UpdateNode(node *Node) bool {
2017-05-08 19:13:29 +02:00
if node == nil {
2018-06-30 01:45:51 +02:00
log.Warn("no new node to update")
return false
}
if GetChannel(node.Wireless.Channel24) == nil {
log.Warnf("wrong wifi24 channel for '%s'", node.NodeID)
return false
}
if GetChannel(node.Wireless.Channel5) == nil {
log.Warnf("wrong wifi5 channel for '%s'", node.NodeID)
return false
2017-05-08 19:13:29 +02:00
}
2017-05-12 21:32:10 +02:00
nodes.Lock()
defer nodes.Unlock()
2017-05-07 03:37:30 +02:00
if n, ok := nodes.List[node.NodeID]; ok {
2017-07-06 09:17:19 +02:00
node.Address = n.Address
2017-05-07 03:37:30 +02:00
go node.SSHUpdate(nodes.ssh, nodes.iface, n)
2018-06-30 01:45:51 +02:00
log.Info("update node", node.NodeID)
2017-05-07 03:37:30 +02:00
}
nodes.List[node.NodeID] = node
nodes.notifyNode(node, true)
return true
2017-05-07 03:37:30 +02:00
}
func (nodes *Nodes) Updater() {
2017-05-12 21:32:10 +02:00
nodes.Lock()
defer nodes.Unlock()
for nodeid, node := range nodes.List {
if n, ok := nodes.Current[nodeid]; ok {
2017-07-06 09:17:19 +02:00
go node.SSHUpdate(nodes.ssh, nodes.iface, n)
2017-05-07 03:37:30 +02:00
}
}
2018-06-30 01:45:51 +02:00
log.Info("updater per ssh")
2017-05-07 03:37:30 +02:00
}
func (nodes *Nodes) Saver() {
2017-05-12 21:32:10 +02:00
nodes.Lock()
2018-06-30 01:45:51 +02:00
file.SaveJSON(nodes.statePath, nodes)
2017-05-12 21:32:10 +02:00
nodes.Unlock()
2018-06-30 01:45:51 +02:00
log.Debug("saved state file")
}