there can be more than one message on the socket; empty the queue when shutting down...
authorRalf Jung <post@ralfj.de>
Sat, 9 Nov 2013 12:46:26 +0000 (13:46 +0100)
committerRalf Jung <post@ralfj.de>
Sat, 9 Nov 2013 12:46:26 +0000 (13:46 +0100)
libtuer.py
spaceapi.py
statemachine.py
tryshell
tuerd
tyshell
tysock.py

index 01c04ae5d725863fecefe6fceddcae4f931833d2..f79dcc6cf4c9403fb0dbcb2bd3ebfc6db7d1996b 100644 (file)
@@ -111,6 +111,13 @@ class ThreadFunction():
                self._q.put((ThreadFunction._CALL, arg))
        
        def stop(self):
                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()
 
                self._q.put((ThreadFunction._TERM, None))
                self._t.join()
 
index eea73f6a2a708fb3061617527e60fb10a76207f7..8e36b0fe21a3d372e0b8e03f443f80baf60365c8 100644 (file)
@@ -4,7 +4,7 @@ import urllib.request, time
 
 from config import spaceApiKey
 
 
 from config import spaceApiKey
 
-RETRY_TIME = 30
+RETRY_TIME = 60
 HEARTBEAT_TIME = 10*60
 
 class SpaceApi:
 HEARTBEAT_TIME = 10*60
 
 class SpaceApi:
index a937f50c035140cef91d504006a0dbbce60f7014..5ad83ddbe22608d144bab3fb9ab34a9c051f223e 100644 (file)
@@ -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)
                        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)
                        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:
                        for cb in self.callbacks:
-                               if cb is not None:
-                                       cb(s)
+                               cb(s, lastMsg)
                def on_leave(self):
                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):
                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)
                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)
index 3d4e3ede848620ace9ef023fe98a8153b838adff..1880dd5fab7a2c1c138c54f9d9524004e243826d 100755 (executable)
--- 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())
        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()
        s.close()
-       print(str(data))
diff --git a/tuerd b/tuerd
index 220b2bca8078dd1efe57be8d1105d0154123846b..c0b4413957d4af20c9543106dcd73de7253bdc6a 100755 (executable)
--- a/tuerd
+++ b/tuerd
@@ -41,9 +41,10 @@ try:
        the_socket.accept()
 except KeyboardInterrupt:
        # this is what we waited for!
        the_socket.accept()
 except KeyboardInterrupt:
        # this is what we waited for!
-       logger.info("Got SIGINT, terminating...")
        pass
 
        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
 # 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 37873bcff4bc21df5a5e4f27dc867466a2ee7f9e..2df0a229b5069a4a4e292a9bcbaf87ccdff1906c 100755 (executable)
--- a/tyshell
+++ b/tyshell
@@ -43,9 +43,11 @@ def sendcmd(addr, cmd):
                s.connect(addr)
                s.settimeout(60.0)
                s.send(cmd.encode())
                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()
                s.close()
-               print(data.decode('utf-8'))
        return run
 
 def exitcmd(c):
        return run
 
 def exitcmd(c):
index 0984b2a480f949178acff6c51defde3c54f97f2f..11b5183b735c757f5740f73303106ce4cdfeca64 100644 (file)
--- a/tysock.py
+++ b/tysock.py
@@ -44,10 +44,11 @@ class TySocket():
                self._sock.listen(1)
        
        def _answer(self, conn):
                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)
                        # 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):
                return answer
        
        def accept(self):