-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener_manager.go
319 lines (289 loc) · 7.75 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
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
package pubnub
import (
"sync"
)
//
type Listener struct {
Status chan *PNStatus
Message chan *PNMessage
Presence chan *PNPresence
Signal chan *PNMessage
UserEvent chan *PNUserEvent
SpaceEvent chan *PNSpaceEvent
MembershipEvent chan *PNMembershipEvent
MessageActionsEvent chan *PNMessageActionsEvent
}
func NewListener() *Listener {
return &Listener{
Status: make(chan *PNStatus),
Message: make(chan *PNMessage),
Presence: make(chan *PNPresence),
Signal: make(chan *PNMessage),
UserEvent: make(chan *PNUserEvent),
SpaceEvent: make(chan *PNSpaceEvent),
MembershipEvent: make(chan *PNMembershipEvent),
MessageActionsEvent: make(chan *PNMessageActionsEvent),
}
}
type ListenerManager struct {
sync.RWMutex
ctx Context
listeners map[*Listener]bool
exitListener chan bool
exitListenerAnnounce 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),
exitListenerAnnounce: 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()
m.pubnub.Config.Log.Println("in removeAllListeners")
for l := range m.listeners {
delete(m.listeners, l)
}
m.Unlock()
}
func (m *ListenerManager) announceStatus(status *PNStatus) {
go func() {
m.RLock()
m.pubnub.Config.Log.Println("announceStatus lock")
AnnounceStatusLabel:
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceStatus exitListener")
break AnnounceStatusLabel
case l.Status <- status:
}
}
m.pubnub.Config.Log.Println("announceStatus unlock")
m.RUnlock()
m.pubnub.Config.Log.Println("announceStatus exit")
}()
}
func (m *ListenerManager) announceMessage(message *PNMessage) {
go func() {
m.RLock()
AnnounceMessageLabel:
for l := range m.listeners {
select {
case <-m.exitListenerAnnounce:
m.pubnub.Config.Log.Println("announceMessage exitListenerAnnounce")
break AnnounceMessageLabel
case l.Message <- message:
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announceSignal(message *PNMessage) {
go func() {
m.RLock()
AnnounceSignalLabel:
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceSignal exitListener")
break AnnounceSignalLabel
case l.Signal <- message:
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announceUserEvent(message *PNUserEvent) {
go func() {
m.RLock()
AnnounceUserEventLabel:
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceUserEvent exitListener")
break AnnounceUserEventLabel
case l.UserEvent <- message:
m.pubnub.Config.Log.Println("l.UserEvent", message)
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announceSpaceEvent(message *PNSpaceEvent) {
go func() {
m.RLock()
AnnounceSpaceEventLabel:
for l := range m.listeners {
m.pubnub.Config.Log.Println("l.SpaceEvent", l)
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceSpaceEvent exitListener")
break AnnounceSpaceEventLabel
case l.SpaceEvent <- message:
m.pubnub.Config.Log.Println("l.SpaceEvent", message)
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announceMembershipEvent(message *PNMembershipEvent) {
go func() {
m.RLock()
AnnounceMembershipEvent:
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceMembershipEvent exitListener")
break AnnounceMembershipEvent
case l.MembershipEvent <- message:
m.pubnub.Config.Log.Println("l.MembershipEvent", message)
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announceMessageActionsEvent(message *PNMessageActionsEvent) {
go func() {
m.RLock()
AnnounceMessageActionsEvent:
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceMessageActionsEvent exitListener")
break AnnounceMessageActionsEvent
case l.MessageActionsEvent <- message:
m.pubnub.Config.Log.Println("l.MessageActionsEvent", message)
}
}
m.RUnlock()
}()
}
func (m *ListenerManager) announcePresence(presence *PNPresence) {
go func() {
m.RLock()
AnnouncePresenceLabel:
for l := range m.listeners {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announcePresence exitListener")
break AnnouncePresenceLabel
case l.Presence <- presence:
}
}
m.RUnlock()
}()
}
// PNStatus is the status struct
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
}
// PNMessage is the Message Response for Subscribe
type PNMessage struct {
Message interface{}
UserMetadata interface{}
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
Publisher string
Timetoken int64
}
// PNPresence is the Message Response for Presence
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
}
// PNUserEvent is the Response for an User Event
type PNUserEvent struct {
Event PNObjectsEvent
UserID string
Description string
Timestamp string
Name string
ExternalID string
ProfileURL string
Email string
Created string
Updated string
ETag string
Custom map[string]interface{}
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
}
// PNSpaceEvent is the Response for a Space Event
type PNSpaceEvent struct {
Event PNObjectsEvent
SpaceID string
Description string
Timestamp string
Name string
Created string
Updated string
ETag string
Custom map[string]interface{}
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
}
// PNMembershipEvent is the Response for a Membership Event
type PNMembershipEvent struct {
Event PNObjectsEvent
UserID string
SpaceID string
Description string
Timestamp string
Custom map[string]interface{}
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
}
// PNMessageActionsEvent is the Response for a Message Actions Event
type PNMessageActionsEvent struct {
Event PNMessageActionsEventType
Data PNMessageActionsResponse
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
}