2020-06-14 05:44:59 +00:00
|
|
|
# module has yet to be made capable of running in multiple processes
|
|
|
|
|
2020-06-13 06:37:05 +00:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import threading
|
2020-06-13 08:16:29 +00:00
|
|
|
import typing
|
2020-06-13 06:37:05 +00:00
|
|
|
import multiprocessing
|
|
|
|
import functools
|
2020-06-14 05:44:59 +00:00
|
|
|
from pony.flask import Pony
|
|
|
|
from pony.orm import Database, Required, Optional, commit, select, db_session
|
2020-06-13 06:37:05 +00:00
|
|
|
|
|
|
|
import websockets
|
2020-06-14 05:44:59 +00:00
|
|
|
from flask import Flask, flash, request, redirect, url_for, render_template, Response, g
|
2020-06-13 06:37:05 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
|
|
|
|
UPLOAD_FOLDER = 'uploads'
|
2020-06-13 08:16:29 +00:00
|
|
|
LOGS_FOLDER = 'logs'
|
2020-06-13 06:37:05 +00:00
|
|
|
|
|
|
|
multidata_folder = os.path.join(UPLOAD_FOLDER, "multidata")
|
|
|
|
os.makedirs(multidata_folder, exist_ok=True)
|
2020-06-13 08:16:29 +00:00
|
|
|
os.makedirs(LOGS_FOLDER, exist_ok=True)
|
2020-06-13 06:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def allowed_file(filename):
|
|
|
|
return filename.endswith('multidata')
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024 # 1 megabyte limit
|
|
|
|
app.config["SECRET_KEY"] = os.urandom(32)
|
2020-06-14 05:44:59 +00:00
|
|
|
app.config["PONY"] = {
|
|
|
|
'provider': 'sqlite',
|
|
|
|
'filename': 'db.db3',
|
|
|
|
'create_db': True
|
|
|
|
}
|
2020-06-13 06:37:05 +00:00
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
db = Database()
|
2020-06-13 06:37:05 +00:00
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
name = "localhost"
|
2020-06-13 06:37:05 +00:00
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
multiworlds = {}
|
2020-06-13 08:16:29 +00:00
|
|
|
|
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
class Multiworld():
|
|
|
|
def __init__(self, multidata: str):
|
|
|
|
self.multidata = multidata
|
2020-06-13 08:16:29 +00:00
|
|
|
self.process: typing.Optional[multiprocessing.Process] = None
|
2020-06-14 05:44:59 +00:00
|
|
|
multiworlds[multidata] = self
|
2020-06-13 08:16:29 +00:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
if self.process and self.process.is_alive():
|
2020-06-13 20:49:57 +00:00
|
|
|
return False
|
2020-06-14 05:44:59 +00:00
|
|
|
|
2020-06-13 08:16:29 +00:00
|
|
|
logging.info(f"Spinning up {self.multidata}")
|
|
|
|
self.process = multiprocessing.Process(group=None, target=run_server_process,
|
2020-06-14 05:44:59 +00:00
|
|
|
args=(self.multidata,),
|
|
|
|
name="MultiHost")
|
2020-06-13 08:16:29 +00:00
|
|
|
self.process.start()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.process:
|
|
|
|
self.process.terminate()
|
|
|
|
self.process = None
|
|
|
|
|
2020-06-13 06:37:05 +00:00
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
|
|
|
def upload_multidata():
|
|
|
|
if request.method == 'POST':
|
|
|
|
# check if the post request has the file part
|
|
|
|
if 'file' not in request.files:
|
|
|
|
flash('No file part')
|
|
|
|
return redirect(request.url)
|
|
|
|
file = request.files['file']
|
|
|
|
# if user does not select file, browser also
|
|
|
|
# submit an empty part without filename
|
|
|
|
if file.filename == '':
|
|
|
|
flash('No selected file')
|
|
|
|
return redirect(request.url)
|
|
|
|
if file and allowed_file(file.filename):
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
|
file.save(os.path.join(multidata_folder, filename))
|
|
|
|
return redirect(url_for('host_multidata',
|
|
|
|
filename=filename))
|
2020-06-13 20:49:57 +00:00
|
|
|
return render_template("upload_multidata.html")
|
2020-06-13 06:37:05 +00:00
|
|
|
|
|
|
|
|
2020-06-13 08:16:29 +00:00
|
|
|
def _read_log(path: str):
|
|
|
|
with open(path) as log:
|
|
|
|
yield from log
|
|
|
|
|
|
|
|
|
2020-06-13 06:37:05 +00:00
|
|
|
@app.route('/log/<filename>')
|
2020-06-13 08:16:29 +00:00
|
|
|
def display_log(filename: str):
|
|
|
|
# noinspection PyTypeChecker
|
|
|
|
return Response(_read_log(os.path.join("logs", filename + ".txt")), mimetype="text/plain;charset=UTF-8")
|
|
|
|
|
|
|
|
|
|
|
|
processstartlock = threading.Lock()
|
2020-06-13 06:37:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/hosted/<filename>')
|
2020-06-14 05:44:59 +00:00
|
|
|
def host_multidata(filename: str):
|
|
|
|
with db_session:
|
2020-06-13 06:37:05 +00:00
|
|
|
multidata = os.path.join(multidata_folder, filename)
|
2020-06-14 05:44:59 +00:00
|
|
|
multiworld = multiworlds.get(multidata, None)
|
|
|
|
if not multiworld:
|
|
|
|
multiworld = Multiworld(multidata)
|
|
|
|
|
|
|
|
with processstartlock:
|
|
|
|
multiworld.start()
|
2020-06-13 08:16:29 +00:00
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
return render_template("host_multidata.html", filename=filename)
|
2020-06-13 06:37:05 +00:00
|
|
|
|
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
def run_server_process(multidata: str):
|
2020-06-13 06:37:05 +00:00
|
|
|
async def main():
|
|
|
|
logging.basicConfig(format='[%(asctime)s] %(message)s',
|
|
|
|
level=logging.INFO,
|
2020-06-13 08:16:29 +00:00
|
|
|
filename=os.path.join(LOGS_FOLDER, os.path.split(multidata)[-1] + ".txt"))
|
2020-06-13 06:37:05 +00:00
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
ctx = Context("", 0, "", 1, 1000,
|
2020-06-13 08:16:29 +00:00
|
|
|
True, "enabled", "goal")
|
|
|
|
ctx.load(multidata, True)
|
2020-06-13 20:49:57 +00:00
|
|
|
ctx.auto_shutdown = 24 * 60 * 60 # 24 hours
|
2020-06-13 06:37:05 +00:00
|
|
|
ctx.init_save()
|
|
|
|
|
2020-06-14 05:44:59 +00:00
|
|
|
ctx.server = websockets.serve(functools.partial(server, ctx=ctx), ctx.host, 0, ping_timeout=None,
|
2020-06-13 06:37:05 +00:00
|
|
|
ping_interval=None)
|
|
|
|
|
|
|
|
await ctx.server
|
2020-06-14 05:44:59 +00:00
|
|
|
for socket in ctx.server.ws_server.sockets:
|
|
|
|
socketname = socket.getsockname()
|
|
|
|
logging.info(f'Hosting game at {socketname[0]}:{socketname[1]}')
|
2020-06-13 06:37:05 +00:00
|
|
|
while ctx.running:
|
|
|
|
await asyncio.sleep(1)
|
2020-06-13 08:16:29 +00:00
|
|
|
logging.info("Shutting down")
|
2020-06-13 06:37:05 +00:00
|
|
|
|
|
|
|
import asyncio
|
2020-06-14 05:44:59 +00:00
|
|
|
if ".." not in sys.path:
|
|
|
|
sys.path.append("..")
|
2020-06-13 08:16:29 +00:00
|
|
|
from MultiServer import Context, server
|
2020-06-13 06:37:05 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(main())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
multiprocessing.freeze_support()
|
2020-06-14 05:44:59 +00:00
|
|
|
db.bind(**app.config["PONY"])
|
|
|
|
db.generate_mapping(create_tables=True)
|
|
|
|
app.run(debug=True)
|