diff --git a/__pycache__/voting.cpython-312.pyc b/__pycache__/voting.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..08e29fc07a41cba4e1a12db4a9d952b7d7fc9226 Binary files /dev/null and b/__pycache__/voting.cpython-312.pyc differ diff --git a/server.py b/server.py index 4b1fa84c775c1d70c616c35131463e491cb1e01f..86d584e27ec95ede52a684bcb305979daea813da 100644 --- a/server.py +++ b/server.py @@ -74,8 +74,7 @@ class Server(): # if the decoded address is not in the server list add it and print the list if newServer_address not in self.serverList: - self.serverList.append(newServer_address) - + self.serverList.append(newServer_address) reply_message = MY_IP multicast_listen_sock.sendto(str.encode(reply_message), address) @@ -279,7 +278,6 @@ class Server(): ring_socket.close() def init_heartbeat(self): - self.leader_heartbeat_last_received = time.time() self.heartbeat_interval = 5 # seconds self.missed_heartbeats_limit = 5 self.missed_heartbeats = 0 @@ -310,27 +308,28 @@ class Server(): while True: data, address = multicast_socket.recvfrom(1024) if data: + self.leader_heartbeat_last_received = time.time() sender_ip = address[0] if sender_ip != MY_IP: - print(f"Received heartbeat from {address}: {data.decode()}") - # Hier kannst du die Logik hinzufügen, um den Status des Heartbeats zu verarbeiten - time.sleep(1) + print(f"Received heartbeat from {sender_ip}: {data.decode()}") + else: + time_since_last_heartbeat = time.time() - self.leader_heartbeat_last_received + if time_since_last_heartbeat > self.heartbeat_interval: + self.missed_heartbeats += 1 + print(f"Missed heartbeat detected. Count: {self.missed_heartbeats}") + if self.missed_heartbeats >= self.missed_heartbeats_limit: + print("Missed heartbeats limit reached. Initiating leader election.") - + thread_election.join() + thread_election.start() + # Reset missed heartbeats count + self.missed_heartbeats = 0 + # Code to initiate a new voting process to elect a new leader + # This could involve calling a function from voting.py or similar logic + time.sleep(self.heartbeat_interval) - # time_since_last_heartbeat = time.time() - self.leader_heartbeat_last_received - # if time_since_last_heartbeat > self.heartbeat_interval: - # self.missed_heartbeats += 1 - # print(f"Missed heartbeat detected. Count: {self.missed_heartbeats}") - # if self.missed_heartbeats >= self.missed_heartbeats_limit: - # print("Missed heartbeats limit reached. Initiating leader election.") - # # Reset missed heartbeats count - # self.missed_heartbeats = 0 - # # Code to initiate a new voting process to elect a new leader - # # This could involve calling a function from voting.py or similar logic - # time.sleep(self.heartbeat_interval) # starting all simultaneously working procedures if __name__== '__main__':