aboutsummaryrefslogtreecommitdiffstats
path: root/python/unassembled/Platform.py
blob: f0f9327aa762deb32ec24cbcc672e7d0b91373ec (plain)
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
try:
    import urllib.request
except:
    import urllib2

import threading
import json
import time

current_req_id = -1

class HTTPClient:
    def __init__(self, url, callback, id=None):
        self.url = url
        self.id = id
        self.callback = callback
        self.stop = False

    def cancel(self):
        self.stop = True
        self.callback = None

    def run(self):
        global current_req_id
        data = urllib2.urlopen(self.url, timeout=310).read()
        if self.stop is True:
            return
        if self.id is not None and current_req_id != self.id:
            return
        if self.callback is not None:
            self.callback(json.loads(data))


class Pubnub(PubnubCore):
    def __init__(
        self,
        publish_key,
        subscribe_key,
        secret_key = False,
        cipher_key = False,
        ssl_on = False,
        origin = 'pubsub.pubnub.com',
        pres_uuid = None
    ) :
        super(Pubnub, self).__init__(
            publish_key = publish_key,
            subscribe_key = subscribe_key,
            secret_key = secret_key,
            cipher_key = cipher_key,
            ssl_on = ssl_on,
            origin = origin,
            uuid = pres_uuid
        )
        if self.python_version == 2:
            self._request = self._request2
        else:
            self._request = self._request3

    def timeout(self, interval, func):
        def cb():
            time.sleep(interval)
            func()
        thread = threading.Thread(target=cb)
        thread.start()

    def _request2_async( self, request, callback, single=False ) :
        global current_req_id
        ## Build URL
        url = self.getUrl(request)
        if single is True:
            id = time.time()
            client = HTTPClient(url, callback, id)
            current_req_id = id
        else:
            client = HTTPClient(url, callback)

        thread = threading.Thread(target=client.run)
        thread.start()
        def abort():
            client.cancel();
        return abort


    def _request2_sync( self, request) :

        ## Build URL
        url = self.getUrl(request)

        ## Send Request Expecting JSONP Response
        try:
            try: usock = urllib2.urlopen( url, None, 310 )
            except TypeError: usock = urllib2.urlopen( url, None )
            response = usock.read()
            usock.close()
            resp_json = json.loads(response)
        except:
            return None
            
            return resp_json


    def _request2(self, request, callback=None, single=False):
        if callback is None:
            return self._request2_sync(request,single=single)
        else:
            self._request2_async(request, callback, single=single)



    def _request3_sync( self, request) :
        ## Build URL
        url = self.getUrl(request)
        ## Send Request Expecting JSONP Response
        try:
            response = urllib.request.urlopen(url,timeout=310)
            resp_json = json.loads(response.read().decode("utf-8"))
        except Exception as e:
            return None
            
        return resp_json

    def _request3_async( self, request, callback, single=False ) :
        pass

    def _request3(self, request, callback=None, single=False):
        if callback is None:
            return self._request3_sync(request,single=single)
        else:
            self._request3_async(request, callback, single=single)