added packet counter random int

The packet counter now starts at a random number between 1 and 9999
instead of always at 1.
This commit is contained in:
Hemna 2024-03-23 17:53:01 -04:00
parent f53df24988
commit 873fc06608
2 changed files with 7 additions and 4 deletions

View File

@ -167,7 +167,7 @@ class Packet:
# 67 displays 64 on the ftm400. (+3 {01 suffix) # 67 displays 64 on the ftm400. (+3 {01 suffix)
# feature req: break long ones into two msgs # feature req: break long ones into two msgs
if not msg: if not msg:
raise ValueError("No message text to send. call prepare() first.") return ""
message = msg[:67] message = msg[:67]
# We all miss George Carlin # We all miss George Carlin

View File

@ -1,9 +1,13 @@
from multiprocessing import RawValue from multiprocessing import RawValue
import random
import threading import threading
import wrapt import wrapt
MAX_PACKET_ID = 9999
class PacketCounter: class PacketCounter:
""" """
Global Packet id counter class. Global Packet id counter class.
@ -17,19 +21,18 @@ class PacketCounter:
""" """
_instance = None _instance = None
max_count = 9999
lock = threading.Lock() lock = threading.Lock()
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):
"""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", 1) cls._instance.val = RawValue("i", random.randint(1, MAX_PACKET_ID))
return cls._instance return cls._instance
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def increment(self): def increment(self):
if self.val.value == self.max_count: if self.val.value == MAX_PACKET_ID:
self.val.value = 1 self.val.value = 1
else: else:
self.val.value += 1 self.val.value += 1