2021-08-16 20:58:15 +02:00
|
|
|
package path
|
|
|
|
|
|
|
|
import (
|
2021-10-12 10:05:23 +02:00
|
|
|
"errors"
|
2021-08-16 20:58:15 +02:00
|
|
|
"fmt"
|
2021-08-25 15:21:26 +02:00
|
|
|
"io/ioutil"
|
2021-08-16 20:58:15 +02:00
|
|
|
"math"
|
2021-08-25 15:21:26 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-08-21 16:54:24 +02:00
|
|
|
"sync"
|
2021-08-16 20:58:15 +02:00
|
|
|
"time"
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
"github.com/KusakabeSi/EtherGuard-VPN/mtypes"
|
|
|
|
orderedmap "github.com/KusakabeSi/EtherGuard-VPN/orderdmap"
|
2021-08-16 20:58:15 +02:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2021-08-20 19:32:50 +02:00
|
|
|
const Infinity = float64(99999)
|
|
|
|
|
|
|
|
func (g *IG) GetCurrentTime() time.Time {
|
2021-08-25 13:54:13 +02:00
|
|
|
return time.Now().Add(g.ntp_offset).Round(0)
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 20:58:15 +02:00
|
|
|
type Latency struct {
|
2021-10-27 03:02:44 +02:00
|
|
|
ping float64
|
|
|
|
ping_old float64
|
|
|
|
additionalCost float64
|
2021-12-02 18:13:48 +01:00
|
|
|
validUntil time.Time
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Fullroute struct {
|
2021-12-02 18:13:48 +01:00
|
|
|
Next mtypes.NextHopTable `yaml:"NextHopTable"`
|
|
|
|
Dist mtypes.DistTable `yaml:"DistanceTable"`
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IG is a graph of integers that satisfies the Graph interface.
|
|
|
|
type IG struct {
|
2021-12-02 18:13:48 +01:00
|
|
|
Vert map[mtypes.Vertex]bool
|
|
|
|
edges map[mtypes.Vertex]map[mtypes.Vertex]*Latency
|
2021-08-23 18:39:04 +02:00
|
|
|
edgelock *sync.RWMutex
|
2021-09-30 23:15:23 +02:00
|
|
|
StaticMode bool
|
2021-08-20 19:32:50 +02:00
|
|
|
JitterTolerance float64
|
|
|
|
JitterToleranceMultiplier float64
|
|
|
|
SuperNodeInfoTimeout time.Duration
|
|
|
|
RecalculateCoolDown time.Duration
|
2021-10-12 10:05:23 +02:00
|
|
|
TimeoutCheckInterval time.Duration
|
2021-09-21 22:03:11 +02:00
|
|
|
recalculateTime time.Time
|
2021-12-02 18:13:48 +01:00
|
|
|
dlTable mtypes.DistTable
|
|
|
|
nhTable mtypes.NextHopTable
|
2021-12-04 03:32:59 +01:00
|
|
|
changed bool
|
2021-08-24 20:16:21 +02:00
|
|
|
NhTableExpire time.Time
|
2021-08-20 19:32:50 +02:00
|
|
|
IsSuperMode bool
|
2021-12-02 18:13:48 +01:00
|
|
|
loglevel mtypes.LoggerInfo
|
2021-08-25 13:54:13 +02:00
|
|
|
|
|
|
|
ntp_wg sync.WaitGroup
|
2021-12-02 18:13:48 +01:00
|
|
|
ntp_info mtypes.NTPinfo
|
2021-08-25 13:54:13 +02:00
|
|
|
ntp_offset time.Duration
|
|
|
|
ntp_servers orderedmap.OrderedMap // serverurl:lentancy
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func NewGraph(num_node int, IsSuperMode bool, theconfig mtypes.GraphRecalculateSetting, ntpinfo mtypes.NTPinfo, loglevel mtypes.LoggerInfo) *IG {
|
2021-08-20 19:32:50 +02:00
|
|
|
g := IG{
|
2021-08-23 18:39:04 +02:00
|
|
|
edgelock: &sync.RWMutex{},
|
2021-09-30 23:15:23 +02:00
|
|
|
StaticMode: theconfig.StaticMode,
|
2021-08-20 19:32:50 +02:00
|
|
|
JitterTolerance: theconfig.JitterTolerance,
|
|
|
|
JitterToleranceMultiplier: theconfig.JitterToleranceMultiplier,
|
2021-12-03 23:46:58 +01:00
|
|
|
RecalculateCoolDown: mtypes.S2TD(theconfig.RecalculateCoolDown),
|
|
|
|
TimeoutCheckInterval: mtypes.S2TD(theconfig.TimeoutCheckInterval),
|
2021-08-25 13:54:13 +02:00
|
|
|
ntp_info: ntpinfo,
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
2021-12-02 18:13:48 +01:00
|
|
|
g.Vert = make(map[mtypes.Vertex]bool, num_node)
|
|
|
|
g.edges = make(map[mtypes.Vertex]map[mtypes.Vertex]*Latency, num_node)
|
2021-08-20 19:32:50 +02:00
|
|
|
g.IsSuperMode = IsSuperMode
|
2021-10-12 10:05:23 +02:00
|
|
|
g.loglevel = loglevel
|
2021-08-25 13:54:13 +02:00
|
|
|
g.InitNTP()
|
2021-08-21 16:54:24 +02:00
|
|
|
return &g
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
|
|
|
|
2021-08-24 10:43:55 +02:00
|
|
|
func (g *IG) GetWeightType(x float64) (y float64) {
|
2021-08-20 19:32:50 +02:00
|
|
|
x = math.Abs(x)
|
2021-08-24 10:43:55 +02:00
|
|
|
y = x
|
|
|
|
if g.JitterTolerance > 0.001 && g.JitterToleranceMultiplier > 1 {
|
|
|
|
t := g.JitterTolerance
|
|
|
|
r := g.JitterToleranceMultiplier
|
|
|
|
y = math.Pow(math.Ceil(math.Pow(x/t, 1/r)), r) * t
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) ShouldUpdate(u mtypes.Vertex, v mtypes.Vertex, newval float64) bool {
|
2021-10-27 03:02:44 +02:00
|
|
|
oldval := math.Abs(g.OldWeight(u, v, false) * 1000)
|
2021-08-24 10:43:55 +02:00
|
|
|
newval = math.Abs(newval * 1000)
|
2021-08-20 19:32:50 +02:00
|
|
|
if g.IsSuperMode {
|
2021-08-24 10:43:55 +02:00
|
|
|
if g.JitterTolerance > 0.001 && g.JitterToleranceMultiplier >= 1 {
|
|
|
|
diff := math.Abs(newval - oldval)
|
|
|
|
x := math.Max(oldval, newval)
|
|
|
|
t := g.JitterTolerance
|
|
|
|
r := g.JitterToleranceMultiplier
|
|
|
|
return diff > t+x*(r-1) // https://www.desmos.com/calculator/raoti16r5n
|
|
|
|
}
|
|
|
|
return oldval == newval
|
2021-08-20 19:32:50 +02:00
|
|
|
} else {
|
2021-08-25 15:21:26 +02:00
|
|
|
return g.GetWeightType(oldval) != g.GetWeightType(newval)
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 12:37:09 +02:00
|
|
|
func (g *IG) CheckAnyShouldUpdate() bool {
|
|
|
|
vert := g.Vertices()
|
|
|
|
for u, _ := range vert {
|
|
|
|
for v, _ := range vert {
|
2021-10-27 03:02:44 +02:00
|
|
|
if u != v {
|
|
|
|
newVal := g.Weight(u, v, false)
|
|
|
|
if g.ShouldUpdate(u, v, newVal) {
|
|
|
|
return true
|
|
|
|
}
|
2021-10-14 12:37:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-20 19:32:50 +02:00
|
|
|
func (g *IG) RecalculateNhTable(checkchange bool) (changed bool) {
|
2021-09-30 23:15:23 +02:00
|
|
|
if g.StaticMode {
|
2021-12-04 03:32:59 +01:00
|
|
|
if g.changed {
|
2021-09-30 23:15:23 +02:00
|
|
|
changed = checkchange
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-10-27 03:02:44 +02:00
|
|
|
if !g.CheckAnyShouldUpdate() {
|
2021-10-12 10:05:23 +02:00
|
|
|
return
|
|
|
|
}
|
2021-09-21 22:03:11 +02:00
|
|
|
if g.recalculateTime.Add(g.RecalculateCoolDown).Before(time.Now()) {
|
2021-10-12 10:05:23 +02:00
|
|
|
dist, next, _ := g.FloydWarshall(false)
|
2021-08-21 16:54:24 +02:00
|
|
|
changed = false
|
2021-08-20 19:32:50 +02:00
|
|
|
if checkchange {
|
|
|
|
CheckLoop:
|
|
|
|
for src, dsts := range next {
|
2021-08-21 16:54:24 +02:00
|
|
|
for dst, old_next := range dsts {
|
2021-08-20 19:32:50 +02:00
|
|
|
nexthop := g.Next(src, dst)
|
2021-08-21 16:54:24 +02:00
|
|
|
if old_next != nexthop {
|
|
|
|
changed = true
|
|
|
|
break CheckLoop
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 11:18:54 +02:00
|
|
|
g.dlTable, g.nhTable = dist, next
|
2021-09-21 22:03:11 +02:00
|
|
|
g.recalculateTime = time.Now()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) RemoveVirt(v mtypes.Vertex, recalculate bool, checkchange bool) (changed bool) { //Waiting for test
|
2021-09-21 22:03:11 +02:00
|
|
|
g.edgelock.Lock()
|
2021-10-12 10:05:23 +02:00
|
|
|
delete(g.Vert, v)
|
|
|
|
delete(g.edges, v)
|
|
|
|
for u, _ := range g.edges {
|
|
|
|
delete(g.edges[u], v)
|
2021-09-21 22:03:11 +02:00
|
|
|
}
|
|
|
|
g.edgelock.Unlock()
|
2021-12-04 03:32:59 +01:00
|
|
|
g.changed = true
|
2021-09-21 22:03:11 +02:00
|
|
|
if recalculate {
|
|
|
|
changed = g.RecalculateNhTable(checkchange)
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) UpdateLatency(src mtypes.Vertex, dst mtypes.Vertex, val float64, TimeToAlive float64, SuperAdditionalCost float64, recalculate bool, checkchange bool) (changed bool) {
|
|
|
|
return g.UpdateLatencyMulti([]mtypes.PongMsg{{
|
|
|
|
Src_nodeID: src,
|
|
|
|
Dst_nodeID: dst,
|
|
|
|
Timediff: val,
|
|
|
|
AdditionalCost: SuperAdditionalCost,
|
|
|
|
TimeToAlive: TimeToAlive,
|
|
|
|
}}, recalculate, checkchange)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *IG) UpdateLatencyMulti(pong_info []mtypes.PongMsg, recalculate bool, checkchange bool) (changed bool) {
|
|
|
|
|
2021-08-26 21:06:15 +02:00
|
|
|
g.edgelock.Lock()
|
2021-12-02 18:13:48 +01:00
|
|
|
should_update := false
|
|
|
|
for _, pong_msg := range pong_info {
|
|
|
|
u := pong_msg.Src_nodeID
|
|
|
|
v := pong_msg.Dst_nodeID
|
|
|
|
w := pong_msg.Timediff
|
|
|
|
additionalCost := pong_msg.AdditionalCost
|
|
|
|
if additionalCost < 0 {
|
|
|
|
additionalCost = 0
|
|
|
|
}
|
|
|
|
g.Vert[u] = true
|
|
|
|
g.Vert[v] = true
|
|
|
|
if _, ok := g.edges[u]; !ok {
|
|
|
|
g.recalculateTime = time.Time{}
|
|
|
|
g.edges[u] = make(map[mtypes.Vertex]*Latency)
|
|
|
|
}
|
|
|
|
g.edgelock.Unlock()
|
|
|
|
should_update = should_update || g.ShouldUpdate(u, v, w)
|
|
|
|
g.edgelock.Lock()
|
|
|
|
|
|
|
|
if _, ok := g.edges[u][v]; ok {
|
|
|
|
g.edges[u][v].ping = w
|
|
|
|
g.edges[u][v].validUntil = time.Now().Add(mtypes.S2TD(pong_msg.TimeToAlive))
|
|
|
|
g.edges[u][v].additionalCost = additionalCost / 1000
|
|
|
|
} else {
|
|
|
|
g.edges[u][v] = &Latency{
|
|
|
|
ping: w,
|
|
|
|
ping_old: Infinity,
|
|
|
|
validUntil: time.Now().Add(mtypes.S2TD(pong_msg.TimeToAlive)),
|
|
|
|
additionalCost: additionalCost / 1000,
|
|
|
|
}
|
2021-10-12 10:05:23 +02:00
|
|
|
}
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
2021-08-21 16:54:24 +02:00
|
|
|
g.edgelock.Unlock()
|
2021-08-26 21:06:15 +02:00
|
|
|
if should_update && recalculate {
|
2021-08-25 11:18:54 +02:00
|
|
|
changed = g.RecalculateNhTable(checkchange)
|
|
|
|
}
|
2021-08-20 19:32:50 +02:00
|
|
|
return
|
|
|
|
}
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) Vertices() map[mtypes.Vertex]bool {
|
|
|
|
vr := make(map[mtypes.Vertex]bool)
|
2021-08-23 19:45:09 +02:00
|
|
|
g.edgelock.RLock()
|
|
|
|
defer g.edgelock.RUnlock()
|
2021-08-23 18:39:04 +02:00
|
|
|
for k, v := range g.Vert { //copy a new list
|
|
|
|
vr[k] = v
|
|
|
|
}
|
|
|
|
return vr
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g IG) Neighbors(v mtypes.Vertex) (vs []mtypes.Vertex) {
|
2021-08-23 18:39:04 +02:00
|
|
|
g.edgelock.RLock()
|
|
|
|
defer g.edgelock.RUnlock()
|
|
|
|
for k := range g.edges[v] { //copy a new list
|
2021-08-16 20:58:15 +02:00
|
|
|
vs = append(vs, k)
|
|
|
|
}
|
|
|
|
return vs
|
|
|
|
}
|
2021-08-20 19:32:50 +02:00
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) Next(u, v mtypes.Vertex) *mtypes.Vertex {
|
2021-08-25 11:18:54 +02:00
|
|
|
if _, ok := g.nhTable[u]; !ok {
|
2021-08-20 19:32:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-25 11:18:54 +02:00
|
|
|
if _, ok := g.nhTable[u][v]; !ok {
|
2021-08-20 19:32:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-25 11:18:54 +02:00
|
|
|
return g.nhTable[u][v]
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) Weight(u, v mtypes.Vertex, withAC bool) (ret float64) {
|
2021-08-23 18:39:04 +02:00
|
|
|
g.edgelock.RLock()
|
|
|
|
defer g.edgelock.RUnlock()
|
2021-10-12 10:05:23 +02:00
|
|
|
//defer func() { fmt.Println(u, v, ret) }()
|
|
|
|
if u == v {
|
|
|
|
return 0
|
|
|
|
}
|
2021-08-21 16:54:24 +02:00
|
|
|
if _, ok := g.edges[u]; !ok {
|
2021-08-20 19:32:50 +02:00
|
|
|
return Infinity
|
|
|
|
}
|
2021-08-21 16:54:24 +02:00
|
|
|
if _, ok := g.edges[u][v]; !ok {
|
2021-08-20 19:32:50 +02:00
|
|
|
return Infinity
|
|
|
|
}
|
2021-12-02 18:13:48 +01:00
|
|
|
if time.Now().After(g.edges[u][v].validUntil) {
|
2021-08-20 19:32:50 +02:00
|
|
|
return Infinity
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
2021-10-27 03:53:01 +02:00
|
|
|
ret = g.edges[u][v].ping
|
2021-10-27 03:02:44 +02:00
|
|
|
if withAC {
|
2021-10-27 03:53:01 +02:00
|
|
|
ret += g.edges[u][v].additionalCost
|
2021-10-27 03:02:44 +02:00
|
|
|
}
|
|
|
|
if ret >= Infinity {
|
|
|
|
return Infinity
|
|
|
|
}
|
|
|
|
return
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) OldWeight(u, v mtypes.Vertex, withAC bool) (ret float64) {
|
2021-10-12 10:05:23 +02:00
|
|
|
g.edgelock.RLock()
|
|
|
|
defer g.edgelock.RUnlock()
|
|
|
|
if u == v {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if _, ok := g.edges[u]; !ok {
|
|
|
|
return Infinity
|
|
|
|
}
|
|
|
|
if _, ok := g.edges[u][v]; !ok {
|
|
|
|
return Infinity
|
|
|
|
}
|
2021-10-27 03:53:01 +02:00
|
|
|
ret = g.edges[u][v].ping_old
|
2021-10-27 03:02:44 +02:00
|
|
|
if withAC {
|
2021-10-27 03:53:01 +02:00
|
|
|
ret += g.edges[u][v].additionalCost
|
2021-10-12 10:05:23 +02:00
|
|
|
}
|
2021-10-27 03:02:44 +02:00
|
|
|
if ret >= Infinity {
|
|
|
|
return Infinity
|
|
|
|
}
|
|
|
|
return
|
2021-10-12 10:05:23 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) SetWeight(u, v mtypes.Vertex, weight float64) {
|
2021-10-12 10:05:23 +02:00
|
|
|
g.edgelock.Lock()
|
|
|
|
defer g.edgelock.Unlock()
|
|
|
|
if _, ok := g.edges[u]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := g.edges[u][v]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.edges[u][v].ping = weight
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) SetOldWeight(u, v mtypes.Vertex, weight float64) {
|
2021-10-12 10:05:23 +02:00
|
|
|
g.edgelock.Lock()
|
|
|
|
defer g.edgelock.Unlock()
|
|
|
|
if _, ok := g.edges[u]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := g.edges[u][v]; !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.edges[u][v].ping_old = weight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *IG) RemoveAllNegativeValue() {
|
|
|
|
vert := g.Vertices()
|
|
|
|
for u, _ := range vert {
|
|
|
|
for v, _ := range vert {
|
2021-10-27 03:02:44 +02:00
|
|
|
if g.Weight(u, v, true) < 0 {
|
2021-10-12 10:05:23 +02:00
|
|
|
if g.loglevel.LogInternal {
|
|
|
|
fmt.Printf("Internal: Remove negative value : edge[%v][%v] = 0\n", u, v)
|
|
|
|
}
|
|
|
|
g.SetWeight(u, v, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) FloydWarshall(again bool) (dist mtypes.DistTable, next mtypes.NextHopTable, err error) {
|
2021-10-12 10:05:23 +02:00
|
|
|
if g.loglevel.LogInternal {
|
|
|
|
if !again {
|
|
|
|
fmt.Println("Internal: Start Floyd Warshall algorithm")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Internal: Start Floyd Warshall algorithm again")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 20:58:15 +02:00
|
|
|
vert := g.Vertices()
|
2021-12-02 18:13:48 +01:00
|
|
|
dist = make(mtypes.DistTable)
|
|
|
|
next = make(mtypes.NextHopTable)
|
2021-08-16 20:58:15 +02:00
|
|
|
for u, _ := range vert {
|
2021-12-02 18:13:48 +01:00
|
|
|
dist[u] = make(map[mtypes.Vertex]float64)
|
|
|
|
next[u] = make(map[mtypes.Vertex]*mtypes.Vertex)
|
2021-08-16 20:58:15 +02:00
|
|
|
for v, _ := range vert {
|
|
|
|
dist[u][v] = Infinity
|
|
|
|
}
|
|
|
|
dist[u][u] = 0
|
|
|
|
for _, v := range g.Neighbors(u) {
|
2021-10-27 03:02:44 +02:00
|
|
|
w := g.Weight(u, v, true)
|
|
|
|
wo := g.Weight(u, v, false)
|
2021-08-16 20:58:15 +02:00
|
|
|
if w < Infinity {
|
|
|
|
v := v
|
|
|
|
dist[u][v] = w
|
|
|
|
next[u][v] = &v
|
|
|
|
}
|
2021-10-27 03:02:44 +02:00
|
|
|
g.SetOldWeight(u, v, wo)
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, _ := range vert {
|
|
|
|
for i, _ := range vert {
|
|
|
|
for j, _ := range vert {
|
|
|
|
if dist[i][k] < Infinity && dist[k][j] < Infinity {
|
|
|
|
if dist[i][j] > dist[i][k]+dist[k][j] {
|
|
|
|
dist[i][j] = dist[i][k] + dist[k][j]
|
|
|
|
next[i][j] = next[i][k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-12 10:05:23 +02:00
|
|
|
for i := range dist {
|
|
|
|
if dist[i][i] < 0 {
|
|
|
|
if !again {
|
|
|
|
if g.loglevel.LogInternal {
|
|
|
|
fmt.Println("Internal: Error: Negative cycle detected")
|
|
|
|
}
|
|
|
|
g.RemoveAllNegativeValue()
|
|
|
|
err = errors.New("negative cycle detected")
|
|
|
|
dist, next, _ = g.FloydWarshall(true)
|
|
|
|
return
|
|
|
|
} else {
|
2021-12-02 18:13:48 +01:00
|
|
|
dist = make(mtypes.DistTable)
|
|
|
|
next = make(mtypes.NextHopTable)
|
2021-10-12 10:05:23 +02:00
|
|
|
err = errors.New("negative cycle detected again!")
|
|
|
|
if g.loglevel.LogInternal {
|
|
|
|
fmt.Println("Internal: Error: Negative cycle detected again")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func Path(u, v mtypes.Vertex, next mtypes.NextHopTable) (path []mtypes.Vertex) {
|
2021-08-16 20:58:15 +02:00
|
|
|
if next[u][v] == nil {
|
2021-12-02 18:13:48 +01:00
|
|
|
return []mtypes.Vertex{}
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
2021-12-02 18:13:48 +01:00
|
|
|
path = []mtypes.Vertex{u}
|
2021-08-16 20:58:15 +02:00
|
|
|
for u != v {
|
|
|
|
u = *next[u][v]
|
|
|
|
path = append(path, u)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2021-12-04 03:32:59 +01:00
|
|
|
func (g *IG) SetNHTable(nh mtypes.NextHopTable) { // set nhTable from supernode
|
2021-08-25 11:18:54 +02:00
|
|
|
g.nhTable = nh
|
2021-12-04 03:32:59 +01:00
|
|
|
g.changed = true
|
2021-08-24 20:16:21 +02:00
|
|
|
g.NhTableExpire = time.Now().Add(g.SuperNodeInfoTimeout)
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) GetNHTable(recalculate bool) mtypes.NextHopTable {
|
2021-10-12 10:05:23 +02:00
|
|
|
if recalculate && time.Now().After(g.NhTableExpire) {
|
2021-09-21 22:03:11 +02:00
|
|
|
g.RecalculateNhTable(false)
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
2021-08-25 11:18:54 +02:00
|
|
|
return g.nhTable
|
2021-08-20 19:32:50 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) GetDtst() mtypes.DistTable {
|
2021-08-24 20:16:21 +02:00
|
|
|
return g.dlTable
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) GetEdges(isOld bool, withAC bool) (edges map[mtypes.Vertex]map[mtypes.Vertex]float64) {
|
2021-08-24 20:16:21 +02:00
|
|
|
vert := g.Vertices()
|
2021-12-02 18:13:48 +01:00
|
|
|
edges = make(map[mtypes.Vertex]map[mtypes.Vertex]float64, len(vert))
|
2021-08-24 20:16:21 +02:00
|
|
|
for src, _ := range vert {
|
2021-12-02 18:13:48 +01:00
|
|
|
edges[src] = make(map[mtypes.Vertex]float64, len(vert))
|
2021-08-24 20:16:21 +02:00
|
|
|
for dst, _ := range vert {
|
2021-08-25 10:13:53 +02:00
|
|
|
if src != dst {
|
2021-10-12 10:05:23 +02:00
|
|
|
if isOld {
|
2021-10-27 03:02:44 +02:00
|
|
|
edges[src][dst] = g.OldWeight(src, dst, withAC)
|
2021-10-12 10:05:23 +02:00
|
|
|
} else {
|
2021-10-27 03:02:44 +02:00
|
|
|
edges[src][dst] = g.Weight(src, dst, withAC)
|
2021-10-12 10:05:23 +02:00
|
|
|
}
|
2021-08-25 10:13:53 +02:00
|
|
|
}
|
2021-08-24 20:16:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) GetBoardcastList(id mtypes.Vertex) (tosend map[mtypes.Vertex]bool) {
|
|
|
|
tosend = make(map[mtypes.Vertex]bool)
|
2021-08-25 11:18:54 +02:00
|
|
|
for _, element := range g.nhTable[id] {
|
2021-08-16 20:58:15 +02:00
|
|
|
tosend[*element] = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func (g *IG) GetBoardcastThroughList(self_id mtypes.Vertex, in_id mtypes.Vertex, src_id mtypes.Vertex) (tosend map[mtypes.Vertex]bool) {
|
|
|
|
tosend = make(map[mtypes.Vertex]bool)
|
2021-08-20 19:32:50 +02:00
|
|
|
for check_id, _ := range g.GetBoardcastList(self_id) {
|
2021-08-25 11:18:54 +02:00
|
|
|
for _, path_node := range Path(src_id, check_id, g.nhTable) {
|
2021-08-20 19:32:50 +02:00
|
|
|
if path_node == self_id && check_id != in_id {
|
2021-08-16 20:58:15 +02:00
|
|
|
tosend[check_id] = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:21:26 +02:00
|
|
|
func printExample() {
|
|
|
|
fmt.Println(`X 1 2 3 4 5 6
|
|
|
|
1 0 0.5 Inf Inf Inf Inf
|
|
|
|
2 0.5 0 0.5 0.5 Inf Inf
|
|
|
|
3 Inf 0.5 0 0.5 0.5 Inf
|
|
|
|
4 Inf 0.5 0.5 0 Inf 0.5
|
|
|
|
5 Inf Inf 0.5 Inf 0 Inf
|
|
|
|
6 Inf Inf Inf 0.5 Inf 0`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func a2n(s string) (ret float64) {
|
|
|
|
if s == "Inf" {
|
|
|
|
return Infinity
|
|
|
|
}
|
|
|
|
ret, err := strconv.ParseFloat(s, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:13:48 +01:00
|
|
|
func a2v(s string) mtypes.Vertex {
|
2021-09-21 22:03:11 +02:00
|
|
|
ret, err := strconv.ParseUint(s, 10, 16)
|
2021-08-25 15:21:26 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-02 18:13:48 +01:00
|
|
|
return mtypes.Vertex(ret)
|
2021-08-25 15:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Solve(filePath string, pe bool) error {
|
|
|
|
if pe {
|
|
|
|
printExample()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-04 03:32:59 +01:00
|
|
|
g := NewGraph(3, false, mtypes.GraphRecalculateSetting{ }, mtypes.NTPinfo{}, mtypes.LoggerInfo{LogInternal: true})
|
2021-08-25 15:21:26 +02:00
|
|
|
inputb, err := ioutil.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
input := string(inputb)
|
|
|
|
lines := strings.Split(input, "\n")
|
|
|
|
verts := strings.Fields(lines[0])
|
|
|
|
for _, line := range lines[1:] {
|
|
|
|
element := strings.Fields(line)
|
|
|
|
src := a2v(element[0])
|
|
|
|
for index, sval := range element[1:] {
|
|
|
|
val := a2n(sval)
|
|
|
|
dst := a2v(verts[index+1])
|
|
|
|
if src != dst && val != Infinity {
|
2021-12-02 18:13:48 +01:00
|
|
|
g.UpdateLatency(src, dst, val, 99999, 0, false, false)
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-12 10:05:23 +02:00
|
|
|
dist, next, err := g.FloydWarshall(false)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error:", err)
|
|
|
|
}
|
2021-08-25 15:21:26 +02:00
|
|
|
|
2021-08-16 20:58:15 +02:00
|
|
|
rr, _ := yaml.Marshal(Fullroute{
|
|
|
|
Dist: dist,
|
|
|
|
Next: next,
|
|
|
|
})
|
|
|
|
fmt.Print(string(rr))
|
2021-08-25 15:21:26 +02:00
|
|
|
|
|
|
|
fmt.Println("\nHuman readable:")
|
|
|
|
fmt.Println("src\tdist\t\tpath")
|
|
|
|
for _, U := range verts[1:] {
|
|
|
|
u := a2v(U)
|
|
|
|
for _, V := range verts[1:] {
|
|
|
|
v := a2v(V)
|
|
|
|
if u != v {
|
|
|
|
fmt.Printf("%d -> %d\t%3f\t%s\n", u, v, dist[u][v], fmt.Sprint(Path(u, v, next)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-08-16 20:58:15 +02:00
|
|
|
}
|