diff --git a/storage/AbstractStorage.py b/storage/AbstractStorage.py new file mode 100644 index 0000000..3768890 --- /dev/null +++ b/storage/AbstractStorage.py @@ -0,0 +1,14 @@ +from abc import ABC, abstractmethod + + +class AbstractStorage(ABC): + + @classmethod + @abstractmethod + def put_responce(self, address, responce): + pass + + @classmethod + @abstractmethod + def save(self): + pass diff --git a/storage/JSONStorage.py b/storage/JSONStorage.py new file mode 100644 index 0000000..3fa9918 --- /dev/null +++ b/storage/JSONStorage.py @@ -0,0 +1,23 @@ +from AbstractStorage import AbstractStorage +import json +from threading import RLock + + +class JSONStorage(AbstractStorage): + + def __init__(self, path): + self.path = path + self.respdict = dict() + self.lock = RLock() + + def put_responce(self, address, responce): + ip, port = address + if ip not in self.respdict.keys(): + self.respdict[ip] = {"open": [], "close": []} + self.respdict[ip]["open" if responce != 0 else "close"].append(port) + + def save(self): + print("saving") + with open(self.path, "w") as f: + json.dump(self.respdict, f) + self.respdict = {} diff --git a/storage/__init__.py b/storage/__init__.py new file mode 100644 index 0000000..d98799d --- /dev/null +++ b/storage/__init__.py @@ -0,0 +1,6 @@ +import sys +import os + + +fil = __file__[:__file__.rindex(os.sep)] +sys.path.insert(0,fil)