yanic/models/graph.go

156 lines
3.5 KiB
Go
Raw Normal View History

2016-03-07 01:29:31 +00:00
package models
import (
"fmt"
"strings"
)
type Graph struct {
Version int `json:"version"`
Batadv struct {
2016-05-16 09:41:45 +00:00
Directed bool `json:"directed"`
Graph []string `json:"graph"`
Nodes []*GraphNode `json:"nodes"`
2016-03-07 01:29:31 +00:00
Links []*GraphLink `json:"links"`
} `json:"batadv"`
}
2016-05-16 09:41:45 +00:00
type GraphNode struct {
ID string `json:"id"`
NodeID string `json:"node_id"`
}
2016-03-07 01:29:31 +00:00
type GraphLink struct {
2016-05-16 09:41:45 +00:00
Source interface{} `json:"source"`
Target interface{} `json:"target"`
2016-03-07 01:29:31 +00:00
VPN bool `json:"vpn"`
TQ float32 `json:"tq"`
Bidirect bool `json:"bidirect"`
}
type GraphBuilder struct {
2016-03-07 11:05:53 +00:00
macToID map[string]string // mapping from MAC address to node id
links map[string]*GraphLink // mapping from $idA-$idB to existing link
vpn map[string]interface{} // IDs/addresses of VPN servers
2016-03-07 01:29:31 +00:00
}
2016-03-20 17:30:44 +00:00
func (nodes *Nodes) BuildGraph() *Graph {
2016-03-07 01:29:31 +00:00
builder := &GraphBuilder{
macToID: make(map[string]string),
links: make(map[string]*GraphLink),
2016-03-07 11:05:53 +00:00
vpn: make(map[string]interface{}),
}
2016-03-07 01:29:31 +00:00
builder.readNodes(nodes.List)
2016-05-16 09:41:45 +00:00
graph := &Graph{Version: 1}
graph.Batadv.Directed = false
graph.Batadv.Nodes, graph.Batadv.Links = builder.Extract()
2016-03-07 01:29:31 +00:00
return graph
}
func (builder *GraphBuilder) readNodes(nodes map[string]*Node) {
// Fill mac->id map
for sourceId, node := range nodes {
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
// is VPN address?
if nodeinfo.VPN {
builder.vpn[sourceId] = nil
}
2016-04-29 06:25:45 +00:00
for _,batinterface := range nodeinfo.Network.Mesh {
interfaces := batinterface.Interfaces
addresses := append(append(interfaces.Other, interfaces.Tunnel...), interfaces.Wireless...)
2016-04-29 06:25:45 +00:00
for _, sourceAddress := range addresses {
builder.macToID[sourceAddress] = sourceId
2016-03-07 11:05:53 +00:00
}
2016-03-07 01:29:31 +00:00
}
}
}
// Add links
for sourceId, node := range nodes {
if neighbours := node.Neighbours; neighbours != nil {
for _, batadvNeighbours := range neighbours.Batadv {
for targetAddress, link := range batadvNeighbours.Neighbours {
if targetId, found := builder.macToID[targetAddress]; found {
builder.addLink(targetId, sourceId, link.Tq)
}
}
}
}
}
}
2016-05-16 09:41:45 +00:00
func (builder *GraphBuilder) Extract() ([]*GraphNode,[]*GraphLink) {
iNodes := 0
iLinks := 0
2016-03-07 01:29:31 +00:00
links := make([]*GraphLink, len(builder.links))
2016-05-16 09:41:45 +00:00
nodes := make([]*GraphNode, len(builder.macToID))
2016-03-07 01:29:31 +00:00
2016-05-16 09:41:45 +00:00
for mac, nodeID := range builder.macToID {
nodes[iNodes] = &GraphNode{
ID: mac,
NodeID: nodeID,
}
iNodes += 1
}
for key, link := range builder.links {
linkPart :=strings.Split(key,"-")
both := 0
for i,node := range nodes{
if(linkPart[0] == node.NodeID){
link.Source = i
both += 1
continue
}
if(linkPart[1]==node.NodeID){
link.Target = i
both += 1
break
}
}
if both == 2 {
links[iLinks] = link
iLinks += 1
}
2016-03-07 01:29:31 +00:00
}
2016-05-16 09:41:45 +00:00
return nodes, links
2016-03-07 01:29:31 +00:00
}
2016-03-07 11:05:53 +00:00
func (builder *GraphBuilder) isVPN(ids ...string) bool {
for _, id := range ids {
if _, found := builder.vpn[id]; found {
return true
}
}
return false
}
2016-03-07 01:29:31 +00:00
func (builder *GraphBuilder) addLink(targetId string, sourceId string, linkTq int) {
2016-03-20 11:09:32 +00:00
// Sort IDs to generate the key
2016-03-07 01:29:31 +00:00
var key string
if strings.Compare(sourceId, targetId) > 0 {
key = fmt.Sprintf("%s-%s", sourceId, targetId)
} else {
key = fmt.Sprintf("%s-%s", targetId, sourceId)
}
var tq float32
if linkTq > 0 {
tq = float32(1.0 / (float32(linkTq) / 255.0))
}
if link, ok := builder.links[key]; !ok {
builder.links[key] = &GraphLink{
2016-03-07 11:05:53 +00:00
VPN: builder.isVPN(sourceId, targetId),
2016-03-07 01:29:31 +00:00
TQ: tq,
}
} else {
// Use lowest of both link qualities
if tq < link.TQ {
link.TQ = tq
}
link.Bidirect = true
}
}