Skip to content
Snippets Groups Projects
Commit 7e98b804 authored by Katharina's avatar Katharina
Browse files

f

parent d5e72d01
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,8 @@ MY_IP = socket.gethostbyname(MY_HOST)
print(f"Listening for leader response on {broadcast_ip}:{broadcast_port}...")
print(MY_HOST,MY_IP)
#stop_listening = False # control for listener
# create client-socket for broadcast
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
......@@ -36,8 +38,8 @@ def listen():
# continue
#ignores broadcast messages with own ip #################please enable it after testing!!!!!!!!!!!!!!!!!!!S
if address[0]==MY_IP:
continue
#if address[0]==MY_IP:
# continue
#####identifyLEADER############
# Erkennen von LEADER-Nachrichten
......@@ -45,8 +47,11 @@ def listen():
leader_uuid = decoded_message.split(": ")[1]
print(f"Leader discovered: {leader_uuid} at {address[0]}:{address[1]}")
leader_address = (address[0], broadcast_port) # IP-Adresse des Leaders speichern
#print(leader_address)
continue
#################################
if "HEARTBEAT:" in decoded_message:
continue
print(decoded_message)
......@@ -64,6 +69,8 @@ def request_leader():
timeout = time.time() + 5 # 10 Sekunden auf Antwort warten
while time.time() < timeout:
if leader_address:
#print("leader address:", leader_address)
#client_socket.sendto("REQUEST_LEAD finish".encode(), leader_address)
return
time.sleep(0.5) # Warten, bis eine Antwort vom Leader eintrifft
#print("No leader found. Unable to send messages.")
......@@ -84,7 +91,7 @@ def sender():
just_nickname= f"{nickname} entered the chat".encode()
#client_socket.sendto(just_nickname, (broadcast_ip, broadcast_port))
client_socket.sendto(just_nickname, leader_address)
#print("is it leader adresse here", leader_address)
while True:
#allows the client to send any message
message = input("")
......
......@@ -49,7 +49,7 @@ def listen(queue, shared_data):
if "REQUEST_LEAD" in decoded_message:
if shared_data['current_leader']:
while True:
#while "REQUEST_LEAD finish" not in decoded_message:
response = f"LEADER: {shared_data['current_leader']}".encode()
time.sleep(2) #wait before sending response that client can prepare for answer
server_socket.sendto(response, (broadcast_ip, broadcast_port))
......@@ -82,9 +82,9 @@ def start_election(queue, shared_data):
while time.time() < timeout:
try:
message = queue.get(timeout=1) #waits 1 sec for a message
if shared_data['server_id']==sender_uuid:
continue
elif "START_ELECTION" in message:
#if shared_data['server_id']==sender_uuid:
# continue
if "START_ELECTION" in message:
sender_uuid = message.split(": ")[1]
active_servers[sender_uuid] = time.time()
print(f"Received UUID for election: {sender_uuid}")
......
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