-
Notifications
You must be signed in to change notification settings - Fork 0
/
mytornado.py
79 lines (65 loc) · 3.18 KB
/
mytornado.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
import tornado.ioloop
import tornado.web
import tornado.websocket
from tornado.queues import Queue
import requests
class OwnerHandler(tornado.websocket.WebSocketHandler):
datasets = {}
id = 0
def open(self):
#verify that the data owner has connected with a valid token
cookie = self.request.headers["cookie"]
if cookie is not None:
token, dataset = cookie.split(",")
self.id = dataset
print("firebase call to verify that the cookie is valid:" + cookie)
val = requests.get("http://us-central1-silo-ml.cloudfunctions.net/verifyOwnerToken", params={'token': token, 'dataset': dataset})
if (val.status_code == 200):
self.id = val.text
OwnerHandler.datasets[self.id] = (self, Queue(maxsize=1))
def on_close(self):
print("an owner has disconnected, need to let firebase know")
requests.get("http://us-central1-silo-ml.cloudfunctions.net/disconnectDevice", params={'dataset_id': self.id})
del OwnerHandler.datasets[self.id]
def on_message(self, msg):
print("looking at which researcher is currently connected to this owner and forwarding them the message")
if OwnerHandler.datasets[self.id][1].qsize() > 0:
ResearcherHandler.resMap[self].write_message(msg)
class ResearcherHandler(tornado.websocket.WebSocketHandler):
dest = 0
resMap = {}
async def open(self):
#verify that the researcher has connected with a valid token
cookie = self.request.headers["cookie"]
if cookie is not None:
print("firebase call to verify that the researcher cookie is valid:" + cookie) #also removes the token and returns the database id
token, dataset = cookie.split(",")
val = requests.get("http://us-central1-silo-ml.cloudfunctions.net/verifyResearcherToken", params={'token': token, 'dataset': dataset})
if (val.status_code == 200):
self.dest = dataset
print(self)
await OwnerHandler.datasets[self.dest][1].put(self)
ResearcherHandler.resMap[OwnerHandler.datasets[self.dest][0]] = self
requests.get("http://us-central1-silo-ml.cloudfunctions.net/setDeviceAsUnavailable", params={'dataset_id': self.dest})
def on_close(self):
print("researcher closing")
if (ResearcherHandler.resMap[OwnerHandler.datasets[self.dest][0]] == self):
print("removing")
requests.get("http://us-central1-silo-ml.cloudfunctions.net/setDeviceAsAvailable", params={'dataset_id': self.dest})
OwnerHandler.datasets[self.dest][1].task_done()
print(OwnerHandler.datasets[self.dest][1].get_nowait())
def on_message(self, msg):
#only send the message if they are first in line
OwnerHandler.datasets[self.dest][0].write_message(msg)
def make_app():
return tornado.web.Application([
(r"/", OwnerHandler),
(r"/share", OwnerHandler),
(r"/research", ResearcherHandler)
])
def check_origin(self, data):
return True
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()