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

election with thread

parent 07245428
No related branches found
No related tags found
1 merge request!2leader election und heartbeat
......@@ -23,10 +23,6 @@ RING_PORT = 10001
MY_HOST = socket.gethostname()
MY_IP = socket.gethostbyname(MY_HOST)
# bind to ring socket
ring_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ring_socket.bind((MY_IP, RING_PORT))
class Server():
def __init__(self):
......@@ -78,6 +74,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)
......@@ -89,7 +86,7 @@ class Server():
time.sleep(1)
self.print_group_view()
# self.basic_lcr()
self.printwt(f'The current leader IP is: {self.leader_uuid}')
# 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
......@@ -149,7 +146,7 @@ class Server():
multicast_send_sock.close()
self.isLeader = True
self.leader_uuid = self.uuid # Hier wird die uuid-Adresse des Leaders zugewiesen
self.printwt(f'I am the only server in the system, so the leader IP is: {self.leader_uuid}')
# self.printwt(f'I am the only server in the system, so the leader IP is: {self.leader_uuid}')
time.sleep(1)
......@@ -208,10 +205,16 @@ class Server():
self.clients.remove(client)
def basic_lcr(self):
# 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 = {
......@@ -220,6 +223,9 @@ class Server():
}
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)
......@@ -273,7 +279,11 @@ if __name__== '__main__':
thread1 = threading.Thread(target = server.ListenForClientAndReply)
thread1.start()
server.basic_lcr()
thread_election = threading.Thread(target = server.basic_lcr)
thread_election.start()
# server.basic_lcr()
# Socket erstellen und binden
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