fix: gsm_simulator doesn't need to be restarted every time
All checks were successful
/ mirror (push) Successful in 8s
All checks were successful
/ mirror (push) Successful in 8s
This commit is contained in:
parent
58f91d217c
commit
f183873104
@ -1,4 +1,3 @@
|
|||||||
#gsm_simulator.py
|
|
||||||
import socket
|
import socket
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
@ -9,13 +8,19 @@ FRAME_SIZE = 1000
|
|||||||
FRAME_DELAY = 0.02
|
FRAME_DELAY = 0.02
|
||||||
|
|
||||||
clients = []
|
clients = []
|
||||||
|
clients_lock = threading.Lock()
|
||||||
|
|
||||||
def handle_client(client_sock, client_id):
|
def handle_client(client_sock, client_id):
|
||||||
print(f"Starting handle_client for Client {client_id}")
|
print(f"Starting handle_client for Client {client_id}")
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
other_client = clients[1 - client_id] if len(clients) == 2 else None
|
while True:
|
||||||
|
other_client = None
|
||||||
|
with clients_lock:
|
||||||
|
if len(clients) == 2 and client_id < len(clients):
|
||||||
|
other_client = clients[1 - client_id]
|
||||||
print(f"Client {client_id} waiting for data, other_client exists: {other_client is not None}")
|
print(f"Client {client_id} waiting for data, other_client exists: {other_client is not None}")
|
||||||
|
|
||||||
|
try:
|
||||||
data = client_sock.recv(1024)
|
data = client_sock.recv(1024)
|
||||||
if not data:
|
if not data:
|
||||||
print(f"Client {client_id} disconnected or no data received")
|
print(f"Client {client_id} disconnected or no data received")
|
||||||
@ -26,33 +31,54 @@ def handle_client(client_sock, client_id):
|
|||||||
other_client.send(frame)
|
other_client.send(frame)
|
||||||
time.sleep(FRAME_DELAY)
|
time.sleep(FRAME_DELAY)
|
||||||
print(f"Forwarded {len(data)} bytes from Client {client_id} to Client {1 - client_id}")
|
print(f"Forwarded {len(data)} bytes from Client {client_id} to Client {1 - client_id}")
|
||||||
except Exception as e:
|
except socket.error as e:
|
||||||
print(f"Error with Client {client_id}: {e}")
|
print(f"Socket error with Client {client_id}: {e}")
|
||||||
break
|
break
|
||||||
|
finally:
|
||||||
print(f"Closing connection for Client {client_id}")
|
print(f"Closing connection for Client {client_id}")
|
||||||
|
with clients_lock:
|
||||||
|
if client_id < len(clients) and clients[client_id] == client_sock:
|
||||||
|
clients.pop(client_id)
|
||||||
|
print(f"Removed Client {client_id} from clients list")
|
||||||
client_sock.close()
|
client_sock.close()
|
||||||
|
|
||||||
def start_simulator():
|
def start_simulator():
|
||||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
server.bind((HOST, PORT))
|
server.bind((HOST, PORT))
|
||||||
server.listen(2)
|
server.listen(2)
|
||||||
print(f"GSM Simulator listening on {HOST}:{PORT}...")
|
print(f"GSM Simulator listening on {HOST}:{PORT}...")
|
||||||
|
|
||||||
while len(clients) < 2:
|
|
||||||
client_sock, addr = server.accept()
|
|
||||||
client_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Keep connection alive
|
|
||||||
clients.append(client_sock)
|
|
||||||
client_id = len(clients) - 1
|
|
||||||
print(f"Client {client_id} connected from {addr}")
|
|
||||||
threading.Thread(target=handle_client, args=(client_sock, client_id), daemon=True).start()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
time.sleep(1)
|
client_sock, addr = server.accept()
|
||||||
|
client_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Keep connection alive
|
||||||
|
with clients_lock:
|
||||||
|
if len(clients) < 2:
|
||||||
|
clients.append(client_sock)
|
||||||
|
client_id = len(clients) - 1
|
||||||
|
else:
|
||||||
|
# Replace a closed socket or reject if both slots are active
|
||||||
|
replaced = False
|
||||||
|
for i in range(len(clients)):
|
||||||
|
if clients[i].fileno() == -1: # Check if socket is closed
|
||||||
|
clients[i] = client_sock
|
||||||
|
client_id = i
|
||||||
|
replaced = True
|
||||||
|
break
|
||||||
|
if not replaced:
|
||||||
|
print(f"Rejecting new client from {addr}: max clients (2) reached")
|
||||||
|
client_sock.close()
|
||||||
|
continue
|
||||||
|
print(f"Client {client_id} connected from {addr}")
|
||||||
|
threading.Thread(target=handle_client, args=(client_sock, client_id), daemon=True).start()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Shutting down simulator...")
|
print("Shutting down simulator...")
|
||||||
|
finally:
|
||||||
|
with clients_lock:
|
||||||
for client in clients:
|
for client in clients:
|
||||||
client.close()
|
client.close()
|
||||||
|
clients.clear()
|
||||||
server.close()
|
server.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user