From e7d8947810e999da468fce19b852a972a678f6c9 Mon Sep 17 00:00:00 2001 From: Kartavya Shukla <87070473+Novfensec@users.noreply.github.com> Date: Thu, 21 Nov 2024 13:38:30 +0530 Subject: [PATCH] Update counter.py --- aprsd/utils/counter.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/aprsd/utils/counter.py b/aprsd/utils/counter.py index 30b6b75..ab3f73f 100644 --- a/aprsd/utils/counter.py +++ b/aprsd/utils/counter.py @@ -1,21 +1,17 @@ -from multiprocessing import RawValue import random import threading - import wrapt - MAX_PACKET_ID = 9999 - class PacketCounter: """ - Global Packet id counter class. + Global Packet ID counter class. - This is a singleton based class that keeps + This is a singleton-based class that keeps an incrementing counter for all packets to - be sent. All new Packet objects gets a new - message id, which is the next number available + be sent. All new Packet objects get a new + message ID, which is the next number available from the PacketCounter. """ @@ -27,25 +23,29 @@ class PacketCounter: """Make this a singleton class.""" if cls._instance is None: cls._instance = super().__new__(cls, *args, **kwargs) - cls._instance.val = RawValue("i", random.randint(1, MAX_PACKET_ID)) + cls._instance._val = random.randint(1, MAX_PACKET_ID) # Initialize counter return cls._instance @wrapt.synchronized(lock) def increment(self): - if self.val.value == MAX_PACKET_ID: - self.val.value = 1 + """Increment the counter, reset if it exceeds MAX_PACKET_ID.""" + if self._val == MAX_PACKET_ID: + self._val = 1 else: - self.val.value += 1 + self._val += 1 @property @wrapt.synchronized(lock) def value(self): - return str(self.val.value) + """Get the current value as a string.""" + return str(self._val) @wrapt.synchronized(lock) def __repr__(self): - return str(self.val.value) + """String representation of the current value.""" + return str(self._val) @wrapt.synchronized(lock) def __str__(self): - return str(self.val.value) + """String representation of the current value.""" + return str(self._val) \ No newline at end of file