3 # CGI wrapper to qchess
14 path = "../qchess-cgi-data/"
16 def open_fifo(name, mode, timeout=None):
18 return open(name, mode)
21 class Worker(threading.Thread):
23 threading.Thread.__init__(self)
30 self.result = open(name, mode)
40 while time.time() - start < timeout:
41 if w.is_alive() == False:
43 if w.exception != None:
50 #sys.stderr.write("FIFO_TIMEOUT!\n")
51 # Recursive to deal with possible race condition
54 f = open_fifo(name, "w", 1)
56 f = open_fifo(name, "r", 1)
60 #sys.stderr.write("Opened other end!\n")
67 raise Exception("FIFO_TIMEOUT")
70 if w.exception != None:
75 os.remove(path+client+".in")
76 os.remove(path+client+".out")
80 if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
82 fifo_out = open_fifo(path+client+".in", "w", 5)
87 fifo_out.write("quit\n")
91 fifo_in = open_fifo(path+client+".out", "r", 5)
96 s = fifo_in.readline().strip(" \r\n")
99 s = fifo_in.readline().strip(" \r\n")
102 log = open(path+client, "a")
103 log.write(" -> %s\n" % str(datetime.datetime.now()))
112 print "Content-Type: text/plain\r\n" #Removed the second new line. Makes parsing everything easier ~BG3
115 form = cgi.FieldStorage()
116 client = cgi.escape(os.environ["REMOTE_ADDR"])
118 #client = "127.0.0.1"
126 request = form["r"].value
132 mode = form["m"].value
141 x = int(form["x"].value)
142 y = int(form["y"].value)
144 if request == "force_quit":
149 if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
150 if request == "quit":
155 print "Game in progress expects x and y."
157 elif request == "start":
159 args = path+"qchess.py --no-graphics"
161 args += " @internal:AgentBishop @fifo:../qchess-cgi-data/"+client
162 elif mode == None or mode == "bishop":
163 args += " @fifo:../qchess-cgi-data/"+client+" @internal:AgentBishop"
164 elif mode == "random":
165 args += " @fifo:../qchess-cgi-data/"+client+" @internal:AgentRandom"
166 elif mode == "eigengame":
167 args += " --server=progcomp.ucc.asn.au @fifo:../qchess-cgi-data/"+client
169 args += " --log=@../qchess-cgi-data/"+client+".log";
171 os.system("echo '"+args+"' | at now")
173 # subprocess.Popen(args)
174 # os.spawnl(os.P_NOWAIT, args)
179 log = open(path+client, "a")
180 log.write("%s" % str(datetime.datetime.now()))
184 print "No game in progress."
188 if not (os.path.exists(path+client+".in") and os.path.exists(path+client+".out")):
189 print "No game in progress."
193 fifo_out = open_fifo(path+client+".in", "w")
197 fifo_out.write("%d %d\n" % (x, y))
202 #sys.stderr.write("\ncgi read from fifo here\n")
204 fifo_in = open_fifo(path+client+".out", "r")
208 # sys.stderr.write("cgi opened fine\n")
209 s = fifo_in.readline().strip(" \r\n")
210 #sys.stderr.write("cgi read first line: "+str(s)+"\n")
211 while s != "SELECT?" and s != "MOVE?" and not s.split(" ")[0] in ["white","black"]:
214 # sys.stderr.write("Read: " + str(s) + "\n")
216 s = fifo_in.readline().strip(" \r\n")
219 if s.split(" ")[0] in ["white", "black"]:
220 #sys.stderr.write("cgi quit!\n")
223 #sys.stderr.write("cgi qchess Done\n")
227 if __name__ == "__main__":
229 sys.exit(main(sys.argv))
232 sys.stderr.write(sys.argv[0] + ": " + str(e) + "\n")