Commit Qwebchess before fucking with it
[progcomp2013.git] / qchess / qchess.cgi
1 #!/usr/bin/python -u
2
3 # CGI wrapper to qchess
4
5 import sys
6 import os
7
8 import cgi
9 import time
10 import threading
11 import datetime
12 import subprocess
13
14 path = "../qchess-cgi-data/"
15
16 def open_fifo(name, mode, timeout=None):
17         if timeout == None:
18                 return open(name, mode)
19         
20         
21         class Worker(threading.Thread):
22                 def __init__(self):
23                         threading.Thread.__init__(self)
24                         self.result = None
25                         self.exception = None
26
27                         
28                 def run(self):          
29                         try:
30                                 self.result = open(name, mode)
31                         except Exception, e:
32                                 self.exception = e
33                                 self.result = None
34                 
35
36         w = Worker()
37         w.start()
38         
39         start = time.time()
40         while time.time() - start < timeout:
41                 if w.is_alive() == False:
42                         w.join()
43                         if w.exception != None:
44                                 raise w.exception
45                         return w.result
46                 time.sleep(0.1)
47         
48         
49         if w.is_alive():
50                 #sys.stderr.write("FIFO_TIMEOUT!\n")
51                 # Recursive to deal with possible race condition
52                 try:
53                         if mode == "r":
54                                 f = open_fifo(name, "w", 1)
55                         else:
56                                 f = open_fifo(name, "r", 1)
57                 except:
58                         pass
59                         
60                 #sys.stderr.write("Opened other end!\n")
61                 while w.is_alive():
62                         time.sleep(0.1)
63                         
64                 w.join()
65                 f.close()
66                 w.result.close()
67                 raise Exception("FIFO_TIMEOUT")
68         else:
69                 w.join()
70                 if w.exception != None:
71                         raise w.exception
72                 return w.result
73
74 def force_quit():
75         os.remove(path+client+".in")
76         os.remove(path+client+".out")
77
78 def quit():
79         
80         if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
81                 try:
82                         fifo_out = open_fifo(path+client+".in", "w", 5)
83                 except:
84                         pass
85                 else:
86                         if fifo_out != None:
87                                 fifo_out.write("quit\n")
88                                 fifo_out.close()
89                 
90                 try:
91                         fifo_in = open_fifo(path+client+".out", "r", 5)
92                 except:
93                         pass
94                 else:
95                         if fifo_in != None:
96                                 s = fifo_in.readline().strip(" \r\n")
97                                 while s != "":
98                         #print s
99                                         s = fifo_in.readline().strip(" \r\n")
100                                         fifo_in.close()
101                         
102         log = open(path+client, "a")
103         log.write(" -> %s\n" % str(datetime.datetime.now()))
104         log.close()
105                         
106         time.sleep(0.5)
107         
108         
109
110
111 def main(argv):
112         print "Content-Type: text/plain\r\n" #Removed the second new line. Makes parsing everything easier ~BG3
113         
114         global client
115         form = cgi.FieldStorage()
116         client = cgi.escape(os.environ["REMOTE_ADDR"])
117         
118         #client = "127.0.0.1"
119         
120         
121         
122
123         
124         try:
125                 #request = argv[1]
126                 request = form["r"].value
127         except:
128                 request = None
129                 mode = None
130         else:
131                 try:
132                         mode = form["m"].value
133                 except:
134                         mode = None
135         
136
137         
138         try:
139                 #x = int(argv[1])       
140                 #y = int(argv[2])
141                 x = int(form["x"].value)
142                 y = int(form["y"].value)
143         except:
144                 if request == "force_quit":
145                         force_quit()
146                         quit()
147                         return 0
148
149                 if os.path.exists(path+client+".in") and os.path.exists(path+client+".out"):
150                         if request == "quit":
151                                 print "Quit."
152                                 quit()
153                                 return 0
154                         
155                         print "Game in progress expects x and y."
156                         return 1
157                 elif request == "start":
158                         print "New game."
159                         args = path+"qchess.py --no-graphics"
160                         if mode == None or mode == "bishop":
161                                 args += " @fifo:../qchess-cgi-data/"+client+" @internal:AgentBishop --log=../qchess-cgi-data/"+client+".log"
162                         if mode == "random":
163                                 args += " @fifo:../qchess-cgi-data/"+client+" @internal:AgentRandom"
164                         elif mode == "eigengame":
165                                 args += " --server=progcomp.ucc.asn.au @fifo:../qchess-cgi-data/"+client
166
167                         os.system("echo '"+args+"' | at now")
168
169                 #       subprocess.Popen(args)
170                 #       os.spawnl(os.P_NOWAIT, args)
171
172
173                         time.sleep(1)
174                         
175                         log = open(path+client, "a")
176                         log.write("%s" % str(datetime.datetime.now()))
177                         log.close()
178                         
179                 else:
180                         print "No game in progress."
181                         return 1
182                         
183         else:
184                 if not (os.path.exists(path+client+".in") and os.path.exists(path+client+".out")):
185                         print "No game in progress."
186                         return 1
187                         
188                 try:
189                         fifo_out = open_fifo(path+client+".in", "w")
190                 except:
191                         quit()
192                 else:
193                         fifo_out.write("%d %d\n" % (x, y))
194                         fifo_out.close()
195                 
196                 
197         
198         #sys.stderr.write("\ncgi read from fifo here\n")
199         try:
200                 fifo_in = open_fifo(path+client+".out", "r")
201         except:
202                 quit()
203         else:
204                 #       sys.stderr.write("cgi opened fine\n")
205                 s = fifo_in.readline().strip(" \r\n")
206                 #sys.stderr.write("cgi read first line: "+str(s)+"\n")  
207                 while s != "SELECT?" and s != "MOVE?" and not s.split(" ")[0] in ["white","black"]:
208                         if s != "":
209                                 print s
210                 #       sys.stderr.write("Read: " + str(s) + "\n")
211                         
212                         s = fifo_in.readline().strip(" \r\n")
213                 print s
214                 fifo_in.close()
215                 if s.split(" ")[0] in ["white", "black"]:
216                         #sys.stderr.write("cgi quit!\n")
217                         quit()
218         
219         #sys.stderr.write("cgi qchess Done\n")
220         return 0
221
222
223 if __name__ == "__main__":
224         try:
225                 sys.exit(main(sys.argv))
226         except Exception, e:
227                 print e
228                 sys.stderr.write(sys.argv[0] + ": " + str(e) + "\n")
229                 sys.exit(1)

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