Trying to fix bug with fifo and network players
[progcomp2013.git] / qchess / qchess.cgi
index 993a36e..035dce0 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python -u
 
 # CGI wrapper to qchess
 
@@ -6,10 +6,10 @@ import sys
 import os
 
 import cgi
-import subprocess
 import time
 import threading
 import datetime
+import subprocess
 
 path = "../qchess-cgi-data/"
 
@@ -71,6 +71,10 @@ def open_fifo(name, mode, timeout=None):
                        raise w.exception
                return w.result
 
+def force_quit():
+       os.remove(path+client+".in")
+       os.remove(path+client+".out")
+
 def quit():
        
        if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
@@ -105,7 +109,7 @@ def quit():
 
 
 def main(argv):
-       print "Content-Type: text/plain\r\n\r\n"
+       print "Content-Type: text/plain\r\n" #Removed the second new line. Makes parsing everything easier ~BG3
        
        global client
        form = cgi.FieldStorage()
@@ -119,24 +123,29 @@ def main(argv):
        
        try:
                #request = argv[1]
-               request = form["r"]
+               request = form["r"].value
        except:
                request = None
                mode = None
        else:
                try:
-                       mode = form["m"]
+                       mode = form["m"].value
                except:
                        mode = None
+       
 
        
        try:
                #x = int(argv[1])       
                #y = int(argv[2])
-               x = form["x"]
-               y = form["y"]
+               x = int(form["x"].value)
+               y = int(form["y"].value)
        except:
-               
+               if request == "force_quit":
+                       force_quit()
+                       quit()
+                       return 0
+
                if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
                        if request == "quit":
                                print "Quit."
@@ -147,14 +156,24 @@ def main(argv):
                        return 1
                elif request == "start":
                        print "New game."
-                       args = [path+"qchess.py", "--no-graphics"]
-                       if mode == None or mode == "bishop":
-                               args += ["@fifo:../qchess-cgi-data/"+client, "@internal:AgentBishop"]
-                       if mode == "random":
-                               args += ["@fifo:../qchess-cgi-data/"+client, "@internal:AgentRandom"]
+                       args = path+"qchess.py --no-graphics"
+                       if mode == "black":
+                               args += " @internal:AgentBishop @fifo:../qchess-cgi-data/"+client
+                       elif mode == None or mode == "bishop":
+                               args += " @fifo:../qchess-cgi-data/"+client+" @internal:AgentBishop"
+                       elif mode == "random":
+                               args += " @fifo:../qchess-cgi-data/"+client+" @internal:AgentRandom"
                        elif mode == "eigengame":
-                               args += ["--server=progcomp.ucc.asn.au", "@fifo:../qchess-cgi-data/"+client]
-                       subprocess.Popen(args)
+                               args += " --server=progcomp.ucc.asn.au @fifo:../qchess-cgi-data/"+client
+
+                       args += " --log=@../qchess-cgi-data/"+client+".log";
+
+                       os.system("echo '"+args+"' | at now")
+
+               #       subprocess.Popen(args)
+               #       os.spawnl(os.P_NOWAIT, args)
+
+
                        time.sleep(1)
                        
                        log = open(path+client, "a")
@@ -186,12 +205,13 @@ def main(argv):
        except:
                quit()
        else:
-               #sys.stderr.write("Opened fine\n")
+               #       sys.stderr.write("cgi opened fine\n")
                s = fifo_in.readline().strip(" \r\n")
-       
+               #sys.stderr.write("cgi read first line: "+str(s)+"\n")  
                while s != "SELECT?" and s != "MOVE?" and not s.split(" ")[0] in ["white","black"]:
                        if s != "":
                                print s
+               #       sys.stderr.write("Read: " + str(s) + "\n")
                        
                        s = fifo_in.readline().strip(" \r\n")
                print s
@@ -200,7 +220,7 @@ def main(argv):
                        #sys.stderr.write("cgi quit!\n")
                        quit()
        
-       #sys.stderr.write("Done\n")
+       #sys.stderr.write("cgi qchess Done\n")
        return 0
 
 
@@ -209,5 +229,5 @@ if __name__ == "__main__":
                sys.exit(main(sys.argv))
        except Exception, e:
                print e
-               sys.stderr.write(str(e) + "\n")
+               sys.stderr.write(sys.argv[0] + ": " + str(e) + "\n")
                sys.exit(1)

UCC git Repository :: git.ucc.asn.au