1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-09-27 15:46:53 -04:00
aprsd/aprsd/utils/counter.py
Hemna 94fb481014 Reworked all packet processing
This patch reworks all the packet processing to use the new
Packets objects.  Nuked all of the messaging classes.

backwards incompatible changes
all messaging.py classes are now gone and replaced by
packets.py classes
2022-12-16 15:58:02 -05:00

49 lines
1.1 KiB
Python

from multiprocessing import RawValue
import threading
import wrapt
class PacketCounter:
"""
Global Packet id counter class.
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
from the PacketCounter.
"""
_instance = None
max_count = 9999
lock = threading.Lock()
def __new__(cls, *args, **kwargs):
"""Make this a singleton class."""
if cls._instance is None:
cls._instance = super().__new__(cls, *args, **kwargs)
cls._instance.val = RawValue("i", 1)
return cls._instance
@wrapt.synchronized(lock)
def increment(self):
if self.val.value == self.max_count:
self.val.value = 1
else:
self.val.value += 1
@property
@wrapt.synchronized(lock)
def value(self):
return self.val.value
@wrapt.synchronized(lock)
def __repr__(self):
return str(self.val.value)
@wrapt.synchronized(lock)
def __str__(self):
return str(self.val.value)