Commits

Benoit Boissinot committed 531076f

init

Comments (0)

Files changed (3)

+application: bigworldwidewall
+version: 1
+runtime: python
+api_version: 1
+
+handlers:
+- url: /tiles(/?.*)
+  script: main.py
+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run.  If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED").  If you want to manage some indexes
+# manually, move them above the marker line.  The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
+from google.appengine.ext import webapp
+from google.appengine.ext.webapp.util import run_wsgi_app
+from django.utils import simplejson as json
+import zlib
+
+from google.appengine.ext import db
+
+#empty_tile = zlib.compress(' '*(32*32))
+empty_tile = 'x\x9cSP\x18\x05\xa3`\x14\x8cT\x00\x00S\x00\x80\x01'
+maxrange = 2**27
+tilelength = 32*32
+
+class Tile(db.Model):
+    x = db.IntegerProperty(required=True)
+    y = db.IntegerProperty(required=True)
+    content = db.ByteStringProperty(required=True)
+
+def split32(s):
+    for i in range(0, tilelength, 32):
+        yield s[i:i+32]
+
+def get_tile(x, y):
+    q = db.Query(Tile)
+    return q.filter('x =', x).filter('y =', y).get()
+
+class Tiles(webapp.RequestHandler):
+    def get(self, *args):
+        if args[0] is None:
+            x, y = 0, 0
+        else:
+            x = long(args[1]) % maxrange
+            y = long(args[2]) % maxrange
+
+        t = get_tile(x, y)
+        if t is None:
+            content = empty_tile
+        else:
+            content = t.content
+
+        content = zlib.decompress(content).decode('utf8')
+        tile = {
+            'coord': (x, y),
+            'content': content, #list(split32(content)),
+        }
+
+        self.response.headers['Content-Type'] = 'application/json'
+        self.response.out.write(json.dumps(tile))
+
+    def post(self, *args):
+        if args[0] is None:
+            t = json.load(self.request.body_file)
+            x, y = t['coord']
+            x = x % maxrange
+            y = y % maxrange
+            content = t['content']
+        else:
+            x = long(args[1]) % maxrange
+            y = long(args[2]) % maxrange
+            content = self.request.body.decode('utf8')
+
+        if len(content) != tilelength or (x, y) == (0, 0):
+            raise Exception("Invalid input")
+
+        content = zlib.compress(content.encode('utf8'))
+
+        tile = get_tile(x, y)
+        if tile is None:
+            tile = Tile(x=x, y=y, content=content)
+        else:
+            tile.content = content
+        tile.put()
+        assert get_tile(x, y)
+
+
+application = webapp.WSGIApplication(
+                                     [(r'/tiles(/(\d+)x(\d+))?', Tiles)],
+                                     debug=True)
+
+def main():
+    run_wsgi_app(application)
+
+if __name__ == "__main__":
+    main()