git.ucc.asn.au
/
progcomp2013.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix the index
[progcomp2013.git]
/
qchess
/
qchess.py
diff --git
a/qchess/qchess.py
b/qchess/qchess.py
index
0ff4209
..
1dd8691
100755
(executable)
--- a/
qchess/qchess.py
+++ b/
qchess/qchess.py
@@
-663,10
+663,15
@@
def open_fifo(name, mode, timeout=None):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
def __init__(self):
threading.Thread.__init__(self)
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):
while time.time() - start < timeout:
if w.is_alive() == False:
w.join()
while time.time() - start < timeout:
if w.is_alive() == False:
w.join()
+ if w.exception != None:
+ raise w.exception
return w.result
time.sleep(0.1)
if w.is_alive():
#sys.stderr.write("FIFO_TIMEOUT!\n")
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")
+ # Recursive to deal with possible race condition
+ try:
+ if mode == "r":
+ f = open_fifo(name, "w", 1)
+ else:
+ f = open_fifo(name, "r", 1)
+ except:
+ pass
#sys.stderr.write("Opened other end!\n")
while w.is_alive():
#sys.stderr.write("Opened other end!\n")
while w.is_alive():
@@
-697,6
+708,8
@@
def open_fifo(name, mode, timeout=None):
raise Exception("FIFO_TIMEOUT")
else:
w.join()
raise Exception("FIFO_TIMEOUT")
else:
w.join()
+ if w.exception != None:
+ raise w.exception
return w.result
return w.result
@@
-709,7
+722,15
@@
class FifoPlayer(Player):
Player.__init__(self, name, colour)
os.mkfifo(self.name+".in")
os.mkfifo(self.name+".out")
Player.__init__(self, name, colour)
os.mkfifo(self.name+".in")
os.mkfifo(self.name+".out")
-
+
+ try:
+ self.fifo_out = open_fifo(self.name+".out","w", FifoPlayer.timeout)
+ except:
+ raise Exception("FIFO_TIMEOUT")
+ else:
+ self.fifo_out.write("START "+colour+"\n")
+ self.fifo_out.close()
+
@@
-877,9
+898,11
@@
class HumanPlayer(Player):
sys.stdout.write("SELECTION?\n")
try:
p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
sys.stdout.write("SELECTION?\n")
try:
p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
+ return p
except:
sys.stderr.write("ILLEGAL GIBBERISH\n")
continue
except:
sys.stderr.write("ILLEGAL GIBBERISH\n")
continue
+
# It's your move captain
def get_move(self):
if isinstance(graphics, GraphicsThread):
# It's your move captain
def get_move(self):
if isinstance(graphics, GraphicsThread):
@@
-895,6
+918,7
@@
class HumanPlayer(Player):
sys.stdout.write("MOVE?\n")
try:
p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
sys.stdout.write("MOVE?\n")
try:
p = map(int, sys.stdin.readline().strip("\r\n ").split(" "))
+ return p
except:
sys.stderr.write("ILLEGAL GIBBERISH\n")
continue
except:
sys.stderr.write("ILLEGAL GIBBERISH\n")
continue
@@
-1660,11
+1684,10
@@
class GameThread(StoppableThread):
self.cond = threading.Condition() # conditional for some reason, I forgot
self.final_result = ""
self.server = server
self.cond = threading.Condition() # conditional for some reason, I forgot
self.final_result = ""
self.server = server
+ self.retry_illegal = False
-
-
-
+
# Run the game (run in new thread with start(), run in current thread with run())
def run(self):
# Run the game (run in new thread with start(), run in current thread with run())
def run(self):
@@
-1791,14
+1814,17
@@
class GameThread(StoppableThread):
break
except Exception,e:
#if False:
break
except Exception,e:
#if False:
- result = e.message
- #sys.stderr.write(result + "\n")
-
- self.stop()
+
- with self.lock:
- self.final_result = self.state["turn"].colour + " " + e.message
- break
+ result = e.message
+ if self.retry_illegal:
+ self.state["turn"].update(result);
+ else:
+ sys.stderr.write("qchess.py exception: "+result + "\n")
+ self.stop()
+ with self.lock:
+ self.final_result = self.state["turn"].colour + " " + e.message
+ break
@@
-2688,6
+2714,7
@@
def main(argv):
global sleep_timeout
global sleep_timeout
+ retry_illegal = False
server_addr = None
max_moves = None
server_addr = None
max_moves = None
@@
-2785,7
+2812,8
@@
def main(argv):
sleep_timeout = -1
else:
sleep_timeout = float(arg[2:].split("=")[1])
sleep_timeout = -1
else:
sleep_timeout = float(arg[2:].split("=")[1])
-
+ elif (arg[1] == '-' and arg[2:] == "retry-illegal"):
+ retry_illegal = not retry_illegal
elif (arg[1] == '-' and arg[2:] == "help"):
# Help
os.system("less data/help.txt") # The best help function
elif (arg[1] == '-' and arg[2:] == "help"):
# Help
os.system("less data/help.txt") # The best help function
@@
-2832,6
+2860,7
@@
def main(argv):
board = Board(style)
board.max_moves = max_moves
game = GameThread(board, players)
board = Board(style)
board.max_moves = max_moves
game = GameThread(board, players)
+ game.retry_illegal = retry_illegal
@@
-2974,4
+3003,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
Friday 21 June 18:15:14
WST 2013
UCC
git Repository :: git.ucc.asn.au