1
0
mirror of https://github.com/craigerl/aprsd.git synced 2025-07-31 12:52:24 -04:00

Merge pull request #182 from Novfensec/Novfensec-patch-1

Update counter.py
This commit is contained in:
Walter A. Boring IV 2024-11-21 08:07:09 -05:00 committed by GitHub
commit c372e1a1e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,21 +1,17 @@
from multiprocessing import RawValue
import random import random
import threading import threading
import wrapt import wrapt
MAX_PACKET_ID = 9999 MAX_PACKET_ID = 9999
class PacketCounter: 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 an incrementing counter for all packets to
be sent. All new Packet objects gets a new be sent. All new Packet objects get a new
message id, which is the next number available message ID, which is the next number available
from the PacketCounter. from the PacketCounter.
""" """
@ -27,25 +23,29 @@ class PacketCounter:
"""Make this a singleton class.""" """Make this a singleton class."""
if cls._instance is None: if cls._instance is None:
cls._instance = super().__new__(cls, *args, **kwargs) 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 return cls._instance
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def increment(self): def increment(self):
if self.val.value == MAX_PACKET_ID: """Increment the counter, reset if it exceeds MAX_PACKET_ID."""
self.val.value = 1 if self._val == MAX_PACKET_ID:
self._val = 1
else: else:
self.val.value += 1 self._val += 1
@property @property
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def value(self): def value(self):
return str(self.val.value) """Get the current value as a string."""
return str(self._val)
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def __repr__(self): def __repr__(self):
return str(self.val.value) """String representation of the current value."""
return str(self._val)
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def __str__(self): def __str__(self):
return str(self.val.value) """String representation of the current value."""
return str(self._val)