Skip to content
Snippets Groups Projects
Commit b3e0975e authored by Robin Leber's avatar Robin Leber
Browse files

leader election funktioniert

parent ffdb0c68
No related branches found
No related tags found
1 merge request!2leader election und heartbeat
......@@ -17,7 +17,7 @@ SERVER_MULTICAST_PORT = 5974
CLIENT_MULTICAST_PORT = 5973
# Listening port ring
RING_PORT = 10001
RING_PORT = 5972
# Local host information
MY_HOST = socket.gethostname()
......@@ -50,7 +50,7 @@ class Server():
# if my IP is not in the server list add it
if MY_IP not in self.serverList:
self.serverList.append(MY_IP)
self.basic_lcr()
# create socket bind to server address
multicast_listen_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
......@@ -75,7 +75,7 @@ class Server():
if newServer_address not in self.serverList:
self.serverList.append(newServer_address)
self.basic_lcr()
reply_message = MY_IP
multicast_listen_sock.sendto(str.encode(reply_message), address)
......@@ -86,7 +86,6 @@ class Server():
time.sleep(1)
self.print_group_view()
# self.basic_lcr()
# self.printwt(f'The current leader IP is: {self.leader_uuid}')
......@@ -109,7 +108,7 @@ class Server():
# if my IP is not in the server list add it
if MY_IP not in self.serverList:
self.serverList.append(MY_IP)
self.basic_lcr()
# listen for IPs from existing servers
maxLoop = 5
......@@ -136,7 +135,6 @@ class Server():
# if reply address is not in the server list, add it
if reply_address not in self.serverList:
self.serverList.append(reply_address)
self.basic_lcr()
# Erhöhe die Anzahl der eingehenden Antworten
num_responses += 1
......@@ -208,65 +206,78 @@ class Server():
self.clients.remove(client)
def basic_lcr(self):
time.sleep(3)
# bind to ring socket
ring_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ring_socket.bind((MY_IP, RING_PORT))
print('++++++++++++++++++++++++++')
print('New leader election starts')
print('sending first leader election message to neighbour')
print('Servers')
print(self.serverList)
neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left')
neighbour_address = (neighbour, RING_PORT)
first_message = {
"mid": self.uuid,
"isLeader ": False
}
self.participant = True
ring_socket.sendto(json.dumps(first_message).encode('utf-8'), neighbour_address)
print('Neighbour')
print(neighbour)
print('\nWaiting to receive election message...\n')
data, address = ring_socket.recvfrom(1024)
election_message = json.loads(data.decode())
print('There is a election message')
print(election_message)
if election_message.get('isLeader'):
print('1: leader info weitergeben')
self.leader_uuid = election_message['mid']
# forward received election message to left neighbour
self.participant = False
ring_socket.sendto(json.dumps(election_message), neighbour)
if election_message.get('mid') < self.uuid and not self.participant:
print('2: mich vorschlagen')
new_election_message = {
try:
ring_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ring_socket.bind((MY_IP, RING_PORT))
print('++++++++++++++++++++++++++')
print('New leader election starts')
print('sending first leader election message to neighbour')
print('Servers')
print(self.serverList)
neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left')
neighbour_address = (neighbour, RING_PORT)
first_message = {
"mid": self.uuid,
"isLeader ": False
"isLeader": False
}
self.participant = True
# send received election message to left neighbour
ring_socket.sendto(json.dumps(new_election_message), neighbour)
elif election_message.get('mid') > self.uuid:
# send received election message to left neighbour
print('3: Jemand anderes vorschlagen')
self.participant = True
ring_socket.sendto(json.dumps(election_message), neighbour)
elif election_message.get('mid') == self.uuid:
print('4: Ich wurde als Leader definiert')
self.leader_uid = self.uuid
self.isLeader = True
new_election_message = {
"mid": self.uuid,
"isLeader ": True
}
# send new election message to left neighbour
self.participant = False
ring_socket.sendto(json.dumps(new_election_message).encode('utf-8'), neighbour_address)
ring_socket.sendto(json.dumps(first_message).encode('utf-8'), neighbour_address)
print('Neighbour')
print(neighbour)
while True:
neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left')
neighbour_address = (neighbour, RING_PORT)
print('\nWaiting to receive election message...\n')
data, address = ring_socket.recvfrom(1024)
election_message = json.loads(data.decode())
print('There is a election message')
print(election_message)
if election_message.get('isLeader') and self.participant:
print('1: leader info weitergeben')
self.leader_uuid = election_message['mid']
# forward received election message to left neighbour
self.participant = False
ring_socket.sendto(json.dumps(election_message).encode('utf-8'), neighbour_address)
elif election_message.get('mid') < self.uuid and not self.participant:
print('2: mich vorschlagen')
new_election_message = {
"mid": self.uuid,
"isLeader": False
}
self.participant = True
# send received election message to left neighbour
ring_socket.sendto(json.dumps(new_election_message).encode('utf-8'), neighbour_address)
elif election_message.get('mid') > self.uuid:
# send received election message to left neighbour
print('3: Jemand anderes vorschlagen')
self.participant = True
ring_socket.sendto(json.dumps(election_message).encode('utf-8'), neighbour_address)
elif election_message.get('mid') == self.uuid and self.participant:
print('4: Ich wurde als Leader definiert')
self.leader_uid = self.uuid
self.isLeader = True
new_election_message = {
"mid": self.uuid,
"isLeader": True
}
# send new election message to left neighbour
self.participant = False
ring_socket.sendto(json.dumps(new_election_message).encode('utf-8'), neighbour_address)
elif election_message.get('isLeader') and not self.participant:
print('5: Leader ist gewählt, Nachricht wurde schon weiteregeben, ELECTION beenden')
except Exception as e:
print(f"An error occurred: {e}")
finally:
ring_socket.close()
# starting all simultaneously working procedures
......@@ -282,8 +293,8 @@ if __name__== '__main__':
thread1 = threading.Thread(target = server.ListenForClientAndReply)
thread1.start()
# thread_election = threading.Thread(target = server.basic_lcr)
# thread_election.start()
thread_election = threading.Thread(target = server.basic_lcr)
thread_election.start()
# server.basic_lcr()
......
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