-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket.go
380 lines (353 loc) · 7.68 KB
/
websocket.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type WSMsg interface {
Type() string
}
type Tag struct {
Name string `json:"name"`
Value json.RawMessage `json:"value"`
}
func websocketHandler(c *MPD) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
log.Printf("new WS connection")
defer log.Printf("end of WS connection")
lock := sync.Mutex{}
writeMsg := func(msg WSMsg) error {
lock.Lock()
defer lock.Unlock()
w, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
return err
}
defer w.Close()
value, err := json.Marshal(msg)
if err != nil {
return err
}
return json.NewEncoder(w).Encode(Tag{
Name: "siren/" + msg.Type(),
Value: value,
})
}
// init client state
config := WSConfig{
ArtistMode: c.artistMode,
MpdHost: c.url,
}
if err := writeMsg(config); err != nil {
log.Printf("writeMsg: %s", err)
return
}
go func(ctx context.Context, conn *websocket.Conn) {
tick := time.NewTicker(55 * time.Second)
defer tick.Stop()
for {
select {
case <-tick.C:
if err := conn.WriteControl(
websocket.PingMessage,
nil,
time.Now().Add(5*time.Second),
); err != nil {
return
}
case <-ctx.Done():
return
}
}
}(r.Context(), conn)
go func() {
for {
func() {
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
w := c.Watch(ctx)
for msg := range w {
if err := writeMsg(msg); err != nil {
log.Printf("writeMsg: %s", err)
go func() {
for range w {
}
}()
return
}
}
}()
select {
case <-r.Context().Done():
return
default:
log.Printf("lost connection to MPD")
time.Sleep(5 * time.Second)
}
}
}()
for {
t, m, err := conn.NextReader()
if err != nil {
log.Printf("conn err: %s", err)
return
}
switch t {
case websocket.TextMessage:
dec := json.NewDecoder(m)
for {
var tag Tag
if err := dec.Decode(&tag); err != nil {
if err != io.EOF {
log.Printf("decode err: %s", err)
}
break
}
cmd, err := parseCmd(tag)
if err != nil {
log.Printf("parseCmd: %s", err)
continue
}
msg, err := handle(c, cmd)
if err != nil {
log.Printf("handle: %s", err)
continue
}
if msg == nil {
continue
}
if err := writeMsg(msg); err != nil {
log.Printf("write response: %s", err)
}
}
default:
log.Printf("got a %d. Ignored", t)
}
}
}
}
type WSConfig struct {
ArtistMode ArtistMode `json:"artistmode"`
MpdHost string `json:"mpdhost"`
}
func (w WSConfig) Type() string { return "config" }
type WSInodes struct {
ID string `json:"id"`
Inodes []Inode `json:"inodes"`
}
func (w WSInodes) Type() string { return "inodes" }
type WSList struct {
ID string `json:"id"`
List []DBEntry `json:"list"`
}
func (w WSList) Type() string { return "list" }
type WSTrack struct {
ID string `json:"id"`
Track Track `json:"track"`
}
func (w WSTrack) Type() string { return "track" }
type CmdLoadDir struct {
ID string `json:"id"`
File string `json:"file"`
}
type CmdAdd struct {
ID string `json:"id"`
}
type CmdList struct {
ID string `json:"id"`
What string `json:"what"`
Artist string `json:"artist"`
Album string `json:"album"`
}
type CmdFindAdd struct {
Artist string `json:"artist"`
Album string `json:"album"`
Track string `json:"track"`
}
type CmdPlayID struct {
ID string `json:"id"`
}
type CmdTrack struct {
ID string `json:"id"`
File string `json:"file"`
}
type CmdSeek struct {
Song string `json:"song"`
Seconds float64 `json:"seconds"`
}
type CmdVolume struct {
Volume float64 `json:"volume"`
}
type CmdPlay struct{}
type CmdStop struct{}
type CmdNext struct{}
type CmdPrevious struct{}
type CmdClear struct{}
type CmdPause struct{}
type CmdUnpause struct{}
func parseCmd(t Tag) (interface{}, error) {
switch t.Name {
case "loaddir":
var c CmdLoadDir
return &c, json.Unmarshal(t.Value, &c)
case "add":
var c CmdAdd
return &c, json.Unmarshal(t.Value, &c)
case "playid":
var c CmdPlayID
return &c, json.Unmarshal(t.Value, &c)
case "track":
var c CmdTrack
return &c, json.Unmarshal(t.Value, &c)
case "seek":
var c CmdSeek
return &c, json.Unmarshal(t.Value, &c)
case "volume":
var c CmdVolume
return &c, json.Unmarshal(t.Value, &c)
case "list":
var c CmdList
return &c, json.Unmarshal(t.Value, &c)
case "findadd":
var c CmdFindAdd
return &c, json.Unmarshal(t.Value, &c)
case "play":
var c CmdPlay
return &c, json.Unmarshal(t.Value, &c)
case "stop":
var c CmdStop
return &c, json.Unmarshal(t.Value, &c)
case "next":
var c CmdNext
return &c, json.Unmarshal(t.Value, &c)
case "previous":
var c CmdPrevious
return &c, json.Unmarshal(t.Value, &c)
case "clear":
var c CmdClear
return &c, json.Unmarshal(t.Value, &c)
case "pause":
var c CmdPause
return &c, json.Unmarshal(t.Value, &c)
case "unpause":
var c CmdUnpause
return &c, json.Unmarshal(t.Value, &c)
default:
return nil, errors.New("unknown tag")
}
}
func handle(c *MPD, cmd interface{}) (WSMsg, error) {
// log.Printf("handle %#v", cmd)
switch args := cmd.(type) {
case *CmdLoadDir:
ins, err := c.LSInfo(args.File)
if err != nil {
return nil, err
}
return WSInodes{
ID: args.ID,
Inodes: ins,
}, nil
case *CmdAdd:
return nil, c.PlaylistAdd(args.ID)
case *CmdList:
var (
ls []DBEntry
err error
)
switch args.What {
case "artists":
ls, err = c.Artists(c.artistMode)
case "artistalbums":
ls, err = c.ArtistAlbums(c.artistMode, args.Artist)
case "araltracks":
ls, err = c.ArtistAlbumTracks(c.artistMode, args.Artist, args.Album)
default:
err = fmt.Errorf("unknown what: %q", args.What)
}
if err != nil {
return nil, err
}
return WSList{
ID: args.ID,
List: ls,
}, nil
case *CmdFindAdd:
p := "findadd"
if a := args.Artist; a != "" {
cmd := "artist"
if c.artistMode == ModeAlbumartist {
cmd = "albumartist"
}
if a := args.Artist; a != "" {
p = fmt.Sprintf("%s %s %q", p, cmd, a)
}
}
if a := args.Album; a != "" {
p = fmt.Sprintf("%s album %q", p, a)
}
if t := args.Track; t != "" {
p = fmt.Sprintf("%s title %q", p, t)
}
return nil, c.Write(p)
case *CmdPlayID:
return nil, c.Write(fmt.Sprintf("playid %q", args.ID))
case *CmdTrack:
ls, err := c.search(fmt.Sprintf("file %q", args.File))
if err != nil {
return nil, err
}
if len(ls) == 0 {
return nil, nil
}
return WSTrack{
ID: args.ID,
Track: ls[0],
}, nil
case *CmdSeek:
return nil, c.Write(fmt.Sprintf("seekid %s %d", args.Song, int(args.Seconds)))
case *CmdVolume:
if v := args.Volume; v >= 0 && v <= 100 {
return nil, c.Write(fmt.Sprintf("setvol %d", int(v)))
}
return nil, nil
case *CmdPlay:
return nil, c.Write("play")
case *CmdStop:
return nil, c.Write("stop")
case *CmdNext:
return nil, c.Write("next")
case *CmdPrevious:
return nil, c.Write("previous")
case *CmdClear:
// won't be a playlist update event when it's still playing (MPD
// 0.20.14)
if err := c.Write("stop"); err != nil {
return nil, err
}
return nil, c.Write("clear")
case *CmdPause:
return nil, c.Write("pause 1")
case *CmdUnpause:
return nil, c.Write("pause 0")
default:
return nil, fmt.Errorf("unknown command type: %T", cmd)
}
}