forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionManagement.go
296 lines (263 loc) · 6.98 KB
/
connectionManagement.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package controlPanel
import (
"crypto/sha256"
"fmt"
"sort"
"sync"
"time"
"github.com/FactomProject/factomd/p2p"
)
var AllConnections *ConnectionsMap
type AllConnectionsTotals struct {
PeerQualityAvg int32
BytesSentTotal uint32
BytesReceivedTotal uint32
MessagesSent uint32
MessagesReceived uint32
}
func NewAllConnectionTotals() *AllConnectionsTotals {
a := new(AllConnectionsTotals)
a.PeerQualityAvg = 0
a.BytesSentTotal = 0
a.BytesReceivedTotal = 0
a.MessagesReceived = 0
a.MessagesSent = 0
return a
}
type ConnectionsMap struct {
connected map[string]p2p.ConnectionMetrics
disconnected map[string]p2p.ConnectionMetrics
Totals AllConnectionsTotals
Lock sync.Mutex
}
func NewConnectionsMap() *ConnectionsMap {
newCM := new(ConnectionsMap)
newCM.Lock.Lock()
defer newCM.Lock.Unlock()
newCM.connected = map[string]p2p.ConnectionMetrics{}
newCM.disconnected = map[string]p2p.ConnectionMetrics{}
newCM.Totals = *(NewAllConnectionTotals())
return newCM
}
func (cm *ConnectionsMap) TallyTotals() {
cons := cm.GetConnectedCopy()
dis := cm.GetDisconnectedCopy()
totals := NewAllConnectionTotals()
var count int32 = 0
var totalQuality int32 = 0
for key := range cons {
peer := cons[key]
totals.BytesSentTotal += peer.BytesSent
totals.BytesReceivedTotal += peer.BytesReceived
totals.MessagesSent += peer.MessagesSent
totals.MessagesReceived += peer.MessagesReceived
count++
totalQuality += peer.PeerQuality
}
if count == 0 {
totals.PeerQualityAvg = 0
} else {
totals.PeerQualityAvg = totalQuality / count
}
for key := range dis {
peer := dis[key]
totals.BytesSentTotal += peer.BytesSent
totals.BytesReceivedTotal += peer.BytesReceived
totals.MessagesSent += peer.MessagesSent
totals.MessagesReceived += peer.MessagesReceived
}
cm.Lock.Lock()
cm.Totals = *totals
cm.Lock.Unlock()
}
func (cm *ConnectionsMap) UpdateConnections(connections map[string]p2p.ConnectionMetrics) {
cm.Lock.Lock()
defer cm.Lock.Unlock()
cm.connected = connections
}
func hashPeerAddress(addr string) string {
hash := sha256.Sum256([]byte(addr))
hashStr := fmt.Sprintf("%x", hash)
return hashStr
}
func (cm *ConnectionsMap) AddConnection(key string, val p2p.ConnectionMetrics) {
cm.Lock.Lock()
defer cm.Lock.Unlock()
if _, ok := cm.disconnected[key]; ok {
delete(cm.disconnected, key)
}
cm.connected[key] = val
}
func (cm *ConnectionsMap) RemoveConnection(key string) {
cm.Lock.Lock()
defer cm.Lock.Unlock()
delete(cm.disconnected, key)
delete(cm.connected, key)
}
func (cm *ConnectionsMap) Connect(key string, val *p2p.ConnectionMetrics) bool {
cm.Lock.Lock()
defer cm.Lock.Unlock()
disVal, ok := cm.disconnected[key]
if ok {
delete(cm.disconnected, key)
}
if val == nil && ok {
cm.connected[key] = disVal
} else if val != nil {
cm.connected[key] = *val
}
return true
}
func (cm *ConnectionsMap) GetConnection(key string) *p2p.ConnectionMetrics {
cm.Lock.Lock()
defer cm.Lock.Unlock()
var ok bool
var ret p2p.ConnectionMetrics
ret, ok = cm.connected[key]
if !ok {
ret, ok = cm.disconnected[key]
if !ok {
return nil
}
}
return &ret
}
func (cm *ConnectionsMap) GetConnectedCopy() map[string]p2p.ConnectionMetrics {
cm.Lock.Lock()
defer cm.Lock.Unlock()
newMap := map[string]p2p.ConnectionMetrics{}
for k, v := range cm.connected {
newMap[k] = v
}
return newMap
}
func (cm *ConnectionsMap) GetDisconnectedCopy() map[string]p2p.ConnectionMetrics {
cm.Lock.Lock()
defer cm.Lock.Unlock()
newMap := map[string]p2p.ConnectionMetrics{}
for k, v := range cm.disconnected {
newMap[k] = v
}
return newMap
}
func (cm *ConnectionsMap) Disconnect(key string, val *p2p.ConnectionMetrics) bool {
cm.Lock.Lock()
defer cm.Lock.Unlock()
conVal, ok := cm.connected[key]
if ok {
delete(cm.connected, key)
}
if val == nil && ok {
cm.disconnected[key] = conVal
} else if val != nil {
cm.disconnected[key] = *val
}
return true
}
func (cm *ConnectionsMap) CleanDisconnected() int {
cm.Lock.Lock()
defer cm.Lock.Unlock()
count := 0
for key := range cm.disconnected {
delete(cm.disconnected, key)
count++
_ = key
}
return count
}
type ConnectionInfoArray []ConnectionInfo
func (slice ConnectionInfoArray) Len() int {
return len(slice)
}
func (slice ConnectionInfoArray) Less(i, j int) bool {
if slice[i].Connection.MomentConnected.Before(slice[j].Connection.MomentConnected) {
return true
}
return false
}
func (slice ConnectionInfoArray) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
type ConnectionInfo struct {
Connected bool
Hash string // Hash of PeerHash (Peerhash contains illegal characters for html ID)
Connection p2p.ConnectionMetrics
ConnectionTimeFormatted string
PeerHash string
}
// Used to send to front ent
func (cm *ConnectionsMap) SortedConnections() ConnectionInfoArray {
list := make([]ConnectionInfo, 0)
cmCopy := cm.GetConnectedCopy()
for key := range cmCopy {
item := new(ConnectionInfo)
if newCon := cm.GetConnection(key); newCon == nil {
continue
} else {
item.Connection = *newCon
item.ConnectionTimeFormatted = FormatDuration(newCon.MomentConnected)
item.Hash = hashPeerAddress(key)
item.PeerHash = key
}
item.Connected = true
list = append(list, *item)
}
disCopy := cm.GetDisconnectedCopy()
for key := range disCopy {
item := new(ConnectionInfo)
if newCon := cm.GetConnection(key); newCon == nil {
continue
} else {
item.Connection = *newCon
item.ConnectionTimeFormatted = FormatDuration(newCon.MomentConnected)
item.Hash = hashPeerAddress(key)
item.PeerHash = key
}
item.Connected = false
list = append(list, *item)
}
var sortedList ConnectionInfoArray
sortedList = list
sort.Sort(sort.Reverse(sortedList))
cm.CleanDisconnected()
return sortedList
}
func FormatDuration(initial time.Time) string {
dif := time.Since(initial)
if dif.Hours() > 24 {
if int(dif.Hours()/24) == 1 {
return fmt.Sprintf("%d%s", int(dif.Hours()/24), " day")
}
return fmt.Sprintf("%d%s", int(dif.Hours()/24), " days")
} else if int(dif.Hours()) > 0 {
if int(dif.Hours()) == 1 {
return fmt.Sprintf("%d%s", int(dif.Hours()), " hr")
}
return fmt.Sprintf("%d%s", int(dif.Hours()), " hrs")
} else if int(dif.Minutes()) > 0 {
if int(dif.Minutes()) == 1 {
return fmt.Sprintf("%d%s", int(dif.Minutes()), " min")
}
return fmt.Sprintf("%d%s", int(dif.Minutes()), " mins")
} else {
return fmt.Sprintf("%d%s", int(dif.Seconds()), " secs")
}
}
// map[string]p2p.ConnectionMetrics
func manageConnections(connections chan interface{}) {
for {
select {
case connectionsMessage := <-connections:
switch connectionsMessage.(type) {
case map[string]p2p.ConnectionMetrics:
newConnections := connectionsMessage.(map[string]p2p.ConnectionMetrics)
AllConnections.UpdateConnections(newConnections)
AllConnections.TallyTotals()
default: // drop that garbage
fmt.Printf("Got garbage data on metrics channel: %+v", connectionsMessage)
}
default:
time.Sleep(400 * time.Millisecond)
}
}
}