Inundation is more thread-safe

This commit is contained in:
Yohann D'ANELLO 2021-01-09 20:41:12 +01:00
parent 4a79fcaabe
commit 8f3b7cd9d2
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
1 changed files with 8 additions and 1 deletions

View File

@ -617,11 +617,13 @@ class User(Peer):
Returns True iff the message was not already received previously.
"""
self.data_lock.acquire()
if (sender_id, nonce) not in self.received_messages:
# If it is a new message, add it to recent_messages
d = self.make_inundation_dict()
pkt = Packet().construct(tlv)
self.recent_messages[(sender_id, nonce)] = [pkt, time.time(), d]
self.data_lock.release()
# in all cases, remove the sender from the list of neighbours to be inundated
self.remove_from_inundation(relay, sender_id, nonce)
@ -629,8 +631,10 @@ class User(Peer):
if (sender_id, nonce) in self.received_messages:
return False
self.data_lock.acquire()
self.add_message(msg) # for display purposes
self.received_messages[(sender_id, nonce)] = Message(msg, sender_id, nonce)
self.data_lock.release()
return True
@ -650,6 +654,7 @@ class User(Peer):
"""
Remove the sender from the list of neighbours to be inundated
"""
self.data_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 peer.addresses:
@ -657,6 +662,7 @@ class User(Peer):
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.data_lock.release()
def clean_inundation(self) -> None:
"""
@ -1073,6 +1079,7 @@ class Listener(Thread):
self.user.refresh_input()
self.user.refresh_emoji_pad()
class Multicastlistener(Thread):
"""
Used to listen on the multicast group to discover new people