From 3d4feadec825d2b6a07eeefdd5ff63086162f728 Mon Sep 17 00:00:00 2001
From: Jeffrey Zhang <jeffreyzhang01@gmail.com>
Date: Mon, 28 Feb 2022 11:16:43 -0600
Subject: [PATCH] Correctly wait for clients to be set up

---
 SelectServer.py         | 18 +++++++++++-------
 TransactionDeliverer.py |  2 --
 main.py                 |  3 ---
 3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/SelectServer.py b/SelectServer.py
index 88d4640..e863ac3 100644
--- a/SelectServer.py
+++ b/SelectServer.py
@@ -2,6 +2,7 @@ import socket
 import pickle
 import select
 import threading
+import time
 from dataclasses import replace
 from Messages import Message
 from TransactionDeliverer import TransactionDeliverer
@@ -18,14 +19,14 @@ class Client:
         message_len = self.socket.recv(2)
 
         if len(message_len) < 2:
-            print(f"Less than two bytes - {self.client_name} - {len(message_len)}")
+            #print(f"Less than two bytes - {self.client_name} - {len(message_len)}")
             return False
 
         message_len = int.from_bytes(message_len, 'big')
         data = self.socket.recv(message_len)
 
         if len(data) < message_len:
-            print("Less than msg len")
+            #print("Less than msg len")
             return False
         
         obj: Message = pickle.loads(data)
@@ -37,6 +38,10 @@ class Client:
         if not self.client_name:
             self.client_name = obj.sender
 
+        # Client sockets may not be set up yet, wait until they are
+        while not self.client_name in self.td.client_sockets:
+            time.sleep(1)
+
         rebroadcast = obj.receive_action(self.td)
 
         # Rebroadcast for reliable multicast
@@ -69,7 +74,7 @@ class Server(threading.Thread):
         self.waiting_sockets = set([self.server_socket])
 
     def disconnect_client(self, client: Client, sock: socket.socket):
-        print(f"Disconnected client {client.client_name}")
+        #print(f"Disconnected client {client.client_name}")
         self.waiting_sockets.remove(sock)
         del self.clients[sock]
         sock.shutdown(socket.SHUT_RDWR)
@@ -90,18 +95,18 @@ class Server(threading.Thread):
             client = Client(self.td, client_socket, ip, port)
             self.clients[client_socket] = client
             self.waiting_sockets.add(client_socket)
-            print(f"Accepted connection from ip: {ip}, port: {port}")
+            #print(f"Accepted connection from ip: {ip}, port: {port}")
         else:
             # Read from node client
             client = self.clients[sock]
             if not client.handle():
                 # No data, disconnect
-                print(f"No data - {client.client_name} - disconnect")
+                #print(f"No data - {client.client_name} - disconnect")
                 self.disconnect_client(client, sock)
 
     def handle_err_socket(self, sock: socket.socket):
         client = self.clients[sock]
-        print(f"Client error - {client.client_name}")
+        #print(f"Client error - {client.client_name}")
         self.disconnect_client(client, sock)
 
     # Called by thread start
@@ -126,7 +131,6 @@ class Server(threading.Thread):
         self.shutdown()
 
     def shutdown(self):
-        print("Server shutdown")
         for s in self.waiting_sockets:
             s.shutdown(socket.SHUT_RDWR)
             s.close()
diff --git a/TransactionDeliverer.py b/TransactionDeliverer.py
index ef44541..80a0e40 100644
--- a/TransactionDeliverer.py
+++ b/TransactionDeliverer.py
@@ -52,7 +52,6 @@ class TransactionDeliverer:
         
     def new_transaction_message(self, transaction: Transaction):
         # Receives a transaction and builds the message to be sending out to all other nodes
-        #print("Sending message")
         transaction_message = TransactionMessage(str(uuid4()), self.node_name, self.node_name, transaction, str(uuid4()), False)
         # Multicasting race condition prevention
         self.received_messages.add(transaction_message.message_id)
@@ -172,7 +171,6 @@ class TransactionDeliverer:
                 sock.close()
 
     def shutdown(self):
-        print("TD shutdown")
         for sock in self.client_sockets.values():
             sock.shutdown(socket.SHUT_RDWR)
             sock.close()
diff --git a/main.py b/main.py
index 9bbb211..686f97f 100644
--- a/main.py
+++ b/main.py
@@ -3,12 +3,10 @@
 import sys
 import threading
 from TransactionDeliverer import TransactionDeliverer
-#from MessageServer import start_server
 from SelectServer import Server
 from Messages import TransactionMessage
 from Transaction import Transaction, TransactionType
 from signal import SIGINT, signal
-import time
 
 is_running = True
 
@@ -67,7 +65,6 @@ if __name__ == "__main__":
         server_thread = Server(td, addresses[0], ports[0])
         server_thread.start()
         td.bring_up_clients()
-        time.sleep(2)
         process_inputs(td)
         server_thread.end.set()
         server_thread.join()
\ No newline at end of file
-- 
GitLab