-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathremotedb.go
563 lines (452 loc) · 13.4 KB
/
remotedb.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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package prolink
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"net"
"sync"
"time"
)
// ErrDeviceNotLinked is returned by RemoteDB if the device being queried is
// not currently 'linked' on the network.
var ErrDeviceNotLinked = fmt.Errorf("The device is not linked on the network")
// allowedDevices specify what device types act as a remote DB server
var allowedDevices = map[DeviceType]bool{
DeviceTypeRB: true,
DeviceTypeCDJ: true,
}
// rbDBServerQueryPort is the consistent port on which we can query the remote
// db server for the port to connect to to communicate with it.
const rbDBServerQueryPort = 12523
// getRemoteDBServerAddr queries the remote device for the port that the remote
// database server is listening on for requests.
func getRemoteDBServerAddr(deviceIP net.IP) (string, error) {
addr := fmt.Sprintf("%s:%d", deviceIP, rbDBServerQueryPort)
conn, err := net.Dial("tcp", addr)
if err != nil {
return "", err
}
defer conn.Close()
parts := [][]byte{
{0x00, 0x00, 0x00, 0x0f},
[]byte("RemoteDBServer"),
{0x00},
}
queryPacket := bytes.Join(parts, nil)
// Request for the port
_, err = conn.Write(queryPacket)
if err != nil {
return "", fmt.Errorf("Failed to query remote DB Server port: %s", err)
}
// Read request response, should be a two byte uint16
data := make([]byte, 2)
_, err = conn.Read(data)
if err != nil {
return "", fmt.Errorf("Failed to retrieve remote DB Server port: %s", err)
}
port := binary.BigEndian.Uint16(data)
return fmt.Sprintf("%s:%d", deviceIP, port), nil
}
type deviceConnection struct {
remoteDB *RemoteDB
device *Device
lock *sync.Mutex
conn net.Conn
txCount uint32
timeout time.Duration
retryEvery time.Duration
disconnect chan bool
}
// connect attempts to open a TCP socket connection to the device. This will
// send the necessary packet sequence in order start communicating with the
// database server once connected.
func (dc *deviceConnection) connect() error {
addr, err := getRemoteDBServerAddr(dc.device.IP)
if err != nil {
return err
}
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
if err := conn.SetDeadline(time.Now().Add(dc.timeout)); err != nil {
return err
}
// Begin connection to the remote database
preamble := fieldNumber04(0x01)
if _, err = conn.Write(preamble.bytes()); err != nil {
return fmt.Errorf("Failed to connect to remote database: %s", err)
}
// No need to keep this response, but it should be a uin32 field, which is
// 5 bytes in length. Discard it.
io.CopyN(ioutil.Discard, conn, 5)
introPacket := &introducePacket{
deviceID: dc.remoteDB.deviceID,
}
if _, err = conn.Write(introPacket.bytes()); err != nil {
return fmt.Errorf("Failed to connect to remote database: %s", err)
}
if _, err := readMessagePacket(conn); err != nil {
return err
}
dc.conn = conn
return nil
}
func (dc *deviceConnection) tryConnect(ticker *time.Ticker) bool {
select {
case <-dc.disconnect:
return true
case <-ticker.C:
return dc.connect() == nil
}
}
func (dc *deviceConnection) ensureConnect() {
dc.disconnect = make(chan bool, 1)
ticker := time.NewTicker(dc.retryEvery)
// Attempt to immediately connect
dc.connect()
for dc.conn == nil && !dc.tryConnect(ticker) {
}
ticker.Stop()
}
// Open begins attempting to connect to the device. If we're unable to connect
// to the device we will retry until the deviceConnection is closed.
func (dc *deviceConnection) Open() {
go dc.ensureConnect()
}
// Close stops any attempts to connect to the device or closes any open socket
// connections with the device.
func (dc *deviceConnection) Close() {
if dc.disconnect != nil {
dc.disconnect <- true
close(dc.disconnect)
}
if dc.conn != nil {
dc.conn.Close()
dc.conn = nil
}
}
// Read implements the Reader interface and will read from the device. This
// updates the read deadline of the connection.
func (dc *deviceConnection) Read(p []byte) (n int, err error) {
dc.conn.SetReadDeadline(time.Now().Add(dc.timeout))
return dc.conn.Read(p)
}
// Write implements the Writer interface and will write to the device. This
// updates the write deadline of the connection.
func (dc *deviceConnection) Write(p []byte) (n int, err error) {
dc.conn.SetWriteDeadline(time.Now().Add(dc.timeout))
return dc.conn.Write(p)
}
// Track contains track information retrieved from the remote database.
type Track struct {
ID uint32
Path string
Title string
Artist string
Album string
Label string
Genre string
Comment string
Key string
Length time.Duration
DateAdded time.Time
Artwork []byte
}
func (t Track) String() string {
format := `Track %d
Path: %s
Title: %s
Artist: %s
Album: %s
Label: %s
Genre: %s
Comment: %s
Key: %s
Length: %s
Added: %s
HasArt: %t`
return fmt.Sprintf(format,
t.ID,
t.Path,
t.Title,
t.Artist,
t.Album,
t.Label,
t.Genre,
t.Comment,
t.Key,
t.Length,
t.DateAdded,
len(t.Artwork) > 0,
)
}
// TrackKey is used to make queries for track metadata.
type TrackKey struct {
TrackID uint32
Slot TrackSlot
Type TrackType
DeviceID DeviceID
// artworkID will be filled in after the track metadata is queried, this
// feild will be needed to lookup the track artwork.
artworkID uint32
}
// NewTrackKey constructs a TrackKey from human friendly representation of
// track keys. Slot and Type are strings, device and trackID are ints.
func NewTrackKey(id int, slot, trackType string, deviceID int) *TrackKey {
tk := TrackKey{
TrackID: uint32(id),
Slot: labelsTrackSlot[slot],
Type: labelsTrackType[trackType],
DeviceID: DeviceID(deviceID),
}
return &tk
}
// RemoteDB provides an interface to talking to the remote database.
type RemoteDB struct {
deviceID DeviceID
conns map[DeviceID]*deviceConnection
connsLock *sync.Mutex
}
// IsLinked reports weather the DB server is available for the given device.
func (rd *RemoteDB) IsLinked(devID DeviceID) bool {
devConn, ok := rd.conns[devID]
return ok && devConn.conn != nil
}
// GetTrack queries the remote db for track details given a track ID.
func (rd *RemoteDB) GetTrack(q *TrackKey) (*Track, error) {
if !rd.IsLinked(q.DeviceID) {
return nil, ErrDeviceNotLinked
}
track, err := rd.executeQuery(q)
// Refresh the connection if we EOF while querying the server
if err != nil && err == io.EOF {
Log.Warn("Got EOF while querying server. Reconnecting", "device", q.DeviceID)
rd.refreshConnection(rd.conns[q.DeviceID].device)
}
return track, err
}
func (rd *RemoteDB) executeQuery(q *TrackKey) (*Track, error) {
// Synchronize queries as not to disrupt the query flow. We could probably
// be a little more precise about where the locks are, but for now the
// entire query is "pretty fast", just lock the whole thing.
lock := rd.conns[q.DeviceID].lock
lock.Lock()
defer lock.Unlock()
if _, ok := rd.conns[q.DeviceID]; !ok {
return nil, fmt.Errorf("Device disconnected during query")
}
track, err := rd.queryTrackMetadata(q)
if err != nil {
return nil, err
}
path, err := rd.queryTrackPath(q)
if err != nil {
return nil, err
}
track.Path = path
artwork, err := rd.getArtwork(q)
if err != nil {
return nil, err
}
track.Artwork = artwork
return track, nil
}
// queryTrackMetadata queries the remote database for various metadata about a
// track, returning a sparse Track object. The track Path and Artwork must be
// looked up as separate queries.
//
// Note that the Artwork ID is populated into the passed TrackKey after this
// call completes.
func (rd *RemoteDB) queryTrackMetadata(q *TrackKey) (*Track, error) {
trackID := make([]byte, 4)
binary.BigEndian.PutUint32(trackID, q.TrackID)
getMetadata := &metadataRequestPacket{
deviceID: rd.deviceID,
slot: q.Slot,
trackType: q.Type,
trackID: q.TrackID,
}
renderRequest := &renderRequestPacket{
deviceID: rd.deviceID,
slot: q.Slot,
trackType: q.Type,
offset: 0,
limit: 32,
}
items, err := rd.getMenuItems(q.DeviceID, getMetadata, renderRequest)
if err != nil {
return nil, err
}
q.artworkID = items[itemTypeTitle].artworkID
duration := time.Duration(items.getNum(itemTypeDuration)) * time.Second
track := &Track{
ID: q.TrackID,
Title: items.getText(itemTypeTitle),
Artist: items.getText(itemTypeArtist),
Album: items.getText(itemTypeAlbum),
Comment: items.getText(itemTypeComment),
Key: items.getText(itemTypeKey),
Genre: items.getText(itemTypeGenre),
Label: items.getText(itemTypeLabel),
Length: duration,
}
return track, nil
}
// queryTrackPath looks up the file path of a track in rekordbox.
func (rd *RemoteDB) queryTrackPath(q *TrackKey) (string, error) {
trackID := make([]byte, 4)
binary.BigEndian.PutUint32(trackID, q.TrackID)
trackInfoRequest := &trackInfoRequestPacket{
deviceID: rd.deviceID,
slot: q.Slot,
trackType: q.Type,
trackID: q.TrackID,
}
renderRequest := &renderRequestPacket{
renderType: renderSystem,
deviceID: rd.deviceID,
slot: q.Slot,
trackType: q.Type,
offset: 0,
limit: 32,
}
items, err := rd.getMenuItems(q.DeviceID, trackInfoRequest, renderRequest)
if err != nil {
return "", err
}
return items.getText(itemTypePath), nil
}
// getMenuItems is used to query a list of menu items. It returns a mapping of
// the menu itemType byte to the menu item packet object.
func (rd *RemoteDB) getMenuItems(devID DeviceID, p1, p2 messagePacket) (menuItems, error) {
if err := rd.sendMessage(devID, p1); err != nil {
return nil, err
}
resp, err := readMessagePacket(rd.conns[devID])
if err != nil {
return nil, err
}
if resp.messageType != msgTypeResponse {
return nil, fmt.Errorf("Invalid menu items request, got response type %#x", resp.messageType)
}
if err := rd.sendMessage(devID, p2); err != nil {
return nil, err
}
// Add 2 for the menu header / footer
entryCount := int(resp.arguments[1].(fieldNumber04)) + 2
items := map[byte]*menuItem{}
for i := 0; i < entryCount; i++ {
entry, err := readMessagePacket(rd.conns[devID])
if err != nil {
return nil, err
}
if entry.messageType != msgTypeMenuItem {
continue
}
item := makeMenuItem(entry)
items[item.itemType] = item
}
return menuItems(items), nil
}
// getArtwork requests artwork of a specific ID from the remote database.
func (rd *RemoteDB) getArtwork(q *TrackKey) ([]byte, error) {
artworkRequest := &requestArtwork{
deviceID: rd.deviceID,
slot: q.Slot,
trackType: q.Type,
artworkID: q.artworkID,
}
if err := rd.sendMessage(q.DeviceID, artworkRequest); err != nil {
return nil, err
}
resp, err := readMessagePacket(rd.conns[q.DeviceID])
if err != nil {
return nil, err
}
if resp.messageType != msgTypeArtwork {
return nil, fmt.Errorf("Invalid artwork request, got response type %#x", resp.messageType)
}
return []byte(resp.arguments[3].(fieldBinary)), nil
}
// sendMessage writes a message packet to the open connection and increments
// the transaction counter.
func (rd *RemoteDB) sendMessage(devID DeviceID, m messagePacket) error {
devConn := rd.conns[devID]
Log.Debug("Sending packet", "packet", m)
m.setTransactionID(devConn.txCount)
if _, err := devConn.Write(m.bytes()); err != nil {
return err
}
devConn.txCount++
return nil
}
// openConnection initializes a new deviceConnection for the specified device.
func (rd *RemoteDB) openConnection(dev *Device) {
if _, ok := allowedDevices[dev.Type]; !ok {
return
}
Log.Info("Opening Remote DB connection", "target", dev)
conn := &deviceConnection{
remoteDB: rd,
device: dev,
lock: &sync.Mutex{},
txCount: 1,
retryEvery: 5 * time.Second,
timeout: 5 * time.Second,
}
conn.Open()
rd.connsLock.Lock()
defer rd.connsLock.Unlock()
rd.conns[dev.ID] = conn
}
// closeConnection closes the active connection for the specified device.
func (rd *RemoteDB) closeConnection(dev *Device) {
if _, ok := rd.conns[dev.ID]; !ok {
return
}
rd.conns[dev.ID].Close()
rd.connsLock.Lock()
defer rd.connsLock.Unlock()
delete(rd.conns, dev.ID)
}
// refreshConnection attempts to reconnect to the specified device.
func (rd *RemoteDB) refreshConnection(dev *Device) {
rd.closeConnection(dev)
rd.openConnection(dev)
}
// setRequestingDeviceID specifies what device ID the requests to the remote DB
// servers should identify themselves as.
func (rd *RemoteDB) setRequestingDeviceID(deviceID DeviceID) {
rd.deviceID = deviceID
}
// activate begins actively listening for devices on the network hat support
// remote database queries to be added to the PRO DJ LINK network. This
// maintains adding and removing of device connections.
func (rd *RemoteDB) activate(dm *DeviceManager) {
// Connect to already active devices on the network
for _, dev := range dm.ActiveDeviceMap() {
rd.openConnection(dev)
}
key := fmt.Sprintf("remotedb_%d", rd.deviceID)
dm.OnDeviceAdded(key, DeviceListenerFunc(rd.openConnection))
dm.OnDeviceRemoved(key, DeviceListenerFunc(rd.closeConnection))
}
// deactivate closes any open remote DB connections and stops waiting to
// connect to new devices that appear on the network.
func (rd *RemoteDB) deactivate(dm *DeviceManager) {
key := fmt.Sprintf("remotedb_%d", rd.deviceID)
dm.RemoveListener(key, DeviceListenerFunc(rd.openConnection))
dm.RemoveListener(key, DeviceListenerFunc(rd.closeConnection))
for _, conn := range rd.conns {
rd.closeConnection(conn.device)
}
}
func newRemoteDB() *RemoteDB {
return &RemoteDB{
conns: map[DeviceID]*deviceConnection{},
connsLock: &sync.Mutex{},
}
}