/
reapp.py
105 lines (83 loc) · 3.2 KB
/
reapp.py
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
#!/usr/bin/python3
import tornado
import tornado.web
import tornado.httpserver
import tornado.ioloop
import json
from tornado import websocket
from collections import OrderedDict
from sms_struct import TableSms
import socket
# client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# client.connect(('localhost', 5001))
class websocketHandler(websocket.WebSocketHandler):
def open(self):
self.initial_number = 0
self.count = 0
self.table = TableSms()
if self.application.mainloop:
self.application.mainloop.on_init()
self.application.mainloop.send = self.send
else:
print('conection received')
def send(self, data):
self.write_message(data)
def on_message(self, msg):
# # client.send(bytes(str(msg).encode()))
data = json.loads(msg)
if data["op"] == "initial_count":
self.initial_number = int(data["data"])
elif data["op"] == "init_data":
self.count = len(self.table.display_name_list)
self.table.add_entry(data["data"])
if self.application.mainloop:
self.application.mainloop.init_data("received " + str(self.count) + " out of " + str(self.initial_number) + " conversation")
else:
print("received " + str(self.count) + " out of " + str(self.initial_number) + " conversation")
elif data["op"] == "done":
if self.application.mainloop:
# self.application.mainloop.done(self.sms_dict)
self.application.mainloop.done()
else:
# print(self.sms_dict)
# print("done")
print("done")
elif data["op"] == "sms_recv":
if self.application.mainloop:
thread_id = self.table.get_thread_id_by_number(data["data"]["number"])
data["data"]["thread_id"] = thread_id
self.application.mainloop.on_recv(data["data"])
# client.send(bytes(str(data["data"]).encode()))
# client.send(b'\n')
else:
print("sms received")
print(data["data"])
elif data["op"] == "sms_confirmation":
if self.application.mainloop:
# client.send(bytes(str(data["data"]).encode()))
# client.send(b'\n')
self.application.mainloop.on_send_confirmation(data)
else:
print("sms confirmation")
print(data["data"])
def on_close(self):
if not self.application.mainloop:
print("connection close")
class App(tornado.web.Application):
def __init__(self, mainloop=None):
self.mainloop = mainloop
if not mainloop:
print("server started")
super(App, self).__init__([(r"/", websocketHandler)], debug=True)
def start_server(mainloop):
app = App(mainloop)
server = tornado.httpserver.HTTPServer(app)
server.listen(5025)
tornado.ioloop.IOLoop.instance().start()
def main():
app = App()
httpserver = tornado.httpserver.HTTPServer(app)
httpserver.listen(5025)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()