Commit e9e685cd authored by Sam Moore's avatar Sam Moore

CGI script working; hopefully

parent 9b5054e5
...@@ -9,7 +9,9 @@ import cgi ...@@ -9,7 +9,9 @@ import cgi
import subprocess import subprocess
import time import time
import threading import threading
import datetime
path = "../qchess-cgi-data/"
def open_fifo(name, mode, timeout=None): def open_fifo(name, mode, timeout=None):
if timeout == None: if timeout == None:
...@@ -20,9 +22,15 @@ def open_fifo(name, mode, timeout=None): ...@@ -20,9 +22,15 @@ def open_fifo(name, mode, timeout=None):
def __init__(self): def __init__(self):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.result = None self.result = None
self.exception = None
def run(self): def run(self):
self.result = open(name, mode) try:
self.result = open(name, mode)
except Exception, e:
self.exception = e
self.result = None
w = Worker() w = Worker()
...@@ -32,16 +40,22 @@ def open_fifo(name, mode, timeout=None): ...@@ -32,16 +40,22 @@ def open_fifo(name, mode, timeout=None):
while time.time() - start < timeout: while time.time() - start < timeout:
if w.is_alive() == False: if w.is_alive() == False:
w.join() w.join()
if w.exception != None:
raise w.exception
return w.result return w.result
time.sleep(0.1) time.sleep(0.1)
if w.is_alive(): if w.is_alive():
#sys.stderr.write("FIFO_TIMEOUT!\n") #sys.stderr.write("FIFO_TIMEOUT!\n")
if mode == "r": # Recursive to deal with possible race condition
f = open(name, "w") try:
else: if mode == "r":
f = open(name, "r") f = open_fifo(name, "w", 1)
else:
f = open_fifo(name, "r", 1)
except:
pass
#sys.stderr.write("Opened other end!\n") #sys.stderr.write("Opened other end!\n")
while w.is_alive(): while w.is_alive():
...@@ -53,97 +67,140 @@ def open_fifo(name, mode, timeout=None): ...@@ -53,97 +67,140 @@ def open_fifo(name, mode, timeout=None):
raise Exception("FIFO_TIMEOUT") raise Exception("FIFO_TIMEOUT")
else: else:
w.join() w.join()
if w.exception != None:
raise w.exception
return w.result return w.result
def quit(): def quit():
try:
fifo_out = open_fifo("../cgi-data/"+client+".in", "w", 5) if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
except: try:
pass fifo_out = open_fifo(path+client+".in", "w", 5)
else: except:
fifo_out.write("quit\n") pass
fifo_out.close() else:
if fifo_out != None:
fifo_out.write("quit\n")
fifo_out.close()
try: try:
fifo_in = open_fifo("../cgi-data/"+client+".out", "w", 5) fifo_in = open_fifo(path+client+".out", "r", 5)
except: except:
pass pass
else: else:
s = fifo_in.readline().strip(" \r\n") if fifo_in != None:
while s != "": s = fifo_in.readline().strip(" \r\n")
while s != "":
#print s #print s
s = fifo_in.readline().strip(" \r\n") s = fifo_in.readline().strip(" \r\n")
fifo_in.close() fifo_in.close()
log = open(path+client, "a")
log.write(" -> %s\n" % str(datetime.datetime.now()))
log.close()
time.sleep(0.5) time.sleep(0.5)
def main(argv): def main(argv):
print "Content-Type: text/plain\r\n\r\n"
global client global client
#form = cgi.FieldStorage() form = cgi.FieldStorage()
#client = cgi.escape(os.environ["REMOTE_ADDR"]) client = cgi.escape(os.environ["REMOTE_ADDR"])
#client = "127.0.0.1"
client = "127.0.0.1"
print "Content-Type: text/plain\r\n\r\n"
try: try:
request = argv[1] #request = argv[1]
request = form["r"]
except: except:
request = None request = None
mode = None
else:
try:
mode = form["m"]
except:
mode = None
try: try:
x = int(argv[1]) #x = int(argv[1])
y = int(argv[2]) #y = int(argv[2])
x = form["x"]
y = form["y"]
except: except:
if request == "quit":
quit()
return 0
if os.path.exists("../cgi-bin/"+client+".in") and os.path.exists("../cgi-bin/"+client+".out"): if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
print "Error: Game in progress expects x and y" if request == "quit":
print "Quit."
quit()
return 0
print "Game in progress expects x and y."
return 1 return 1
else: elif request == "start":
print "NEW GAME" print "New game."
args = ["./qchess.py"] args = [path+"qchess.py", "--no-graphics"]
if request == None: if mode == None or mode == "bishop":
args += ["@fifo:../cgi-data/"+client, "@internal:AgentBishop"] args += ["@fifo:../qchess-cgi-data/"+client, "@internal:AgentBishop"]
elif request == "eigengame": if mode == "random":
args += ["--server=progcomp.ucc.asn.au", "@fifo:../cgi-data/"+client] 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) subprocess.Popen(args)
time.sleep(1) time.sleep(1)
log = open(path+client, "a")
log.write("%s" % str(datetime.datetime.now()))
log.close()
else:
print "No game in progress."
return 1
else: else:
if not (os.path.exists(path+client+".in") and os.path.exists(path+client+".out")):
fifo_out = open_fifo("../cgi-data/"+client+".in", "w") print "No game in progress."
fifo_out.write("%d %d\n" % (x, y)) return 1
fifo_out.close()
try:
fifo_out = open_fifo(path+client+".in", "w")
except:
quit()
else:
fifo_out.write("%d %d\n" % (x, y))
fifo_out.close()
sys.stderr.write("\ncgi read from fifo here\n") #sys.stderr.write("\ncgi read from fifo here\n")
try: try:
fifo_in = open_fifo("../cgi-data/"+client+".out", "r") fifo_in = open_fifo(path+client+".out", "r")
except: except:
quit() quit()
else: else:
sys.stderr.write("Opened fine\n") #sys.stderr.write("Opened fine\n")
s = fifo_in.readline().strip(" \r\n") s = fifo_in.readline().strip(" \r\n")
while s != "SELECT?" and s != "MOVE?" and s.split(" ")[0] not in ["white", "black"]: while s != "SELECT?" and s != "MOVE?" and not s.split(" ")[0] in ["white","black"]:
if s != "": if s != "":
print s print s
s = fifo_in.readline().strip(" \r\n") s = fifo_in.readline().strip(" \r\n")
print s print s
fifo_in.close() fifo_in.close()
if s.split(" ")[0] in ["white", "black"]: if s.split(" ")[0] in ["white", "black"]:
#sys.stderr.write("cgi quit!\n")
quit() quit()
sys.stderr.write("Done\n") #sys.stderr.write("Done\n")
return 0 return 0
...@@ -151,5 +208,6 @@ if __name__ == "__main__": ...@@ -151,5 +208,6 @@ if __name__ == "__main__":
try: try:
sys.exit(main(sys.argv)) sys.exit(main(sys.argv))
except Exception, e: except Exception, e:
print "Exception: ", e print e
sys.stderr.write(str(e) + "\n")
sys.exit(1) sys.exit(1)
...@@ -663,10 +663,15 @@ def open_fifo(name, mode, timeout=None): ...@@ -663,10 +663,15 @@ def open_fifo(name, mode, timeout=None):
def __init__(self): def __init__(self):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.result = None self.result = None
self.exception = None
def run(self): def run(self):
self.result = open(name, mode) try:
self.result = open(name, mode)
except Exception, e:
self.exception = e
self.result = None
w = Worker() w = Worker()
...@@ -676,16 +681,22 @@ def open_fifo(name, mode, timeout=None): ...@@ -676,16 +681,22 @@ def open_fifo(name, mode, timeout=None):
while time.time() - start < timeout: while time.time() - start < timeout:
if w.is_alive() == False: if w.is_alive() == False:
w.join() w.join()
if w.exception != None:
raise w.exception
return w.result return w.result
time.sleep(0.1) time.sleep(0.1)
if w.is_alive(): if w.is_alive():
#sys.stderr.write("FIFO_TIMEOUT!\n") #sys.stderr.write("FIFO_TIMEOUT!\n")
if mode == "r": # Recursive to deal with possible race condition
f = open(name, "w") try:
else: if mode == "r":
f = open(name, "r") f = open_fifo(name, "w", 1)
else:
f = open_fifo(name, "r", 1)
except:
pass
#sys.stderr.write("Opened other end!\n") #sys.stderr.write("Opened other end!\n")
while w.is_alive(): while w.is_alive():
...@@ -697,6 +708,8 @@ def open_fifo(name, mode, timeout=None): ...@@ -697,6 +708,8 @@ def open_fifo(name, mode, timeout=None):
raise Exception("FIFO_TIMEOUT") raise Exception("FIFO_TIMEOUT")
else: else:
w.join() w.join()
if w.exception != None:
raise w.exception
return w.result return w.result
...@@ -1792,7 +1805,7 @@ class GameThread(StoppableThread): ...@@ -1792,7 +1805,7 @@ class GameThread(StoppableThread):
except Exception,e: except Exception,e:
#if False: #if False:
result = e.message result = e.message
#sys.stderr.write(result + "\n") sys.stderr.write("qchess.py exception: "+result + "\n")
self.stop() self.stop()
...@@ -2974,4 +2987,4 @@ if __name__ == "__main__": ...@@ -2974,4 +2987,4 @@ if __name__ == "__main__":
# --- main.py --- # # --- main.py --- #
# EOF - created from make on Sun May 19 00:54:03 WST 2013 # EOF - created from make on Sun May 19 12:36:10 WST 2013
...@@ -147,7 +147,7 @@ class GameThread(StoppableThread): ...@@ -147,7 +147,7 @@ class GameThread(StoppableThread):
except Exception,e: except Exception,e:
#if False: #if False:
result = e.message result = e.message
#sys.stderr.write(result + "\n") sys.stderr.write("qchess.py exception: "+result + "\n")
self.stop() self.stop()
......
...@@ -38,10 +38,15 @@ def open_fifo(name, mode, timeout=None): ...@@ -38,10 +38,15 @@ def open_fifo(name, mode, timeout=None):
def __init__(self): def __init__(self):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.result = None self.result = None
self.exception = None
def run(self): def run(self):
self.result = open(name, mode) try:
self.result = open(name, mode)
except Exception, e:
self.exception = e
self.result = None
w = Worker() w = Worker()
...@@ -51,16 +56,22 @@ def open_fifo(name, mode, timeout=None): ...@@ -51,16 +56,22 @@ def open_fifo(name, mode, timeout=None):
while time.time() - start < timeout: while time.time() - start < timeout:
if w.is_alive() == False: if w.is_alive() == False:
w.join() w.join()
if w.exception != None:
raise w.exception
return w.result return w.result
time.sleep(0.1) time.sleep(0.1)
if w.is_alive(): if w.is_alive():
#sys.stderr.write("FIFO_TIMEOUT!\n") #sys.stderr.write("FIFO_TIMEOUT!\n")
if mode == "r": # Recursive to deal with possible race condition
f = open(name, "w") try:
else: if mode == "r":
f = open(name, "r") f = open_fifo(name, "w", 1)
else:
f = open_fifo(name, "r", 1)
except:
pass
#sys.stderr.write("Opened other end!\n") #sys.stderr.write("Opened other end!\n")
while w.is_alive(): while w.is_alive():
...@@ -72,6 +83,8 @@ def open_fifo(name, mode, timeout=None): ...@@ -72,6 +83,8 @@ def open_fifo(name, mode, timeout=None):
raise Exception("FIFO_TIMEOUT") raise Exception("FIFO_TIMEOUT")
else: else:
w.join() w.join()
if w.exception != None:
raise w.exception
return w.result return w.result
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment