diff --git a/chat_server.py b/chat_server.py index 8ad5315650f3fcbdf0e0da9614e33c6440a7e11d..c3681d9a96e0ae250ec339b7edb2a029ddab2c42 100644 --- a/chat_server.py +++ b/chat_server.py @@ -39,6 +39,7 @@ if __name__ == '__main__': print("Successfully sent message") message, server = broadcast_socket.recvfrom(1024) + print("after receive") match = re.search(r'\b([A-Za-z])\b$', message.decode('utf-8')) server_id = match.group(1) diff --git a/server.py b/server.py index 2f0ae56792c7b6c653d3be13ae38ef00ec6c71b3..00dc61f41d03ff3518ab76ec2fd48a86f0ce6656 100644 --- a/server.py +++ b/server.py @@ -17,6 +17,7 @@ client_forward_message_multicast_port = 51000 multicast_group_ip = '224.0.1.1' +last_heartbeat_timestamp = None class Server(multiprocessing.Process): client_cache_key_offset = 0 @@ -120,10 +121,12 @@ class Server(multiprocessing.Process): cache_update_listener_thread = threading.Thread(target=self.listen_for_cache_update) client_message_listener_thread = threading.Thread(target=self.listen_for_client_messages) heartbeat_receive_thread = threading.Thread(target=self.listen_for_heartbeats) + heartbeat_timeout_thread = threading.Thread(target=self.check_heartbeat_timeout) cache_update_listener_thread.start() client_message_listener_thread.start() heartbeat_receive_thread.start() + heartbeat_timeout_thread.start() def get_broadcast_address(self): IP = self.server_address @@ -131,8 +134,8 @@ class Server(multiprocessing.Process): host = ipaddress.IPv4Address(IP) net = ipaddress.IPv4Network(IP + '/' + MASK, False) - print('Host:', ipaddress.IPv4Address(int(host) & int(net.hostmask))) - print('Broadcast:', net.broadcast_address) + # print('Host:', ipaddress.IPv4Address(int(host) & int(net.hostmask))) + # print('Broadcast:', net.broadcast_address) broadcast_address = str(net.broadcast_address) return broadcast_address @@ -152,7 +155,8 @@ class Server(multiprocessing.Process): acknowledgment_received = False try: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) + if self.os == "macOS": + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) s.settimeout(2) # Timeout for the connection # Combine server address and port into a tuple server_address_with_port = (server_address, server_port) @@ -172,7 +176,7 @@ class Server(multiprocessing.Process): try: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) - s.bind((self.server_address, server_heartbeat_tcp_listener_port)) # Bind to any available port + s.bind((self.server_address, server_heartbeat_tcp_listener_port)) actual_port = s.getsockname()[1] print(f"Heartbeat Listener Started on port {actual_port}") s.listen() @@ -180,13 +184,26 @@ class Server(multiprocessing.Process): with conn: data = conn.recv(1024) if data == b'HEARTBEAT': - # Hier Code zum Verarbeiten des Heartbeats - # (z.B., Aktualisieren des Zeitstempels für den letzten Heartbeat) + # Handle the received heartbeat print(f"Heartbeat received from {addr}") - # Hier Code zum Senden des Acknowledgment + # Update the timestamp of the last received heartbeat + self.last_heartbeat_timestamp = time.time() + # Send an acknowledgment conn.sendall(b'ACK') except socket.error as e: print(f"Error: {e}") + + def check_heartbeat_timeout(self): + while True: + time.sleep(5) # Adjust the interval as needed + if self.last_heartbeat_timestamp is not None: + current_time = time.time() + # Define the timeout period (15 seconds) + timeout_duration = 15 + if current_time - self.last_heartbeat_timestamp >= timeout_duration: + print(f"No heartbeats received for {timeout_duration} seconds. Initiating LCR...") + # Call a function to initiate the LCR algorithm here + self.initiate_lcr_algorithm() # find highest server ID in cache def get_last_server_id(self):