home · contact · privacy
Server/py: Improve server instance conflict management.
[plomrogue] / plomrogue-server.py
index 1751781eafc2ec3db6f8857b07777735fac57783..986732f034c89089a6e9897eed30d74f6a46ba5f 100755 (executable)
@@ -9,14 +9,16 @@ import time
 def setup_server_io(io_db):
     """Fill IO files DB with proper file( path)s. Write process IO test string.
 
-    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"].
+    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"].
     """
     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"
@@ -43,7 +45,8 @@ def cleanup_server_io(io_db):
     def helper(file_key, path_key):
         if file_key in io_db:
             io_db[file_key].close()
-            if not io_db["kicked_by_rival"]:
+            if not io_db["kicked_by_rival"] \
+               and os.access(io_db[path_key], os.F_OK):
                 os.remove(io_db[path_key])
     helper("file_out", "path_out")
     helper("file_in", "path_in")
@@ -62,6 +65,7 @@ def detect_atomic_leftover(path, tmp_suffix):
 
 def obey(cmd, io_db, prefix):
     """"""
+    server_test(io_db)
     print("input " + prefix + ": " + cmd)
     try:
         tokens = shlex.split(cmd, comments=True)
@@ -137,6 +141,10 @@ def server_test(io_db):
         raise SystemExit(msg)
 
 
+def io_loop():
+    return False
+
+
 io_db = {}
 world_db = {}
 try:
@@ -152,15 +160,14 @@ try:
             raise SystemExit("No record file found to replay.")
         world_db["turn"] = 0
         file = open(io_db["path_record"], "r")
+        prefix = "record file line "
         line_n = 1
-        for line in file.readlines():
-            if world_db["turn"] >= opts.replay:
-                break
-            obey(line.rstrip(), io_db, "record file line " + str(line_n))
+        while world_db["turn"] < opts.replay:
+            obey(file.readline().rstrip(), io_db, prefix + str(line_n))
+            line_n = line_n + 1
+        while io_loop():
+            obey(file.readline().rstrip(), io_db, prefix + str(line_n))
             line_n = line_n + 1
-        while 1:
-            server_test(io_db)
-        # what to do next?
         file.close()
     else:
         if os.access(io_db["path_save"], os.F_OK):
@@ -171,9 +178,9 @@ try:
                 raise SystemExit(msg)
             obey_lines_in_file(io_db["path_worldconf"], "world config ")
             obey("MAKE_WORLD " + str(int(time.time())), io_db, "in file")
-        while 1:
+        while io_loop():
             server_test(io_db)
-        # print("DUMMY: Run io_loop().")
+            # more?
 except SystemExit as exit:
     print("ABORTING: " + exit.args[0])
 except: