From bc0cf7ac663b50d9e33fa288c847393f3f5a1f1f Mon Sep 17 00:00:00 2001 From: krameler <philipp@huschauer.de> Date: Fri, 3 Jan 2020 02:44:54 +0100 Subject: [PATCH 1/2] Fix search for available scale_id Broke when already connected to 6 scales or when scale_id didn't get reset --- server.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server.py b/server.py index 07d094c..fc4e3e4 100644 --- a/server.py +++ b/server.py @@ -41,7 +41,6 @@ def clientThread(client_id, conn, client_ip, port): def startServer(): - scale_id = 0 soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # this is for easy starting/killing the app soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) @@ -66,16 +65,19 @@ def startServer(): while True: conn, addr = soc.accept() ip, port = str(addr[0]), str(addr[1]) - while True: # checking for available scale ids - if variables.list_scale_id[scale_id] == False: - break - scale_id += 1 + + #checking for available scale ids + try: + scale_id = variables.list_scale_id.index(False) + except ValueError: + print('Connection refused: No available scale_id found!') + conn.close() + continue print('Accepting connection from ' + ip + ':' + port + ' Id is:' + str(scale_id)) try: Thread(target=clientThread, args=(scale_id, conn, ip, port), daemon=True).start() variables.list_scale_id[scale_id] = True - scale_id = 0 except: print("Terrible error!") import traceback From ad01dd1464f04e9f64edf77c203db78a2ab620ad Mon Sep 17 00:00:00 2001 From: krameler <philipp@huschauer.de> Date: Sat, 4 Jan 2020 20:31:03 +0100 Subject: [PATCH 2/2] Fixup lists and queues to accommodate 6 scales/clients --- variables.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/variables.py b/variables.py index 24c103e..128f7f1 100644 --- a/variables.py +++ b/variables.py @@ -4,29 +4,29 @@ en_log = False en_blink = False -list_scale_id = [False, False, False, False, False] -list_client_ip = ["NC", "NC", "NC", "NC", "NC"] +list_scale_id = [False, False, False, False, False, False] +list_client_ip = ["NC", "NC", "NC", "NC", "NC", "NC"] list_cell_units = ["", "", "", "", "", ""] list_scale_mom = ["", "", "", "", "", ""] port_server = 4242 queues_recv = [] -for i in range(5): +for i in range(6): queues_recv.append(queue.Queue()) queues_send = [] -for i in range(5): +for i in range(6): queues_send.append(queue.Queue()) queues_ack = [] -for i in range(5): +for i in range(6): queues_ack.append(queue.Queue()) queues_mes = [] -for i in range(5): +for i in range(6): queues_mes.append(queue.LifoQueue()) lock_file_mes = [] -for i in range(5): +for i in range(6): lock = Lock() lock_file_mes.append(lock)