-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.go
346 lines (296 loc) · 8.33 KB
/
radio.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
// this file deals with the global state of the system
package main
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/himanshub16/upnext-backend/cluster"
"sort"
"sync"
"time"
)
type RadioType string
type HookType string
const (
masterRadio RadioType = "masterRadio"
peerRadio RadioType = "peerRadio"
)
const (
nowPlayingHook HookType = "nowPlaying"
playerTimeHook HookType = "playerTime"
queueHook HookType = "queue"
)
type Radio struct {
radioType RadioType
shm *cluster.SharedMem
running bool
queue []Link
nowPlaying *Link
playerStartTimeSec uint64
playerCurTimeSec uint64
tickResSec time.Duration
queueRefreshDur time.Duration
nextQueueRefreshAt time.Time
queueCapacity int64
nowPlayingHooks map[uuid.UUID](chan interface{})
nowPlayingHooksMutex *sync.Mutex
playerTimeHooks map[uuid.UUID](chan interface{})
playerTimeHooksMutex *sync.Mutex
queueHooks map[uuid.UUID](chan interface{})
queueHooksMutex *sync.Mutex
interrupt chan interface{}
}
func IsValidHookType(htype HookType) bool {
switch htype {
case nowPlayingHook:
return true
case playerTimeHook:
return true
case queueHook:
return true
default:
return false
}
}
// type RadioState struct {
// NowPlaying Link `json:"now_playing"`
// PlayerCurTimeSec uint64 `json:"player_cur_time_sec"`
// Queue []Link `json:"queue"`
// }
var _service Service
func NewRadio(__service Service, shm *cluster.SharedMem) *Radio {
_service = __service
return &Radio{
shm: shm,
running: false,
nowPlaying: nil,
playerCurTimeSec: 0,
playerStartTimeSec: 0,
queue: make([]Link, 0),
tickResSec: 1,
queueRefreshDur: time.Second * 10,
queueCapacity: 5,
nowPlayingHooks: make(map[uuid.UUID](chan interface{})),
nowPlayingHooksMutex: &sync.Mutex{},
playerTimeHooks: make(map[uuid.UUID](chan interface{})),
playerTimeHooksMutex: &sync.Mutex{},
queueHooks: make(map[uuid.UUID](chan interface{})),
queueHooksMutex: &sync.Mutex{},
interrupt: make(chan interface{}, 1),
}
}
func (r *Radio) MasterEngine() {
r.nowPlaying = nil
r.playerCurTimeSec = 0
r.playerStartTimeSec = 0
ticker := time.NewTicker(time.Second * r.tickResSec)
defer ticker.Stop()
for {
select {
case <-r.interrupt:
return
case t := <-ticker.C:
r.singleIteration(t)
}
}
}
func (r *Radio) PeerEngine() {
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
for {
select {
case t := <-ticker.C:
if t.After(r.shm.LastUpdatedAt) {
r.updateStateFromShm()
// fmt.Println("shm updated", r.nowPlaying, r.queue, r.playerCurTimeSec)
r.broadcastUpdate(nowPlayingHook, r.nowPlaying)
r.broadcastUpdate(queueHook, r.queue)
r.broadcastUpdate(playerTimeHook, r.playerCurTimeSec)
}
case <-r.interrupt:
return
// case v := <-r.shm.PeerChan:
// fmt.Println("radio got update to", v.Ts)
// // r.broadcastUpdate(v.Ts, v.Mem)
// // update radio parameters from hook variables
// fmt.Println(r.shm.ReadVar(string(nowPlayingHook)))
}
}
}
func (r *Radio) updateStateFromShm() {
// nowPlaying
np, err := json.Marshal(r.shm.ReadVar(string(nowPlayingHook)))
if err != nil {
fmt.Println("failed to marshal nowplaying")
}
if err = json.Unmarshal(np, &r.nowPlaying); err != nil {
fmt.Println("failed to unmarshal nowplaying")
}
pt, err := json.Marshal(r.shm.ReadVar(string(playerTimeHook)))
if err != nil {
fmt.Println("failed to marshal nowplaying")
}
if err = json.Unmarshal(pt, &r.playerCurTimeSec); err != nil {
fmt.Println("failed to unmarshal playerCurTimeSec")
}
q, err := json.Marshal(r.shm.ReadVar(string(queueHook)))
if err != nil {
fmt.Println("failed to marshal nowplaying")
}
if err = json.Unmarshal(q, &r.queue); err != nil {
fmt.Println("failed to unmarshal queue")
}
}
func (r *Radio) singleIteration(t time.Time) {
if len(r.queue) == 0 ||
t.After(r.nextQueueRefreshAt) {
gotSome := r.refreshQueue()
if gotSome == 0 {
fmt.Println("There are no links available.")
}
} else if r.nowPlaying == nil ||
r.playerCurTimeSec > uint64(r.nowPlaying.Duration) {
// first set the current song as expired
r.nowPlaying = &r.queue[0]
r.playerStartTimeSec = uint64(t.Unix())
r.queue = r.queue[1:len(r.queue)]
r.nowPlaying.IsExpired = true
_service.UpdateLink(*r.nowPlaying)
// r.broadcastUpdate(nowPlayingHook, *r.nowPlaying)
r.shm.WriteVar(string(nowPlayingHook), *r.nowPlaying, true)
fmt.Println("now playing changed to", r.nowPlaying.LinkID)
// r.curState.NowPlaying = *r.nowPlaying
// r.curState.PlayerCurTimeSec = r.playerCurTimeSec
// r.curState.Queue = r.queue
// r.broadcastUpdate()
}
r.ReorderQueue()
// r.broadcastUpdate(queueHook, r.queue)
r.shm.WriteVar(string(queueHook), r.queue, true)
if r.nowPlaying != nil {
r.playerCurTimeSec = uint64(t.Unix()) - r.playerStartTimeSec
// r.broadcastUpdate(playerTimeHook, r.playerCurTimeSec)
r.shm.WriteVar(string(playerTimeHook), r.playerCurTimeSec, true)
fmt.Println(t.Unix(), r.nowPlaying.LinkID, r.playerCurTimeSec)
if r.playerCurTimeSec > uint64(r.nowPlaying.Duration) {
r.nowPlaying = nil
}
} else {
r.playerCurTimeSec = 1 << 30
// r.broadcastUpdate(playerTimeHook, r.playerCurTimeSec)
r.shm.WriteVar(string(playerTimeHook), r.playerCurTimeSec, true)
}
r.broadcastUpdate(nowPlayingHook, r.nowPlaying)
r.broadcastUpdate(queueHook, r.queue)
r.broadcastUpdate(playerTimeHook, r.playerCurTimeSec)
}
func (r *Radio) Start() {
if r.radioType == masterRadio {
// start an asynchronous radio which manages player state with time
r.MasterEngine()
} else if r.radioType == peerRadio {
// just subscribe to whatever channel is available
r.PeerEngine()
}
}
func (r *Radio) SwitchMode(newMode RadioType) {
if r.running {
r.Shutdown()
}
r.radioType = newMode
go r.Start()
r.running = true
}
func (r *Radio) refreshQueue() int {
r.queue = _service.GetAllLinks(5)
r.nextQueueRefreshAt = time.Now().Add(r.queueRefreshDur)
return len(r.queue)
}
func (r *Radio) ReorderQueue() {
// update total votes for all links in queue
linkIDs := make([]int64, len(r.queue))
for i, l := range r.queue {
linkIDs[i] = l.LinkID
}
totalVotes := service.GetTotalVoteForLinks(linkIDs)
for i, l := range r.queue {
if score, ok := totalVotes[l.LinkID]; ok {
r.queue[i].TotalVotes = score
} else {
r.queue[i].TotalVotes = 0
}
}
// sort on the basis of total votes
sort.Slice(r.queue, func(i, j int) bool {
return r.queue[i].TotalVotes > r.queue[j].TotalVotes
})
}
func (r *Radio) Shutdown() {
// close engine
r.interrupt <- true
// clean and close all channels
for id := range r.nowPlayingHooks {
close(r.nowPlayingHooks[id])
}
for id := range r.queueHooks {
close(r.queueHooks[id])
}
for id := range r.playerTimeHooks {
close(r.playerTimeHooks[id])
}
}
func (r *Radio) broadcastUpdate(htype HookType, msg interface{}) {
fmt.Println("update to broadcast ", htype)
switch htype {
case nowPlayingHook:
for id := range r.nowPlayingHooks {
// already a struct / can be marshalled to json
r.nowPlayingHooks[id] <- msg
}
case queueHook:
for id := range r.queueHooks {
// already a struct / can be marshalled to json
r.queueHooks[id] <- msg
}
case playerTimeHook:
for id := range r.playerTimeHooks {
// already a struct / can be marshalled to json
r.playerTimeHooks[id] <- msg
}
}
}
func (r *Radio) RegisterHook(htype HookType) (uuid.UUID, chan interface{}) {
id := uuid.New()
c := make(chan interface{})
switch htype {
case nowPlayingHook:
r.nowPlayingHooksMutex.Lock()
r.nowPlayingHooks[id] = c
defer r.nowPlayingHooksMutex.Unlock()
case queueHook:
r.queueHooksMutex.Lock()
r.queueHooks[id] = c
defer r.queueHooksMutex.Unlock()
case playerTimeHook:
r.playerTimeHooksMutex.Lock()
r.playerTimeHooks[id] = c
defer r.playerTimeHooksMutex.Unlock()
}
return id, c
}
func (r *Radio) DeregisterHook(htype HookType, id uuid.UUID) {
switch htype {
case nowPlayingHook:
r.nowPlayingHooksMutex.Lock()
delete(r.nowPlayingHooks, id)
defer r.nowPlayingHooksMutex.Unlock()
case queueHook:
r.queueHooksMutex.Lock()
delete(r.queueHooks, id)
defer r.queueHooksMutex.Unlock()
case playerTimeHook:
r.playerTimeHooksMutex.Lock()
delete(r.playerTimeHooks, id)
defer r.playerTimeHooksMutex.Unlock()
}
}