| 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
 | try:
    from hashlib import sha256
    digestmod = sha256
except ImportError:
    import Crypto.Hash.SHA256 as digestmod
    sha256 = digestmod.new
import hmac
class EmptyLock():
    def __enter__(self):
        pass
    def __exit__(self,a,b,c):
        pass
empty_lock = EmptyLock()
class PubnubCoreAsync(PubnubBase):
    def start(self): pass 
    def stop(self):  pass
    def __init__(
        self,
        publish_key,
        subscribe_key,
        secret_key = False,
        cipher_key = False,
        auth_key = None,
        ssl_on = False,
        origin = 'pubsub.pubnub.com',
        uuid = None,
        _tt_lock=empty_lock,
        _channel_list_lock=empty_lock
    ) :
        """
        #**
        #* Pubnub
        #*
        #* Init the Pubnub Client API
        #*
        #* @param string publish_key required key to send messages.
        #* @param string subscribe_key required key to receive messages.
        #* @param string secret_key required key to sign messages.
        #* @param boolean ssl required for 2048 bit encrypted messages.
        #* @param string origin PUBNUB Server Origin.
        #**
        ## Initiat Class
        pubnub = Pubnub( 'PUBLISH-KEY', 'SUBSCRIBE-KEY', 'SECRET-KEY', False )
        """
        super(PubnubCoreAsync, self).__init__(
            publish_key=publish_key,
            subscribe_key=subscribe_key,
            secret_key=secret_key,
            cipher_key=cipher_key,
            auth_key=auth_key,
            ssl_on=ssl_on,
            origin=origin,
            UUID=uuid
        )        
        self.subscriptions              = {}
        self.timetoken                  = 0
        self.last_timetoken             = 0
        self.version                    = '3.3.4'
        self.accept_encoding            = 'gzip'
        self.SUB_RECEIVER               = None
        self._connect                   = None
        self._tt_lock                   = _tt_lock
        self._channel_list_lock         = _channel_list_lock
    def get_channel_list(self, channels):
        channel = ''
        first = True
        with self._channel_list_lock:
            for ch in channels:
                if not channels[ch]['subscribed']:
                    continue
                if not first:
                    channel += ','
                else:
                    first = False
                channel += ch
        return channel
    def get_channel_array(self):
        channels = self.subscriptions
        channel = []
        with self._channel_list_lock:
            for ch in channels:
                if not channels[ch]['subscribed']:
                    continue
                channel.append(ch)
        return channel
    def each(l, func):
        if func is None:
            return
        for i in l:
            func(i)
    def subscribe( self, args=None, sync=False ) :
        """
        #**
        #* Subscribe
        #*
        #* This is NON-BLOCKING.
        #* Listen for a message on a channel.
        #*
        #* @param array args with channel and message.
        #* @return false on fail, array on success.
        #**
        ## Subscribe Example
        def receive(message) :
            print(message)
            return True
        ## On Connect Callback
        def connected() :
            pubnub.publish({
                'channel' : 'hello_world',
                'message' : { 'some_var' : 'text' }
            })
        ## Subscribe
        pubnub.subscribe({
            'channel'  : 'hello_world',
            'connect'  : connected,
            'callback' : receive
        })
        """
        if args is None:
            _invoke(error, "Arguments Missing")
            return
        channel         = args['channel']       if 'channel'    in args else None
        callback        = args['callback']      if 'callback'   in args else None
        connect         = args['connect']       if 'connect'    in args else None
        disconnect      = args['disconnect']    if 'disconnect' in args else None
        reconnect       = args['reconnect']     if 'reconnect'  in args else None
        error           = args['error']         if 'error'      in args else None
        with self._tt_lock:
            self.last_timetoken = self.timetoken if self.timetoken != 0 else self.last_timetoken
            self.timetoken = 0
        if channel is None:
            _invoke(error, "Channel Missing")
            return
        if callback is None:
            _invoke(error, "Callback Missing")
            return
        if sync is True and self.susbcribe_sync is not None:
            self.susbcribe_sync(args)
            return
        def _invoke(func,msg=None):
            if func is not None:
                if msg is not None:
                    func(msg)
                else:
                    func()
        def _invoke_connect():
            if self._channel_list_lock:
                with self._channel_list_lock:
                    for ch in self.subscriptions:
                        chobj = self.subscriptions[ch]
                        if chobj['connected'] is False:
                            chobj['connected'] = True
                            _invoke(chobj['connect'],chobj['name'])
        def _invoke_error(channel_list=None, err=None):
            if channel_list is None:
                for ch in self.subscriptions:
                    chobj = self.subscriptions[ch]
                    _invoke(chobj['error'],err)
            else:
                for ch in channel_list:
                    chobj = self.subscriptions[ch]
                    _invoke(chobj['error'],err)
        '''
        if callback is None:
            _invoke(error, "Callback Missing")
            return
        if channel is None:
            _invoke(error, "Channel Missing")
            return
        '''
        def _get_channel():
            for ch in self.subscriptions:
                chobj = self.subscriptions[ch]
                if chobj['subscribed'] is True:
                    return chobj
        ## New Channel?
        if not channel in self.subscriptions or self.subscriptions[channel]['subscribed'] is False:
            with self._channel_list_lock:
                self.subscriptions[channel] = {
                    'name'          : channel,
                    'first'         : False,
                    'connected'     : False,
                    'subscribed'    : True,
                    'callback'      : callback,
                    'connect'       : connect,
                    'disconnect'    : disconnect,
                    'reconnect'     : reconnect,
                    'error'         : error
                }
        ## return if already connected to channel
        if channel in self.subscriptions and 'connected' in self.subscriptions[channel] and self.subscriptions[channel]['connected'] is True:
            _invoke(error, "Already Connected")
            return
            
            
        ## SUBSCRIPTION RECURSION 
        def _connect():
          
            self._reset_offline()
            def sub_callback(response):
                ## ERROR ?
                if not response or ('message' in response and response['message'] == 'Forbidden'):
                    _invoke_error(response['payload']['channels'], response['message'])
                    _connect()
                    return
                _invoke_connect()
                with self._tt_lock:
                    self.timetoken = self.last_timetoken if self.timetoken == 0 and self.last_timetoken != 0 else response[1]
                    if len(response) > 2:
                        channel_list = response[2].split(',')
                        response_list = response[0]
                        for ch in enumerate(channel_list):
                            if ch[1] in self.subscriptions:
                                chobj = self.subscriptions[ch[1]]
                                _invoke(chobj['callback'],self.decrypt(response_list[ch[0]]))
                    else:
                        response_list = response[0]
                        chobj = _get_channel()
                        for r in response_list:
                            if chobj:
                                _invoke(chobj['callback'], self.decrypt(r))
                    _connect()
            channel_list = self.get_channel_list(self.subscriptions)
            if len(channel_list) <= 0:
                return
            ## CONNECT TO PUBNUB SUBSCRIBE SERVERS
            try:
                self.SUB_RECEIVER = self._request( { "urlcomponents" : [
                    'subscribe',
                    self.subscribe_key,
                    channel_list,
                    '0',
                    str(self.timetoken)
                ], "urlparams" : {"uuid":self.uuid, "auth" : self.auth_key} }, sub_callback, sub_callback, single=True )
            except Exception as e:
                print(e)
                self.timeout( 1, _connect)
                return
        self._connect = _connect
        ## BEGIN SUBSCRIPTION (LISTEN FOR MESSAGES)
        _connect()
    def _reset_offline(self):
        if self.SUB_RECEIVER is not None:
            self.SUB_RECEIVER()
        self.SUB_RECEIVER = None
    def CONNECT(self):
        self._reset_offline()
        self._connect()
    def unsubscribe( self, args ):
        if 'channel' in self.subscriptions is False:
            return False
        channel = str(args['channel'])
        ## DISCONNECT
        with self._channel_list_lock:
            if channel in self.subscriptions:
                self.subscriptions[channel]['connected']    = 0
                self.subscriptions[channel]['subscribed']   = False
                self.subscriptions[channel]['timetoken']    = 0
                self.subscriptions[channel]['first']        = False
        self.CONNECT()
 |