-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubnub.go
368 lines (294 loc) · 9.75 KB
/
pubnub.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
package pubnub
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"sync"
)
// Default constants
const (
// Version :the version of the SDK
Version = "4.1.3"
// MaxSequence for publish messages
MaxSequence = 65535
)
const (
// StrMissingPubKey show Missing Publish Key message
StrMissingPubKey = "Missing Publish Key"
// StrMissingSubKey show Missing Subscribe Key message
StrMissingSubKey = "Missing Subscribe Key"
// StrMissingChannel show Channel message
StrMissingChannel = "Missing Channel"
// StrMissingChannelGroup show Channel Group message
StrMissingChannelGroup = "Missing Channel Group"
// StrMissingMessage show Missing Message message
StrMissingMessage = "Missing Message"
// StrMissingSecretKey show Missing Secret Key message
StrMissingSecretKey = "Missing Secret Key"
// StrMissingUUID show Missing UUID message
StrMissingUUID = "Missing UUID"
// StrMissingDeviceID show Missing Device ID message
StrMissingDeviceID = "Missing Device ID"
// StrMissingPushType show Missing Push Type message
StrMissingPushType = "Missing Push Type"
)
// PubNub No server connection will be established when you create a new PubNub object.
// To establish a new connection use Subscribe() function of PubNub type.
type PubNub struct {
sync.RWMutex
Config *Config
nextPublishSequence int
publishSequenceMutex sync.RWMutex
subscriptionManager *SubscriptionManager
telemetryManager *TelemetryManager
client *http.Client
subscribeClient *http.Client
ctx Context
cancel func()
}
//
func (pn *PubNub) Publish() *publishBuilder {
return newPublishBuilder(pn)
}
func (pn *PubNub) PublishWithContext(ctx Context) *publishBuilder {
return newPublishBuilderWithContext(pn, ctx)
}
func (pn *PubNub) Fire() *fireBuilder {
return newFireBuilder(pn)
}
func (pn *PubNub) FireWithContext(ctx Context) *fireBuilder {
return newFireBuilderWithContext(pn, ctx)
}
func (pn *PubNub) Subscribe() *subscribeBuilder {
return newSubscribeBuilder(pn)
}
func (pn *PubNub) History() *historyBuilder {
return newHistoryBuilder(pn)
}
func (pn *PubNub) HistoryWithContext(ctx Context) *historyBuilder {
return newHistoryBuilderWithContext(pn, ctx)
}
func (pn *PubNub) Fetch() *fetchBuilder {
return newFetchBuilder(pn)
}
func (pn *PubNub) FetchWithContext(ctx Context) *fetchBuilder {
return newFetchBuilderWithContext(pn, ctx)
}
func (pn *PubNub) SetState() *setStateBuilder {
return newSetStateBuilder(pn)
}
func (pn *PubNub) SetStateWithContext(ctx Context) *setStateBuilder {
return newSetStateBuilderWithContext(pn, ctx)
}
func (pn *PubNub) Grant() *grantBuilder {
return newGrantBuilder(pn)
}
func (pn *PubNub) GrantWithContext(ctx Context) *grantBuilder {
return newGrantBuilderWithContext(pn, ctx)
}
func (pn *PubNub) Unsubscribe() *unsubscribeBuilder {
return newUnsubscribeBuilder(pn)
}
func (pn *PubNub) AddListener(listener *Listener) {
pn.subscriptionManager.AddListener(listener)
}
func (pn *PubNub) RemoveListener(listener *Listener) {
pn.subscriptionManager.RemoveListener(listener)
}
func (pn *PubNub) GetListeners() map[*Listener]bool {
return pn.subscriptionManager.GetListeners()
}
func (pn *PubNub) Leave() *leaveBuilder {
return newLeaveBuilder(pn)
}
func (pn *PubNub) LeaveWithContext(ctx Context) *leaveBuilder {
return newLeaveBuilderWithContext(pn, ctx)
}
func (pn *PubNub) heartbeat() *heartbeatBuilder {
return newHeartbeatBuilder(pn)
}
func (pn *PubNub) heartbeatWithContext(ctx Context) *heartbeatBuilder {
return newHeartbeatBuilderWithContext(pn, ctx)
}
// SetClient Set a client for transactional requests
func (pn *PubNub) SetClient(c *http.Client) {
pn.Lock()
pn.client = c
pn.Unlock()
}
// GetClient Get a client for transactional requests
func (pn *PubNub) GetClient() *http.Client {
pn.Lock()
defer pn.Unlock()
if pn.client == nil {
if pn.Config.UseHTTP2 {
pn.client = NewHTTP2Client(pn.Config.ConnectTimeout,
pn.Config.NonSubscribeRequestTimeout)
} else {
pn.client = NewHTTP1Client(pn.Config.ConnectTimeout,
pn.Config.NonSubscribeRequestTimeout)
}
}
return pn.client
}
func (pn *PubNub) SetSubscribeClient(client *http.Client) {
pn.Lock()
pn.subscribeClient = client
pn.Unlock()
}
// GetSubscribeClient Get a client for transactional requests
func (pn *PubNub) GetSubscribeClient() *http.Client {
pn.Lock()
defer pn.Unlock()
if pn.subscribeClient == nil {
if pn.Config.UseHTTP2 {
pn.subscribeClient = NewHTTP2Client(pn.Config.ConnectTimeout,
pn.Config.SubscribeRequestTimeout)
} else {
pn.subscribeClient = NewHTTP1Client(pn.Config.ConnectTimeout,
pn.Config.SubscribeRequestTimeout)
}
}
return pn.subscribeClient
}
func (pn *PubNub) GetSubscribedChannels() []string {
return pn.subscriptionManager.getSubscribedChannels()
}
func (pn *PubNub) GetSubscribedGroups() []string {
return pn.subscriptionManager.getSubscribedGroups()
}
func (pn *PubNub) UnsubscribeAll() {
pn.subscriptionManager.unsubscribeAll()
}
func (pn *PubNub) ListPushProvisions() *listPushProvisionsRequestBuilder {
return newListPushProvisionsRequestBuilder(pn)
}
func (pn *PubNub) ListPushProvisionsWithContext(
ctx Context) *listPushProvisionsRequestBuilder {
return newListPushProvisionsRequestBuilderWithContext(pn, ctx)
}
func (pn *PubNub) AddPushNotificationsOnChannels() *addPushNotificationsOnChannelsBuilder {
return newAddPushNotificationsOnChannelsBuilder(pn)
}
func (pn *PubNub) AddPushNotificationsOnChannelsWithContext(
ctx Context) *addPushNotificationsOnChannelsBuilder {
return newAddPushNotificationsOnChannelsBuilderWithContext(pn, ctx)
}
func (pn *PubNub) RemovePushNotificationsFromChannels() *removeChannelsFromPushBuilder {
return newRemoveChannelsFromPushBuilder(pn)
}
func (pn *PubNub) RemovePushNotificationsFromChannelsWithContext(
ctx Context) *removeChannelsFromPushBuilder {
return newRemoveChannelsFromPushBuilderWithContext(pn, ctx)
}
func (pn *PubNub) RemoveAllPushNotifications() *removeAllPushChannelsForDeviceBuilder {
return newRemoveAllPushChannelsForDeviceBuilder(pn)
}
func (pn *PubNub) RemoveAllPushNotificationsWithContext(
ctx Context) *removeAllPushChannelsForDeviceBuilder {
return newRemoveAllPushChannelsForDeviceBuilderWithContext(pn, ctx)
}
func (pn *PubNub) AddChannelToChannelGroup() *addChannelToChannelGroupBuilder {
return newAddChannelToChannelGroupBuilder(pn)
}
func (pn *PubNub) AddChannelToChannelGroupWithContext(
ctx Context) *addChannelToChannelGroupBuilder {
return newAddChannelToChannelGroupBuilderWithContext(pn, ctx)
}
func (pn *PubNub) RemoveChannelFromChannelGroup() *removeChannelFromChannelGroupBuilder {
return newRemoveChannelFromChannelGroupBuilder(pn)
}
func (pn *PubNub) RemoveChannelFromChannelGroupWithContext(
ctx Context) *removeChannelFromChannelGroupBuilder {
return newRemoveChannelFromChannelGroupBuilderWithContext(pn, ctx)
}
func (pn *PubNub) DeleteChannelGroup() *deleteChannelGroupBuilder {
return newDeleteChannelGroupBuilder(pn)
}
func (pn *PubNub) DeleteChannelGroupWithContext(
ctx Context) *deleteChannelGroupBuilder {
return newDeleteChannelGroupBuilderWithContext(pn, ctx)
}
func (pn *PubNub) ListChannelsInChannelGroup() *allChannelGroupBuilder {
return newAllChannelGroupBuilder(pn)
}
func (pn *PubNub) ListChannelsInChannelGroupWithContext(
ctx Context) *allChannelGroupBuilder {
return newAllChannelGroupBuilderWithContext(pn, ctx)
}
func (pn *PubNub) GetState() *getStateBuilder {
return newGetStateBuilder(pn)
}
func (pn *PubNub) GetStateWithContext(ctx Context) *getStateBuilder {
return newGetStateBuilderWithContext(pn, ctx)
}
func (pn *PubNub) HereNow() *hereNowBuilder {
return newHereNowBuilder(pn)
}
func (pn *PubNub) HereNowWithContext(ctx Context) *hereNowBuilder {
return newHereNowBuilderWithContext(pn, ctx)
}
func (pn *PubNub) WhereNow() *whereNowBuilder {
return newWhereNowBuilder(pn)
}
func (pn *PubNub) WhereNowWithContext(ctx Context) *whereNowBuilder {
return newWhereNowBuilderWithContext(pn, ctx)
}
func (pn *PubNub) Time() *timeBuilder {
return newTimeBuilder(pn)
}
func (pn *PubNub) TimeWithContext(ctx Context) *timeBuilder {
return newTimeBuilderWithContext(pn, ctx)
}
func (pn *PubNub) DeleteMessages() *historyDeleteBuilder {
return newHistoryDeleteBuilder(pn)
}
func (pn *PubNub) DeleteMessagesWithContext(ctx Context) *historyDeleteBuilder {
return newHistoryDeleteBuilderWithContext(pn, ctx)
}
func (pn *PubNub) Destroy() {
pn.Config.Log.Println("Calling Destroy")
pn.cancel()
pn.Config.Log.Println("calling RemoveAllListeners")
pn.subscriptionManager.RemoveAllListeners()
pn.Config.Log.Println("after RemoveAllListeners")
if pn.telemetryManager.ExitTelemetryManager != nil {
pn.Config.Log.Println("calling exitTelemetryManager")
pn.telemetryManager.ExitTelemetryManager <- true
pn.Config.Log.Println("after exitTelemetryManager")
}
pn.Config.Log.Println("calling subscriptionManager Destroy")
pn.subscriptionManager.Destroy()
pn.Config.Log.Println("After Destroy")
}
func (pn *PubNub) getPublishSequence() int {
pn.publishSequenceMutex.Lock()
defer pn.publishSequenceMutex.Unlock()
if pn.nextPublishSequence == MaxSequence {
pn.nextPublishSequence = 1
} else {
pn.nextPublishSequence++
}
return pn.nextPublishSequence
}
func NewPubNub(pnconf *Config) *PubNub {
ctx, cancel := contextWithCancel(backgroundContext)
if pnconf.Log == nil {
pnconf.Log = log.New(ioutil.Discard, "", log.Ldate|log.Ltime|log.Lshortfile)
}
pnconf.Log.Println(fmt.Sprintf("PubNub Go v4 SDK: %s\npnconf: %v", Version, pnconf))
pn := &PubNub{
Config: pnconf,
nextPublishSequence: 0,
ctx: ctx,
cancel: cancel,
}
pn.subscriptionManager = newSubscriptionManager(pn, ctx)
pn.telemetryManager = newTelemetryManager(
pnconf.MaximumLatencyDataAge, ctx)
return pn
}
func NewPubNubDemo() *PubNub {
return NewPubNub(NewDemoConfig())
}