From: Ralf Jung Date: Sat, 9 Nov 2013 12:46:26 +0000 (+0100) Subject: there can be more than one message on the socket; empty the queue when shutting down... X-Git-Url: https://git.ralfj.de/saartuer.git/commitdiff_plain/d084286e81b121106a0a165e3c151762635466e0?ds=inline;hp=b8b6e14e6923809efc61c71dd94b1e66026aad9b there can be more than one message on the socket; empty the queue when shutting down a callback-thread; increase the SpaceAPI-retry-time --- diff --git a/libtuer.py b/libtuer.py index 01c04ae..f79dcc6 100644 --- a/libtuer.py +++ b/libtuer.py @@ -111,6 +111,13 @@ class ThreadFunction(): self._q.put((ThreadFunction._CALL, arg)) def stop(self): + # empty the queue + try: + while True: + self._q.get_nowait() + except queue.Empty: + pass + # now wait till the job-in-progress is done self._q.put((ThreadFunction._TERM, None)) self._t.join() diff --git a/spaceapi.py b/spaceapi.py index eea73f6..8e36b0f 100644 --- a/spaceapi.py +++ b/spaceapi.py @@ -4,7 +4,7 @@ import urllib.request, time from config import spaceApiKey -RETRY_TIME = 30 +RETRY_TIME = 60 HEARTBEAT_TIME = 10*60 class SpaceApi: diff --git a/statemachine.py b/statemachine.py index a937f50..5ad83dd 100644 --- a/statemachine.py +++ b/statemachine.py @@ -205,19 +205,20 @@ class StateMachine(): nervlist = [(OPEN_REPEAT_TIMEOUT, lambda: self.actor().act(Actor.CMD_UNLOCK)) for t in range(OPEN_REPEAT_NUMBER)] nervlist += [(OPEN_REPEAT_TIMEOUT, self.could_not_open)] super().__init__(sm,nervlist) - self.callbacks=[callback] - # TODO: can we send "202 processing: Trying to unlock the door" here? Are the callbacks multi-use? + self.callbacks = [] self.actor().act(Actor.CMD_UNLOCK) - def notify(self, did_it_work): - s = "200 okay: door unlocked" if did_it_work else ("500 internal server error: Couldn't unlock door with %d tries à %f seconds" % (OPEN_REPEAT_NUMBER,OPEN_REPEAT_TIMEOUT)) + # enqueue the callback + self.handle_cmd_unlock_event(callback) + def notify(self, s, lastMsg): for cb in self.callbacks: - if cb is not None: - cb(s) + cb(s, lastMsg) def on_leave(self): - self.notify(not self.pins().door_locked) + s = "200 okay: door unlocked" if not self.pins().door_locked else ("500 internal server error: Couldn't unlock door with %d tries à %f seconds" % (OPEN_REPEAT_NUMBER,OPEN_REPEAT_TIMEOUT)) + self.notify(s, lastMsg=True) def handle_cmd_unlock_event(self,callback): - # TODO: 202 notification also here if possible - self.callbacks.append(callback) + if callback is not None: + callback("202 processing: Trying to unlock the door", lastMsg=False) + self.callbacks.append(callback) def could_not_open(self): logger.critical("StateMachine: Couldn't open door after %d tries. Going back to StateZu." % OPEN_REPEAT_NUMBER) return StateMachine.StateZu(self.state_machine) diff --git a/tryshell b/tryshell index 3d4e3ed..1880dd5 100755 --- a/tryshell +++ b/tryshell @@ -10,6 +10,8 @@ while True: s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(tuerSock) s.send(command.encode()) - data = s.recv(64) + while True: + data = s.recv(256) + if not len(data): break + print(data) s.close() - print(str(data)) diff --git a/tuerd b/tuerd index 220b2bc..c0b4413 100755 --- a/tuerd +++ b/tuerd @@ -41,9 +41,10 @@ try: the_socket.accept() except KeyboardInterrupt: # this is what we waited for! - logger.info("Got SIGINT, terminating...") pass +logger.info("Terminating...") + # bring 'em all down the_waker.stop() # this one first, it "randomly" calls other threads the_pins.stop() # as does this diff --git a/tyshell b/tyshell index 37873bc..2df0a22 100755 --- a/tyshell +++ b/tyshell @@ -43,9 +43,11 @@ def sendcmd(addr, cmd): s.connect(addr) s.settimeout(60.0) s.send(cmd.encode()) - data = s.recv(256) + while True: + data = s.recv(256) + if not len(data): break + print(data.decode('utf-8')) s.close() - print(data.decode('utf-8')) return run def exitcmd(c): diff --git a/tysock.py b/tysock.py index 0984b2a..11b5183 100644 --- a/tysock.py +++ b/tysock.py @@ -44,10 +44,11 @@ class TySocket(): self._sock.listen(1) def _answer(self, conn): - def answer(msg): + def answer(msg, lastMsg = True): # this is called in another thread, so it should be quick and not touch the TySocket waynesend(conn, msg) - conn.close() + if lastMsg: + conn.close() return answer def accept(self):