Skip to content
Snippets Groups Projects
Commit 0cc20933 authored by Katharina's avatar Katharina
Browse files

leader

parent d6f49be7
No related branches found
No related tags found
No related merge requests found
......@@ -36,8 +36,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
......
......@@ -28,6 +28,7 @@ def broadcast(message):
# Listener function
def listen(queue, shared_data):
"""Receives messages and updates shared state."""
print(f"Listener from {shared_data['server_id']} is listening on {server_socket}")
global active_servers
while True:
try:
......@@ -75,7 +76,7 @@ def start_election(queue, shared_data):
print("Starting election...")
broadcast(f"START_ELECTION: {shared_data['server_id']}")
timeout = time.time() + 20 # 20-second timeout
timeout = time.time() + 20 # 20-second timeout, waiting for other servers
highest_id = shared_data['server_id']
while time.time() < timeout:
......@@ -100,12 +101,12 @@ def start_election(queue, shared_data):
except multiprocessing.queues.Empty:
continue
if highest_id == shared_data['server_id']:
shared_data['current_leader'] = shared_data['server_id']
broadcast(f"LEADER: {shared_data['current_leader']}")
print(f"I am the leader: {shared_data['current_leader']}")
else:
print(f"Leader election finished, leader is {highest_id}")
if highest_id == shared_data['server_id']:
shared_data['current_leader'] = shared_data['server_id']
broadcast(f"LEADER: {shared_data['current_leader']}")
print(f"I am the leader: {shared_data['current_leader']}")
else:
print(f"Leader election finished, leader is {highest_id}")
# Heartbeat function
......
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