-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.go
66 lines (57 loc) · 1.47 KB
/
subscribe.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
// Copyright 2021-2023 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Peers changes Subscribe module of Webrts server package
package teowebrtc_server
import (
"sync"
"github.com/teonet-go/teowebrtc_client"
)
// subscribe data structure and method receiver
type subscribe struct {
subscribeID int
subscribeMap
*sync.RWMutex
}
type subscribeMap map[int]subscribeData
type subscribeData struct {
peer string
dc *teowebrtc_client.DataChannel
f func()
}
// Init subscribe object
func (s *subscribe) Init() {
s.subscribeMap = make(subscribeMap)
s.RWMutex = new(sync.RWMutex)
}
// Add function to subscribe and return subscribe ID
func (s *subscribe) add(peer string, dc *teowebrtc_client.DataChannel, f func()) int {
s.Lock()
defer s.Unlock()
s.subscribeID++
s.subscribeMap[s.subscribeID] = subscribeData{peer, dc, f}
return s.subscribeID
}
// Delete from subscribe by ID or Peer name
func (s *subscribe) del(id interface{}, dc ...*teowebrtc_client.DataChannel) {
s.Lock()
defer s.Unlock()
switch v := id.(type) {
case int:
delete(s.subscribeMap, v)
case string:
for id, md := range s.subscribeMap {
if md.peer == v && len(dc) > 0 && md.dc == dc[0] {
delete(s.subscribeMap, id)
}
}
}
}
// Process all functions in subscribe map
func (s *subscribe) process() {
s.RLock()
defer s.RUnlock()
for _, sd := range s.subscribeMap {
sd.f()
}
}