From: Sam Moore Date: Mon, 24 Jun 2013 15:56:14 +0000 (+0800) Subject: Fix bugs with FifoPlayer and Networking X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=0a0868c019e4f68356a8793058d407539548759b;p=progcomp2013.git Fix bugs with FifoPlayer and Networking --- diff --git a/qchess/qchess.py b/qchess/qchess.py index 1dd8691..1279ec1 100755 --- a/qchess/qchess.py +++ b/qchess/qchess.py @@ -651,6 +651,8 @@ class Player(): def base_player(self): return self + + @@ -722,6 +724,7 @@ class FifoPlayer(Player): 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) @@ -731,10 +734,7 @@ class FifoPlayer(Player): self.fifo_out.write("START "+colour+"\n") self.fifo_out.close() - - - - + def update(self, result): sys.stderr.write("update fifo called\n") try: @@ -780,15 +780,16 @@ class FifoPlayer(Player): try: self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout) except: - os.remove(self.name+".in") - os.remove(self.name+".out") - #raise Exception("FIFO_TIMEOUT") - + pass else: self.fifo_out.write(result + "\n") self.fifo_out.close() + + try: os.remove(self.name+".in") os.remove(self.name+".out") + except OSError: + pass # Player that runs from another process class ExternalAgent(Player): @@ -796,6 +797,7 @@ class ExternalAgent(Player): def __init__(self, name, colour): Player.__init__(self, name, colour) + #raise Exception("waht") self.p = subprocess.Popen(name,bufsize=0,stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True,universal_newlines=True) self.send_message(colour) @@ -877,6 +879,7 @@ class HumanPlayer(Player): def __init__(self, name, colour): Player.__init__(self, name, colour) + # Select your preferred account def select(self): if isinstance(graphics, GraphicsThread): @@ -945,7 +948,8 @@ class InternalAgent(Player): self.board = Board(style = "agent") - + def argForm(self): + return "@internal:"+self.name def update(self, result): @@ -1434,8 +1438,10 @@ class Network(): if self.src in ready: s = self.src.recv(1) else: - raise Exception("UNRESPONSIVE") + raise Exception("NET_UNRESPONSIVE") + + debug("Network get_response s = " + str(s)) while s[len(s)-1] != '\n': # Timeout on each character in the message @@ -1446,7 +1452,7 @@ class Network(): if self.src in ready: s += self.src.recv(1) else: - raise Exception("UNRESPONSIVE") + raise Exception("NET_UNRESPONSIVE") return s.strip(" \r\n") @@ -1460,7 +1466,7 @@ class Network(): if self.src in ready: self.src.send(s + "\n") else: - raise Exception("UNRESPONSIVE") + raise Exception("NET_UNRESPONSIVE") @@ -2607,13 +2613,13 @@ def dedicated_server(): def client(addr, player="@human"): - + debug("Client " + player + " starts") s = socket.socket() s.connect((addr, 4562)) [colour,port] = s.recv(1024).strip(" \r\n").split(" ") - #debug("Colour: " + colour + ", port: " + port) + debug("Colour: " + colour + ", port: " + port) s.shutdown(socket.SHUT_RDWR) s.close() @@ -2623,7 +2629,8 @@ def client(addr, player="@human"): else: p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, player]) p.wait() - return 0# --- server.py --- # + return 0 +# --- server.py --- # #!/usr/bin/python -u # Do you know what the -u does? It unbuffers stdin and stdout @@ -2646,6 +2653,7 @@ sleep_timeout = None [game, graphics] = [None, None] def make_player(name, colour): + debug(name) if name[0] == '@': if name[1:] == "human": return HumanPlayer(name, colour) @@ -2736,17 +2744,7 @@ def main(argv): i += 1 arg = argv[i] if arg[0] != '-': - p = make_player(arg, colour) - if not isinstance(p, Player): - sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n") - return 100 - players.append(p) - if colour == "white": - colour = "black" - elif colour == "black": - pass - else: - sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n") + players.append(arg) continue # Option parsing goes here @@ -2831,11 +2829,23 @@ def main(argv): sys.stderr.write("Only a single player may be provided when --server is used\n") return 1 if len(players) == 1: - return client(server_addr, players[0].name) + return client(server_addr, players[0]) else: return client(server_addr) - + for i in xrange(len(players)): + p = make_player(players[i], colour) + if not isinstance(p, Player): + sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n") + return 100 + players[i] = p + if colour == "white": + colour = "black" + elif colour == "black": + pass + else: + sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n") + # Create the board # Construct a GameThread! Make it global! Damn the consequences! @@ -3003,4 +3013,4 @@ if __name__ == "__main__": # --- main.py --- # -# EOF - created from make on Friday 21 June 18:15:14 WST 2013 +# EOF - created from make on Monday 24 June 23:55:46 WST 2013 diff --git a/qchess/src/main.py b/qchess/src/main.py index 44aa360..3bb80f2 100644 --- a/qchess/src/main.py +++ b/qchess/src/main.py @@ -20,6 +20,7 @@ sleep_timeout = None [game, graphics] = [None, None] def make_player(name, colour): + debug(name) if name[0] == '@': if name[1:] == "human": return HumanPlayer(name, colour) @@ -110,17 +111,7 @@ def main(argv): i += 1 arg = argv[i] if arg[0] != '-': - p = make_player(arg, colour) - if not isinstance(p, Player): - sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n") - return 100 - players.append(p) - if colour == "white": - colour = "black" - elif colour == "black": - pass - else: - sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n") + players.append(arg) continue # Option parsing goes here @@ -205,11 +196,23 @@ def main(argv): sys.stderr.write("Only a single player may be provided when --server is used\n") return 1 if len(players) == 1: - return client(server_addr, players[0].name) + return client(server_addr, players[0]) else: return client(server_addr) - + for i in xrange(len(players)): + p = make_player(players[i], colour) + if not isinstance(p, Player): + sys.stderr.write(sys.argv[0] + " : Fatal error creating " + colour + " player\n") + return 100 + players[i] = p + if colour == "white": + colour = "black" + elif colour == "black": + pass + else: + sys.stderr.write(sys.argv[0] + " : Too many players (max 2)\n") + # Create the board # Construct a GameThread! Make it global! Damn the consequences! diff --git a/qchess/src/network.py b/qchess/src/network.py index b685037..a50a976 100644 --- a/qchess/src/network.py +++ b/qchess/src/network.py @@ -149,9 +149,11 @@ class Network(): if self.src in ready: s = self.src.recv(1) else: - raise Exception("UNRESPONSIVE") + raise Exception("NET_UNRESPONSIVE") + debug("Network get_response s = " + str(s)) + while s[len(s)-1] != '\n': # Timeout on each character in the message if network_timeout_delay > 0.0: @@ -161,7 +163,7 @@ class Network(): if self.src in ready: s += self.src.recv(1) else: - raise Exception("UNRESPONSIVE") + raise Exception("NET_UNRESPONSIVE") return s.strip(" \r\n") @@ -175,7 +177,7 @@ class Network(): if self.src in ready: self.src.send(s + "\n") else: - raise Exception("UNRESPONSIVE") + raise Exception("NET_UNRESPONSIVE") diff --git a/qchess/src/player.py b/qchess/src/player.py index 37cca67..a574995 100644 --- a/qchess/src/player.py +++ b/qchess/src/player.py @@ -26,6 +26,8 @@ class Player(): def base_player(self): return self + + @@ -97,6 +99,7 @@ class FifoPlayer(Player): 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) @@ -106,10 +109,7 @@ class FifoPlayer(Player): self.fifo_out.write("START "+colour+"\n") self.fifo_out.close() - - - - + def update(self, result): sys.stderr.write("update fifo called\n") try: @@ -155,15 +155,16 @@ class FifoPlayer(Player): try: self.fifo_out = open_fifo(self.name+".out", "w", FifoPlayer.timeout) except: - os.remove(self.name+".in") - os.remove(self.name+".out") - #raise Exception("FIFO_TIMEOUT") - + pass else: self.fifo_out.write(result + "\n") self.fifo_out.close() + + try: os.remove(self.name+".in") os.remove(self.name+".out") + except OSError: + pass # Player that runs from another process class ExternalAgent(Player): @@ -171,6 +172,7 @@ class ExternalAgent(Player): def __init__(self, name, colour): Player.__init__(self, name, colour) + #raise Exception("waht") self.p = subprocess.Popen(name,bufsize=0,stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True,universal_newlines=True) self.send_message(colour) @@ -252,6 +254,7 @@ class HumanPlayer(Player): def __init__(self, name, colour): Player.__init__(self, name, colour) + # Select your preferred account def select(self): if isinstance(graphics, GraphicsThread): @@ -320,7 +323,8 @@ class InternalAgent(Player): self.board = Board(style = "agent") - + def argForm(self): + return "@internal:"+self.name def update(self, result): diff --git a/qchess/src/server.py b/qchess/src/server.py index 5479997..ffd539a 100644 --- a/qchess/src/server.py +++ b/qchess/src/server.py @@ -62,13 +62,13 @@ def dedicated_server(): def client(addr, player="@human"): - + debug("Client " + player + " starts") s = socket.socket() s.connect((addr, 4562)) [colour,port] = s.recv(1024).strip(" \r\n").split(" ") - #debug("Colour: " + colour + ", port: " + port) + debug("Colour: " + colour + ", port: " + port) s.shutdown(socket.SHUT_RDWR) s.close() @@ -78,4 +78,4 @@ def client(addr, player="@human"): else: p = subprocess.Popen(["python", "qchess.py", "@network:"+addr+":"+port, player]) p.wait() - return 0 \ No newline at end of file + return 0