From c41180a4d0c1b8b5f0f3f88c905eb69becc89ff5 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Mon, 23 Feb 2015 12:27:52 +0100 Subject: [PATCH] Server/py: Some refactoring, explaining. --- plomrogue-server.py | 68 ++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/plomrogue-server.py b/plomrogue-server.py index 8f44015..db5789b 100755 --- a/plomrogue-server.py +++ b/plomrogue-server.py @@ -9,25 +9,21 @@ import time def setup_server_io(): """Fill IO files DB with proper file( path)s. Write process IO test string. - Set io_db["kicked_by_rival"] to False. Decide file paths. Ensure IO files - directory at server/. Remove any old in file if found. Set up new in file - (io_db["file_in"]) for reading at io_db["path_in"], and new out file - (io_db["file_out"]) for writing at io_db["path_out"]. Start out file with - process hash line of format PID + " " + floated UNIX time - (io_db["teststring"]). Run detect_atomic_leftover on io_db["path_record"] - and io_db["path_save"]. + Ensure IO files directory at server/. Remove any old input file if found. + Set up new input file for reading, and new output file for writing. Start + output file with process hash line of format PID + " " + floated UNIX time + (io_db["teststring"]). Raise SystemExit if file is found at path of either + record or save file plus io_db["tmp_suffix"]. """ - io_dir = "server/" - io_db["kicked_by_rival"] = False - io_db["path_in"] = io_dir + "in" - io_db["path_out"] = io_dir + "out" - io_db["path_worldstate"] = io_dir + "worldstate" - io_db["path_record"] = "record" - io_db["path_save"] = "save" - io_db["path_worldconf"] = "confserver/world" - io_db["tmp_suffix"] = "_tmp" + def detect_atomic_leftover(path, tmp_suffix): + path_tmp = path + tmp_suffix + msg = "Found file '" + path_tmp + "' that may be a leftover from an " \ + "aborted previous attempt to write '" + path + "'. Aborting " \ + "until matter is resolved by removing it from its current path." + if os.access(path_tmp, os.F_OK): + raise SystemExit(msg) io_db["teststring"] = str(os.getpid()) + " " + str(time.time()) - os.makedirs(io_dir, exist_ok=True) + os.makedirs(io_db["path_server"], exist_ok=True) io_db["file_out"] = open(io_db["path_out"], "w") io_db["file_out"].write(io_db["teststring"] + "\n") io_db["file_out"].flush() @@ -55,16 +51,6 @@ def cleanup_server_io(): io_db["file_record"].close() -def detect_atomic_leftover(path, tmp_suffix): - """Raise explained SystemExit if file is found at path + tmp_suffix.""" - path_tmp = path + tmp_suffix - msg = "Found file '" + path_tmp + "' that may be a leftover from an " \ - "aborted previous attempt to write '" + path + "'. Aborting until " \ - "the matter is resolved by removing it from its current path." - if os.access(path_tmp, os.F_OK): - raise SystemExit(msg) - - def obey(command, prefix, replay=False, do_record=False): """Call function from commands_db mapped to command's first token. @@ -144,7 +130,9 @@ def parse_command_line_arguments(): def server_test(): """Ensure valid server out file belonging to current process. - On failure, set io_db["kicked_by_rival"] and raise SystemExit. + This is done by comparing io_db["teststring"] to what's found at the start + of the current file at io_db["path_out"]. On failure, set + io_db["kicked_by_rival"] and raise SystemExit. """ if not os.access(io_db["path_out"], os.F_OK): raise SystemExit("Server output file has disappeared.") @@ -213,8 +201,27 @@ commands_db = { "MAKE_WORLD": (1, False, command_makeworld) } -io_db = {} -world_db = {} + +"""World state database,""" +world_db = { + "turn": 0 +} + + +"""File IO database.""" +io_db = { + "path_save": "save", + "path_record": "record", + "path_worldconf": "confserver/world", + "path_server": "server/", + "path_in": "server/in", + "path_out": "server/out", + "path_worldstate": "server/worldstate", + "tmp_suffix": "_tmp", + "kicked_by_rival": False +} + + try: opts = parse_command_line_arguments() setup_server_io() @@ -226,7 +233,6 @@ try: " (if so late a turn is to be found).") if not os.access(io_db["path_record"], os.F_OK): raise SystemExit("No record file found to replay.") - world_db["turn"] = 0 io_db["file_record"] = open(io_db["path_record"], "r") io_db["file_record"].prefix = "recod file line " io_db["file_record"].line_n = 1 -- 2.30.2