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

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

@ -3,4 +3,4 @@
rm ./rtbw.sock rm ./rtbw.sock
rm ./buffer.dat 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_GET = 1
CMD_CLEAR = 2 CMD_CLEAR = 2
CMD_SHUTDOWN = 3 CMD_SHUTDOWN = 3
CMD_GET_CLEAR = 4
def __init__(self): def __init__(self):
self.uCommand = 0 self.uCommand = 0

Loading…
Cancel
Save