forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener_manager.go
142 lines (126 loc) · 2.83 KB
/
listener_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
package pubnub
import (
"sync"
)
//
type Listener struct {
Status chan *PNStatus
Message chan *PNMessage
Presence chan *PNPresence
}
func NewListener() *Listener {
return &Listener{
Status: make(chan *PNStatus),
Message: make(chan *PNMessage),
Presence: make(chan *PNPresence),
}
}
type ListenerManager struct {
sync.RWMutex
ctx Context
listeners map[*Listener]bool
exitListener chan bool
pubnub *PubNub
}
func newListenerManager(ctx Context, pn *PubNub) *ListenerManager {
return &ListenerManager{
listeners: make(map[*Listener]bool, 2),
ctx: ctx,
exitListener: make(chan bool),
pubnub: pn,
}
}
func (m *ListenerManager) addListener(listener *Listener) {
m.Lock()
m.listeners[listener] = true
m.Unlock()
}
func (m *ListenerManager) removeListener(listener *Listener) {
delete(m.listeners, listener)
}
func (m *ListenerManager) removeAllListeners() {
m.Lock()
for l := range m.listeners {
delete(m.listeners, l)
}
m.Unlock()
}
func (m *ListenerManager) announceStatus(status *PNStatus) {
go func() {
m.RLock()
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceStatus exitListener")
return
case l.Status <- status:
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announceMessage(message *PNMessage) {
go func() {
m.RLock()
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceMessage exitListener")
return
case l.Message <- message:
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announcePresence(presence *PNPresence) {
m.RLock()
for l := range m.listeners {
select {
case l.Presence <- presence:
}
}
m.RUnlock()
}
//
type PNStatus struct {
Category StatusCategory
Operation OperationType
ErrorData error
Error bool
TLSEnabled bool
StatusCode int
UUID string
AuthKey string
Origin string
ClientRequest interface{} // Should be same for non-google environment
AffectedChannels []string
AffectedChannelGroups []string
}
type PNMessage struct {
Message interface{}
UserMetadata interface{}
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
Publisher string
Timetoken int64
}
type PNPresence struct {
Event string
UUID string
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
Occupancy int
Timetoken int64
Timestamp int64
UserMetadata map[string]interface{}
State interface{}
Join []string
Leave []string
Timeout []string
HereNowRefresh bool
}