-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.py
188 lines (152 loc) · 4.37 KB
/
web.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import tornado.web
import tornado.ioloop
from zmqfan import zmqsub
import threading
import re
import sys
import time
import os
import concurrent.futures as futures
import traceback
JSOK = re.compile('^[a-z0-9\-_\.]+\.js$')
class ProtoFuture(object) :
def __init__(self, fut, continuation) :
#print 'ProtoFuture.__init__'
self.fut = fut
self.continuation = continuation
self.cbs = []
self.fut.add_done_callback(self.completed)
def add_done_callback(self, cb) :
#print 'ProtoFuture.add_done_callback'
self.cbs.append(cb)
def completed(self, fut) :
#print 'ProtoFuture.completed'
for cb in self.cbs :
cb(self)
def result(self) :
#print 'ProtoFuture.result'
return self.continuation(self.fut.result())
class TraceThread(threading.Thread) :
def __init__(self, sock, sample_cap=3600) :
self.sock = sock
self.ok = True
self.samples = []
self.sample_cap = sample_cap
threading.Thread.__init__(self)
def stop(self) :
self.ok = False
def get_trace(self, a, b, gt) :
r = []
for samp in self.samples :
ts, sample = samp
if ts > gt :
r.append((ts, sample[a][b]))
return r
def run(self) :
while self.ok :
msg = self.sock.last_msg()
if not msg :
time.sleep(0.1)
continue
samples = self.samples[:]
samples.append((long(time.time() * 1000), msg))
self.samples = samples[-self.sample_cap:]
time.sleep(5.0)
class JSHandler(tornado.web.RequestHandler):
def get(self, fn):
if not hasattr(self.__class__, 'fcache') :
self.__class__.fcache = {}
if fn not in self.__class__.fcache :
if not JSOK.match(fn) :
raise tornado.web.HTTPError(404)
if not os.path.exists(fn) :
raise tornado.web.HTTPError(404)
d = open(fn).read()
self.__class__.fcache[fn] = d
self.write(self.__class__.fcache[fn])
class InterfaceHandler(tornado.web.RequestHandler) :
def get(self) :
self.write(self.application.__interface__)
class BaseHandler(tornado.web.RequestHandler):
def wj(self, status, j) :
self.application.__io_instance__.add_callback(lambda: self._wj(status, j))
def _wj(self, status, j) :
self.set_status(status)
self.set_header('Access-Control-Allow-Origin', '*')
self.set_header('Cache-Control', 'no-cache')
self.set_header('Content-Type', 'application/json')
self.write(j)
self.finish()
class JSONHandler(BaseHandler):
NO_WRAP = False
@tornado.web.asynchronous
def get(self, *a):
cn = self.__class__.__name__
cn += ' ' * (14 - len(cn))
print '[%s] [json/%s] %s GET args %s' % (time.ctime(), cn, self.request.remote_ip, str(a))
try :
result = self.process_request(*a)
except Exception, e :
result = e
if isinstance(result, (futures.Future, ProtoFuture)) :
result.add_done_callback(self.handle_response)
else :
self.handle_response(result)
def handle_response(self, result) :
error = None
status = 200
try :
if isinstance(result, (futures.Future, ProtoFuture)) :
result = result.result()
elif isinstance(result, Exception) :
traceback.print_exc()
raise result
if self.NO_WRAP :
resp = result
else :
resp = {'status' : 'ok', 'data' : result}
except ValueError :
status = 404
error = 'bad input'
except KeyError :
status = 404
error = 'not found'
except :
status = 500
print 'exception while fetching deferred result'
traceback.print_exc()
error = 'exception'
if error :
resp = {'status' : 'failure', 'reason' : error}
self.wj(status, resp)
class TraceListHandler(JSONHandler):
def process_request(self):
ts, i = self.application.__traces__.samples[0]
return dict([(k,i[k].keys()) for k in i])
class TraceHandler(JSONHandler):
def process_request(self, tracetype, tracename, *a):
try :
gt = long(a[0])
except :
gt = 0
return self.application.__traces__.get_trace(tracetype, tracename, gt)
if __name__ == '__main__' :
handler_set = [
(r"/$", InterfaceHandler),
(r"/([a-z0-9\.\-]+\.js)$", JSHandler),
(r"/trace$", TraceListHandler),
(r"/trace/([a-z0-9\.\-_]+)/([a-z0-9\.\-_]+)/([0-9]+)$", TraceHandler)
]
sock = zmqsub.ConnectSub(sys.argv[1])
tt = TraceThread(sock)
tt.start()
traces = tt
application = tornado.web.Application(handler_set)
application.__traces__ = traces
application.__interface__ = open('interface.html').read()
application.listen(8008)
application.__io_instance__ = tornado.ioloop.IOLoop.instance()
try :
application.__io_instance__.start()
finally :
tt.stop()