Repository navigation
Expand file tree
/
Copy pathcache.py
More file actions
executable file
·70 lines (59 loc) · 1.88 KB
/
Copy pathcache.py
File metadata and controls
executable file
·70 lines (59 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import sys
from timeit import default_timer as timer
import zmq
import time
import signal
import logging
import helpers
import yaml
# Load config
with open("/home/pi/.development/the-cake-can-wait/config.yaml", "r") as configfile:
config = yaml.safe_load(configfile)
# Constants
cache_period = config["user_cache"]["cache_period"]
scanner_endpoint = config["general"]["scanner_endpoint"]
prehook_endpoint = config["general"]["prehook_endpoint"]
#Logging setup
helpers.setup_logging()
logger = logging.getLogger("user_cache")
logger.debug("Setting up sockets")
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.connect(scanner_endpoint)
subscriber.setsockopt(zmq.SUBSCRIBE, b'')
subscriber.setsockopt(zmq.CONFLATE, 1)
prehook = context.socket(zmq.REP)
prehook.bind(prehook_endpoint)
def fexit(signal,frame):
subscriber.close()
prehook.close()
context.term()
logger.info("Terminating cache.")
sys.exit(0)
signal.signal(signal.SIGINT, fexit)
poller = zmq.Poller()
poller.register(subscriber, zmq.POLLIN)
poller.register(prehook, zmq.POLLIN)
release = 0
uid = None
while True:
socks = dict(poller.poll(1000))
logger.debug("Socket poll returned %s", socks)
if subscriber in socks and socks[subscriber] == zmq.POLLIN:
uid = subscriber.recv_pyobj()
release = timer() + cache_period
logger.debug("Received new uid: %s | Release time is %s", uid, release)
if prehook in socks and socks[prehook] == zmq.POLLIN:
logger.debug("Received request from prehook")
prehook.recv_pyobj()
reply = uid if uid != None else 0
logger.debug("Sending reply to prehook")
prehook.send_pyobj(reply)
uid = None
release = 0
time.sleep(0.2)
if release != 0 and timer() > release:
logger.debug("Releasing cached uid")
uid = None
release = 0