status report command introduced
[saartuer.git] / tysock.py
index 3951d1f12b8a9adb8b59b0155dc270c6f1f5573e..b987567168bd9ca93a23e3ff0252b76a3f0d9c2d 100644 (file)
--- a/tysock.py
+++ b/tysock.py
@@ -1,4 +1,4 @@
-import socket, os, stat
+import socket, os, stat, struct, pwd, errno
 from statemachine import StateMachine
 from libtuer import logger
 SO_PEERCRED = 17 # DO - NOT - TOUCH
@@ -26,7 +26,11 @@ def forcerm(name):
 class TySocket():
        CMDs = {
                b'buzz': StateMachine.CMD_BUZZ,
-               b'open': StateMachine.CMD_OPEN,
+               b'unlock': StateMachine.CMD_UNLOCK,
+               b'lock': StateMachine.CMD_LOCK,
+               b'fallback_mode_on': StateMachine.CMD_FALLBACK_ON,
+               b'fallback_mode_off': StateMachine.CMD_FALLBACK_OFF,
+               b'status': StateMachine.CMD_STATUS,
        }
        
        def __init__(self, sm):
@@ -44,10 +48,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):
@@ -68,13 +73,14 @@ class TySocket():
                                if data in self.CMDs:
                                        self._sm.callback(self.CMDs[data], self._answer(conn))
                                        # _answer will be called, and it will close the connection
+                                       continue # make sure we break so we don't close it
                                else:
                                        waynesend(conn, 'Command not found')
-                                       conn.close()
                        except KeyboardInterrupt:
                                raise # forward Ctrl-C to the outside
+                       except socket.timeout:
+                               # it's okay
+                               logger.info("TySocket: Connection timed out")
                        except Exception as e:
-                               logger.error("TySocket: Something went wrong: %s" % str(e))
-       
-       def stop(self):
-               pass
+                               logger.critical("TySocket: Something went wrong: %s" % str(e))
+                       conn.close()