-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.py
273 lines (251 loc) · 11 KB
/
web_server.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
import socket, threading, json
host = "127.0.0.1"
port = 8550
web_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
web_server.bind((host, port))
web_server.listen(1)
clients = []
waiting_singles = []
waiting_doubles = []
ready_servers = []
games = []
lock = threading.Lock()
class game_play:
user1: socket.socket
user2: socket.socket
tic_server: socket.socket
def __init__(self, user1, user2, server):
self.user1 = user1
self.user2 = user2
self.tic_server = server
class ready_server:
tic_server: socket.socket
status: str
def __init__(self, server):
self.tic_server = server
self.status = 'WAIT'
def handle(cli: socket.socket):
global waiting_doubles, waiting_singles
while True:
try:
message = json.loads(cli.recv(1024).decode("ascii"))
print(message)
ty = message["type"]
if ty == 'create_server':
lock.acquire()
ready_servers.append(ready_server(cli))
message = '{"type":"accept"}'
cli.send(message.encode('ascii'))
if len(waiting_doubles) > 1:
us1 = waiting_doubles.pop(0)
us2 = waiting_doubles.pop(0)
games.append(game_play(us1, us2, cli))
table = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
feed1 = '{"type":"accept", "player":1, "table":' + str(table) + '}'
feed2 = '{"type":"accept", "player":2, "table":' + str(table) + '}'
us1.send(feed1.encode('ascii'))
us2.send(feed2.encode('ascii'))
for ser in ready_servers:
if ser.tic_server == cli:
ser.status = "BUSY"
break
feed = '{"type":"INIT", "single":false}'
cli.send(feed.encode('ascii'))
elif waiting_singles:
us = waiting_singles.pop(0)
games.append(game_play(us, None, cli))
table = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
feed = '{"type":"accept", "player":1, "table":' + str(table) + '}'
us.send(feed.encode('ascii'))
for ser in ready_servers:
if ser.tic_server == cli:
ser.status = "BUSY"
break
feed1 = '{"type":"INIT", "single":true}'
cli.send(feed1.encode('ascii'))
lock.release()
elif ty == 'start_single_game':
found = False
lock.acquire()
for ser in ready_servers:
if ser.status == 'WAIT':
tic_server = ser.tic_server
message = '{"type":"INIT", "single":true}'
tic_server.send(message.encode('ascii'))
games.append(game_play(cli, None, tic_server))
ser.status = 'BUSY'
table = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
message = '{"type":"accept", "player":1, "table":' + str(table) + '}'
cli.send(message.encode('ascii'))
found = True
break
if not found:
message = '{"type":"decline", "message":"all servers are busy. please wait..."}'
cli.send(message.encode('ascii'))
waiting_singles.append(cli)
lock.release()
elif ty == 'start_double_game':
lock.acquire()
if len(waiting_doubles) == 0:
feed = '{"type":"decline", "message":"no competitor is available. please wait..."}'
cli.send(feed.encode('ascii'))
waiting_doubles.append(cli)
else:
found = False
waiting_doubles.append(cli)
for ser in ready_servers:
if ser.status == 'WAIT':
tic_server = ser.tic_server
feed = '{"type":"INIT", "single":false}'
tic_server.send(feed.encode('ascii'))
us1 = waiting_doubles.pop(0)
us2 = waiting_doubles.pop(0)
games.append(game_play(us1, us2, tic_server))
ser.status = 'BUSY'
table = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
feed1 = '{"type":"accept", "player":1, "table":' + str(table) + '}'
feed2 = '{"type":"accept", "player":2, "table":' + str(table) + '}'
us1.send(feed1.encode('ascii'))
us2.send(feed2.encode('ascii'))
found = True
break
if (not found) | (len(waiting_doubles) > 0):
message = '{"type":"decline", "message":"all servers are busy. please wait..."}'
cli.send(message.encode('ascii'))
lock.release()
elif ty == 'move':
tic_server: socket.socket
player: int
for g in games:
if g.user1 == cli:
tic_server = g.tic_server
player = 1
break
elif g.user2 == cli:
tic_server = g.tic_server
player = 2
message["sender"] = player
tic_server.send(json.dumps(message).encode('ascii'))
elif ty == 'exit':
game: game_play
player: int
found = False
for g in games:
if g.user1 == cli:
game = g
player = 1
found = True
break
elif g.user2 == cli:
game = g
player = 2
found = True
break
lock.acquire()
if found:
feed = '{"type":"forfeit", "sender":%d }' % player
game.tic_server.send(feed.encode("ascii"))
else:
if cli in waiting_singles:
waiting_singles.remove(cli)
elif cli in waiting_doubles:
waiting_doubles.remove(cli)
lock.release()
elif ty in ['send', 'get']:
tic_server: socket.socket
player: int
for g in games:
if g.user1 == cli:
tic_server = g.tic_server
player = 1
break
elif g.user2 == cli:
tic_server = g.tic_server
player = 2
break
message["sender"] = player
tic_server.send(json.dumps(message).encode('ascii'))
else:
game: game_play
for g in games:
if g.tic_server == cli:
game = g
break
user1 = game.user1
user2 = game.user2
if ty == 'finish':
games.remove(game)
lock.acquire()
if len(waiting_doubles) > 1:
us1 = waiting_doubles.pop(0)
us2 = waiting_doubles.pop(0)
games.append(game_play(us1, us2, cli))
table = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
feed1 = '{"type":"accept", "player":1, "table":' + str(table) + '}'
feed2 = '{"type":"accept", "player":2, "table":' + str(table) + '}'
us1.send(feed1.encode('ascii'))
us2.send(feed2.encode('ascii'))
feed = '{"type":"INIT", "single":false}'
cli.send(feed.encode('ascii'))
elif waiting_singles:
us = waiting_singles.pop(0)
games.append(game_play(us, None, cli))
table = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
feed = '{"type":"accept", "player":1, "table":' + str(table) + '}'
us.send(feed.encode('ascii'))
feed = '{"type":"INIT", "single":true}'
cli.send(feed.encode('ascii'))
else:
for ser in ready_servers:
if ser.tic_server == cli:
ser.status = "WAIT"
break
lock.release()
if 1 in message["rec"]:
user1.send(json.dumps(message).encode('ascii'))
if (2 in message["rec"]) and user2:
user2.send(json.dumps(message).encode('ascii'))
except socket.error:
print("Error in handling")
clients.remove(cli)
for g in games:
if (g.user1 == cli) | (g.user2 == cli) | (g.tic_server == cli):
games.remove(g)
feed = '{"type":"finish", "message":"The game interrupt.", "table":[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]}'
if g.user1 != cli:
g.user1.send(feed.encode('ascii'))
if g.user2 != cli and g.user2:
g.user2.send(feed.encode('ascii'))
for ser in ready_servers:
if ser.tic_server == g.tic_server:
ser.status = "WAIT"
break
break
for ser in ready_servers:
if ser.tic_server == cli:
ready_servers.remove(ser)
break
if cli in waiting_doubles:
waiting_doubles.remove(cli)
elif cli in waiting_singles:
waiting_singles.remove(cli)
cli.close()
break
def res():
while True:
client, address = web_server.accept()
print(f'connecting with {address}')
clients.append(client)
thread = threading.Thread(target=handle, args=(client,))
thread.start()
def echo():
while True:
cam = input()
if cam == '/users':
print("number of users: " + str(len(clients) - len(ready_servers)))
else:
print("please enter valid command: /users")
client_thread = threading.Thread(target=res)
client_thread.start()
echo_thread = threading.Thread(target=echo)
echo_thread.start()