-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelay_manager.go
383 lines (300 loc) · 7.85 KB
/
relay_manager.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
package moqt
import (
"context"
"errors"
"io"
"log/slog"
"sync"
"sync/atomic"
)
type RelayManager interface {
RelayAnnouncements(ServerSession, Interest) error
/*
* Serve subscription to the relay manager
*/
RelayTrack(ServerSession, Subscription) error
TrackManager
}
func NewRelayManager() RelayManager {
return &relayManager{
trackTree: newTrackTree(),
}
}
var _ RelayManager = (*relayManager)(nil)
type relayManager struct {
//
trackTree *trackTree
trackManager
}
func (manager *relayManager) RelayAnnouncements(sess ServerSession, interest Interest) error {
annstr, err := sess.OpenAnnounceStream(interest)
if err != nil {
return err
}
go func() {
for {
// Receive announcements
ann, err := annstr.ReceiveAnnouncements()
if err != nil {
slog.Error("failed to receive announcements", slog.String("error", err.Error()))
return
}
// Serve announcements
err = manager.ServeAnnouncements(ann)
if err != nil {
slog.Error("failed to serve announcements", slog.String("error", err.Error()))
return
}
}
}()
return nil
}
func (manager *relayManager) RelayTrack(sess ServerSession, sub Subscription) error {
substr, err := sess.OpenSubscribeStream(sub)
if err != nil {
slog.Error("failed to open a subscribe stream", slog.String("error", err.Error()))
return err
}
// Create a track buffer
trackBuf := NewTrackBuffer(sub)
// Serve the subscription
err = manager.ServeTrack(sub, trackBuf)
if err != nil {
slog.Error("failed to serve the subscription", slog.String("error", err.Error()))
return err
}
go func() {
for {
// Receive data
stream, err := sess.AcceptDataStream(substr, context.Background())
if err != nil {
slog.Error("failed to receive data", slog.String("error", err.Error()))
return
}
// Initialize a group buffer
groupBuf := NewGroupBuffer(stream.GroupSequence(), stream.GroupPriority())
// Add the group buffer to the track buffer
trackBuf.AddGroup(groupBuf)
// Receive data from the stream
go func(stream ReceiveDataStream) {
/*
* Receive data from the stream
*/
for {
// Receive the next frame
frame, err := stream.NextFrame()
// Store the frame to the group buffer
if len(frame) > 0 {
groupBuf.Write(frame)
}
if err != nil {
if err == io.EOF {
groupBuf.Close()
} else {
slog.Error("failed to receive a frame", slog.String("error", err.Error()))
}
return
}
}
}(stream)
}
}()
return nil
}
func newTrackTree() *trackTree {
return &trackTree{
rootNode: &trackPrefixNode{
trackPrefix: "",
children: make(map[string]*trackPrefixNode),
},
}
}
type trackTree struct {
rootNode *trackPrefixNode
}
func (tree trackTree) insertTrackPrefix(trackPrefixParts []string) *trackPrefixNode {
return tree.rootNode.addDescendants(trackPrefixParts)
}
func (tree trackTree) removeTrackPrefix(trackPrefixParts []string) error {
_, err := tree.rootNode.removeDescendants(trackPrefixParts)
return err
}
func (tree trackTree) traceTrackPrefix(trackPrefixParts []string) (*trackPrefixNode, bool) {
return tree.rootNode.traceDescendant(trackPrefixParts)
}
func (tree trackTree) handleDescendants(trackPrefixParts []string, op func(*trackPrefixNode) error) error {
return tree.rootNode.handleDescendants(trackPrefixParts, op)
}
func (trackPrefixNode *trackPrefixNode) handleDescendants(trackPrefixParts []string, op func(*trackPrefixNode) error) error {
err := op(trackPrefixNode)
if err != nil {
return err
}
if len(trackPrefixParts) == 0 {
return nil
}
node := trackPrefixNode.addDescendants(trackPrefixParts[0:1])
return node.handleDescendants(trackPrefixParts[1:], op)
}
type trackPrefixNode struct {
trackPrefix string
/*
* Children of the node
*/
children map[string]*trackPrefixNode
cdMu sync.RWMutex
/*
* Announcer
*/
announcementBuffer announcementBuffer
/*
* Track node
*/
track *trackNameNode
}
func (node *trackPrefixNode) addDescendants(trackPrefixParts []string) *trackPrefixNode {
if node == nil {
return nil
}
if len(trackPrefixParts) == 0 {
return node
}
trackPrefixPart := trackPrefixParts[0]
node.cdMu.RLock()
defer node.cdMu.RUnlock()
child, exists := node.children[trackPrefixPart]
if !exists {
child = &trackPrefixNode{
trackPrefix: node.trackPrefix + "/" + trackPrefixPart,
children: make(map[string]*trackPrefixNode),
announcementBuffer: newAnnouncementBuffer(),
}
node.children[trackPrefixPart] = child
}
return child.addDescendants(trackPrefixParts[1:])
}
func (node *trackPrefixNode) removeDescendants(trackPrefixParts []string) (bool, error) {
if node == nil {
return false, errors.New("track namespace not found at " + trackPrefixParts[0])
}
if len(trackPrefixParts) == 0 {
if len(node.children) == 0 {
return true, nil
}
return false, nil
}
value := trackPrefixParts[0]
node.cdMu.RLock()
defer node.cdMu.RUnlock()
child, exists := node.children[value]
if !exists {
return false, errors.New("track namespace not found at " + value)
}
ok, err := child.removeDescendants(trackPrefixParts[1:])
if err != nil {
return false, err
}
if ok {
delete(node.children, value)
return (len(node.children) == 0), nil
}
return false, nil
}
func (node *trackPrefixNode) traceDescendant(trackPrefixParts []string) (*trackPrefixNode, bool) {
if len(trackPrefixParts) == 0 {
return node, true
}
node.cdMu.RLock()
defer node.cdMu.RUnlock()
child, exists := node.children[trackPrefixParts[0]]
if !exists || child == nil {
return nil, false
}
return child.traceDescendant(trackPrefixParts[1:])
}
func (node *trackPrefixNode) initTrack() {
node.cdMu.Lock()
defer node.cdMu.Unlock()
if node.track == nil {
node.track = &trackNameNode{
trackPath: node.trackPrefix,
}
}
}
/*
* Announcer
*/
func newAnnouncementBuffer() announcementBuffer {
return announcementBuffer{
annCond: sync.NewCond(&sync.Mutex{}),
announcements: make([]Announcement, 0),
}
}
type announcementBuffer struct {
annCond *sync.Cond
announcements []Announcement
waiting uint64
completed uint64
}
func (announcer announcementBuffer) WaitAnnouncements() []Announcement {
announcer.annCond.L.Lock()
defer announcer.annCond.L.Unlock()
atomic.AddUint64(&announcer.waiting, 1)
for len(announcer.announcements) == 0 {
announcer.annCond.Wait()
}
// Clean the buffer if all the waiting clients have received the announcements
if atomic.AddUint64(&announcer.completed, 1) == atomic.LoadUint64(&announcer.waiting) {
announcer.Clean()
atomic.StoreUint64(&announcer.waiting, 0)
atomic.StoreUint64(&announcer.completed, 0)
}
return announcer.announcements
}
func (announcer announcementBuffer) Broadcast() {
announcer.annCond.Broadcast()
}
func (announcer *announcementBuffer) Add(ann Announcement) {
announcer.annCond.L.Lock()
defer announcer.annCond.L.Unlock()
announcer.announcements = append(announcer.announcements, ann)
}
func (announcer *announcementBuffer) Clean() {
announcer.annCond.L.Lock()
defer announcer.annCond.L.Unlock()
announcer.announcements = make([]Announcement, 0)
}
// func (announcer announcementBuffer) WaitAndGet() []Announcement {
// announcer.Wait()
// return announcer.Get()
// }
type trackNameNode struct {
trackPath string
/*
* Session serving the track
*/
sess ServerSession
sessMu sync.Mutex
/*
* Announcement received from the session
*/
announcement Announcement
/*
* Subscription sent to the session
*/
subscription Subscription
/*
* Frame queue
*/
trackBuf *TrackBuffer
mu sync.Mutex
}
func (node *trackNameNode) SetSession(sess ServerSession) error {
node.sessMu.Lock()
defer node.sessMu.Unlock()
if node.sess != nil && node.sess != sess {
slog.Debug("the session serving the track has been changed to another session", slog.String("track path", node.trackPath))
}
node.sess = sess
return nil
}