-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_tictactoe.py
304 lines (272 loc) · 11.8 KB
/
server_tictactoe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from server import Server
from typing import Tuple
import json
import socket
from lib.constant import (
SYN_FLAG,
ACK_FLAG,
FIN_ACK_FLAG,
PAYLOAD_SIZE,
SYN_ACK_FLAG,
TIMEOUT_LISTEN,
WINDOW_SIZE,
)
from lib.segment import Segment
class ServerTictactoe(Server):
def __init__(self):
super().__init__()
self.board = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "],
]
self.current_player = 0
self.counter = 0
self.winner = None
self.game_over = False
def print_board(self):
print("\n 0 1 2")
for row in range(3):
print(row, end=" ")
for col in range(3):
print(f"| {self.board[row][col]} ", end="")
print("|")
if row != 2:
print(" -----------")
def check_if_won(self):
# Checks if there is a winner in the current game state
for row in range(3):
if self.board[row][0] == self.board[row][1] == self.board[row][2] != " ":
self.winner = self.board[row][0]
self.game_over = True
return True
for col in range(3):
if self.board[0][col] == self.board[1][col] == self.board[2][col] != " ":
self.winner = self.board[0][col]
self.game_over = True
return True
if self.board[0][0] == self.board[1][1] == self.board[2][2] != " ":
self.winner = self.board[0][0]
self.game_over = True
return True
if self.board[0][2] == self.board[1][1] == self.board[2][2] != " ":
self.winner = self.board[0][2]
self.game_over = True
return True
return False
def apply_move(self, move, player):
# Apply move to game board and check winner or tie
if self.game_over:
return
self.counter += 1
row, col = int(move[0]), int(move[1])
self.board[row][col] = player
self.print_board()
if self.check_if_won():
if self.winner == "O":
print("You win!")
elif self.winner == "X":
print("You lose!")
else:
if self.counter == 9:
print("It's a tie!")
def display_board(self):
print("\n 0 1 2")
for row in range(3):
print(row, end=" ")
for col in range(3):
print(f"| {self.board[row][col]} ", end="")
print("|")
if row != 2:
print(" -----------")
def listen_for_clients(self):
self.logger.debug("[!] Listening to broadcast address for clients.")
while len(self.client_list) < 2:
try:
client = self.connection.listen_single_segment(TIMEOUT_LISTEN)
client_address = client[1]
ip, port = client_address
self.client_list.append(client_address)
self.logger.debug(f"[!] Received request from {ip}:{port}")
except socket.timeout:
if len(self.client_list) == 0:
self.logger.error("[!] Timeout error for listening client. Exiting")
else:
self.logger.warning("[!] Timeout error for listening client")
break
# -- Game Handler --
def start_game(self):
self.logger.debug("[!] Starting game...")
for client in self.client_list:
self.three_way_handshake(client)
self.info_transfer(client)
def info_transfer(self, client_address: Tuple[str, int]):
num_of_segment = 6
sequence_base = 2
reset_conn = False
request_number = 2
while sequence_base < num_of_segment and not reset_conn:
# for i in range(sequence_max):
# # Start sending segment x
# self.logger.debug(
# f"[!] [Client {client_address[0]}:{client_address[1]}] Sending Segment {sequence_base + i}"
# )
# if i + sequence_base < num_of_segment:
# self.connection.send_data(
# self.list_segment[i + sequence_base - 2].get_bytes(),
# client_address,
# )
for i in range(4):
try:
data, response_address = self.get_segment(client_address)
segment = Segment()
segment.set_from_bytes(data)
# Various segment conditions
if (
client_address[1] == response_address[1]
and segment.get_flag() == ACK_FLAG
and segment.get_header()["ack_num"] == sequence_base + 1
):
self.logger.debug(
f"[!] [Client {client_address[0]}:{client_address[1]}] Received ACK {sequence_base + 1}"
)
sequence_base += 1
elif segment.get_flag() == SYN_ACK_FLAG:
self.logger.debug(
f"[!] [Client {client_address[0]}:{client_address[1]}] Received SYN ACK Flag, client ask to reset connection"
)
reset_conn = True
break
elif (
self.segment.valid_checksum()
and self.segment.get_header()["seq_num"] == request_number
):
payload = self.segment.get_payload()
self.board = payload['board']
self.current_player = payload['current_player']
self.winner = payload['winner']
self.logger.debug(
f"[!] [Server {client_address[0]}:{client_address[1]}] Received Segment {request_number}"
)
self.logger.debug(
f"[!] [Server {client_address[0]}:{client_address[1]}] Sending ACK {request_number + 1}"
)
request_number += 1
self.send_ack(client_address, request_number)
move = input("Enter your move (row,column): ")
self.apply_move(move.split(","), "O")
response = Segment()
game_info = {
"board": self.board,
"current_player": self.current_player,
"winner": self.winner,
}
json_data = json.dumps(game_info)
header = response.get_header()
header["seq_num"] = sequence_base + 1
header["ack_num"] = sequence_base
response.set_header(header)
response.set_payload(json_data.encode())
self.connection.send_data(response.get_bytes(), client_address)
if (self.winner != None or self.counter == 9):
break
elif segment.get_flag() != ACK_FLAG:
self.logger.warning(
f"[!] [Client {client_address[0]}:{client_address[1]}] Received Wrong Flag"
)
else:
self.logger.warning(
f"[!] [Client {client_address[0]}:{client_address[1]}] Received Wrong ACK"
)
request_number = segment.get_header()["ack_num"]
if request_number > sequence_base:
sequence_base = request_number
except socket.timeout:
self.logger.error(
f"[!] [Client {client_address[0]}:{client_address[1]}] ACK response timeout. Resending previous sequence number"
)
if reset_conn:
self.three_way_handshake(client_address)
self.file_transfer(client_address)
else:
self.logger.info(
f"[!] [Client {client_address[0]}:{client_address[1]}] File transfer complete. Sending FIN"
)
sendFIN = Segment()
sendFIN.set_flag(["FIN"])
self.connection.send_data(sendFIN.get_bytes(), client_address)
is_ack = False
game_info = {
"board": self.board,
"current_player": self.current_player,
"winner": self.winner,
}
json_data = json.dumps(game_info)
self.connection.send_data(json_data.encode(), client_address)
# Wait for ack
while not is_ack:
try:
data, response_address = self.get_segment(client_address)
segment = Segment()
segment.set_from_bytes(data)
if (
client_address[1] == response_address[1]
and segment.get_flag() == FIN_ACK_FLAG
):
self.logger.debug(
f"[!] [Client {client_address[0]}:{client_address[1]}] Received FIN-ACK"
)
sequence_base += 1
is_ack = True
if self.parallel:
self.parallel_client_list.pop(client_address)
except socket.timeout:
self.logger.error(
f"[!] [Client {client_address[0]}:{client_address[1]}] ACK response timeout. Resending FIN"
)
self.connection.send_data(sendFIN.get_bytes(), client_address)
# send ACK and tear down connection
self.logger.info(
f"[!] [Client {client_address[0]}:{client_address[1]}] Sending ACK. Tearing down connection."
)
segmentACK = Segment()
segmentACK.set_flag(["ACK"])
self.connection.send_data(segmentACK.get_bytes(), client_address)
def three_way_handshake(self, client_address: Tuple[str, int]) -> bool:
return super().three_way_handshake(client_address)
def make_move(self, position: int):
# Make a move on the Tic Tac Toe board
if 0 <= position < 9 and self.board[position] == " ":
self.board[position] = "X" if self.current_player == 0 else "O"
return True
else:
print("Invalid move. Try again.")
return False
def check_winner(self) -> bool:
for row in range(3):
if self.board[row][0] == self.board[row][1] == self.board[row][2] != " ":
self.winner = self.board[row][0]
self.game_over = True
return True
for col in range(3):
if self.board[0][col] == self.board[1][col] == self.board[2][col] != " ":
self.winner = self.board[0][col]
self.game_over = True
return True
if self.board[0][0] == self.board[1][1] == self.board[2][2] != " ":
self.winner = self.board[0][0]
self.game_over = True
return True
if self.board[0][2] == self.board[1][1] == self.board[2][2] != " ":
self.winner = self.board[0][2]
self.game_over = True
return True
return False
def switch_player(self):
# Switch to the next player
self.current_player = 1 - self.current_player
if __name__ == "__main__":
main = ServerTictactoe()
main.listen_for_clients()
if not main.parallel:
main.start_game()