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