Exemplo n.º 1
0
    def handle_set_liveries_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        instance["data"]["liveries"] = msg["liveries"]
        kv.set("instance-" + msg["instance_id"], json.dumps(instance))

        return {"success": True}
Exemplo n.º 2
0
    def handle_set_liveries_request(self, msg):
        instance = json.loads(kv.get("instance-" + msg["instance_id"]))
        admin_pw = msg["admin_pw"]

        # verify password
        if admin_pw != instance["admin_pw"]:
            return {"success": False, "error_msg": "Invalid password."}

        instance["data"]["liveries"] = msg["liveries"]
        kv.set("instance-" + msg["instance_id"], json.dumps(instance))

        return {"success": True}
def upgrade():
    upgrader = Upgrader()
    instance_list = json.loads(kv.get("instance-list", "[]"))
    for instance_id in instance_list:
        upgraded = False
        instance = json.loads(kv.get("instance-"+instance_id))
        schema_version = instance.get("schema_version", 0)
        while schema_version < 1:
            print("upgrading %s from %d to %d" % (instance_id, schema_version, schema_version+1))
            getattr(upgrader, "upgrade_%d_to_%d" % (schema_version, schema_version+1))(instance)
            schema_version = instance.get("schema_version", 0)
            upgraded = True
        if upgraded:
            kv.set("instance-"+instance_id, json.dumps(instance))
Exemplo n.º 4
0
    def handle_transaction_request(self, msg):
        instance = json.loads(kv.get("instance-" + self.instance_id))
        if not instance:
            return {"success": False, "error_msg": "instance does not exist."}

        if self.spectator_mode:
            return {"success": True, "transaction_applied": False}

        for key in msg["transaction"]["deleted_object_ids"]:
            if key not in instance["data"]["objects"]:
                return {"success": True, "transaction_applied": False, "log": "object already deleted: %s" % key}

        for key, value in msg["transaction"]["preconditions"].items():
            if key not in instance["data"]["objects"]:
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "object to be updated does not exist: %s" % key,
                }
            if pformat(value) != pformat(instance["data"]["objects"][key]):
                pprint.pprint(value)
                pprint.pprint(instance["data"]["objects"][key])
                return {"success": True, "transaction_applied": False, "log": "precondition not met: %s" % key}

        for key, value in msg["transaction"]["updated_data"].items():
            instance["data"]["objects"][key] = msg["transaction"]["updated_data"][key]

        for key in msg["transaction"]["deleted_object_ids"]:
            del instance["data"]["objects"][key]

        instance["data"]["version"] += 1
        kv.set("instance-" + self.instance_id, json.dumps(instance))

        for ws in logged_in_websockets:
            if ws != self:
                try:
                    changeset = msg["transaction"]
                    changeset["version_after"] = instance["data"]["version"]
                    changeset = ws.filter_tx(changeset)
                    changeset_str = json.dumps({"type": "changeset", "changeset": changeset})
                    ws.write_message(changeset_str)
                except:
                    print("Error sending changeset.")
                    pass

        changeset = msg["transaction"]
        changeset["version_after"] = instance["data"]["version"]
        changeset = self.filter_tx(changeset)
        return {"success": True, "transaction_applied": True, "changeset": changeset}
def upgrade():
    upgrader = Upgrader()
    instance_list = json.loads(kv.get("instance-list", "[]"))
    for instance_id in instance_list:
        upgraded = False
        instance = json.loads(kv.get("instance-" + instance_id))
        schema_version = instance.get("schema_version", 0)
        while schema_version < 1:
            print("upgrading %s from %d to %d" %
                  (instance_id, schema_version, schema_version + 1))
            getattr(upgrader, "upgrade_%d_to_%d" %
                    (schema_version, schema_version + 1))(instance)
            schema_version = instance.get("schema_version", 0)
            upgraded = True
        if upgraded:
            kv.set("instance-" + instance_id, json.dumps(instance))
Exemplo n.º 6
0
    def handle_create_instance_request(self, msg):
        instance_id = kv.get("next_instance_id") or "1"
        kv.set("next_instance_id", base36encode(int(instance_id, 36) + 1))

        instance = {}
        instance["id"] = instance_id
        instance["schema_version"] = 1
        instance["mizname"] = msg["filename"]
        instance["mizhash"] = msg["md5hash"]
        if msg["no_passwords"]:
            instance["red_pw"] = ""
            instance["blue_pw"] = ""
            instance["admin_pw"] = ""
        else:
            instance["red_pw"] = util.makepw()
            instance["blue_pw"] = util.makepw()
            instance["admin_pw"] = util.makepw()
        instance["red_spectator_pw"] = util.makepw()
        instance["blue_spectator_pw"] = util.makepw()

        instance["data"] = msg["data"]

        for obj in instance["data"]["objects"].values():
            if "x" in obj and "z" in obj:
                lon, lat = dcs_proj(obj["z"], obj["x"], inverse=True)
                obj["lon"] = lon
                obj["lat"] = lat
                del obj["z"]
                del obj["x"]

        kv.set("instance-" + instance_id, json.dumps(instance))

        instance_list = json.loads(kv.get("instance-list", "[]"))
        instance_list.append(instance_id)

        kv.set("instance-list", json.dumps(instance_list))

        return {
            "success": True,
            "instance_id": instance_id,
            "red_pw": instance["red_pw"],
            "blue_pw": instance["blue_pw"],
            "red_spectator_pw": instance["red_spectator_pw"],
            "blue_spectator_pw": instance["blue_spectator_pw"],
            "admin_pw": instance["admin_pw"],
        }
