-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
109 lines (95 loc) · 2.04 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"sync"
"time"
)
var seqNumIncMutex sync.Mutex // for sequence number increment
var UIDIncMutex sync.Mutex // for packet UID increment
// get the current sequence number and increment it
func getSeqNum() int32 {
seqNumIncMutex.Lock()
tmp := SequenceNumber
SequenceNumber++
seqNumIncMutex.Unlock()
return tmp
}
// get the current packet UID and increment it
func getUID() int {
UIDIncMutex.Lock()
tmp := UID
UID++
UIDIncMutex.Unlock()
return tmp
}
// convert subsys id to name
func subsysID2Name(id uint8) string {
for n, i := range SUBSYS_MAP {
if id == i {
return n
}
}
return ""
}
// convert subsys name to id
func subsysName2ID(name string) int {
return int(SUBSYS_MAP[name])
}
// find node by name in Subsystems and Switches
func findNodeByName(name string) Node {
for _, subsys := range Subsystems {
if subsys.Name() == name {
return subsys
}
}
for _, sw := range Switches {
if sw.Name() == name {
return sw
}
}
return &Switch{name: ""}
}
// Find undirected link by nodes O(E)
func findLinkByNodes(n1, n2 Node) *Link {
for _, l := range Links {
if l.sender1.Owner == n1.Name() && l.sink1.Owner == n2.Name() {
return l
}
if l.sender1.Owner == n2.Name() && l.sink1.Owner == n1.Name() {
return l
}
}
return nil
}
// Collect statistics and send to frontend via websocket
func collectStatistics() {
// save to db
go func() {
for {
var tmp = map[string][2]int{}
for _, s := range Subsystems {
tmp[s.Name()] = [2]int{s.fwdCnt, s.recvCnt}
}
for _, sw := range Switches {
tmp[sw.Name()] = [2]int{sw.fwdCnt, sw.recvCnt}
}
saveStatsIO(tmp)
time.Sleep(SAVE_STATS_PERIOD * time.Second)
}
}()
// send to frontend
for {
var tmp = map[string][2]int{}
for _, s := range Subsystems {
tmp[s.Name()] = [2]int{s.fwdCnt, s.recvCnt}
}
for _, sw := range Switches {
tmp[sw.Name()] = [2]int{sw.fwdCnt, sw.recvCnt}
}
WSLog <- Log{
Type: WSLOG_STAT,
Msg: "",
Statistics: tmp,
}
time.Sleep(UPLOAD_STATS_PERIOD * time.Second)
}
}