diff --git a/src/netnode.py b/src/netnode.py --- a/src/netnode.py +++ b/src/netnode.py @@ -1,11 +1,17 @@ +import os import socket import logging as log +import config as conf from networkers import NetworkReader,NetworkWriter from hashtree import HashTree +lockFile=os.path.join(conf.directory,"dirty.lock") + + class FailedConnection(Exception): pass +class LockedException(Exception): pass class BaseConnection: # abstract @@ -47,6 +53,19 @@ class NetNode: self._newLeaves=dict() + def isLocked(self): + return os.path.isfile(lockFile) + + def _lock(self): + try: + f=open(lockFile,"x") + f.close() + except FileExistsError: + raise LockedException() + + def _unlock(self): + os.remove(lockFile) + def _updateTree(self): log.info("updating hash tree...") self._tree.batchUpdate(self._newLeaves.items())