From 2d670a56a1b1702c59ec00db094d61a51d0b8204 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 5 Jan 2021 20:05:19 +0100 Subject: [PATCH] Merge clients that have the same id if it speaks on multiple addresses --- squinnondation/hazel.py | 19 +++++++++++++++++++ squinnondation/messages.py | 7 +++++++ squinnondation/squinnondation.py | 2 -- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/squinnondation/hazel.py b/squinnondation/hazel.py index c276daa..4af8e31 100644 --- a/squinnondation/hazel.py +++ b/squinnondation/hazel.py @@ -50,6 +50,12 @@ class Hazelnut: def main_address(self) -> Tuple[str, int]: return self.addresses[0] + def __repr__(self): + return f"{self.id or ''}: {self.nickname or ''}, {self.addresses}" + + def __str__(self): + return repr(self) + class Squirrel(Hazelnut): """ @@ -122,8 +128,11 @@ class Squirrel(Hazelnut): creates a new hazelnut. """ if (address, port) in self.hazelnuts: + self.add_system_message(str(self.hazelnuts)) return self.hazelnuts[(address, port)] hazelnut = Hazelnut(address=address, port=port) + self.hazelnuts[(address, port)] = hazelnut + self.add_system_message(str(self.hazelnuts)) return hazelnut def send_packet(self, client: Hazelnut, pkt: Packet) -> int: @@ -640,12 +649,22 @@ class Squirrel(Hazelnut): def update_hazelnut_table(self, hazelnut: Hazelnut) -> None: for addr in hazelnut.addresses: if addr in self.hazelnuts: + # Merge with the previous hazel old_hazel = self.hazelnuts[addr] for other_addr in old_hazel.addresses: if other_addr not in hazelnut.addresses: hazelnut.addresses.append(other_addr) + self.hazelnuts[other_addr] = hazelnut self.hazelnuts[addr] = hazelnut + for other_hazel in list(self.hazelnuts.values()): + if other_hazel.id == hazelnut.id > 0 and other_hazel != hazelnut: + # The hazelnut with the same id is known as a different address. We merge everything + for addr in other_hazel.addresses: + self.hazelnuts[addr] = hazelnut + if addr not in hazelnut.addresses: + hazelnut.addresses.append(addr) + def send_neighbours(self) -> None: """ Update the number of symmetric neighbours and diff --git a/squinnondation/messages.py b/squinnondation/messages.py index 35c85f7..f938c66 100644 --- a/squinnondation/messages.py +++ b/squinnondation/messages.py @@ -167,6 +167,13 @@ class HelloTLV(TLV): if self.is_long and self.dest_id == squirrel.id: time_hl = time.time() + if sender.id != self.source_id: + squirrel.send_packet(sender, Packet.construct(WarningTLV.construct( + f"You were known as the ID {sender.id}, but you declared that you have the ID {self.source_id}."))) + squirrel.add_system_message(f"A client known as the id {sender.id} declared that it uses " + f"the id {self.source_id}.") + sender.id = self.source_id + # Add entry to/actualize the active hazelnuts dictionnary sender.last_hello_time = time_h sender.last_long_hello_time = time_hl diff --git a/squinnondation/squinnondation.py b/squinnondation/squinnondation.py index 2b93e9b..f17da92 100644 --- a/squinnondation/squinnondation.py +++ b/squinnondation/squinnondation.py @@ -78,8 +78,6 @@ class Squinnondation: if instance.args.client_address and instance.args.client_port: hazelnut = Hazelnut(address=instance.args.client_address, port=instance.args.client_port) - hazelnut.potential = True - squirrel.update_hazelnut_table(hazelnut) htlv = HelloTLV().construct(8, squirrel) pkt = Packet().construct(htlv) squirrel.send_packet(hazelnut, pkt)