Exemplo n.º 7
0
    def handle_create_instance_request(self, msg):
        instance_id = kv.get("next_instance_id") or "1"
        kv.set("next_instance_id", base36encode(int(instance_id, 36) + 1))

        instance = {}
        instance["id"] = instance_id
        instance["schema_version"] = 1
        instance["mizname"] = msg["filename"]
        instance["mizhash"] = msg["md5hash"]
        if msg["no_passwords"]:
            instance["red_pw"] = ""
            instance["blue_pw"] = ""
            instance["admin_pw"] = ""
        else:
            instance["red_pw"] = util.makepw()
            instance["blue_pw"] = util.makepw()
            instance["admin_pw"] = util.makepw()
        instance["red_spectator_pw"] = util.makepw()
        instance["blue_spectator_pw"] = util.makepw()

        instance["data"] = msg["data"]

        for obj in instance["data"]["objects"].values():
            if "x" in obj and "z" in obj:
                lon, lat = dcs_proj(obj["z"], obj["x"], inverse=True)
                obj["lon"] = lon
                obj["lat"] = lat
                del obj["z"]
                del obj["x"]

        kv.set("instance-" + instance_id, json.dumps(instance))

        instance_list = json.loads(kv.get("instance-list", "[]"))
        instance_list.append(instance_id)

        kv.set("instance-list", json.dumps(instance_list))

        return {
            "success": True,
            "instance_id": instance_id,
            "red_pw": instance["red_pw"],
            "blue_pw": instance["blue_pw"],
            "red_spectator_pw": instance["red_spectator_pw"],
            "blue_spectator_pw": instance["blue_spectator_pw"],
            "admin_pw": instance["admin_pw"],
        }
Exemplo n.º 8
0
    def handle_transaction_request(self, msg):
        instance = json.loads(kv.get("instance-" + self.instance_id))
        if not instance:
            return {"success": False, "error_msg": "instance does not exist."}

        if self.spectator_mode:
            return {"success": True, "transaction_applied": False}

        for key in msg["transaction"]["deleted_object_ids"]:
            if key not in instance["data"]["objects"]:
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "object already deleted: %s" % key
                }

        for key, value in msg["transaction"]["preconditions"].items():
            if key not in instance["data"]["objects"]:
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "object to be updated does not exist: %s" % key
                }
            if pformat(value) != pformat(instance["data"]["objects"][key]):
                pprint.pprint(value)
                pprint.pprint(instance["data"]["objects"][key])
                return {
                    "success": True,
                    "transaction_applied": False,
                    "log": "precondition not met: %s" % key
                }

        for key, value in msg["transaction"]["updated_data"].items():
            instance["data"]["objects"][key] = msg["transaction"][
                "updated_data"][key]

        for key in msg["transaction"]["deleted_object_ids"]:
            del instance["data"]["objects"][key]

        instance["data"]["version"] += 1
        kv.set("instance-" + self.instance_id, json.dumps(instance))

        for ws in logged_in_websockets:
            if ws != self:
                try:
                    changeset = msg["transaction"]
                    changeset["version_after"] = instance["data"]["version"]
                    changeset = ws.filter_tx(changeset)
                    changeset_str = json.dumps({
                        "type": "changeset",
                        "changeset": changeset
                    })
                    ws.write_message(changeset_str)
                except:
                    print("Error sending changeset.")
                    pass

        changeset = msg["transaction"]
        changeset["version_after"] = instance["data"]["version"]
        changeset = self.filter_tx(changeset)
        return {
            "success": True,
            "transaction_applied": True,
            "changeset": changeset
        }
Exemplo n.º 9
0
    def on_close(self):
        if self in logged_in_websockets:
            logged_in_websockets.remove(self)


app = tornado.web.Application([
    (r'/', IndexHandler),
    (r'/js/(.*)', tornado.web.StaticFileHandler, {
        'path': 'js/'
    }),
    (r'/websocket/', WebsocketHandler),
    (r'/airports.kml', AirportsKmlHandler),
],
                              debug=True)

if __name__ == "__main__":
    if sys.argv[1] == "export":
        print(json.dumps(kv.as_dict()))
    elif sys.argv[1] == "import":
        d = json.loads(sys.stdin.read())
        for key in d:
            kv.set(key, d[key])
    elif sys.argv[1] == "upgrade":
        import instance_upgrade
        instance_upgrade.upgrade()
    else:
        app.listen(int(sys.argv[1]))
        print("Server running @ port {0} - Hit CTRL-C to quit".format(
            int(sys.argv[1])))
        tornado.ioloop.IOLoop.instance().start()
Exemplo n.º 10
0
    def on_close(self):
        if self in logged_in_websockets:
            logged_in_websockets.remove(self)


app = tornado.web.Application(
    [
        (r"/", IndexHandler),
        (r"/js/(.*)", tornado.web.StaticFileHandler, {"path": "js/"}),
        (r"/websocket/", WebsocketHandler),
        (r"/airports.kml", AirportsKmlHandler),
    ],
    debug=True,
)

if __name__ == "__main__":
    if sys.argv[1] == "export":
        print(json.dumps(kv.as_dict()))
    elif sys.argv[1] == "import":
        d = json.loads(sys.stdin.read())
        for key in d:
            kv.set(key, d[key])
    elif sys.argv[1] == "upgrade":
        import instance_upgrade

        instance_upgrade.upgrade()
    else:
        app.listen(int(sys.argv[1]))
        print("Server running @ port {0} - Hit CTRL-C to quit".format(int(sys.argv[1])))
        tornado.ioloop.IOLoop.instance().start()