99
1010from tornado .options import define , options
1111
12- define ( "port" , default = 8888 , help = "run on the given port" , type = int )
12+ import json
1313
1414
15- # TODO: ugly clunky global
16- current_full_trace = None
15+ define ("port" , default = 8888 , help = "run on the given port" , type = int )
1716
1817
1918class Application (tornado .web .Application ):
19+ # singleton
20+ current_full_trace = None
21+
2022 def __init__ (self ):
2123 handlers = [
2224 (r"/js/(.*)" ,
@@ -26,8 +28,12 @@ def __init__(self):
2628 tornado .web .StaticFileHandler ,
2729 {"path" : os .path .join (os .path .dirname (__file__ ), 'css/' )}),
2830 (r"/" , MainHandler ),
29- (r"/post" , PostHandler ),
3031 (r"/chatsocket" , ChatSocketHandler ),
32+ # respond to HTTP POST requests:
33+ (r"/wholetrace" , WholeTraceHandler ),
34+ (r"/append" , AppendHandler ),
35+ (r"/replace" , ReplaceHandler ),
36+ (r"/clear" , ClearHandler ),
3137 ]
3238 tornado .web .Application .__init__ (self , handlers )
3339
@@ -37,14 +43,31 @@ def get(self):
3743 self .render ("opt-ipy.html" )
3844
3945
40- class PostHandler (tornado .web .RequestHandler ):
46+ class WholeTraceHandler (tornado .web .RequestHandler ):
4147 def post (self ):
4248 message = self .request .body
43- #logging.info("got message %r", message)
44- global current_full_trace
45- current_full_trace = message
46- if current_full_trace :
47- ChatSocketHandler .send_updates (current_full_trace )
49+ dat = json .loads (message )
50+ Application .current_full_trace = dat
51+
52+ js_msg = dict (payload = Application .current_full_trace , type = 'wholetrace' )
53+ ChatSocketHandler .send_updates (json .dumps (js_msg ))
54+
55+
56+ class AppendHandler (tornado .web .RequestHandler ):
57+ def post (self ):
58+ pass
59+
60+
61+ class ReplaceHandler (tornado .web .RequestHandler ):
62+ def post (self ):
63+ pass
64+
65+
66+ class ClearHandler (tornado .web .RequestHandler ):
67+ def post (self ):
68+ Application .current_full_trace = None
69+ js_msg = dict (type = 'clear' )
70+ ChatSocketHandler .send_updates (json .dumps (js_msg ))
4871
4972
5073class ChatSocketHandler (tornado .websocket .WebSocketHandler ):
@@ -56,9 +79,11 @@ def allow_draft76(self):
5679
5780 def open (self ):
5881 ChatSocketHandler .waiters .add (self )
59- global current_full_trace
60- if current_full_trace :
61- ChatSocketHandler .send_updates (current_full_trace )
82+ # when a new connection is made, send the entire trace to only
83+ # THIS browser
84+ if Application .current_full_trace :
85+ js_msg = dict (payload = Application .current_full_trace , type = 'wholetrace' )
86+ self .write_message (json .dumps (js_msg ))
6287
6388 def on_close (self ):
6489 ChatSocketHandler .waiters .remove (self )
0 commit comments