Don't need to use a lock when manipulating packets
This commit is contained in:
		@@ -130,22 +130,17 @@ class Squirrel(Hazelnut):
 | 
			
		||||
        """
 | 
			
		||||
        Send a formatted packet to a client.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
        if len(pkt) > 1024:
 | 
			
		||||
            # The packet is too large to be sent by the protocol. We split the packet in subpackets.
 | 
			
		||||
            return sum(self.send_packet(client, subpkt) for subpkt in pkt.split(1024))
 | 
			
		||||
        res = self.send_raw_data(client, pkt.marshal())
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
        return res
 | 
			
		||||
 | 
			
		||||
    def send_raw_data(self, client: Hazelnut, data: bytes) -> int:
 | 
			
		||||
        """
 | 
			
		||||
        Send a raw packet to a client.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
        res = self.socket.sendto(data, client.main_address)
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
        return res
 | 
			
		||||
        return self.socket.sendto(data, client.main_address)
 | 
			
		||||
 | 
			
		||||
    def receive_packet(self) -> Tuple[Packet, Hazelnut]:
 | 
			
		||||
        """
 | 
			
		||||
@@ -368,7 +363,6 @@ class Squirrel(Hazelnut):
 | 
			
		||||
        """
 | 
			
		||||
        Remove the sender from the list of neighbours to be inundated
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
        if (sender_id, nonce) in self.recent_messages:
 | 
			
		||||
            # If a peer is late in its acknowledgement, the absence of the previous if causes an error.
 | 
			
		||||
            for addr in hazel.addresses:
 | 
			
		||||
@@ -376,26 +370,19 @@ class Squirrel(Hazelnut):
 | 
			
		||||
 | 
			
		||||
            if not self.recent_messages[(sender_id, nonce)][2]:  # If dictionnary is empty, remove the message
 | 
			
		||||
                self.recent_messages.pop((sender_id, nonce), None)
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
 | 
			
		||||
    def clean_inundation(self) -> None:
 | 
			
		||||
        """
 | 
			
		||||
        Remove messages which are overdue (older than 2 minutes) from the inundation dictionnary.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
 | 
			
		||||
        for key in self.recent_messages:
 | 
			
		||||
            if time.time() - self.recent_messages[key][1] > 120:
 | 
			
		||||
                self.recent_messages.pop(key)
 | 
			
		||||
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
 | 
			
		||||
    def main_inundation(self) -> None:
 | 
			
		||||
        """
 | 
			
		||||
        The main inundation function.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
 | 
			
		||||
        for key in self.recent_messages:
 | 
			
		||||
            k = list(self.recent_messages[key][2].keys())
 | 
			
		||||
            for key2 in k:
 | 
			
		||||
@@ -418,8 +405,6 @@ class Squirrel(Hazelnut):
 | 
			
		||||
                    self.send_packet(hazelnut, pkt)
 | 
			
		||||
                    self.recent_messages[key][2].pop(key2)
 | 
			
		||||
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
 | 
			
		||||
    def add_system_message(self, msg: str) -> None:
 | 
			
		||||
        """
 | 
			
		||||
        Add a new system log message.
 | 
			
		||||
@@ -619,8 +604,6 @@ class Squirrel(Hazelnut):
 | 
			
		||||
        Returns a list of hazelnuts the squirrel should contact if it does
 | 
			
		||||
        not have enough symmetric neighbours.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
 | 
			
		||||
        res = []
 | 
			
		||||
        lp = len(self.potentialhazelnuts)
 | 
			
		||||
        val = list(self.potentialhazelnuts.values())
 | 
			
		||||
@@ -628,30 +611,22 @@ class Squirrel(Hazelnut):
 | 
			
		||||
        for i in range(min(lp, max(0, self.minNS - self.nbNS))):
 | 
			
		||||
            a = randint(0, lp - 1)
 | 
			
		||||
            res.append(val[a])
 | 
			
		||||
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
        return res
 | 
			
		||||
 | 
			
		||||
    def send_hello(self) -> None:
 | 
			
		||||
        """
 | 
			
		||||
        Sends a long HelloTLV to all active neighbours.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
 | 
			
		||||
        for hazelnut in self.activehazelnuts.values():
 | 
			
		||||
            htlv = HelloTLV().construct(16, self, hazelnut)
 | 
			
		||||
            pkt = Packet().construct(htlv)
 | 
			
		||||
            self.send_packet(hazelnut, pkt)
 | 
			
		||||
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
 | 
			
		||||
    def verify_activity(self) -> None:
 | 
			
		||||
        """
 | 
			
		||||
        All neighbours that have not sent a HelloTLV in the last 2
 | 
			
		||||
        minutes are considered not active.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
 | 
			
		||||
        val = list(self.activehazelnuts.values())  # create a copy because the dict size will change
 | 
			
		||||
 | 
			
		||||
        for hazelnut in val:
 | 
			
		||||
@@ -662,8 +637,6 @@ class Squirrel(Hazelnut):
 | 
			
		||||
                hazelnut.active = False
 | 
			
		||||
                self.update_hazelnut_table(hazelnut)
 | 
			
		||||
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
 | 
			
		||||
    def update_hazelnut_table(self, hazelnut: Hazelnut) -> None:
 | 
			
		||||
        for addr in hazelnut.addresses:
 | 
			
		||||
            if addr in self.hazelnuts:
 | 
			
		||||
@@ -678,8 +651,6 @@ class Squirrel(Hazelnut):
 | 
			
		||||
        Update the number of symmetric neighbours and
 | 
			
		||||
        send all neighbours NeighbourTLV
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
 | 
			
		||||
        nb_ns = 0
 | 
			
		||||
        # could send the same to all neighbour, but it means that neighbour
 | 
			
		||||
        # A could receive a message with itself in it -> if the others do not pay attention, trouble
 | 
			
		||||
@@ -696,14 +667,10 @@ class Squirrel(Hazelnut):
 | 
			
		||||
                self.activehazelnuts[key].symmetric = False
 | 
			
		||||
        self.nbNS = nb_ns
 | 
			
		||||
 | 
			
		||||
        self.refresh_lock.release()
 | 
			
		||||
 | 
			
		||||
    def leave(self) -> None:
 | 
			
		||||
        """
 | 
			
		||||
        The program is exited. We send a GoAway to our neighbours, then close the program.
 | 
			
		||||
        """
 | 
			
		||||
        self.refresh_lock.acquire()
 | 
			
		||||
 | 
			
		||||
        # Last inundation
 | 
			
		||||
        self.main_inundation()
 | 
			
		||||
        self.clean_inundation()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user