Debian Codegraph @

case_studies/micro_fractal/tilegen/mandelbrot_service.py

raw
1 #!/usr/bin/python
2
3 # Copyright 2015 Google Inc. All rights reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17
18 """Serve fractal images over HTTP by calling out to mandelbrot.
19 """
20
21 import hashlib
22 import json
23 import math
24 import os
25 import string
26 import subprocess
27 import time
28
29 import flask
30
31 app = flask.Flask(__name__)
32
33 if __name__ == "__main__":
34 CONF = {
35 'width': 128,
36 'height': 128,
37 'iters': 100,
38 }
39 else:
40 CONF = None
41 for i in xrange(1, 60):
42 print 'Attempting to read conf.json: Attempt %d' % i
43 try:
44 with open('conf.json') as conf_file:
45 CONF = json.load(conf_file)
46 print 'Success!'
47 break
48 except IOError:
49 time.sleep(1)
50
51 if CONF == None:
52 sys.stderr.write('ERROR: Could not open conf.json.')
53 sys.exit(1)
54
55
56 with open ('mandelbrot.cpp', "r") as stream:
57 content = stream.read()
58 ETAG = hashlib.sha1(content).hexdigest()
59
60 def check_etag():
61 request_etag = flask.request.headers.get('If-None-Match', None)
62 print 'Request ETag: %s' % request_etag
63 return ETAG == request_etag
64
65 @app.route("/")
66 def handle_fractal():
67 """Get fractal coordinates from query string, call mandelbrot to generate image.
68
69 Returns:
70 The image, wrapped in an HTML response.
71 """
72
73 if check_etag():
74 return flask.make_response(), 304
75
76 level = int(flask.request.args.get("l", "0"))
77 x = float(int(flask.request.args.get("x", "0")))
78 y = float(int(flask.request.args.get("y", "0")))
79
80 if level < 0:
81 level = 0
82
83 grid_size = math.pow(2, level)
84
85 x0 = "%.30g" % ((x - 0) / grid_size)
86 y0 = "%.30g" % ((y - 0) / grid_size)
87 x1 = "%.30g" % ((x + 1) / grid_size)
88 y1 = "%.30g" % ((y + 1) / grid_size)
89
90 print "Tile: %s %s %s %s" % (x0, y0, x1, y1)
91
92 width = str(CONF['width'])
93 height = str(CONF['height'])
94 iters = str(CONF['iters'])
95
96 cmd = ['./mandelbrot', width, height, iters, x0, y0, x1, y1]
97 image_data = subprocess.check_output(cmd)
98
99 response = flask.make_response(image_data)
100 response.headers["Content-Type"] = "image/png"
101 response.headers["cache-control"] = "public, max-age=600"
102 response.headers["ETag"] = ETAG
103
104 return response
105
106 @app.route("/thumb")
107 def handle_thumb():
108 """Get fractal coordinates from query string, call mandelbrot to generate image.
109
110 Returns:
111 The image, wrapped in an HTML response.
112 """
113
114 if check_etag():
115 return flask.make_response(), 304
116
117 level = int(flask.request.args.get("l", "0"))
118 x = float(flask.request.args.get("x", "0"))
119 y = float(flask.request.args.get("y", "0"))
120
121 if level < 0:
122 level = 0
123
124 grid_size = math.pow(2, -level)
125
126 x0 = "%.30g" % (x - 1.5*grid_size)
127 y0 = "%.30g" % (y - 1.5*grid_size)
128 x1 = "%.30g" % (x + 1.5*grid_size)
129 y1 = "%.30g" % (y + 1.5*grid_size)
130
131 print "Thumbnail: %s %s %s %s" % (x0, y0, x1, y1)
132
133 width = str(CONF['thumb_width'])
134 height = str(CONF['thumb_height'])
135 iters = str(CONF['iters'])
136
137 cmd = ['./mandelbrot', width, height, iters, x0, y0, x1, y1]
138 image_data = subprocess.check_output(cmd)
139
140 response = flask.make_response(image_data)
141 response.headers["Content-Type"] = "image/png"
142 response.headers["cache-control"] = "public, max-age=600"
143 response.headers["ETag"] = ETAG
144
145 return response
146
147 if __name__ == "__main__":
148 app.run(host="0.0.0.0", port=8000, debug=False)