Added atomic get-clear

master
Ringo Wantanabe 6 years ago
parent e57456a73b
commit 280cf1f965
No known key found for this signature in database
GPG Key ID: C1C1CD34CF2907B2

@ -85,10 +85,8 @@ class StatBuffer:
SLV_NOUI = 3 #Remove all user inputed information (sub, com,name,trip, file info)
SLV_HIGH = 0xFF #Keep only post number
def __init__(self):
self._mutex = Lock()
pass
def _lock(self):
self._mutex.acquire()
def _unlock(self):
@ -176,7 +174,7 @@ class MemoryBuffer(StatBuffer):
return super()._decode(self.store[-1])["no"]
finally:
super()._unlock()
def readno(self, floor):
def readno(self, floor, ca=False):
super()._lock()
posts = list()
nl = len(self.store)-1
@ -185,6 +183,8 @@ class MemoryBuffer(StatBuffer):
if(entry["no"]<=floor): break
posts.append(entry)
nl-=1
if ca:
self.store = list()
super()._unlock()
return posts
@ -253,7 +253,7 @@ class FileBuffer(StatBuffer):
else: return 0
finally:
super()._unlock()
def readno(self, floor):
def readno(self, floor, ca=False):
super()._lock()
posts = list()
if self.file.tell()>0:
@ -263,6 +263,8 @@ class FileBuffer(StatBuffer):
posts.append(ent)
ent = self._readentry()
self.file.seek(0,2)
if ca:
self.file.truncate(0)
super()._unlock()
return posts
@ -352,9 +354,12 @@ class Daemon(threading.Thread):
self.buf = buf
self.running=True
threading.Thread.__init__(self)
def _get(self,con, fr):
log("[daemon]: Recieved get from "+str(fr))
data = self.buf.readno(fr)
def _get(self,con, fr, ca=False):
if ca:
log("[daemon]: Recieved get-clear from "+str(fr))
else:
log("[daemon]: Recieved get from "+str(fr))
data = self.buf.readno(fr, ca)
js = json.dumps(data)
con.send(js.encode("utf-8"))
def run(self):
@ -373,6 +378,8 @@ class Daemon(threading.Thread):
self.running=False
elif cmd.uCommand == Command.CMD_GET: #receive entries from <data>
self._get(con, struct.unpack("L", cmd.uData)[0])
elif cmd.uCommand == Command.CMD_GET_CLEAR: #receive entries from <data> then clear
self._get(con, struct.unpack("L", cmd.uData)[0], True)
elif cmd.uCommand == Command.CMD_CLEAR: #clear buffer
log("[daemon]: Recieved clear")
self.buf.clear()

@ -26,6 +26,7 @@ def parsecmd(s):
if s=="get": return Command.CMD_GET
elif s=="stop": return Command.CMD_SHUTDOWN
elif s=="clear": return Command.CMD_CLEAR
elif s=="get-clear": return Command.CMD_GET_CLEAR
else: return None
parser = argparse.ArgumentParser(description="rtbwpy daemon control.")
@ -43,7 +44,7 @@ if cmd==None:
data = args.data
if cmd == Command.CMD_GET:
if cmd == Command.CMD_GET or cmd == Command.CMD_GET_CLEAR:
if data== None:
data = struct.pack("L", 0)
else:
@ -58,7 +59,7 @@ cmd = Command.build(cmd, data)
con.send(cmd.serialise())
if cmd.uCommand == Command.CMD_GET:
if cmd.uCommand == Command.CMD_GET or cmd.uCommand == Command.CMD_GET_CLEAR:
json = con.recv().decode("utf-8")
print(json)

@ -3,4 +3,4 @@
rm ./rtbw.sock
rm ./buffer.dat
python3 rtbw.py --debug --buffer buffer.dat --daemon rtbw.sock bant 10
python3 rtbw.py --buffer buffer.dat --daemon rtbw.sock bant 10

@ -27,6 +27,7 @@ class Command(object):
CMD_GET = 1
CMD_CLEAR = 2
CMD_SHUTDOWN = 3
CMD_GET_CLEAR = 4
def __init__(self):
self.uCommand = 0

Loading…
Cancel
Save