Commit 639646ad authored by Sam Moore's avatar Sam Moore

Fixed networking!

*touches wood*
parent 2f1d1b63
...@@ -272,10 +272,10 @@ class Board(): ...@@ -272,10 +272,10 @@ class Board():
# Update the board when a piece has been selected # Update the board when a piece has been selected
# "type" is apparently reserved, so I'll use "state" # "type" is apparently reserved, so I'll use "state"
def update_select(self, x, y, type_index, state): def update_select(self, x, y, type_index, state, sanity=True):
piece = self.grid[x][y] piece = self.grid[x][y]
if piece.types[type_index] == "unknown": if piece.types[type_index] == "unknown":
if not state in self.unrevealed_types[piece.colour].keys(): if not state in self.unrevealed_types[piece.colour].keys() and sanity == True:
raise Exception("SANITY: Too many " + piece.colour + " " + state + "s") raise Exception("SANITY: Too many " + piece.colour + " " + state + "s")
self.unrevealed_types[piece.colour][state] -= 1 self.unrevealed_types[piece.colour][state] -= 1
if self.unrevealed_types[piece.colour][state] <= 0: if self.unrevealed_types[piece.colour][state] <= 0:
...@@ -291,12 +291,12 @@ class Board(): ...@@ -291,12 +291,12 @@ class Board():
piece.possible_moves = None piece.possible_moves = None
# Update the board when a piece has been moved # Update the board when a piece has been moved
def update_move(self, x, y, x2, y2): def update_move(self, x, y, x2, y2, sanity=True):
piece = self.grid[x][y] piece = self.grid[x][y]
#print "Moving " + str(x) + "," + str(y) + " to " + str(x2) + "," + str(y2) + "; possible_moves are " + str(self.possible_moves(piece)) #print "Moving " + str(x) + "," + str(y) + " to " + str(x2) + "," + str(y2) + "; possible_moves are " + str(self.possible_moves(piece))
if not [x2,y2] in self.possible_moves(piece): if not [x2,y2] in self.possible_moves(piece) and sanity == True:
raise Exception("ILLEGAL move " + str(x2)+","+str(y2)) raise Exception("ILLEGAL move " + str(x2)+","+str(y2))
self.grid[x][y] = None self.grid[x][y] = None
...@@ -331,7 +331,7 @@ class Board(): ...@@ -331,7 +331,7 @@ class Board():
# Update the board from a string # Update the board from a string
# Guesses what to do based on the format of the string # Guesses what to do based on the format of the string
def update(self, result): def update(self, result, sanity=True):
#print "Update called with \"" + str(result) + "\"" #print "Update called with \"" + str(result) + "\""
# String always starts with 'x y' # String always starts with 'x y'
try: try:
...@@ -341,7 +341,7 @@ class Board(): ...@@ -341,7 +341,7 @@ class Board():
raise Exception("GIBBERISH \""+ str(result) + "\"") # Raise expectations raise Exception("GIBBERISH \""+ str(result) + "\"") # Raise expectations
piece = self.grid[x][y] piece = self.grid[x][y]
if piece == None: if piece == None and sanity == True:
raise Exception("EMPTY") raise Exception("EMPTY")
# If a piece is being moved, the third token is '->' # If a piece is being moved, the third token is '->'
...@@ -354,7 +354,7 @@ class Board(): ...@@ -354,7 +354,7 @@ class Board():
raise Exception("GIBBERISH \"" + str(result) + "\"") # Raise the alarm raise Exception("GIBBERISH \"" + str(result) + "\"") # Raise the alarm
# Move the piece (take opponent if possible) # Move the piece (take opponent if possible)
self.update_move(x, y, x2, y2) self.update_move(x, y, x2, y2, sanity)
else: else:
# Otherwise we will just assume a piece has been selected # Otherwise we will just assume a piece has been selected
...@@ -364,8 +364,9 @@ class Board(): ...@@ -364,8 +364,9 @@ class Board():
except: except:
raise Exception("GIBBERISH \"" + result + "\"") # Throw a hissy fit raise Exception("GIBBERISH \"" + result + "\"") # Throw a hissy fit
# Select the piece # Select the piece
self.update_select(x, y, type_index, state) self.update_select(x, y, type_index, state, sanity)
return result return result
...@@ -631,7 +632,7 @@ class Player(): ...@@ -631,7 +632,7 @@ class Player():
self.colour = colour self.colour = colour
def update(self, result): def update(self, result):
pass return result
def reset_board(self, s): def reset_board(self, s):
pass pass
...@@ -691,7 +692,7 @@ class ExternalAgent(Player): ...@@ -691,7 +692,7 @@ class ExternalAgent(Player):
def update(self, result): def update(self, result):
#print "Update " + str(result) + " called for AgentPlayer" #print "Update " + str(result) + " called for AgentPlayer"
self.send_message(result) self.send_message(result)
return result
def get_move(self): def get_move(self):
...@@ -777,6 +778,7 @@ class HumanPlayer(Player): ...@@ -777,6 +778,7 @@ class HumanPlayer(Player):
pass pass
else: else:
sys.stdout.write(result + "\n") sys.stdout.write(result + "\n")
return result
# Default internal player (makes random moves) # Default internal player (makes random moves)
...@@ -792,7 +794,8 @@ class InternalAgent(Player): ...@@ -792,7 +794,8 @@ class InternalAgent(Player):
def update(self, result): def update(self, result):
self.board.update(result) self.board.update(result)
self.board.verify() #self.board.verify()
return result
def reset_board(self, s): def reset_board(self, s):
self.board.reset_board(s) self.board.reset_board(s)
...@@ -1131,7 +1134,10 @@ network_timeout_delay = 1.0 # Maximum time between two characters being received ...@@ -1131,7 +1134,10 @@ network_timeout_delay = 1.0 # Maximum time between two characters being received
class Network(): class Network():
def __init__(self, colour, address = None): def __init__(self, colour, address = None):
self.socket = socket.socket() self.socket = socket.socket()
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#self.socket.setblocking(0) #self.socket.setblocking(0)
self.server = (address == None)
if colour == "white": if colour == "white":
self.port = 4562 self.port = 4562
...@@ -1143,7 +1149,7 @@ class Network(): ...@@ -1143,7 +1149,7 @@ class Network():
# print str(self) + " listens on port " + str(self.port) # print str(self) + " listens on port " + str(self.port)
if address == None: if address == None:
self.host = socket.gethostname() self.host = "0.0.0.0" #socket.gethostname() # Breaks things???
self.socket.bind((self.host, self.port)) self.socket.bind((self.host, self.port))
self.socket.listen(5) self.socket.listen(5)
...@@ -1158,6 +1164,7 @@ class Network(): ...@@ -1158,6 +1164,7 @@ class Network():
self.src.send("ok\n") self.src.send("ok\n")
if self.get_response() == "QUIT": if self.get_response() == "QUIT":
self.src.close() self.src.close()
self.address = (address, self.port)
def get_response(self): def get_response(self):
# Timeout the start of the message (first character) # Timeout the start of the message (first character)
...@@ -1203,6 +1210,7 @@ class Network(): ...@@ -1203,6 +1210,7 @@ class Network():
game.stop() game.stop()
return True return True
class NetworkSender(Player,Network): class NetworkSender(Player,Network):
...@@ -1213,38 +1221,52 @@ class NetworkSender(Player,Network): ...@@ -1213,38 +1221,52 @@ class NetworkSender(Player,Network):
self.address = address self.address = address
def connect(self): def connect(self):
Network.__init__(self, self.base_player.colour, self.address) nAttempts=3
for i in range(nAttempts):
try:
Network.__init__(self, self.colour, self.address)
debug(str(self) +" connected to " + str(self.address))
return
except Exception, e:
debug(str(self) +" attempt " + str(i) + ": " + str(e.message))
raise Exception("NETWORK - Can't connect to " + str(self.address))
def select(self): def select(self):
[x,y] = self.base_player.select() [x,y] = self.base_player.select()
choice = self.board.grid[x][y] choice = self.board.grid[x][y]
s = str(x) + " " + str(y) s = str(x) + " " + str(y)
#print str(self) + ".select sends " + s #debug(str(self) + " sends: " + str(s))
self.send_message(s) self.send_message(s)
return [x,y] return [x,y]
def get_move(self): def get_move(self):
[x,y] = self.base_player.get_move() [x,y] = self.base_player.get_move()
s = str(x) + " " + str(y) s = str(x) + " " + str(y)
#print str(self) + ".get_move sends " + s #debug(str(self) + " sends: " + str(s))
self.send_message(s) self.send_message(s)
return [x,y] return [x,y]
def update(self, s): def update(self, s):
self.base_player.update(s) self.base_player.update(s)
if self.server == True:
#debug(str(self) + " sends: " + str(s))
self.send_message(s)
return s
s = s.split(" ") s = s.split(" ")
[x,y] = map(int, s[0:2]) [x,y] = map(int, s[0:2])
selected = self.board.grid[x][y] selected = self.board.grid[x][y]
if selected != None and selected.colour == self.colour and len(s) > 2 and not "->" in s: if selected != None and selected.colour == self.colour and len(s) > 2 and not "->" in s:
s = " ".join(s[0:3]) s = " ".join(s[0:3])
for i in range(2): for i in range(2):
if selected.types[i][0] == '?': if selected.types[i][0] != '?':
s += " " + str(selected.types[i]) s += " " + str(selected.types[i])
else: else:
s += " unknown" s += " unknown"
#print str(self) + ".update sends " + s #debug(str(self) +" sending: " + str(s))
self.send_message(s) self.send_message(s)
...@@ -1252,24 +1274,48 @@ class NetworkSender(Player,Network): ...@@ -1252,24 +1274,48 @@ class NetworkSender(Player,Network):
self.base_player.quit(final_result) self.base_player.quit(final_result)
#self.src.send("QUIT " + str(final_result) + "\n") #self.src.send("QUIT " + str(final_result) + "\n")
self.src.close() self.src.close()
def __str__(self):
s = "NetworkSender:"
if self.server:
s += "server"
else:
s += "client"
s += ":"+str(self.address)
return s
class NetworkReceiver(Player,Network): class NetworkReceiver(Player,Network):
def __init__(self, colour, address=None): def __init__(self, colour, address=None):
Player.__init__(self, "NetworkReceiver", colour) s = "@network"
if address != None:
s += ":"+str(address)
Player.__init__(self, s, colour)
self.address = address self.address = address
self.board = None self.board = None
def connect(self): def connect(self):
Network.__init__(self, self.colour, self.address) nAttempts=3
for i in range(nAttempts):
try:
Network.__init__(self, self.colour, self.address)
debug(str(self) +" connected to " + str(self.address))
return
except Exception, e:
debug(str(self) +" attempt " + str(i) + ": " + str(e.message))
raise Exception("NETWORK - Can't connect to " + str(self.address))
def select(self): def select(self):
s = self.get_response() s = self.get_response()
#print str(self) + ".select gets " + s #debug(str(self) +".select reads: " + str(s))
[x,y] = map(int,s.split(" ")) [x,y] = map(int,s.split(" "))
if x == -1 and y == -1: if x == -1 and y == -1:
#print str(self) + ".select quits the game" #print str(self) + ".select quits the game"
...@@ -1279,7 +1325,7 @@ class NetworkReceiver(Player,Network): ...@@ -1279,7 +1325,7 @@ class NetworkReceiver(Player,Network):
return [x,y] return [x,y]
def get_move(self): def get_move(self):
s = self.get_response() s = self.get_response()
#print str(self) + ".get_move gets " + s #debug(str(self) +".get_move reads: " + str(s))
[x,y] = map(int,s.split(" ")) [x,y] = map(int,s.split(" "))
if x == -1 and y == -1: if x == -1 and y == -1:
#print str(self) + ".get_move quits the game" #print str(self) + ".get_move quits the game"
...@@ -1289,29 +1335,26 @@ class NetworkReceiver(Player,Network): ...@@ -1289,29 +1335,26 @@ class NetworkReceiver(Player,Network):
return [x,y] return [x,y]
def update(self, result): def update(self, result):
if self.server == True:
result = result.split(" ") return result
[x,y] = map(int, result[0:2]) s = self.get_response()
selected = self.board.grid[x][y] #debug(str(self) + ".update reads: " + str(s))
if selected != None and selected.colour == self.colour and len(result) > 2 and not "->" in result: if not "->" in s.split(" "):
s = self.get_response() self.board.update(s, sanity=False)
#print str(self) + ".update - receives " + str(s) return s
s = s.split(" ")
selected.choice = int(s[2])
for i in range(2):
selected.types[i] = str(s[3+i])
if s[3+i] == "unknown":
selected.types[i] = '?'+selected.types[i]
else:
selected.types[i] = selected.types[i][1:]
selected.current_type = selected.types[selected.choice]
else:
pass
#print str(self) + ".update - ignore result " + str(result)
def quit(self, final_result): def quit(self, final_result):
self.src.close() self.src.close()
def __str__(self):
s = "NetworkReceiver:"
if self.server:
s += "server"
else:
s += "client"
s += ":"+str(self.address)
return s
# --- network.py --- # # --- network.py --- #
import threading import threading
...@@ -1505,6 +1548,9 @@ def log(s): ...@@ -1505,6 +1548,9 @@ def log(s):
for l in log_files: for l in log_files:
l.write(s) l.write(s)
def debug(s):
sys.stderr.write("# DEBUG: " + s + "\n")
def log_init(board, players): def log_init(board, players):
for l in log_files: for l in log_files:
...@@ -1546,13 +1592,17 @@ class GameThread(StoppableThread): ...@@ -1546,13 +1592,17 @@ class GameThread(StoppableThread):
[x,y] = p.select() # Player selects a square [x,y] = p.select() # Player selects a square
if self.stopped(): if self.stopped():
break break
if not (isinstance(p, Network) and p.server == False):
result = self.board.select(x, y, colour = p.colour)
else:
#debug(str(self) + " don't update local board")
result = ""
result = p.update(result)
result = self.board.select(x, y, colour = p.colour)
for p2 in self.players: for p2 in self.players:
p2.update(result) # Inform players of what happened if p2 != p:
result = p2.update(result) # Inform players of what happened
log(result) log(result)
...@@ -2565,6 +2615,10 @@ def main(argv): ...@@ -2565,6 +2615,10 @@ def main(argv):
if graphics != None: if graphics != None:
graphics.board.display_grid(graphics.window, graphics.grid_sz) graphics.board.display_grid(graphics.window, graphics.grid_sz)
graphics.message("Connecting to " + p.colour + " player...") graphics.message("Connecting to " + p.colour + " player...")
# Handle race condition by having clients wait longer than servers to connect
if p.address != None:
time.sleep(0.2)
p.connect() p.connect()
...@@ -2633,4 +2687,4 @@ if __name__ == "__main__": ...@@ -2633,4 +2687,4 @@ if __name__ == "__main__":
sys.exit(102) sys.exit(102)
# --- main.py --- # # --- main.py --- #
# EOF - created from make on Wed Mar 27 13:05:44 WST 2013 # EOF - created from make on Fri Mar 29 18:33:24 WST 2013
...@@ -172,10 +172,10 @@ class Board(): ...@@ -172,10 +172,10 @@ class Board():
# Update the board when a piece has been selected # Update the board when a piece has been selected
# "type" is apparently reserved, so I'll use "state" # "type" is apparently reserved, so I'll use "state"
def update_select(self, x, y, type_index, state): def update_select(self, x, y, type_index, state, sanity=True):
piece = self.grid[x][y] piece = self.grid[x][y]
if piece.types[type_index] == "unknown": if piece.types[type_index] == "unknown":
if not state in self.unrevealed_types[piece.colour].keys(): if not state in self.unrevealed_types[piece.colour].keys() and sanity == True:
raise Exception("SANITY: Too many " + piece.colour + " " + state + "s") raise Exception("SANITY: Too many " + piece.colour + " " + state + "s")
self.unrevealed_types[piece.colour][state] -= 1 self.unrevealed_types[piece.colour][state] -= 1
if self.unrevealed_types[piece.colour][state] <= 0: if self.unrevealed_types[piece.colour][state] <= 0:
...@@ -191,12 +191,12 @@ class Board(): ...@@ -191,12 +191,12 @@ class Board():
piece.possible_moves = None piece.possible_moves = None
# Update the board when a piece has been moved # Update the board when a piece has been moved
def update_move(self, x, y, x2, y2): def update_move(self, x, y, x2, y2, sanity=True):
piece = self.grid[x][y] piece = self.grid[x][y]
#print "Moving " + str(x) + "," + str(y) + " to " + str(x2) + "," + str(y2) + "; possible_moves are " + str(self.possible_moves(piece)) #print "Moving " + str(x) + "," + str(y) + " to " + str(x2) + "," + str(y2) + "; possible_moves are " + str(self.possible_moves(piece))
if not [x2,y2] in self.possible_moves(piece): if not [x2,y2] in self.possible_moves(piece) and sanity == True:
raise Exception("ILLEGAL move " + str(x2)+","+str(y2)) raise Exception("ILLEGAL move " + str(x2)+","+str(y2))
self.grid[x][y] = None self.grid[x][y] = None
...@@ -231,7 +231,7 @@ class Board(): ...@@ -231,7 +231,7 @@ class Board():
# Update the board from a string # Update the board from a string
# Guesses what to do based on the format of the string # Guesses what to do based on the format of the string
def update(self, result): def update(self, result, sanity=True):
#print "Update called with \"" + str(result) + "\"" #print "Update called with \"" + str(result) + "\""
# String always starts with 'x y' # String always starts with 'x y'
try: try:
...@@ -241,7 +241,7 @@ class Board(): ...@@ -241,7 +241,7 @@ class Board():
raise Exception("GIBBERISH \""+ str(result) + "\"") # Raise expectations raise Exception("GIBBERISH \""+ str(result) + "\"") # Raise expectations
piece = self.grid[x][y] piece = self.grid[x][y]
if piece == None: if piece == None and sanity == True:
raise Exception("EMPTY") raise Exception("EMPTY")
# If a piece is being moved, the third token is '->' # If a piece is being moved, the third token is '->'
...@@ -254,7 +254,7 @@ class Board(): ...@@ -254,7 +254,7 @@ class Board():
raise Exception("GIBBERISH \"" + str(result) + "\"") # Raise the alarm raise Exception("GIBBERISH \"" + str(result) + "\"") # Raise the alarm
# Move the piece (take opponent if possible) # Move the piece (take opponent if possible)
self.update_move(x, y, x2, y2) self.update_move(x, y, x2, y2, sanity)
else: else:
# Otherwise we will just assume a piece has been selected # Otherwise we will just assume a piece has been selected
...@@ -264,8 +264,9 @@ class Board(): ...@@ -264,8 +264,9 @@ class Board():
except: except:
raise Exception("GIBBERISH \"" + result + "\"") # Throw a hissy fit raise Exception("GIBBERISH \"" + result + "\"") # Throw a hissy fit
# Select the piece # Select the piece
self.update_select(x, y, type_index, state) self.update_select(x, y, type_index, state, sanity)
return result return result
......
...@@ -33,13 +33,17 @@ class GameThread(StoppableThread): ...@@ -33,13 +33,17 @@ class GameThread(StoppableThread):
[x,y] = p.select() # Player selects a square [x,y] = p.select() # Player selects a square
if self.stopped(): if self.stopped():
break break
if not (isinstance(p, Network) and p.server == False):
result = self.board.select(x, y, colour = p.colour)
else:
#debug(str(self) + " don't update local board")
result = ""
result = p.update(result)
result = self.board.select(x, y, colour = p.colour)
for p2 in self.players: for p2 in self.players:
p2.update(result) # Inform players of what happened if p2 != p:
result = p2.update(result) # Inform players of what happened
log(result) log(result)
......
...@@ -173,6 +173,9 @@ def log(s): ...@@ -173,6 +173,9 @@ def log(s):
for l in log_files: for l in log_files: