-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
319 lines (278 loc) · 12.8 KB
/
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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import socket
import sys
import threading
import logging
from typing import List
logging.basicConfig(level=logging.INFO,
format='%(name)s: %(message)s',
)
class Socket_address():
def __init__(self, ip, port):
self.__ip = ip
self.__port = port
def getIp(self):
return self.__ip
def setIp(self, ip):
self.__ip = ip
def getPort(self):
return self.__port
def setPort(self, port):
self.__port = port
def __eq__(self, address):
if isinstance(address, Socket_address):
return self.getIp() == address.getIp() and self.getPort() == address.getPort()
return False
class Client():
def __init__(self, socket: socket.socket, address: Socket_address, username: str):
self.__socket = socket
self.__address = address
self.__username = username
def getSocket(self):
return self.__socket
def setSocket(self, socket):
self.__socket = socket
def getAddress(self):
return self.__address
def setAddress(self, address):
self.__address = address
def getUsername(self):
return self.__username
def setUsername(self, username):
self.__username = username
def __eq__(self, client):
if isinstance(client, Client):
return self.getUsername() == client.getUsername()
return False
class Server:
def __init__(self, server_address, close_event):
self.host = server_address[0]
self.port = server_address[1]
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clients: List[Client] = list()
self.close_event = close_event
self.lock = threading.Lock()
self.logger = logging.getLogger("Server")
self.chat_logger = logging.getLogger("Chat")
def start(self):
self.server_socket.setsockopt(
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind((self.host, self.port))
self.server_socket.listen(5)
self.logger.info(f"Listening on {self.host}:{self.port}")
while not self.close_event.is_set():
try:
client_socket, client_address = self.server_socket.accept()
self.logger.info(
f"Connection from client {client_address[0]}:{client_address[1]}")
username = self._request_client_username(
client_socket, client_address, "Server << Enter your username:", 0)
if username:
# assign a thread for each new subscribed client
client_handler = threading.Thread(
target=self._handle_client, args=(Client(socket=client_socket, address=client_address, username=username),))
client_handler.start()
except KeyboardInterrupt:
self._close_server()
except socket.error as e:
if 'bad file descriptor' in str(e):
self.logger.error(
"Server socket is closed or shutdown..!")
else:
self.logger.error(
f"Error accepting or handling new connections: {e}")
# handle incoming messages for each client
def _handle_client(self, client: Client):
try:
while not self.close_event.is_set():
data = client.getSocket().recv(2048)
if not data:
break
else:
message = data.decode().strip()
if message == "close":
self._disconnect_client(client)
else:
client_username = client.getUsername()
self.chat_logger.info(
f"{client_username} << {message}")
with self.lock:
self._broadcast(
f"{client_username} << {message}".encode(),
[client_obj for client_obj in self.clients if client_obj.getUsername(
) != client_username]
)
except KeyboardInterrupt:
self._close_server()
except socket.error as e:
# send a close signal to client's socket when ERROR
self.logger.warning(f"Error handling client: {e}")
# close connection with client on ERROR
with self.lock:
self.clients.remove(client)
def _broadcast(self, message, clients: list[Client]):
for client in clients:
try:
client.getSocket().send(message)
except Exception as e:
self.logger.error(f"Error broadcasting to client: {e}")
# close connection with client on ERROR
with self.lock:
self.clients.remove(client)
def _request_client_username(self, client_socket, client_address, message, nbr_of_attempts):
max_nbr_of_attempts = 3
if nbr_of_attempts >= max_nbr_of_attempts:
# close client's connection
client_socket.send(
"close".encode())
else:
# request username
client_socket.send(message.encode())
# wait for client's response
username = client_socket.recv(2048).decode().strip()
if len(username) > 0:
# block access to self.clients variable
self.lock.acquire()
# check if username is not already taken
if username not in [client.getUsername() for client in self.clients]:
# username accepted
self.clients.append(
Client(
socket=client_socket,
address=Socket_address(
ip=client_address[0],
port=client_address[1]
),
username=username
)
)
# permet access to self.clients variable
self.lock.release()
client_socket.send(
f"Server << Welcome {username} :)".encode())
self.logger.info(f"{username} joined the chatroom")
# inform other clients
with self.lock:
self._broadcast(
f"{username} joined the chatroom".encode(), [client for client in self.clients if client.getUsername() != username])
return username
else:
# permet access to self.clients variable if first condition not satisfied.
# Preventing undefinete block of access to self.clients
self.lock.release()
# request another username
self._request_client_username(
client_socket, client_address, f"Server << Username is already taken. Connection will be closed on no attempts left. {3 - nbr_of_attempts - 1} attempts left. Enter a different username:", nbr_of_attempts + 1)
else:
# invalide username format
self._request_client_username(
client_socket, client_address, f"Server << Invalid username. Connection will be closed on no attempts left. {3 - nbr_of_attempts - 1} attempts left. Enter a different username:", nbr_of_attempts + 1)
def delete_client_from_clients_list(self, client_to_delete: Client):
return [client for client in self.clients if client.getUsername() != client_to_delete.getUsername()]
def _disconnect_client(self, client: Client):
client_username = client.getUsername()
# close connection with client
with self.lock:
self.clients.remove(client)
self.logger.info(
f"{client_username} has disconnected")
with self.lock:
self._broadcast(f"Server << {client_username} has disconnected".encode(),
[client_obj for client_obj in self.clients if client_obj != client])
def _close_server(self):
self.logger.warning(
f"Server is shutting down. Informing clients...")
# send closing message to all subscribed clients
for client in self.clients:
client_username = client.getUsername()
try:
self.logger.warning(
f"Informing client {client_username}...")
# close connection from client side
client_socket = client.getSocket()
client_socket.send("close".encode())
# close connection from server side
client_socket.shutdown(socket.SHUT_RDWR)
client_socket.close()
except Exception as e:
self.logger.error(
f"Error sending shutdown message to {client_username}: {e}")
# clear clients list
with self.lock:
self.clients.clear()
try:
# Close the server socket
if not sys.stdin.closed:
sys.stdin.close()
self.server_socket.shutdown(socket.SHUT_RDWR)
self.server_socket.close()
self.close_event.set()
except Exception as e:
self.logger.error({e})
self.logger.warning("Server has shut down.")
class UserInputHandler:
def __init__(self, server: Server, close_event):
self.server = server
self.close_event = close_event
self.logger = logging.getLogger("Input")
def start(self):
try:
while not self.close_event.is_set():
self.logger.info(
"Enter message (to close connection, type 'close .' to shut down the server, or 'close /username/' to disconnect a user):")
message = sys.stdin.readline().strip()
if message.split(" ")[0] == "close":
message_array = message.split(" ")
if len(message_array) == 1:
# Neither the host or the username is specified
self.logger.warning(
"Invalid argument. Type 'close .' to shut down the server, or 'close /username/' to disconnect a user")
else:
target_to_close = message_array[1]
if target_to_close == ".":
# "." stands for the host machine, which is the server
# closing the server
self.server._close_server()
else:
# close the client's socket
client_username = target_to_close
# if client_username != "":
found = False
for client in self.server.clients:
if client_username == client.getUsername():
found = True
client.getSocket().send("close".encode())
self.server._disconnect_client(client)
if not found:
self.logger.warning(
"Username does not exist. Type 'close .' to shut down the server, or 'close /username/' to disconnect a user")
# else:
# self.logger.warning(
# "Invalid argument. Type 'close .' to shut down the server, or 'close /username/' to disconnect a user")
else:
# broadcast a message to all subscribed clients
self.server._broadcast(
f"Server << {message}".encode(), self.server.clients)
except Exception as e:
if 'I/O operation on closed file.' in {e}:
pass
def stop(self):
self.close_event.set()
if __name__ == "__main__":
close_event = threading.Event()
server = Server(("127.0.0.1", 12345), close_event)
start_thread = threading.Thread(target=server.start)
user_input_handler = UserInputHandler(server, close_event)
user_input_thread = threading.Thread(target=user_input_handler.start)
try:
start_thread.start()
# send_thread.start()
user_input_thread.start()
# waiting for the closing flag
server.close_event.wait()
except KeyboardInterrupt:
close_event.set()
logging.warning("Server has shut down.")
finally:
user_input_handler.stop()
user_input_thread.join()
logging.info(f"Exiting...")