-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrol.go
168 lines (142 loc) · 4.07 KB
/
control.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/maghul/go.slf"
"golang.org/x/text/encoding/charmap"
)
type playerJson struct {
Name string
Id string
}
var decoder = charmap.ISO8859_1.NewDecoder()
func initControl(mux *http.ServeMux) {
mux.HandleFunc("/control/restart", restart)
mux.HandleFunc("/control/start", start)
mux.HandleFunc("/control/stop", stop)
mux.HandleFunc("/control/notify", notify)
mux.HandleFunc("/control/logger", handleLogger)
mux.HandleFunc("/notifications.json", notifications)
}
func restart(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Restarting squareplay server\r\n")
shutdownAll()
}
func getHeader(r *http.Request, name string) string {
iv := r.Header.Get(name)
dv, err := decoder.String(iv)
if err != nil {
panic(err)
}
return dv
}
func (pj *playerJson) String() string {
return fmt.Sprintf("player:[{'Name': '%s', 'Id': '%s'}]", pj.Name, pj.Id)
}
func decodePlayers(w http.ResponseWriter, r *http.Request) []*playerJson {
pa := make([]*playerJson, 0)
d := json.NewDecoder(r.Body)
err := d.Decode(&pa)
if err != nil {
slog.Debug.Println("Error decoding '", r.Body, "': err=", err)
w.WriteHeader(500)
fmt.Fprintln(w, "decodePlayers: Error decoding '", r.Body, "': err=", err)
}
return pa
}
func start(w http.ResponseWriter, r *http.Request) {
host := getHost(r.Host)
w.Header().Add("Content-Type", "text/text")
fmt.Fprintf(w, "[\r\n")
for _, pj := range decodePlayers(w, r) {
slog.Info.Println("Starting player: ", pj, " from host", host)
_, err := startPlayer(pj.Name, pj.Id, host)
if err != nil {
slog.Info.Printf("Player %s[%s] could not be started: %v", pj.Name, pj.Id, err)
w.WriteHeader(404)
fmt.Fprintf(w, "{ \"%s\": \"Failed\" }\r\n", pj.Id)
} else {
slog.Info.Printf("Player %s[%s] started", pj.Name, pj.Id)
fmt.Fprintf(w, "{ \"%s\": \"OK\" }\r\n", pj.Id)
}
}
fmt.Fprintf(w, "]\r\n")
}
func stop(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/text")
fmt.Fprintf(w, "[\r\n")
for _, pj := range decodePlayers(w, r) {
err := stopPlayer(pj.Name, pj.Id)
if err != nil {
w.WriteHeader(404)
slog.Info.Printf("Player %s[%s] could not be stopped: %v", pj.Name, pj.Id, err)
fmt.Fprintf(w, "{ \"%s\": \"Failed\" }\r\n", pj.Id)
} else {
slog.Info.Printf("Player %s[%s] stopped", pj.Name, pj.Id)
fmt.Fprintf(w, "{ \"%s\": \"Bye\" }\r\n", pj.Id)
}
}
fmt.Fprintf(w, "]\r\n")
}
func notify(w http.ResponseWriter, r *http.Request) {
broadcastNotifications()
}
func handleLogger(w http.ResponseWriter, r *http.Request) {
url := r.URL.String()
is := strings.LastIndex(url, "/")
if is < 0 {
slog.Debug.Println("handleLogger: Failed to decode URL=", url)
w.WriteHeader(400)
fmt.Fprintln(w, "handleLogger: Failed to decode URL=", url)
return
}
ss := strings.Split(url[is:], "?")
err := slf.SetLevel(ss[0], ss[1])
if err != nil {
slog.Debug.Println("handleLogger: ", err)
w.WriteHeader(400)
fmt.Fprintln(w, "handleLogger: ", err)
return
}
}
func handleRaopDebug(w http.ResponseWriter, r *http.Request) {
url := r.URL.String()
is := strings.LastIndex(url, "/")
if is < 0 {
slog.Debug.Println("Failed to decode URL=", url)
w.WriteHeader(400)
fmt.Fprintln(w, "handleLogger: Failed to decode URL=", url)
return
}
ss := strings.Split(url[is:], "?")
logger := strings.ToLower(ss[0])
level := strings.ToLower(ss[1])
switch level {
case "true":
raopdDebug(logger, true)
case "false":
raopdDebug(logger, false)
}
}
func notifications(w http.ResponseWriter, r *http.Request) {
h := getHost(r.Host)
w.Header().Add("Transfer-Encoding", "chunked")
w.Header().Add("Content-Type", "text/text")
// w.Header().Add("Content-Length", "should not be set")
fmt.Fprintf(w, "{ \"serverStatus\": \"OK\" }\r\n")
w.(http.Flusher).Flush()
nc := make(chan []byte, 32)
h.addNotificationChannel(nc)
defer h.removeNotificationChannel(nc)
for {
not := <-nc
slog.Debug.Println("Notification: ", string(not))
w.Write(not)
w.(http.Flusher).Flush()
}
}
func broadcastNotifications() {
// Broadcast all cached notifications.
}