Improve main thread logging, commit stop_flag

This commit is contained in:
Xevion
2022-06-12 16:11:14 -05:00
parent 7cdafc7eb0
commit 5050a1154f

View File

@@ -1,5 +1,6 @@
import logging import logging
import socket import socket
import sys
import threading import threading
from server import handler from server import handler
@@ -21,21 +22,22 @@ clients = []
# Receiving / Listening Function # Receiving / Listening Function
def receive(): def receive():
stop_flag: bool = False
try: try:
logger.debug('Waiting for connections...')
while True: while True:
conn = None
try: try:
# Accept Connection # Accept Connection
logger.debug('Waiting for connections...')
conn, address = server.accept() conn, address = server.accept()
logger.info(f"New connection from {address}") logger.info(f"New connection from {address}")
client = handler.Client(conn, address, clients) client = handler.Client(conn, address, clients, lambda: stop_flag)
clients.append(client) clients.append(client)
client.request_nickname() client.request_nickname()
# Inform all clients of new client, give new client connections list # Inform all clients of new client, give new client connections list
if len(clients) > 0:
logger.debug('Informing all connected clients of incoming connection.')
for client in clients: for client in clients:
client.send_connections_list() client.send_connections_list()
@@ -50,8 +52,9 @@ def receive():
logger.critical(e, exc_info=e) logger.critical(e, exc_info=e)
break break
except KeyboardInterrupt: except KeyboardInterrupt:
logger.info('User stopped server manually.') logger.info('User stopped server manually. Enabling stop flag.')
return stop_flag = True
sys.exit()
if __name__ == '__main__': if __name__ == '__main__':