-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
127 lines (94 loc) · 3.86 KB
/
server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import json
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.options as opt
import personalized_information as pi
opt.define("port", default = 9000, help = "Server Port Number", type = int)
class Display(object):
def __init__(self, place):
self.place = place
self.devices = set()
self.update_queue = []
def has_device(self, device_id):
return device_id in self.devices
def add_device(self, dev_id):
self.devices.add(dev_id)
print("Adding cards for {0} in {1}".format(dev_id, self.place))
self.update_queue += self.get_data(dev_id, self.place)
def remove_device(self, dev_id):
print("Removing all cards for {0}".format(dev_id))
self.devices.remove(dev_id)
self.update_queue.append(
{"op": "-", "user_name": pi.User.get_user_name(dev_id)})
def get_update(self):
if self.update_queue:
return self.update_queue.pop(0)
return {"op": "0"}
def get_data(self, device_id, place):
return pi.get_data(device_id, place)
class CurlHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
dev_id = self.get_argument("device_id")
disp_id = self.get_argument("display_id")
curr_connection = get_connected_display(dev_id)
if curr_connection:
if disp_id and curr_connection != disp_id:
DISPLAYS[disp_id].add_device(dev_id)
DISPLAYS[curr_connection].remove_device(dev_id)
elif not disp_id:
DISPLAYS[curr_connection].remove_device(dev_id)
elif disp_id:
DISPLAYS[disp_id].add_device(dev_id)
self.finish()
class AndroidHandler(tornado.websocket.WebSocketHandler):
def open(self):
print("Connection with Android established")
def on_message(self, message):
print("Received message {0} from Android".format(message))
dev_id, disp_id = message.split("|")
curr_connection = get_connected_display(dev_id)
if curr_connection:
if disp_id and curr_connection != disp_id:
DISPLAYS[disp_id].add_device(dev_id)
DISPLAYS[curr_connection].remove_device(dev_id)
elif not disp_id:
DISPLAYS[curr_connection].remove_device(dev_id)
elif disp_id:
DISPLAYS[disp_id].add_device(dev_id)
def on_close(self):
print("Connection with Android terminated")
DISPLAYS = {"Jifi": Display("sf"), "NETGEAR": Display("miami")}
def get_connected_display(device_id):
for disp_id in DISPLAYS:
if DISPLAYS[disp_id].has_device(device_id):
return disp_id
class SFUpdateHandler(tornado.websocket.WebSocketHandler):
def open(self):
print("Connection with SF display established")
def on_message(self, message):
update = DISPLAYS["Jifi"].get_update()
print("Sending message to SF: {0}".format(update))
self.write_message(json.dumps(update))
def on_close(self):
print("Connection with SF display terminated")
class MiamiUpdateHandler(tornado.websocket.WebSocketHandler):
def open(self):
print("Connection with Miami display established")
def on_message(self, message):
update = DISPLAYS["NETGEAR"].get_update()
print("Sending message to Miami: {0}".format(update))
self.write_message(json.dumps(update))
def on_close(self):
print("Connection with Miami display terminated")
application = tornado.web.Application([
(r"/push_updates", AndroidHandler),
(r"/curl_updates", CurlHandler),
(r"/get_sf_updates", SFUpdateHandler),
(r"/get_miami_updates", MiamiUpdateHandler),
])
if __name__ == "__main__":
print("Server running on port {0}".format(opt.options.port))
application.listen(opt.options.port)
tornado.ioloop.IOLoop.instance().start()