send_ack_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)#reuse of the address
send_ack_socket.sendto(msg.encode('utf-8'),(right_neighbour,acknowledgement_port))# Send the acknowledgment message to the right neighbor using the acknowledgement_port
send_ack_socket.close()# Close the socket after sending the message
members_IP=members_IP2# Update the local members list
print(f"Ring update received: {members_IP}")
send_acknowledgement()# Send an acknowledgment for the received update
send_update_to_ring()# Forward the updated member list to the next neighbor
send_update_to_ring()# Forward the updated members list to the next neighbor
exceptjson.JSONDecodeError:
print("Error decoding the JSON data.")# Handle errors in decoding the JSON data
...
...
@@ -199,12 +204,12 @@ def new_server_in_ring():
new_server_ip,new_server_port=addr# Extract the IP and port of the new server from the received address
new_IP=data.decode()
new_IP=new_IP.split(": ")[1]# Extract the IP address from the message
ifnew_IPinmembers_IP:# Check if the IP already exists. This might happen if a server temporarily lost connection. #######Tritt das überhaupt ein? In Gruppe diskutieren
f"There are already servers. I am your leader: {ip_address}".encode()# Create a message for the new server# If the server already exists, send the Welcome Message
ifnew_IPinmembers_IP:# Check if the IP already exists. This might happen if a server temporarily lost connection.
f"There are already servers. I am your leader: {ip_address}".encode()# Create a message for the new server. If the server already exists, send the Welcome Message
else:
members_IP.append(new_IP)# If the server is new, add its IP to the list
msg=f"There are already servers. I am your leader: {ip_address}".encode()# Create a message for the new server
#AttributeError: 'bytes' object has no attribute 'encode'. Did you mean: 'decode'?
sock.sendto(msg,(new_IP,new_server_port))# Send the greeting message back to the new server
print(f"The updated IP_Ring is: {members_IP}")
send_update_to_ring()# Update the ring topology
...
...
@@ -217,15 +222,15 @@ def server_enters():
"""
globalmembers_UUID
globalleader_ip
max_retries=3
max_retries=3#retry of the sending for a maximum of 3 times
success=False
intervall=2#time to wait fpr a response
intervall=2#time to wait for a response
msg=f"I am new: {ip_address}".encode()# Greeting message from the new server
forattemptinrange(max_retries):
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)# Create a UDP socket
sock.setsockopt(socket.SOL_SOCKET,socket.SO_BROADCAST,1)# Set the socket to broadcast mode
sock.settimeout(intervall)# Set a timeout to wait for a response --> ggf. anpassen, je nach geschwindigkeit mit Handy Internet
sock.settimeout(intervall)# Set a timeout to wait for a response
server_client_heartbeat.sendto(msg.encode('utf-8'),(broadcast_ip,heartbeat_client_broadcast_port))# # Send the broadcast message to the specified broadcast IP and port
server_client_heartbeat.sendto(msg.encode('utf-8'),(broadcast_ip,heartbeat_client_broadcast_port))# Send the broadcast message to the specified broadcast IP and port
server_client_heartbeat.close()# Close the socket to free up resources
time.sleep(2)# Wait for 15 seconds before sending the next heartbeat
time.sleep(2)# Wait for 2 seconds before sending the next heartbeat
########################### End - Client Heartbeat ###########################
########################### Start - observation value changes leader ###########################