Skip to content
Snippets Groups Projects
Commit 3d4feade authored by Jeffrey Zhang's avatar Jeffrey Zhang
Browse files

Correctly wait for clients to be set up

parent 3ebf343d
No related branches found
No related tags found
No related merge requests found
......@@ -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()
......
......@@ -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()
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment