X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=qchess%2Fsrc%2Fnetwork.py;h=a50a97601fcfbce7cfce69714f661317ae2f734b;hp=32a7b503a9720632f2ceee4c836ccec53293ffd5;hb=HEAD;hpb=64978647120812a13948e2146de81281b38a54d5 diff --git a/qchess/src/network.py b/qchess/src/network.py index 32a7b50..a50a976 100644 --- a/qchess/src/network.py +++ b/qchess/src/network.py @@ -14,7 +14,7 @@ class NetworkPlayer(Player): return "NetworkPlayer<"+str(self.colour)+","+str(self.player)+">" def select(self): - debug(str(self) + " select called") + #debug(str(self) + " select called") if self.player != None: s = self.player.select() self.send_message(str(s[0]) + " " + str(s[1])) @@ -23,21 +23,24 @@ class NetworkPlayer(Player): for p in game.players: if p != self and isinstance(p, NetworkPlayer) and p.player == None: p.network.send_message(str(s[0]) + " " + str(s[1])) + if s == [-1,-1]: + game.final_result = "network terminate" + game.stop() return s def send_message(self, message): - debug(str(self) + " send_message(\""+str(message)+"\") called") + #debug(str(self) + " send_message(\""+str(message)+"\") called") self.network.send_message(message) def get_response(self): - debug(str(self) + " get_response() called") + #debug(str(self) + " get_response() called") s = self.network.get_response() - debug(str(self) + " get_response() returns \""+str(s)+"\"") + #debug(str(self) + " get_response() returns \""+str(s)+"\"") return s def get_move(self): - debug(str(self) + " get_move called") + #debug(str(self) + " get_move called") if self.player != None: s = self.player.get_move() self.send_message(str(s[0]) + " " + str(s[1])) @@ -46,15 +49,23 @@ class NetworkPlayer(Player): for p in game.players: if p != self and isinstance(p, NetworkPlayer) and p.player == None: p.network.send_message(str(s[0]) + " " + str(s[1])) + + if s == [-1,-1]: + game.final_result = "network terminate" + game.stop() return s def update(self, result): - debug(str(self) + " update(\""+str(result)+"\") called") + #debug(str(self) + " update(\""+str(result)+"\") called") if self.network.server == True: if self.player == None: self.send_message(result) elif self.player != None: result = self.get_response() + if result == "-1 -1": + game.final_result = "network terminate" + game.stop() + return "-1 -1" self.board.update(result, deselect=False) @@ -73,7 +84,10 @@ class NetworkPlayer(Player): return self.player.base_player() def quit(self, result): - pass + try: + self.send_message("-1 -1") + except: + pass class Network(): def __init__(self, address = (None,4562)): @@ -87,7 +101,7 @@ class Network(): self.connected = False def connect(self): - debug(str(self) + "Tries to connect") + #debug(str(self) + "Tries to connect") self.connected = True if self.address[0] == None: self.host = "0.0.0.0" #socket.gethostname() # Breaks things??? @@ -135,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: @@ -147,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") @@ -161,7 +177,7 @@ class Network(): if self.src in ready: self.src.send(s + "\n") else: - raise Exception("UNRESPONSIVE") + raise Exception("NET_UNRESPONSIVE")