qchess.cgi 2.76 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python

# CGI wrapper to qchess

import sys
import os

import cgi
import subprocess
import time
import threading


def open_fifo(name, mode, timeout=None):
	if timeout == None:
		return open(name, mode)
	
	
	class Worker(threading.Thread):
		def __init__(self):
			threading.Thread.__init__(self)
			self.result = None
			
		def run(self):		
			self.result = open(name, mode)
		

	w = Worker()
	w.start()
	
	start = time.time()
	while time.time() - start < timeout:
		if w.is_alive() == False:
			w.join()
			return w.result
		time.sleep(0.1)
	
	
	if w.is_alive():
		#sys.stderr.write("FIFO_TIMEOUT!\n")
		if mode == "r":
			f = open(name, "w")
		else:
			f = open(name, "r")
			
		#sys.stderr.write("Opened other end!\n")
		while w.is_alive():
			time.sleep(0.1)
			
		w.join()
		f.close()
		w.result.close()
		raise Exception("FIFO_TIMEOUT")
	else:
		w.join()
		return w.result

def quit():
	try:
		fifo_out = open_fifo("../cgi-data/"+client+".in", "w", 5)
	except:
		pass
	else:
		fifo_out.write("quit\n")
		fifo_out.close()
		
	try:
		fifo_in = open_fifo("../cgi-data/"+client+".out", "w", 5)
	except:
		pass
	else:
		s = fifo_in.readline().strip(" \r\n")
		while s != "":
			#print s
			s = fifo_in.readline().strip(" \r\n")
			fifo_in.close()
	time.sleep(0.5)
	
	


def main(argv):
	global client
	#form = cgi.FieldStorage()
	#client = cgi.escape(os.environ["REMOTE_ADDR"])
	
	client = "127.0.0.1"
	
	
	print "Content-Type: text/plain\r\n\r\n"

	
	try:
		request = argv[1]
	except:
		request = None

	
	try:
		x = int(argv[1])	
		y = int(argv[2])
	except:
		if request == "quit":
			quit()
			return 0
		
		if os.path.exists("../cgi-bin/"+client+".in") and os.path.exists("../cgi-bin/"+client+".out"):
			print "Error: Game in progress expects x and y"
			return 1
		else:
			print "NEW GAME"
			args = ["./qchess.py"]
			if request == None:
				args += ["@fifo:../cgi-data/"+client, "@internal:AgentBishop"]
			elif request == "eigengame":
				args += ["--server=progcomp.ucc.asn.au", "@fifo:../cgi-data/"+client]
			subprocess.Popen(args)
			time.sleep(1)
			
	else:
		
		fifo_out = open_fifo("../cgi-data/"+client+".in", "w")
		fifo_out.write("%d %d\n" % (x, y))
		fifo_out.close()
		
		
	
	sys.stderr.write("\ncgi read from fifo here\n")
	try:
		fifo_in = open_fifo("../cgi-data/"+client+".out", "r")
	except:
		quit()
	else:
		sys.stderr.write("Opened fine\n")
		s = fifo_in.readline().strip(" \r\n")
	
		while s != "SELECT?" and s != "MOVE?" and s.split(" ")[0] not in ["white", "black"]:
			if s != "":
				print s
			s = fifo_in.readline().strip(" \r\n")
		print s
		fifo_in.close()
		if s.split(" ")[0] in ["white", "black"]:
			quit()
	
	sys.stderr.write("Done\n")
	return 0


if __name__ == "__main__":
	try:
		sys.exit(main(sys.argv))
	except Exception, e:
		print "Exception: ", e
		sys.exit(1)