-
Notifications
You must be signed in to change notification settings - Fork 1
/
host-panel.go
345 lines (285 loc) · 7.98 KB
/
host-panel.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"net/http"
"strconv"
"sync"
"time"
"github.com/creativenucleus/bytejammer/embed"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/tyler-sommer/stick"
)
const statusSendPeriod = 5 * time.Second
// HostPanel is the web interface for the host to manage their system, and the port should be private to them.
// It handles the startup of a server (potentially multiple)
// It does not handle the connections to the clients directly.
type HostPanel struct {
wsOperator *websocket.Conn
wsMutex sync.Mutex
session *JamSession
chLog chan string
statusTicker *time.Ticker
}
func startHostPanel(port int) error {
// #TODO: replace
hostSession := "session"
webServer := &http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadHeaderTimeout: 3 * time.Second,
}
subFs, err := fs.Sub(embed.WebStaticAssets, "web-static")
if err != nil {
return err
}
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(subFs))))
fmt.Printf("In a web browser, go to http://localhost:%d/%s/operator\n", port, hostSession)
hp := HostPanel{
chLog: make(chan string),
}
http.HandleFunc("/", hp.webIndex)
http.HandleFunc(fmt.Sprintf("/%s/operator", hostSession), hp.webOperator)
http.HandleFunc(fmt.Sprintf("/%s/ws-operator", hostSession), hp.wsWebOperator())
http.HandleFunc(fmt.Sprintf("/%s/api/server.json", hostSession), hp.webApiServer)
http.HandleFunc(fmt.Sprintf("/%s/api/machine.json", hostSession), hp.webApiMachine)
if err := webServer.ListenAndServe(); err != nil {
return err
}
return nil
}
func (hp *HostPanel) webIndex(w http.ResponseWriter, r *http.Request) {
_, err := w.Write(embed.ServerIndexHtml)
if err != nil {
log.Println("write:", err)
}
}
func (hp *HostPanel) webOperator(w http.ResponseWriter, r *http.Request) {
env := stick.New(nil)
err := env.Execute(string(embed.ServerOperatorHtml), w, map[string]stick.Value{"session_key": "session"})
if err != nil {
log.Println("write:", err)
}
}
func (hp *HostPanel) wsWebOperator() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var err error
hp.wsOperator, err = wsUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer func() {
hp.wsOperator.Close()
hp.wsOperator = nil
}()
go hp.wsOperatorRead()
go hp.wsOperatorWrite()
// #TODO: handle exit
for {
}
}
}
func (hp *HostPanel) wsOperatorRead() {
for {
var msg Msg
err := hp.wsOperator.ReadJSON(&msg)
if err != nil {
log.Println("read:", err)
break
}
switch msg.Type {
case "identify-machines":
hp.handleIdentifyMachines()
case "connect-machine-client":
hp.handleConnectMachineClient(msg.ConnectMachineClient)
case "disconnect-machine-client":
hp.handleDisconnectMachineClient(msg.DisconnectMachineClient)
case "close-machine":
hp.handleCloseMachine(msg.CloseMachine)
case "stop-server":
hp.handleStopServer()
default:
log.Printf("Message Type not understood: %s\n", msg.Type)
}
}
}
func (hp *HostPanel) wsOperatorWrite() {
hp.statusTicker = time.NewTicker(statusSendPeriod)
defer func() {
hp.statusTicker.Stop()
}()
for {
select {
// case <-done:
// return
case <-hp.statusTicker.C:
hp.sendServerStatus(false)
case logMsg := <-hp.chLog:
hp.sendLog(logMsg)
}
}
}
func (hp *HostPanel) webApiServer(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
// #TODO: Cleaner way to do this?
type reqType struct {
Port string `json:"port"`
SessionName string `json:"session-name"`
}
var req reqType
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
apiOutErr(w, err, http.StatusBadRequest)
return
}
port, err := strconv.Atoi(req.Port)
if err != nil {
apiOutErr(w, err, http.StatusBadRequest)
return
}
// #TODO: This is not great - return some detail
if hp.session != nil {
apiOutErr(w, errors.New("Server already running"), http.StatusBadRequest)
return
}
hp.session, err = startJamSession(port, req.SessionName, hp.chLog)
if err != nil {
hp.chLog <- fmt.Sprintf("Server failed to launch: %s", err)
apiOutErr(w, err, http.StatusInternalServerError)
return
}
hp.sendLog(fmt.Sprintf("Server launched"))
hp.sendServerStatus(true)
default:
apiOutErr(w, errors.New("Method not allowed"), http.StatusMethodNotAllowed)
}
}
func (hp *HostPanel) webApiMachine(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
// #TODO: Cleaner way to do this?
type reqType struct {
Platform string `json:"platform"`
Mode string `json:"mode"`
ClientUuid string `json:"client-uuid"`
}
var req reqType
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
apiOutErr(w, err, http.StatusBadRequest)
return
}
switch req.Mode {
case "unassigned":
_, err := hp.session.startMachine()
if err != nil {
apiOutErr(w, fmt.Errorf("TIC-80 Launch (unassigned): %w", err), http.StatusBadRequest)
return
}
hp.sendLog("TIC-80 Launched (unassigned)")
case "jammer":
connUuid, err := uuid.Parse(req.ClientUuid)
if err != nil {
apiOutErr(w, fmt.Errorf("TIC-80 Launch (jammer): %w", err), http.StatusBadRequest)
return
}
_, err = hp.session.startMachineForConn(connUuid)
if err != nil {
apiOutErr(w, fmt.Errorf("TIC-80 Launch (jammer): %w", err), http.StatusBadRequest)
return
}
hp.sendLog("TIC-80 Launched for (jammer)")
case "jukebox":
playlist, err := readPlaylist("")
if err != nil {
apiOutErr(w, err, http.StatusInternalServerError)
return
}
err = startLocalJukebox(playlist)
if err != nil {
apiOutErr(w, err, http.StatusInternalServerError)
return
}
hp.sendLog("TIC-80 Launched for (playlist)")
default:
apiOutErr(w, errors.New("Unexpected mode (should be jammer or jukebox)"), http.StatusBadRequest)
}
hp.sendServerStatus(true)
apiOutResponse(w, nil, http.StatusCreated)
default:
apiOutErr(w, errors.New("Method not allowed"), http.StatusMethodNotAllowed)
}
}
func (hp *HostPanel) handleStopServer() {
if hp.session == nil {
hp.chLog <- "Requested server stop, but no server is running"
return
}
hp.session.stop()
hp.sendServerStatus(true)
}
func (hp *HostPanel) handleIdentifyMachines() {
if hp.session == nil {
hp.chLog <- "Requested identify machines, but no server is running"
return
}
hp.session.identifyMachines()
hp.sendServerStatus(true)
}
func (hp *HostPanel) handleConnectMachineClient(data DataConnectMachineClient) {
if hp.session == nil {
hp.chLog <- "Requested connect, but no server is running"
return
}
hp.session.connectMachineClient(data)
hp.sendServerStatus(true)
}
func (hp *HostPanel) handleDisconnectMachineClient(data DataDisconnectMachineClient) {
if hp.session == nil {
hp.chLog <- "Requested disconnect, but no server is running"
return
}
hp.session.disconnectMachineClient(data)
hp.sendServerStatus(true)
}
func (hp *HostPanel) handleCloseMachine(data DataCloseMachine) {
if hp.session == nil {
hp.chLog <- "Requested close machine, but no server is running"
return
}
hp.session.closeMachine(data)
hp.sendServerStatus(true)
}
// #TODO: resetTicker could be improved - we should set that true if the code requests a status send
func (hp *HostPanel) sendServerStatus(resetTicker bool) {
// #TODO: This is not great - should be driven by the server tick?
if hp.session == nil {
return
}
if resetTicker {
hp.statusTicker.Reset(statusSendPeriod)
}
status := hp.session.getStatus()
err := hp.sendData(&status)
if err != nil {
log.Println("read:", err)
}
}
func (hp *HostPanel) sendLog(message string) {
msg := Msg{Type: "log", Log: DataLog{Msg: message}}
fmt.Printf("-> HOST PANEL: %s\n", message)
err := hp.sendData(&msg)
if err != nil {
log.Println("read:", err)
}
}
func (hp *HostPanel) sendData(data interface{}) error {
hp.wsMutex.Lock()
defer hp.wsMutex.Unlock()
return hp.wsOperator.WriteJSON(data)
}