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