forked from hcientist/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
opt-ipy-server.py
executable file
·105 lines (78 loc) · 2.92 KB
/
opt-ipy-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
# Adapted from https://github.com/facebook/tornado/tree/master/demos/websocket
import logging
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.websocket
import os.path
from tornado.options import define, options
import json
define("port", default=8888, help="run on the given port", type=int)
class Application(tornado.web.Application):
# singleton
current_full_trace = None
def __init__(self):
handlers = [
(r"/js/(.*)",
tornado.web.StaticFileHandler,
{"path": os.path.join(os.path.dirname(__file__), 'js/')}),
(r"/css/(.*)",
tornado.web.StaticFileHandler,
{"path": os.path.join(os.path.dirname(__file__), 'css/')}),
(r"/", MainHandler),
(r"/chatsocket", ChatSocketHandler),
# respond to HTTP POST requests:
(r"/wholetrace", WholeTraceHandler),
(r"/difftrace", DiffTraceHandler),
(r"/clear", ClearHandler),
]
tornado.web.Application.__init__(self, handlers)
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("opt-ipy.html")
class WholeTraceHandler(tornado.web.RequestHandler):
def post(self):
message = self.request.body
dat = json.loads(message.decode())
Application.current_full_trace = dat
js_msg=dict(payload=Application.current_full_trace, type='wholetrace')
ChatSocketHandler.send_updates(json.dumps(js_msg))
class DiffTraceHandler(tornado.web.RequestHandler):
def post(self):
# TODO: implement me using, say,
# https://code.google.com/p/google-diff-match-patch/
pass
class ClearHandler(tornado.web.RequestHandler):
def post(self):
Application.current_full_trace = None
js_msg=dict(type='clear')
ChatSocketHandler.send_updates(json.dumps(js_msg))
class ChatSocketHandler(tornado.websocket.WebSocketHandler):
waiters = set()
def allow_draft76(self):
# for iOS 5.0 Safari
return True
def open(self):
ChatSocketHandler.waiters.add(self)
# when a new connection is made, send the entire trace to only
# THIS browser
if Application.current_full_trace:
js_msg=dict(payload=Application.current_full_trace, type='wholetrace')
self.write_message(json.dumps(js_msg))
def on_close(self):
ChatSocketHandler.waiters.remove(self)
@classmethod
def send_updates(cls, chat):
#logging.info("sending message to %d waiters", len(cls.waiters))
for waiter in cls.waiters:
try:
waiter.write_message(chat)
except:
logging.error("Error sending message", exc_info=True)
def main():
tornado.options.parse_command_line()
app = Application()
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()