3 # CGI wrapper to qchess
14 def open_fifo(name, mode, timeout=None):
16 return open(name, mode)
19 class Worker(threading.Thread):
21 threading.Thread.__init__(self)
25 self.result = open(name, mode)
32 while time.time() - start < timeout:
33 if w.is_alive() == False:
40 #sys.stderr.write("FIFO_TIMEOUT!\n")
46 #sys.stderr.write("Opened other end!\n")
53 raise Exception("FIFO_TIMEOUT")
60 fifo_out = open_fifo("../cgi-data/"+client+".in", "w", 5)
64 fifo_out.write("quit\n")
68 fifo_in = open_fifo("../cgi-data/"+client+".out", "w", 5)
72 s = fifo_in.readline().strip(" \r\n")
75 s = fifo_in.readline().strip(" \r\n")
84 #form = cgi.FieldStorage()
85 #client = cgi.escape(os.environ["REMOTE_ADDR"])
90 print "Content-Type: text/plain\r\n\r\n"
103 if request == "quit":
107 if os.path.exists("../cgi-bin/"+client+".in") and os.path.exists("../cgi-bin/"+client+".out"):
108 print "Error: Game in progress expects x and y"
112 args = ["./qchess.py"]
114 args += ["@fifo:../cgi-data/"+client, "@internal:AgentBishop"]
115 elif request == "eigengame":
116 args += ["--server=progcomp.ucc.asn.au", "@fifo:../cgi-data/"+client]
117 subprocess.Popen(args)
122 fifo_out = open_fifo("../cgi-data/"+client+".in", "w")
123 fifo_out.write("%d %d\n" % (x, y))
128 sys.stderr.write("\ncgi read from fifo here\n")
130 fifo_in = open_fifo("../cgi-data/"+client+".out", "r")
134 sys.stderr.write("Opened fine\n")
135 s = fifo_in.readline().strip(" \r\n")
137 while s != "SELECT?" and s != "MOVE?" and s.split(" ")[0] not in ["white", "black"]:
140 s = fifo_in.readline().strip(" \r\n")
143 if s.split(" ")[0] in ["white", "black"]:
146 sys.stderr.write("Done\n")
150 if __name__ == "__main__":
152 sys.exit(main(sys.argv))
154 print "Exception: ", e