2023-09-28 15:30:54 -04:00
|
|
|
from collections import OrderedDict
|
2022-12-15 17:23:54 -05:00
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
|
2022-12-24 13:53:06 -05:00
|
|
|
from oslo_config import cfg
|
2022-12-15 17:23:54 -05:00
|
|
|
import wrapt
|
|
|
|
|
|
|
|
from aprsd.packets import seen_list
|
2024-04-05 15:02:26 -04:00
|
|
|
from aprsd.utils import objectstore
|
2022-12-15 17:23:54 -05:00
|
|
|
|
|
|
|
|
2022-12-24 13:53:06 -05:00
|
|
|
CONF = cfg.CONF
|
2022-12-15 17:23:54 -05:00
|
|
|
LOG = logging.getLogger("APRSD")
|
|
|
|
|
|
|
|
|
2024-04-11 21:40:43 -04:00
|
|
|
class PacketList(objectstore.ObjectStoreMixin):
|
2022-12-15 17:23:54 -05:00
|
|
|
_instance = None
|
|
|
|
lock = threading.Lock()
|
2022-12-18 21:44:23 -05:00
|
|
|
_total_rx: int = 0
|
|
|
|
_total_tx: int = 0
|
2022-12-15 17:23:54 -05:00
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
if cls._instance is None:
|
|
|
|
cls._instance = super().__new__(cls)
|
2023-11-17 11:36:39 -05:00
|
|
|
cls._maxlen = 100
|
2024-04-05 15:02:26 -04:00
|
|
|
cls.data = {
|
|
|
|
"types": {},
|
|
|
|
"packets": OrderedDict(),
|
|
|
|
}
|
2022-12-15 17:23:54 -05:00
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
@wrapt.synchronized(lock)
|
2022-12-17 20:02:49 -05:00
|
|
|
def rx(self, packet):
|
|
|
|
"""Add a packet that was received."""
|
2022-12-18 21:44:23 -05:00
|
|
|
self._total_rx += 1
|
2023-09-28 12:19:18 -04:00
|
|
|
self._add(packet)
|
2023-11-17 11:36:39 -05:00
|
|
|
ptype = packet.__class__.__name__
|
2024-04-05 15:02:26 -04:00
|
|
|
if not ptype in self.data["types"]:
|
|
|
|
self.data["types"][ptype] = {"tx": 0, "rx": 0}
|
|
|
|
self.data["types"][ptype]["rx"] += 1
|
2022-12-17 20:02:49 -05:00
|
|
|
seen_list.SeenList().update_seen(packet)
|
|
|
|
|
|
|
|
@wrapt.synchronized(lock)
|
|
|
|
def tx(self, packet):
|
|
|
|
"""Add a packet that was received."""
|
2022-12-18 21:44:23 -05:00
|
|
|
self._total_tx += 1
|
2023-09-28 12:19:18 -04:00
|
|
|
self._add(packet)
|
2023-11-17 11:36:39 -05:00
|
|
|
ptype = packet.__class__.__name__
|
2024-04-05 15:02:26 -04:00
|
|
|
if not ptype in self.data["types"]:
|
|
|
|
self.data["types"][ptype] = {"tx": 0, "rx": 0}
|
|
|
|
self.data["types"][ptype]["tx"] += 1
|
2022-12-15 17:23:54 -05:00
|
|
|
seen_list.SeenList().update_seen(packet)
|
|
|
|
|
|
|
|
@wrapt.synchronized(lock)
|
2023-09-28 12:19:18 -04:00
|
|
|
def add(self, packet):
|
|
|
|
self._add(packet)
|
|
|
|
|
|
|
|
def _add(self, packet):
|
2024-04-11 21:40:43 -04:00
|
|
|
if packet.key in self.data["packets"]:
|
|
|
|
self.data["packets"].move_to_end(packet.key)
|
|
|
|
elif len(self.data["packets"]) == self.maxlen:
|
|
|
|
self.data["packets"].popitem(last=False)
|
2024-04-05 15:02:26 -04:00
|
|
|
self.data["packets"][packet.key] = packet
|
2023-09-28 12:19:18 -04:00
|
|
|
|
2024-04-11 22:55:01 -04:00
|
|
|
@wrapt.synchronized(lock)
|
2023-11-17 11:36:39 -05:00
|
|
|
def copy(self):
|
2024-04-11 21:40:43 -04:00
|
|
|
return self.data.copy()
|
2023-11-17 11:36:39 -05:00
|
|
|
|
2023-09-28 12:19:18 -04:00
|
|
|
@property
|
|
|
|
def maxlen(self):
|
|
|
|
return self._maxlen
|
|
|
|
|
|
|
|
@wrapt.synchronized(lock)
|
|
|
|
def find(self, packet):
|
2024-04-11 21:40:43 -04:00
|
|
|
return self.data["packets"][packet.key]
|
2023-09-28 12:19:18 -04:00
|
|
|
|
2024-04-11 22:55:01 -04:00
|
|
|
@wrapt.synchronized(lock)
|
2023-09-28 12:19:18 -04:00
|
|
|
def __len__(self):
|
2024-04-05 15:02:26 -04:00
|
|
|
return len(self.data["packets"])
|
2022-12-15 17:23:54 -05:00
|
|
|
|
|
|
|
@wrapt.synchronized(lock)
|
2022-12-18 21:44:23 -05:00
|
|
|
def total_rx(self):
|
|
|
|
return self._total_rx
|
2022-12-15 17:23:54 -05:00
|
|
|
|
|
|
|
@wrapt.synchronized(lock)
|
2022-12-18 21:44:23 -05:00
|
|
|
def total_tx(self):
|
|
|
|
return self._total_tx
|
2024-03-29 11:51:15 -04:00
|
|
|
|
2024-04-11 22:24:02 -04:00
|
|
|
@wrapt.synchronized(lock)
|
2024-04-02 14:07:37 -04:00
|
|
|
def stats(self, serializable=False) -> dict:
|
2024-03-29 11:51:15 -04:00
|
|
|
stats = {
|
2024-04-11 22:24:02 -04:00
|
|
|
"total_tracked": self._total_rx + self._total_rx,
|
|
|
|
"rx": self._total_rx,
|
|
|
|
"tx": self._total_tx,
|
2024-04-05 15:02:26 -04:00
|
|
|
"types": self.data["types"],
|
|
|
|
"packets": self.data["packets"],
|
2024-03-29 11:51:15 -04:00
|
|
|
}
|
|
|
|
return stats
|