-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPNsFeedBackService.py
More file actions
59 lines (44 loc) · 1.59 KB
/
Copy pathAPNsFeedBackService.py
File metadata and controls
59 lines (44 loc) · 1.59 KB
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
from struct import pack
from binascii import a2b_hex
import time
from tornado.netutil import TCPServer
def packed_ushort_big_endian(num):
return pack('>H', num)
def packed_uint_big_endian(num):
return pack('>I', num)
def Generate_FeedBackData(num):
FeedBackData = ''
for i in range(0,num):
expire = int(time.time()+i*300)
expire_bin = packed_uint_big_endian(expire)
token_bin = packed_ushort_big_endian(i)
token_length_bin = packed_ushort_big_endian(len(token_bin))
FeedBackData = FeedBackData + expire_bin + token_length_bin + token_bin
return FeedBackData
class APNsFeedBackService(TCPServer):
def __init__(self,
cert_file,
key_file,
data_num,
**kwargs):
self.data_num = data_num
TCPServer.__init__(self,
ssl_options={"keyfile": key_file,
"certfile": cert_file},
**kwargs)
def handle_stream(self, stream, address):
FeedBackConnection(self.data_num, stream, address)
class FeedBackConnection(object):
def __init__(self, data_num, stream, address):
self.data_num = data_num
self.stream = stream
self.address = address
self.write_feedback()
def write_feedback(self):
if not self.stream.closed():
data = Generate_FeedBackData(self.data_num)
self.stream.write(data, self._on_write_complete)
def _on_write_complete(self):
self.stream.close()
if self.stream.closed():
print 'closed'