2017-05-20 12:46:29 +00:00
|
|
|
package meshviewerFFRGB
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-03 14:30:28 +00:00
|
|
|
"log"
|
2017-05-20 12:46:29 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-01-07 20:00:56 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
2017-05-20 12:46:29 +00:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
2017-12-05 22:17:49 +00:00
|
|
|
const (
|
|
|
|
LINK_TYPE_WIRELESS = "wifi"
|
|
|
|
LINK_TYPE_TUNNEL = "vpn"
|
|
|
|
LINK_TYPE_FALLBACK = "other"
|
|
|
|
)
|
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
func transform(nodes *runtime.Nodes) *Meshviewer {
|
|
|
|
|
|
|
|
meshviewer := &Meshviewer{
|
|
|
|
Timestamp: jsontime.Now(),
|
|
|
|
Nodes: make([]*Node, 0),
|
|
|
|
Links: make([]*Link, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
links := make(map[string]*Link)
|
2018-01-03 14:30:28 +00:00
|
|
|
typeList := make(map[string]string)
|
2017-05-20 12:46:29 +00:00
|
|
|
|
|
|
|
nodes.RLock()
|
|
|
|
defer nodes.RUnlock()
|
|
|
|
|
|
|
|
for _, nodeOrigin := range nodes.List {
|
|
|
|
node := NewNode(nodes, nodeOrigin)
|
|
|
|
meshviewer.Nodes = append(meshviewer.Nodes, node)
|
|
|
|
|
2017-11-01 19:29:00 +00:00
|
|
|
if !nodeOrigin.Online {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
if nodeinfo := nodeOrigin.Nodeinfo; nodeinfo != nil {
|
|
|
|
if meshes := nodeinfo.Network.Mesh; meshes != nil {
|
|
|
|
for _, mesh := range meshes {
|
2017-12-05 22:17:49 +00:00
|
|
|
for _, addr := range mesh.Interfaces.Wireless {
|
|
|
|
typeList[addr] = LINK_TYPE_WIRELESS
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
for _, addr := range mesh.Interfaces.Tunnel {
|
|
|
|
typeList[addr] = LINK_TYPE_TUNNEL
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, linkOrigin := range nodes.NodeLinks(nodeOrigin) {
|
|
|
|
var key string
|
|
|
|
// keep source and target in the same order
|
2017-12-05 22:17:49 +00:00
|
|
|
switchSourceTarget := strings.Compare(linkOrigin.SourceAddress, linkOrigin.TargetAddress) > 0
|
2017-05-20 12:46:29 +00:00
|
|
|
if switchSourceTarget {
|
2017-12-05 22:17:49 +00:00
|
|
|
key = fmt.Sprintf("%s-%s", linkOrigin.SourceAddress, linkOrigin.TargetAddress)
|
2017-05-20 12:46:29 +00:00
|
|
|
} else {
|
2017-12-05 22:17:49 +00:00
|
|
|
key = fmt.Sprintf("%s-%s", linkOrigin.TargetAddress, linkOrigin.SourceAddress)
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
if link := links[key]; link != nil {
|
2017-12-05 22:17:49 +00:00
|
|
|
linkType, linkTypeFound := typeList[linkOrigin.SourceAddress]
|
|
|
|
if !linkTypeFound {
|
|
|
|
linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
|
|
|
|
}
|
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
if switchSourceTarget {
|
2017-12-05 22:17:49 +00:00
|
|
|
link.TargetTQ = linkOrigin.TQ
|
|
|
|
|
|
|
|
linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
|
|
|
|
if !linkTypeFound {
|
|
|
|
linkType, linkTypeFound = typeList[linkOrigin.SourceAddress]
|
2018-01-03 14:30:28 +00:00
|
|
|
}
|
2017-05-20 12:46:29 +00:00
|
|
|
} else {
|
2017-12-05 22:17:49 +00:00
|
|
|
link.SourceTQ = linkOrigin.TQ
|
2018-01-03 14:30:28 +00:00
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
|
|
|
|
if linkTypeFound && linkType != link.Type {
|
|
|
|
if link.Type == LINK_TYPE_FALLBACK {
|
|
|
|
link.Type = linkType
|
|
|
|
} else {
|
|
|
|
log.Printf("different linktypes for '%s' - '%s' prev: '%s' new: '%s' source: '%s' target: '%s'", linkOrigin.SourceAddress, linkOrigin.TargetAddress, link.Type, linkType, typeList[linkOrigin.SourceAddress], typeList[linkOrigin.TargetAddress])
|
|
|
|
}
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
link := &Link{
|
2017-12-05 22:17:49 +00:00
|
|
|
Source: linkOrigin.SourceID,
|
|
|
|
SourceAddress: linkOrigin.SourceAddress,
|
|
|
|
Target: linkOrigin.TargetID,
|
|
|
|
TargetAddress: linkOrigin.TargetAddress,
|
|
|
|
SourceTQ: linkOrigin.TQ,
|
|
|
|
TargetTQ: linkOrigin.TQ,
|
|
|
|
}
|
|
|
|
|
|
|
|
linkType, linkTypeFound := typeList[linkOrigin.SourceAddress]
|
|
|
|
if !linkTypeFound {
|
|
|
|
linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
|
2017-05-20 12:46:29 +00:00
|
|
|
if switchSourceTarget {
|
|
|
|
link.Source = linkOrigin.TargetID
|
2017-12-05 22:17:49 +00:00
|
|
|
link.SourceAddress = linkOrigin.TargetAddress
|
2017-05-20 12:46:29 +00:00
|
|
|
link.Target = linkOrigin.SourceID
|
2017-12-05 22:17:49 +00:00
|
|
|
link.TargetAddress = linkOrigin.SourceAddress
|
|
|
|
|
|
|
|
linkType, linkTypeFound = typeList[linkOrigin.TargetAddress]
|
|
|
|
if !linkTypeFound {
|
|
|
|
linkType, linkTypeFound = typeList[linkOrigin.SourceAddress]
|
|
|
|
}
|
2017-05-20 12:46:29 +00:00
|
|
|
}
|
2017-12-05 22:17:49 +00:00
|
|
|
|
|
|
|
if linkTypeFound {
|
|
|
|
link.Type = linkType
|
|
|
|
} else {
|
|
|
|
link.Type = LINK_TYPE_FALLBACK
|
2018-01-03 14:30:28 +00:00
|
|
|
}
|
2017-05-20 12:46:29 +00:00
|
|
|
links[key] = link
|
|
|
|
meshviewer.Links = append(meshviewer.Links, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return meshviewer
|
|
|
|
}
|