Merge clients that have the same id if it speaks on multiple addresses
This commit is contained in:
		@@ -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 '<unknown id>'}: {self.nickname or '<unknown nickname>'}, {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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user