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

fixed leader election

parent 6259c91a
Branches
No related tags found
1 merge request!2leader election und heartbeat
......@@ -208,21 +208,33 @@ class Server():
self.clients.remove(client)
def basic_lcr(self):
print('New leader election starts')
print('++++++++++++++++++++++++++')
print('server list')
print(self.serverList)
print('New leader election starts')
print('sending first leader election message to neighbour')
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('\nWaiting to receive election message...\n')
data, address = ring_socket.recvfrom(1024)
election_message = json.loads(data.decode())
neighbour = get_neighbour(form_ring(self.serverList), MY_IP, 'left')
print('There is a election message')
print(election_message)
if election_message['isLeader']:
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['mid'] < self.uuid and not self.participant:
if election_message.get('mid') < self.uuid and not self.participant:
print('2: mich vorschlagen')
new_election_message = {
"mid": self.uuid,
"isLeader ": False
......@@ -230,11 +242,13 @@ class Server():
self.participant = True
# send received election message to left neighbour
ring_socket.sendto(json.dumps(new_election_message), neighbour)
elif election_message['mid'] > self.uuid:
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['mid'] == self.uuid:
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 = {
......@@ -243,7 +257,7 @@ class Server():
}
# send new election message to left neighbour
self.participant = False
ring_socket.sendto(json.dumps(new_election_message), neighbour)
ring_socket.sendto(json.dumps(new_election_message).encode('utf-8'), neighbour_address)
# starting all simultaneously working procedures
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment