4 network_timeout_start = -1.0 # Timeout in seconds to wait for the start of a message
5 network_timeout_delay = 1.0 # Maximum time between two characters being received
8 def __init__(self, colour, address = None):
9 self.socket = socket.socket()
10 #self.socket.setblocking(0)
19 # print str(self) + " listens on port " + str(self.port)
22 self.host = socket.gethostname()
23 self.socket.bind((self.host, self.port))
26 self.src, self.address = self.socket.accept()
28 if self.get_response() == "QUIT":
32 self.socket.connect((address, self.port))
33 self.src = self.socket
35 if self.get_response() == "QUIT":
38 def get_response(self):
39 # Timeout the start of the message (first character)
40 if network_timeout_start > 0.0:
41 ready = select.select([self.src], [], [], network_timeout_start)[0]
47 raise Exception("UNRESPONSIVE")
50 while s[len(s)-1] != '\n':
51 # Timeout on each character in the message
52 if network_timeout_delay > 0.0:
53 ready = select.select([self.src], [], [], network_timeout_delay)[0]
59 raise Exception("UNRESPONSIVE")
61 return s.strip(" \r\n")
63 def send_message(self,s):
64 if network_timeout_start > 0.0:
65 ready = select.select([], [self.src], [], network_timeout_start)[1]
70 self.src.send(s + "\n")
72 raise Exception("UNRESPONSIVE")
74 def check_quit(self, s):
78 game.final_result = " ".join(s[1:]) + " " + str(opponent(self.colour))
84 class NetworkSender(Player,Network):
85 def __init__(self, base_player, address = None):
86 self.base_player = base_player
87 Player.__init__(self, base_player.name, base_player.colour)
89 self.address = address
92 Network.__init__(self, self.base_player.colour, self.address)
97 [x,y] = self.base_player.select()
98 choice = self.board.grid[x][y]
99 s = str(x) + " " + str(y)
100 #print str(self) + ".select sends " + s
105 [x,y] = self.base_player.get_move()
106 s = str(x) + " " + str(y)
107 #print str(self) + ".get_move sends " + s
112 self.base_player.update(s)
114 [x,y] = map(int, s[0:2])
115 selected = self.board.grid[x][y]
116 if selected != None and selected.colour == self.colour and len(s) > 2 and not "->" in s:
119 if selected.types[i][0] == '?':
120 s += " " + str(selected.types[i])
123 #print str(self) + ".update sends " + s
127 def quit(self, final_result):
128 self.base_player.quit(final_result)
129 #self.src.send("QUIT " + str(final_result) + "\n")
132 class NetworkReceiver(Player,Network):
133 def __init__(self, colour, address=None):
135 Player.__init__(self, "NetworkReceiver", colour)
137 self.address = address
142 Network.__init__(self, self.colour, self.address)
147 s = self.get_response()
148 #print str(self) + ".select gets " + s
149 [x,y] = map(int,s.split(" "))
150 if x == -1 and y == -1:
151 #print str(self) + ".select quits the game"
153 game.final_state = "network terminated " + self.colour
157 s = self.get_response()
158 #print str(self) + ".get_move gets " + s
159 [x,y] = map(int,s.split(" "))
160 if x == -1 and y == -1:
161 #print str(self) + ".get_move quits the game"
163 game.final_state = "network terminated " + self.colour
167 def update(self, result):
169 result = result.split(" ")
170 [x,y] = map(int, result[0:2])
171 selected = self.board.grid[x][y]
172 if selected != None and selected.colour == self.colour and len(result) > 2 and not "->" in result:
173 s = self.get_response()
174 #print str(self) + ".update - receives " + str(s)
176 selected.choice = int(s[2])
178 selected.types[i] = str(s[3+i])
179 if s[3+i] == "unknown":
180 selected.types[i] = '?'+selected.types[i]
182 selected.types[i] = selected.types[i][1:]
183 selected.current_type = selected.types[selected.choice]
186 #print str(self) + ".update - ignore result " + str(result)
189 def quit(self, final_result):