mirror of
https://github.com/craigerl/aprsd.git
synced 2025-03-23 04:44:31 -04:00
Merge pull request #182 from Novfensec/Novfensec-patch-1
Update counter.py
This commit is contained in:
commit
c372e1a1e9
@ -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)
|
Loading…
Reference in New Issue
Block a user