From 0b4db651a658c1db5e3c4dcf8acd6f4d85e4c1db Mon Sep 17 00:00:00 2001 From: leberr <robin.leber@student.reutlingen-university.de> Date: Tue, 30 Jan 2024 10:42:37 +0100 Subject: [PATCH] voting: basic lcr algo (noch nicht eingebunden) --- server.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++------ voting.py | 6 ------ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/server.py b/server.py index e563590..65fc251 100644 --- a/server.py +++ b/server.py @@ -3,6 +3,10 @@ import threading from datetime import datetime import time import struct +import uuid +import json +from voting import form_ring, get_neighbour + MULTICAST_GROUP_IP = '224.1.1.1' @@ -12,18 +16,27 @@ SERVER_MULTICAST_PORT = 5974 # Listening port Client Discovery CLIENT_MULTICAST_PORT = 5973 +# Listening port ring +RING_PORT = 10001 + # Local host information 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): - self.leader_IP = '' # fix the leader IP - self.serverList = [] # list if servers and their addresses self.clients = [] self.informServer = False + self.serverList = [] # list if servers and their addresses + self.leader_uuid = '' # fix the leader IP self.isLeader = False # New variable to track if the server is the leader + self.uuid = str(uuid.uuid4()) + self.participant = False def printwt(self, msg): @@ -75,7 +88,7 @@ class Server(): time.sleep(1) self.print_group_view() - self.printwt(f'The current leader IP is: {self.leader_IP}') + 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 @@ -134,8 +147,8 @@ class Server(): if num_responses == 1: multicast_send_sock.close() self.isLeader = True - self.leader_IP = MY_IP # Hier wird die IP-Adresse des Leaders zugewiesen - self.printwt(f'I am the only server in the system, so the leader IP is: {self.leader_IP}') + 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}') time.sleep(1) @@ -166,7 +179,7 @@ class Server(): self.printwt(data.decode()) # if Iam the leader, answer the client including my IP - if MY_IP == self.leader_IP: + if MY_IP == self.leader_uuid: reply_message = MY_IP listen_socket.sendto(str.encode(reply_message), addr) self.printwt('Replied my IP to new client') @@ -192,6 +205,40 @@ class Server(): client.send(message) except: self.clients.remove(client) + + def basic_lcr(self): + print('\nWaiting to receive election message...\n') + data, address = ring_socket.recvfrom(1024) + election_message = json.loads(data.decode()) + neighbour = get_neighbour(form_ring(), MY_IP, 'left') + + if election_message['isLeader']: + 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: + 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), neighbour) + elif election_message['mid'] > self.uuid: + # send received election message to left neighbour + self.participant = True + ring_socket.sendto(json.dumps(election_message), neighbour) + elif election_message['mid'] == self.uuid: + 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), neighbour) # starting all simultaneously working procedures diff --git a/voting.py b/voting.py index 71bb3de..09eff57 100644 --- a/voting.py +++ b/voting.py @@ -5,10 +5,6 @@ def form_ring(members): sorted_ip_ring = [socket.inet_ntoa(node) for node in sorted_binary_ring] return sorted_ip_ring -members = ['192.168.0.1', '130.234.204.2', '130.234.203.2', '130.234.204.1', '182.4.3.111'] -ring = form_ring(members) -print(ring) - def get_neighbour(ring, current_node_ip, direction='left'): current_node_index = ring.index(current_node_ip) if current_node_ip in ring else -1 @@ -25,5 +21,3 @@ def get_neighbour(ring, current_node_ip, direction='left'): return ring[current_node_index - 1] else: return None -neighbour = get_neighbour(ring, '130.234.204.2', 'rigth') -print(neighbour) \ No newline at end of file -- GitLab