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

some beautifying

parent b3e0975e
No related branches found
No related tags found
1 merge request!2leader election und heartbeat
...@@ -86,7 +86,6 @@ class Server(): ...@@ -86,7 +86,6 @@ class Server():
time.sleep(1) time.sleep(1)
self.print_group_view() self.print_group_view()
# self.printwt(f'The current leader IP is: {self.leader_uuid}')
#this function enables the server to send a multicast to the server group and receive the answers of existing members #this function enables the server to send a multicast to the server group and receive the answers of existing members
...@@ -146,8 +145,7 @@ class Server(): ...@@ -146,8 +145,7 @@ class Server():
if num_responses == 1: if num_responses == 1:
multicast_send_sock.close() multicast_send_sock.close()
self.isLeader = True self.isLeader = True
self.leader_uuid = self.uuid # Hier wird die uuid-Adresse des Leaders zugewiesen self.leader_uuid = self.uuid # Hier wird die uuid-Adresse des Leaders zugewiesens
# self.printwt(f'I am the only server in the system, so the leader IP is: {self.leader_uuid}')
time.sleep(1) time.sleep(1)
...@@ -214,23 +212,22 @@ class Server(): ...@@ -214,23 +212,22 @@ class Server():
ring_socket.bind((MY_IP, RING_PORT)) ring_socket.bind((MY_IP, RING_PORT))
print('++++++++++++++++++++++++++') print('++++++++++++++++++++++++++')
print('New leader election starts') print('New leader election starts')
print('sending first leader election message to neighbour')
print('Servers') print('Servers')
print(self.serverList) print(self.serverList)
neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left') neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left')
print('Neighbour')
print(neighbour)
neighbour_address = (neighbour, RING_PORT) neighbour_address = (neighbour, RING_PORT)
first_message = { first_message = {
"mid": self.uuid, "mid": self.uuid,
"isLeader": False "isLeader": False
} }
self.participant = True self.participant = True
print('sending first leader election message to neighbour')
ring_socket.sendto(json.dumps(first_message).encode('utf-8'), neighbour_address) ring_socket.sendto(json.dumps(first_message).encode('utf-8'), neighbour_address)
print('Neighbour')
print(neighbour)
while True: while True:
neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left') neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left')
neighbour_address = (neighbour, RING_PORT) neighbour_address = (neighbour, RING_PORT)
...@@ -238,7 +235,7 @@ class Server(): ...@@ -238,7 +235,7 @@ class Server():
print('\nWaiting to receive election message...\n') print('\nWaiting to receive election message...\n')
data, address = ring_socket.recvfrom(1024) data, address = ring_socket.recvfrom(1024)
election_message = json.loads(data.decode()) election_message = json.loads(data.decode())
print('There is a election message') print('There is a election message:')
print(election_message) print(election_message)
if election_message.get('isLeader') and self.participant: if election_message.get('isLeader') and self.participant:
...@@ -247,6 +244,7 @@ class Server(): ...@@ -247,6 +244,7 @@ class Server():
# forward received election message to left neighbour # forward received election message to left neighbour
self.participant = False self.participant = False
ring_socket.sendto(json.dumps(election_message).encode('utf-8'), neighbour_address) ring_socket.sendto(json.dumps(election_message).encode('utf-8'), neighbour_address)
print(f'Leader is {self.leader_uuid}')
elif election_message.get('mid') < self.uuid and not self.participant: elif election_message.get('mid') < self.uuid and not self.participant:
print('2: mich vorschlagen') print('2: mich vorschlagen')
new_election_message = { new_election_message = {
...@@ -272,6 +270,7 @@ class Server(): ...@@ -272,6 +270,7 @@ class Server():
# send new election message to left neighbour # send new election message to left neighbour
self.participant = False self.participant = False
ring_socket.sendto(json.dumps(new_election_message).encode('utf-8'), neighbour_address) ring_socket.sendto(json.dumps(new_election_message).encode('utf-8'), neighbour_address)
print(f'I am Leader {self.leader_uuid}')
elif election_message.get('isLeader') and not self.participant: elif election_message.get('isLeader') and not self.participant:
print('5: Leader ist gewählt, Nachricht wurde schon weiteregeben, ELECTION beenden') print('5: Leader ist gewählt, Nachricht wurde schon weiteregeben, ELECTION beenden')
except Exception as e: except Exception as e:
...@@ -295,9 +294,6 @@ if __name__== '__main__': ...@@ -295,9 +294,6 @@ if __name__== '__main__':
thread_election = threading.Thread(target = server.basic_lcr) thread_election = threading.Thread(target = server.basic_lcr)
thread_election.start() thread_election.start()
# server.basic_lcr()
# Socket erstellen und binden # Socket erstellen und binden
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
......
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