-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
496 lines (434 loc) · 11.7 KB
/
web.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/AmyangXYZ/sgo"
"github.com/AmyangXYZ/sgo/middlewares"
"github.com/gorilla/websocket"
)
const (
// http listening address:port
addr = ":8000"
)
var (
// websocket config
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
simStartedFlag = 0
stopFlowSig chan bool
)
// runHTTPSever starts a http server
func runHTTPSever() {
// heartbeat for ws communication
go func() {
for {
l := Log{
Type: WSLOG_HEARTBEAT,
Msg: "heartbeat",
}
WSLog <- l
time.Sleep(10 * time.Second)
}
}()
app := sgo.New()
app.SetTemplates("templates", nil)
// cors middleware
app.USE(middlewares.CORS(middlewares.CORSOpt{}))
// home page handler
app.GET("/", index)
// static files handler
app.GET("/static/*files", static)
// api handlers
app.GET("/api/boottime", getBootTime)
app.GET("/ws/comm", wsComm)
app.GET("/api/topologies", getTopoTags)
app.GET("/api/topology", getTopo)
app.POST("/api/topology", postTopo)
app.OPTIONS("/api/topology", sgo.PreflightHandler)
app.GET("/api/stats/:name/io", getStatsIOByName)
app.GET("/api/stats/:name/delay", getStatsDelayByName)
app.POST("/api/links", postDefaultSetting)
app.OPTIONS("/api/links", sgo.PreflightHandler)
app.POST("/api/link/:name", postLink)
app.OPTIONS("/api/link/:name", sgo.PreflightHandler)
app.GET("/api/mars/:delay", getMarsDelay)
app.POST("/api/priorities", postPriorities)
app.OPTIONS("/api/priorities", sgo.PreflightHandler)
app.POST("/api/flows", postFlows)
app.OPTIONS("/api/flows", sgo.PreflightHandler)
app.GET("/api/flows/start_flag", getStartedFlag)
app.GET("/api/flows/stop", stopFlows)
app.GET("/api/switch/:id", getSwitchSchedule)
app.POST("/api/fault/switch/:id", postFault)
app.OPTIONS("/api/fault/switch/:id", sgo.PreflightHandler)
app.GET("/api/fault/clear", clearFault)
app.GET("/api/animation", getCurrentAnimationFlag)
app.GET("/api/animation/:flag", getAnimationFlag)
app.GET("/api/reroute", getCurrentRerouteFlag)
app.GET("/api/reroute/:flag", getRerouteFlag)
app.GET("/api/dupeli", getCurrentDupEliFlag)
app.GET("/api/dupeli/:flag", getDupEliFlag)
app.GET("/api/frer", getCurrentFRERFlag)
app.GET("/api/frer/:flag", getFRERFlag)
if err := app.Run(addr); err != nil {
log.Fatal("Listen error", err)
}
}
// Index page handler.
func index(ctx *sgo.Context) error {
return ctx.Render(200, "index")
}
// Static files handler.
func static(ctx *sgo.Context) error {
staticHandle := http.StripPrefix("/static",
http.FileServer(http.Dir("./static")))
staticHandle.ServeHTTP(ctx.Resp, ctx.Req)
return nil
}
// getBootTime returns the boot time of the server
func getBootTime(ctx *sgo.Context) error {
return ctx.Text(200, fmt.Sprintf("%d", boottime))
}
// wsComm handles websocket communication
func wsComm(ctx *sgo.Context) error {
ws, err := upgrader.Upgrade(ctx.Resp, ctx.Req, nil)
breakSig := make(chan bool)
if err != nil {
return err
}
// fmt.Println("ws/comm connected")
defer func() {
ws.Close()
// fmt.Println("ws/comm client closed")
}()
go func() {
for {
_, _, err := ws.ReadMessage()
if err != nil {
breakSig <- true
}
}
}()
for {
select {
case l := <-WSLog:
ws.WriteJSON(l)
case <-breakSig:
return nil
}
}
}
func getCurrentAnimationFlag(ctx *sgo.Context) error {
return ctx.Text(200, strconv.FormatBool(ANIMATION_ENABLED))
}
func getAnimationFlag(ctx *sgo.Context) error {
flag := ctx.Param("flag")
if flag == "true" {
ANIMATION_ENABLED = true
fmt.Println("ANIMATION enabled")
} else {
ANIMATION_ENABLED = false
fmt.Println("ANIMATION disabled")
}
return ctx.Text(200, strconv.FormatBool(ANIMATION_ENABLED))
}
func getCurrentRerouteFlag(ctx *sgo.Context) error {
return ctx.Text(200, strconv.FormatBool(REROUTE_ENABLED))
}
func getRerouteFlag(ctx *sgo.Context) error {
flag := ctx.Param("flag")
if flag == "true" {
REROUTE_ENABLED = true
fmt.Println("REROUTE enabled")
} else {
REROUTE_ENABLED = false
fmt.Println("REROUTE disabled")
}
return ctx.Text(200, strconv.FormatBool(REROUTE_ENABLED))
}
func getCurrentDupEliFlag(ctx *sgo.Context) error {
return ctx.Text(200, strconv.FormatBool(DUP_ELI_ENABLED))
}
func getDupEliFlag(ctx *sgo.Context) error {
flag := ctx.Param("flag")
if flag == "true" {
DUP_ELI_ENABLED = true
fmt.Println("DUP_ELI enabled")
} else {
DUP_ELI_ENABLED = false
fmt.Println("DUP_ELP disabled")
}
return ctx.Text(200, strconv.FormatBool(DUP_ELI_ENABLED))
}
func getCurrentFRERFlag(ctx *sgo.Context) error {
return ctx.Text(200, strconv.FormatBool(FRER_ENABLED))
}
func getFRERFlag(ctx *sgo.Context) error {
flag := ctx.Param("flag")
if flag == "true" {
FRER_ENABLED = true
fmt.Println("FRER enabled")
} else {
FRER_ENABLED = false
fmt.Println("FRER disabled")
}
return ctx.Text(200, strconv.FormatBool(FRER_ENABLED))
}
func getSwitchSchedule(ctx *sgo.Context) error {
id, _ := strconv.Atoi(ctx.Param("id"))
Switches[id].Neighbors = []string{}
for i := 0; i < Switches[id].portsOutIdx; i++ {
Switches[id].Neighbors = append(Switches[id].Neighbors, Switches[id].portsOut[i].Neighbor)
}
return ctx.JSON(200, 1, "success", Switches[id])
}
func getTopoTags(ctx *sgo.Context) error {
tags, err := queryTopoTags()
if err != nil {
return ctx.JSON(500, 0, err.Error(), nil)
}
if len(tags) == 0 {
return ctx.JSON(200, 0, "no result found", nil)
}
return ctx.JSON(200, 1, "success", tags)
}
func getTopo(ctx *sgo.Context) error {
topo, err := queryTopo(ctx.Param("tag"))
if err != nil {
return ctx.JSON(500, 0, err.Error(), nil)
}
if len(topo.Nodes) == 0 || len(topo.Edges) == 0 {
return ctx.JSON(200, 0, "no result found", nil)
}
if ctx.Param("tag") != ActiveTopoTag {
ActiveTopoTag = ctx.Param("tag")
loadTopo(topo)
}
return ctx.JSON(200, 1, "success", topo)
}
func postTopo(ctx *sgo.Context) error {
body, err := io.ReadAll(ctx.Req.Body)
if err != nil {
fmt.Println(err)
return err
}
var topo TopologyData
fmt.Println(string(body))
if err = json.Unmarshal(body, &topo); err != nil {
fmt.Println(err)
return err
}
loadTopo(topo)
insertTopo(topo)
return ctx.Text(200, "")
}
type Priority struct {
Name string `json:"name"`
Priority json.Number `json:"priority"`
}
func postPriorities(ctx *sgo.Context) error {
body, err := io.ReadAll(ctx.Req.Body)
if err != nil {
fmt.Println(err)
return err
}
var priorities []Priority
// fmt.Println(string(body))
if err = json.Unmarshal(body, &priorities); err != nil {
fmt.Println(err)
return err
}
for _, p := range priorities {
for _, s := range Subsystems {
if s.name == p.Name {
tmp, _ := p.Priority.Int64()
s.Priority = int(tmp)
break
}
}
}
return nil
}
func getStatsIOByName(ctx *sgo.Context) error {
stats, err := queryStatsIOByName(ctx.Param("name"))
if err != nil {
return ctx.JSON(500, 0, err.Error(), nil)
}
if len(stats) == 0 {
return ctx.JSON(200, 0, "no result found", nil)
}
return ctx.JSON(200, 1, "success", stats)
}
func getStatsDelayByName(ctx *sgo.Context) error {
stats, err := queryStatsDelayByName(ctx.Param("name"))
if err != nil {
return ctx.JSON(500, 0, err.Error(), nil)
}
if len(stats) == 0 {
return ctx.JSON(200, 0, "no result found", nil)
}
return ctx.JSON(200, 1, "success", stats)
}
// set link properties
func postLink(ctx *sgo.Context) error {
fmt.Println(ctx.Params())
if linkName := ctx.Param("name"); linkName != "" {
if nodes := strings.Split(linkName, " > "); len(nodes) == 2 {
for _, l := range Links {
if (l.sender1.Owner == nodes[0] && l.sink1.Owner == nodes[1]) ||
(l.sender2.Owner == nodes[0] && l.sink2.Owner == nodes[1]) {
l.Bandwidth, _ = strconv.ParseFloat(ctx.Param("bandwidth"), 64)
l.PacketLossRate, _ = strconv.ParseFloat(ctx.Param("loss"), 64)
l.Distance, _ = strconv.ParseFloat(ctx.Param("distance"), 64)
l.Failed, _ = strconv.ParseBool(ctx.Param("failed"))
}
}
}
}
return ctx.Text(200, fmt.Sprintf("%d", boottime))
}
func getMarsDelay(ctx *sgo.Context) error {
delay, _ := strconv.Atoi(ctx.Param("delay"))
for _, l := range Links {
if l.sender1.Owner == "GCC" || l.sender2.Owner == "GCC" {
b, _ := strconv.ParseFloat(ctx.Param("bandwidth"), 64)
l.Bandwidth = b * 1024 // in Kbps
d, _ := strconv.ParseFloat(ctx.Param("distance"), 64)
l.Distance = d // in km
l.HardcodedDelay = float64(delay)
}
}
fmt.Println("set ground-habitat delay to", delay)
return ctx.Text(200, "biu")
}
func postDefaultSetting(ctx *sgo.Context) error {
if ctx.Param("type") == "wired" {
for _, l := range Links {
if l.sender1.Owner != "GCC" && l.sender2.Owner != "GCC" {
b, _ := strconv.ParseFloat(ctx.Param("bandwidth"), 64)
l.Bandwidth = b * 1024 * 1024 * 1024 // in Gbps
d, _ := strconv.ParseFloat(ctx.Param("distance"), 64)
l.Distance = d
}
}
}
if ctx.Param("type") == "wireless" {
for _, l := range Links {
if l.sender1.Owner == "GCC" || l.sender2.Owner == "GCC" {
b, _ := strconv.ParseFloat(ctx.Param("bandwidth"), 64)
l.Bandwidth = b * 1024 // in Kbps
d, _ := strconv.ParseFloat(ctx.Param("distance"), 64)
l.Distance = d // in km
}
}
}
return ctx.Text(200, fmt.Sprintf("%d", boottime))
}
type Flow struct {
Name string `json:"name"` // subsys name
ID int `json:"id"` // subsys id
Dst []string `json:"dst"`
Freq string `json:"freq"`
}
func postFlows(ctx *sgo.Context) error {
body, err := io.ReadAll(ctx.Req.Body)
if err != nil {
fmt.Println(err)
return err
}
fmt.Println(string(body))
var flows []Flow
err = json.Unmarshal(body, &flows)
if err != nil {
fmt.Println(err)
return err
}
simStartedFlag = 1
stopFlowSig = make(chan bool, 56)
subsysList := []string{"GCC", "HMS", "STR", "SPL", "PWR", "ECLSS", "AGT", "IE", "EXT", "DTB", "COORD"}
for _, f := range flows {
for _, subsys := range Subsystems {
if subsys.Name() == f.Name {
for i, flag := range f.Dst {
if flag == "X" {
go func(dstID int, f Flow) {
for {
select {
case <-stopFlowSig:
return
default:
subsys.CreateFlow(dstID)
if freq, err := strconv.ParseFloat(f.Freq, 64); err == nil {
time.Sleep(time.Duration(1/freq*1000*1000*1000) * time.Nanosecond)
} else {
fmt.Println(err)
return
}
}
}
}(subsysName2ID(subsysList[i]), f)
// delay between different flows
// time.Sleep(200 * time.Millisecond)
}
}
break
}
}
}
return ctx.Text(200, "biu")
}
func getStartedFlag(ctx *sgo.Context) error {
return ctx.Text(200, fmt.Sprintf("%d", simStartedFlag))
}
func stopFlows(ctx *sgo.Context) error {
simStartedFlag = 0
for i := 0; i < cap(stopFlowSig); i++ {
stopFlowSig <- true
}
return ctx.Text(200, "stop all flows")
}
func postFault(ctx *sgo.Context) error {
id, _ := strconv.Atoi(ctx.Param("id"))
duration, _ := strconv.Atoi(ctx.Param("duration"))
Switches[id].Faults[ctx.Param("type")] = Fault{
Type: ctx.Param("type"),
Happening: true,
Durtaion: duration,
}
go func() {
time.Sleep(time.Duration(duration) * time.Second)
Switches[id].Faults[ctx.Param("type")] = Fault{
Type: ctx.Param("type"),
Happening: false,
}
}()
fmt.Printf("Inject %s fault on SW%d, duration: %d s\n", ctx.Param("type"), id, duration)
WSLog <- Log{
Type: WSLOG_MSG,
Msg: fmt.Sprintf("Inject %s fault on SW%d, duration: %d s\n", ctx.Param("type"), id, duration),
}
return ctx.Text(200, "")
}
func clearFault(ctx *sgo.Context) error {
for _, sw := range Switches {
for k, v := range sw.Faults {
v.Happening = false
sw.Faults[k] = v
}
}
fmt.Println("Faults cleared")
WSLog <- Log{
Type: WSLOG_MSG,
Msg: "Faults cleared",
}
return ctx.Text(200, "")
}