forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener_manager.go
363 lines (321 loc) · 8.82 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
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
package pubnub
import (
"sync"
)
// Listener type has all the `types` of response events
type Listener struct {
Status chan *PNStatus
Message chan *PNMessage
Presence chan *PNPresence
Signal chan *PNMessage
UUIDEvent chan *PNUUIDEvent
ChannelEvent chan *PNChannelEvent
MembershipEvent chan *PNMembershipEvent
MessageActionsEvent chan *PNMessageActionsEvent
File chan *PNFilesEvent
}
// NewListener initates the listener to facilitate the event handling
func NewListener() *Listener {
return &Listener{
Status: make(chan *PNStatus),
Message: make(chan *PNMessage),
Presence: make(chan *PNPresence),
Signal: make(chan *PNMessage),
UUIDEvent: make(chan *PNUUIDEvent),
ChannelEvent: make(chan *PNChannelEvent),
MembershipEvent: make(chan *PNMembershipEvent),
MessageActionsEvent: make(chan *PNMessageActionsEvent),
File: make(chan *PNFilesEvent),
}
}
// ListenerManager is used in the internal handling of listeners.
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) {
m.pubnub.Config.Log.Println("before removeListener")
m.Lock()
m.pubnub.Config.Log.Println("in removeListener lock")
delete(m.listeners, listener)
m.Unlock()
m.pubnub.Config.Log.Println("after removeListener")
}
func (m *ListenerManager) removeAllListeners() {
m.pubnub.Config.Log.Println("in removeAllListeners")
m.Lock()
lis := m.listeners
for l := range lis {
delete(m.listeners, l)
}
m.Unlock()
}
func (m *ListenerManager) copyListeners() map[*Listener]bool {
m.Lock()
lis := make(map[*Listener]bool)
for k, v := range m.listeners {
lis[k] = v
}
m.Unlock()
return lis
}
func (m *ListenerManager) announceStatus(status *PNStatus) {
go func() {
lis := m.copyListeners()
AnnounceStatusLabel:
for l := range lis {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceStatus exitListener")
break AnnounceStatusLabel
case l.Status <- status:
}
}
m.pubnub.Config.Log.Println("announceStatus exit")
}()
}
func (m *ListenerManager) announceMessage(message *PNMessage) {
go func() {
lis := m.copyListeners()
AnnounceMessageLabel:
for l := range lis {
select {
case <-m.exitListenerAnnounce:
m.pubnub.Config.Log.Println("announceMessage exitListenerAnnounce")
break AnnounceMessageLabel
case l.Message <- message:
}
}
}()
}
func (m *ListenerManager) announceSignal(message *PNMessage) {
go func() {
lis := m.copyListeners()
AnnounceSignalLabel:
for l := range lis {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceSignal exitListener")
break AnnounceSignalLabel
case l.Signal <- message:
}
}
}()
}
func (m *ListenerManager) announceUUIDEvent(message *PNUUIDEvent) {
go func() {
lis := m.copyListeners()
AnnounceUUIDEventLabel:
for l := range lis {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceUUIDEvent exitListener")
break AnnounceUUIDEventLabel
case l.UUIDEvent <- message:
m.pubnub.Config.Log.Println("l.UUIDEvent", message)
}
}
}()
}
func (m *ListenerManager) announceChannelEvent(message *PNChannelEvent) {
go func() {
lis := m.copyListeners()
AnnounceChannelEventLabel:
for l := range lis {
m.pubnub.Config.Log.Println("l.ChannelEvent", l)
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceChannelEvent exitListener")
break AnnounceChannelEventLabel
case l.ChannelEvent <- message:
m.pubnub.Config.Log.Println("l.ChannelEvent", message)
}
}
}()
}
func (m *ListenerManager) announceMembershipEvent(message *PNMembershipEvent) {
go func() {
lis := m.copyListeners()
AnnounceMembershipEvent:
for l := range lis {
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)
}
}
}()
}
func (m *ListenerManager) announceMessageActionsEvent(message *PNMessageActionsEvent) {
go func() {
lis := m.copyListeners()
AnnounceMessageActionsEvent:
for l := range lis {
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)
}
}
}()
}
func (m *ListenerManager) announcePresence(presence *PNPresence) {
go func() {
lis := m.copyListeners()
AnnouncePresenceLabel:
for l := range lis {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announcePresence exitListener")
break AnnouncePresenceLabel
case l.Presence <- presence:
}
}
}()
}
func (m *ListenerManager) announceFile(file *PNFilesEvent) {
go func() {
lis := m.copyListeners()
AnnounceFileLabel:
for l := range lis {
select {
case <-m.exitListener:
m.pubnub.Config.Log.Println("announceFile exitListener")
break AnnounceFileLabel
case l.File <- file:
}
}
}()
}
// 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
}
// PNUUIDEvent is the Response for an User Event
type PNUUIDEvent struct {
Event PNObjectsEvent
UUID string
Description string
Timestamp string
Name string
ExternalID string
ProfileURL string
Email string
Updated string
ETag string
Custom map[string]interface{}
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
}
// PNChannelEvent is the Response for a Space Event
type PNChannelEvent struct {
Event PNObjectsEvent
ChannelID string
Description string
Timestamp string
Name 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
UUID string
ChannelID 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
}
// PNFilesEvent is the Response for a Files Event
type PNFilesEvent struct {
File PNFileMessageAndDetails
UserMetadata interface{}
SubscribedChannel string
ActualChannel string
Channel string
Subscription string
Publisher string
Timetoken int64
}