better flow control for pipeListener
This commit is contained in:
parent
130f7f73f2
commit
5dae7a798d
29
listeners.py
29
listeners.py
|
@ -113,20 +113,23 @@ class KeypadListener(ExceptionThread):
|
|||
except AttributeError:
|
||||
pass
|
||||
|
||||
# TODO: these are not threadsafe
|
||||
# TODO: this code gets really confused if the pipe is deleted
|
||||
class PipeListener(ExceptionThread):
|
||||
def __init__(self, callback, path):
|
||||
self._path = path
|
||||
|
||||
if os.path.exists(self._path):
|
||||
if not stat.S_ISFIFO(os.stat(self._path)[0]):
|
||||
os.remove(self._path)
|
||||
os.mkfifo(self._path)
|
||||
_rootDir = '/tmp'
|
||||
_pipeMode = 0o0777
|
||||
|
||||
def __init__(self, callback, name):
|
||||
self._path = os.path.join(self._rootDir, name)
|
||||
|
||||
if not os.path.exists(self._path):
|
||||
os.mkfifo(self._path, mode=self._pipeMode)
|
||||
else:
|
||||
os.mkfifo(self._path)
|
||||
|
||||
os.chmod(self._path, 0o0777)
|
||||
st_mode = os.state(self._path).st_mode
|
||||
if not stat.S_ISFIFO(st_mode):
|
||||
os.remove(self._path)
|
||||
os.mkfifo(self._path, mode=self._pipeMode)
|
||||
elif st_mode % 0o10000 != self._pipeMode:
|
||||
os.chmod(self._path, self._pipeMode)
|
||||
|
||||
def listenForSecret():
|
||||
while 1:
|
||||
|
@ -139,6 +142,8 @@ class PipeListener(ExceptionThread):
|
|||
logger.debug('Started pipe listener at path %s', self._path)
|
||||
|
||||
def __del__(self):
|
||||
if os.path.exists(self._path):
|
||||
try:
|
||||
os.remove(self._path)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
logger.debug('Cleaned up pipe listener at path %s', self._path)
|
||||
|
|
|
@ -175,7 +175,7 @@ class StateMachine:
|
|||
|
||||
self.secretListener = PipeListener(
|
||||
callback = secretCallback,
|
||||
path = '/tmp/secret'
|
||||
name = 'secret'
|
||||
)
|
||||
|
||||
self.keypadListener = KeypadListener(
|
||||
|
|
Loading…
Reference in New Issue