mirror of https://github.com/craigerl/aprsd.git
Update packets to use wrapt
This patch updates the aprsd/packets.py to use wrapt for it's method lock synchornization.
This commit is contained in:
parent
e5f60b5ce1
commit
967959e7b3
|
@ -3,6 +3,8 @@ import logging
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import wrapt
|
||||||
|
|
||||||
from aprsd import utils
|
from aprsd import utils
|
||||||
from aprsd.utils import objectstore
|
from aprsd.utils import objectstore
|
||||||
|
|
||||||
|
@ -18,6 +20,7 @@ class PacketList:
|
||||||
"""Class to track all of the packets rx'd and tx'd by aprsd."""
|
"""Class to track all of the packets rx'd and tx'd by aprsd."""
|
||||||
|
|
||||||
_instance = None
|
_instance = None
|
||||||
|
lock = threading.Lock()
|
||||||
config = None
|
config = None
|
||||||
|
|
||||||
packet_list = {}
|
packet_list = {}
|
||||||
|
@ -29,7 +32,6 @@ class PacketList:
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
cls._instance = super().__new__(cls)
|
cls._instance = super().__new__(cls)
|
||||||
cls._instance.packet_list = utils.RingBuffer(1000)
|
cls._instance.packet_list = utils.RingBuffer(1000)
|
||||||
cls._instance.lock = threading.Lock()
|
|
||||||
cls._instance.config = kwargs["config"]
|
cls._instance.config = kwargs["config"]
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
|
@ -37,47 +39,47 @@ class PacketList:
|
||||||
if config:
|
if config:
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
|
@wrapt.synchronized(lock)
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
with self.lock:
|
return iter(self.packet_list)
|
||||||
return iter(self.packet_list)
|
|
||||||
|
|
||||||
|
@wrapt.synchronized(lock)
|
||||||
def add(self, packet):
|
def add(self, packet):
|
||||||
with self.lock:
|
packet["ts"] = time.time()
|
||||||
packet["ts"] = time.time()
|
if (
|
||||||
if (
|
"fromcall" in packet
|
||||||
"fromcall" in packet
|
and packet["fromcall"] == self.config["aprs"]["login"]
|
||||||
and packet["fromcall"] == self.config["aprs"]["login"]
|
):
|
||||||
):
|
self.total_tx += 1
|
||||||
self.total_tx += 1
|
else:
|
||||||
else:
|
self.total_recv += 1
|
||||||
self.total_recv += 1
|
self.packet_list.append(packet)
|
||||||
self.packet_list.append(packet)
|
SeenList().update_seen(packet)
|
||||||
SeenList().update_seen(packet)
|
|
||||||
|
|
||||||
|
@wrapt.synchronized(lock)
|
||||||
def get(self):
|
def get(self):
|
||||||
with self.lock:
|
return self.packet_list.get()
|
||||||
return self.packet_list.get()
|
|
||||||
|
|
||||||
|
@wrapt.synchronized(lock)
|
||||||
def total_received(self):
|
def total_received(self):
|
||||||
with self.lock:
|
return self.total_recv
|
||||||
return self.total_recv
|
|
||||||
|
|
||||||
|
@wrapt.synchronized(lock)
|
||||||
def total_sent(self):
|
def total_sent(self):
|
||||||
with self.lock:
|
return self.total_tx
|
||||||
return self.total_tx
|
|
||||||
|
|
||||||
|
|
||||||
class WatchList(objectstore.ObjectStoreMixin):
|
class WatchList(objectstore.ObjectStoreMixin):
|
||||||
"""Global watch list and info for callsigns."""
|
"""Global watch list and info for callsigns."""
|
||||||
|
|
||||||
_instance = None
|
_instance = None
|
||||||
|
lock = threading.Lock()
|
||||||
data = {}
|
data = {}
|
||||||
config = None
|
config = None
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
cls._instance = super().__new__(cls)
|
cls._instance = super().__new__(cls)
|
||||||
cls._instance.lock = threading.Lock()
|
|
||||||
if "config" in kwargs:
|
if "config" in kwargs:
|
||||||
cls._instance.config = kwargs["config"]
|
cls._instance.config = kwargs["config"]
|
||||||
cls._instance._init_store()
|
cls._instance._init_store()
|
||||||
|
@ -112,12 +114,12 @@ class WatchList(objectstore.ObjectStoreMixin):
|
||||||
def callsign_in_watchlist(self, callsign):
|
def callsign_in_watchlist(self, callsign):
|
||||||
return callsign in self.data
|
return callsign in self.data
|
||||||
|
|
||||||
|
@wrapt.synchronized(lock)
|
||||||
def update_seen(self, packet):
|
def update_seen(self, packet):
|
||||||
with self.lock:
|
callsign = packet["from"]
|
||||||
callsign = packet["from"]
|
if self.callsign_in_watchlist(callsign):
|
||||||
if self.callsign_in_watchlist(callsign):
|
self.data[callsign]["last"] = datetime.datetime.now()
|
||||||
self.data[callsign]["last"] = datetime.datetime.now()
|
self.data[callsign]["packets"].append(packet)
|
||||||
self.data[callsign]["packets"].append(packet)
|
|
||||||
|
|
||||||
def last_seen(self, callsign):
|
def last_seen(self, callsign):
|
||||||
if self.callsign_in_watchlist(callsign):
|
if self.callsign_in_watchlist(callsign):
|
||||||
|
@ -160,19 +162,20 @@ class SeenList(objectstore.ObjectStoreMixin):
|
||||||
"""Global callsign seen list."""
|
"""Global callsign seen list."""
|
||||||
|
|
||||||
_instance = None
|
_instance = None
|
||||||
|
lock = threading.Lock()
|
||||||
data = {}
|
data = {}
|
||||||
config = None
|
config = None
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
cls._instance = super().__new__(cls)
|
cls._instance = super().__new__(cls)
|
||||||
cls._instance.lock = threading.Lock()
|
|
||||||
if "config" in kwargs:
|
if "config" in kwargs:
|
||||||
cls._instance.config = kwargs["config"]
|
cls._instance.config = kwargs["config"]
|
||||||
cls._instance._init_store()
|
cls._instance._init_store()
|
||||||
cls._instance.data = {}
|
cls._instance.data = {}
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
|
@wrapt.synchronized(lock)
|
||||||
def update_seen(self, packet):
|
def update_seen(self, packet):
|
||||||
callsign = None
|
callsign = None
|
||||||
if "fromcall" in packet:
|
if "fromcall" in packet:
|
||||||
|
|
Loading…
Reference in New Issue