/
main.go
424 lines (378 loc) · 12.1 KB
/
main.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
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/pprof"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
uuid "github.com/google/uuid"
"github.com/pkg/browser"
dist "github.com/wowsims/sod/binary_dist"
"github.com/wowsims/sod/sim"
"github.com/wowsims/sod/sim/core"
proto "github.com/wowsims/sod/sim/core/proto"
googleProto "google.golang.org/protobuf/proto"
)
func init() {
sim.RegisterAll()
}
var (
Version string
outdated int
)
func main() {
if Version == "" {
Version = "development"
}
var useFS = flag.Bool("usefs", false, "Use local file system for client files. Set to true during development.")
var wasm = flag.Bool("wasm", false, "Use wasm for sim instead of web server apis. Can only be used with usefs=true")
var simName = flag.String("sim", "", "Name of simulator to launch (ex: balance_druid, elemental_shaman, etc)")
var host = flag.String("host", "localhost:3333", "URL to host the interface on.")
var launch = flag.Bool("launch", true, "auto launch browser")
var skipVersionCheck = flag.Bool("nvc", false, "set true to skip version check")
flag.Parse()
fmt.Printf("Version: %s\n", Version)
if !*skipVersionCheck && Version != "development" {
go func() {
resp, err := http.Get("https://api.github.com/repos/wowsims/sod/releases/latest")
if err != nil {
return
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return
}
result := struct {
Tag string `json:"tag_name"`
URL string `json:"html_url"`
Name string `json:"name"`
}{}
if err := json.Unmarshal(body, &result); err != nil {
return
}
if result.Tag != Version {
outdated = 2
fmt.Printf("New version of simulator available: %s\n\tDownload at: %s\n", result.Name, result.URL)
} else {
outdated = 1
}
}()
}
s := &server{
progMut: sync.RWMutex{},
asyncProgresses: map[string]*asyncProgress{},
}
s.runServer(*useFS, *host, *launch, *simName, *wasm, bufio.NewReader(os.Stdin))
}
// Handlers to decode and handle each proto function
var handlers = map[string]apiHandler{
"/raidSim": {msg: func() googleProto.Message { return &proto.RaidSimRequest{} }, handle: func(msg googleProto.Message) googleProto.Message {
return core.RunRaidSim(msg.(*proto.RaidSimRequest))
}},
"/statWeights": {msg: func() googleProto.Message { return &proto.StatWeightsRequest{} }, handle: func(msg googleProto.Message) googleProto.Message {
return core.StatWeights(msg.(*proto.StatWeightsRequest))
}},
"/computeStats": {msg: func() googleProto.Message { return &proto.ComputeStatsRequest{} }, handle: func(msg googleProto.Message) googleProto.Message {
return core.ComputeStats(msg.(*proto.ComputeStatsRequest))
}},
}
var asyncAPIHandlers = map[string]asyncAPIHandler{
"/raidSimAsync": {msg: func() googleProto.Message { return &proto.RaidSimRequest{} }, handle: func(msg googleProto.Message, reporter chan *proto.ProgressMetrics) {
core.RunRaidSimAsync(msg.(*proto.RaidSimRequest), reporter)
}},
"/statWeightsAsync": {msg: func() googleProto.Message { return &proto.StatWeightsRequest{} }, handle: func(msg googleProto.Message, reporter chan *proto.ProgressMetrics) {
core.StatWeightsAsync(msg.(*proto.StatWeightsRequest), reporter)
}},
"/bulkSimAsync": {msg: func() googleProto.Message { return &proto.BulkSimRequest{} }, handle: func(msg googleProto.Message, reporter chan *proto.ProgressMetrics) {
// TODO: we can use context's to cancel stuff.
// We should have all the async APIs take in context and let it be cancelled via its async ID.
core.RunBulkSimAsync(context.Background(), msg.(*proto.BulkSimRequest), reporter)
}},
}
type server struct {
progMut sync.RWMutex
asyncProgresses map[string]*asyncProgress
}
type apiHandler struct {
msg func() googleProto.Message
handle func(googleProto.Message) googleProto.Message
}
type asyncAPIHandler struct {
msg func() googleProto.Message
handle func(googleProto.Message, chan *proto.ProgressMetrics)
}
type asyncProgress struct {
id string
latestProgress atomic.Value
}
func (s *server) addNewSim() *asyncProgress {
newID := uuid.NewString()
simProgress := &asyncProgress{
id: newID,
}
simProgress.latestProgress.Store(&proto.ProgressMetrics{})
s.progMut.Lock()
s.asyncProgresses[newID] = simProgress
s.progMut.Unlock()
return simProgress
}
func (s *server) handleAsyncAPI(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
return
}
endpoint := r.URL.Path
handler, ok := asyncAPIHandlers[endpoint]
if !ok {
log.Printf("Invalid Endpoint: %s", endpoint)
w.WriteHeader(http.StatusNotFound)
return
}
msg := handler.msg()
if err := googleProto.Unmarshal(body, msg); err != nil {
log.Printf("Failed to parse request: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// reporter channel is handed into the core simulation.
// as the simulation advances it will push changes to the channel
// these changes will be consumed by the goroutine below so the asyncProgress endpoint can fetch the results.
reporter := make(chan *proto.ProgressMetrics, 100)
handler.handle(msg, reporter)
// Generate a new async simulation
simProgress := s.addNewSim()
// Now launch a background process that pulls progress reports off the reporter channel
// and pushes it into the async progress cache.
go func() {
for {
select {
case <-time.After(time.Minute * 10):
// if we get no progress after 10 minutes, delete the pending sim and exit.
s.progMut.Lock()
delete(s.asyncProgresses, simProgress.id)
s.progMut.Unlock()
return
case progMetric := <-reporter:
if progMetric == nil {
return
}
simProgress.latestProgress.Store(progMetric)
if progMetric.FinalRaidResult != nil || progMetric.FinalWeightResult != nil || progMetric.FinalBulkResult != nil {
return
}
}
}
}()
protoResult := &proto.AsyncAPIResult{
ProgressId: simProgress.id,
}
outbytes, err := googleProto.Marshal(protoResult)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/x-protobuf")
w.Write(outbytes)
}
func (s *server) setupAsyncServer() {
// All async handlers here will call the addNewSim, generating a new UUID and cached progress state.
for route := range asyncAPIHandlers {
http.HandleFunc(route, func(w http.ResponseWriter, r *http.Request) {
s.handleAsyncAPI(w, r)
})
}
// asyncProgress will fetch the current progress of a simulation by its UUID.
http.HandleFunc("/asyncProgress", func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
return
}
msg := &proto.AsyncAPIResult{}
if err := googleProto.Unmarshal(body, msg); err != nil {
log.Printf("Failed to parse request: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// Read lock the map of all progress statuses, fetching current one.
s.progMut.RLock()
progress, ok := s.asyncProgresses[msg.ProgressId]
s.progMut.RUnlock()
if !ok {
w.WriteHeader(http.StatusNoContent)
return
}
latest := progress.latestProgress.Load().(*proto.ProgressMetrics)
outbytes, err := googleProto.Marshal(latest)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
// If this was the last result, delete the cache for this simulation.
if latest.FinalRaidResult != nil || latest.FinalWeightResult != nil || latest.FinalBulkResult != nil {
s.progMut.Lock()
delete(s.asyncProgresses, msg.ProgressId)
s.progMut.Unlock()
}
w.Header().Add("Content-Type", "application/x-protobuf")
w.Write(outbytes)
})
}
func (s *server) runServer(useFS bool, host string, launchBrowser bool, simName string, wasm bool, inputReader *bufio.Reader) {
s.setupAsyncServer()
var fs http.Handler
if useFS {
log.Printf("Using local file system for development.")
fs = http.FileServer(http.Dir("./dist"))
} else {
log.Printf("Embedded file server running.")
fs = http.FileServer(http.FS(dist.FS))
}
for route := range handlers {
http.HandleFunc(route, handleAPI)
}
http.HandleFunc("/version", func(resp http.ResponseWriter, req *http.Request) {
msg := fmt.Sprintf(`{"version": "%s", "outdated": %d}`, Version, outdated)
resp.Write([]byte(msg))
})
http.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
http.Redirect(resp, req, "/sod/", http.StatusPermanentRedirect)
return
}
resp.Header().Add("Cache-Control", "no-cache")
if strings.HasSuffix(req.URL.Path, ".wasm") {
resp.Header().Set("Content-Type", "application/wasm")
}
if strings.HasSuffix(req.URL.Path, ".js") {
resp.Header().Set("Content-Type", "application/javascript")
}
if !useFS || (useFS && !wasm) {
if strings.HasSuffix(req.URL.Path, "sim_worker.js") {
req.URL.Path = strings.Replace(req.URL.Path, "sim_worker.js", "net_worker.js", 1)
}
}
fs.ServeHTTP(resp, req)
})
if launchBrowser {
if strings.HasPrefix(host, ":") {
host = "localhost" + host
}
url := fmt.Sprintf("http://%s/sod/%s", host, simName)
log.Printf("Launching interface on %s", url)
go func() {
err := browser.OpenURL(url)
if err != nil {
fmt.Printf("Error launching browser: %#v\n", err.Error())
fmt.Printf("You will need to manually open your web browser to %s\n", url)
}
}()
}
go func() {
// Launch server!
if err := http.ListenAndServe(host, nil); err != nil {
log.Printf("Failed to shutdown server: %s", err)
os.Exit(1)
}
log.Printf("Server shutdown successfully.")
os.Exit(0)
}()
// used to read a CTRL+C
c := make(chan os.Signal, 10)
signal.Notify(c, syscall.SIGINT)
go func() {
<-c
log.Printf("Shutting down")
os.Exit(0)
}()
fmt.Printf("Enter Command... '?' for list\n")
for {
fmt.Printf("> ")
text, err := inputReader.ReadString('\n')
if err != nil {
// block forever
<-c
os.Exit(-1)
}
if len(text) == 0 {
continue
}
command := strings.TrimSpace(text)
switch command {
case "profile":
filename := fmt.Sprintf("profile_%d.cpu", time.Now().Unix())
fmt.Printf("Running profiling for 15 seconds, output to %s\n", filename)
f, err := os.Create(filename)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
go func() {
time.Sleep(time.Second * 15)
pprof.StopCPUProfile()
f.Close()
fmt.Printf("Profiling complete.\n> ")
}()
case "sims":
s.progMut.RLock()
fmt.Printf("Total Sims Running: %d\n", len(s.asyncProgresses))
for _, v := range s.asyncProgresses {
latest := (v.latestProgress.Load()).(*proto.ProgressMetrics)
fmt.Printf("Process: %s (%d sims)\n\t Progress: %d/%d\n", v.id, latest.TotalSims, latest.CompletedIterations, latest.TotalIterations)
}
s.progMut.RUnlock()
case "quit":
os.Exit(1)
case "?":
fmt.Printf("Commands:\n\tsims - Lists all active async sims running currently.\n\tprofile - start a CPU profile for debugging performance\n\tquit - exits\n\n")
case "":
// nothing.
default:
fmt.Printf("Unknown command: '%s'", command)
}
}
}
// handleAPI is generic handler for any api function using protos.
func handleAPI(w http.ResponseWriter, r *http.Request) {
endpoint := r.URL.Path
body, err := io.ReadAll(r.Body)
if err != nil {
return
}
handler, ok := handlers[endpoint]
if !ok {
log.Printf("Invalid Endpoint: %s", endpoint)
w.WriteHeader(http.StatusNotFound)
return
}
msg := handler.msg()
if err := googleProto.Unmarshal(body, msg); err != nil {
log.Printf("Failed to parse request: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
result := handler.handle(msg)
outbytes, err := googleProto.Marshal(result)
if err != nil {
log.Printf("[ERROR] Failed to marshal result: %s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/x-protobuf")
w.Write(outbytes)
}