]> gitweb.maison.local Git - rome.git/commitdiff
Added : Remus (mostly copied from Romulus)
authorlionel <lionel@isis.maison.local>
Wed, 31 Aug 2016 15:44:35 +0000 (17:44 +0200)
committerlionel <lionel@isis.maison.local>
Wed, 31 Aug 2016 15:44:35 +0000 (17:44 +0200)
remus

diff --git a/remus b/remus
index c086a06900a51ed3607ff7977bdfe675465d6563..297277048f5042bafc2df0fc761e5c5be47ceef7 100755 (executable)
--- a/remus
+++ b/remus
@@ -6,23 +6,25 @@
 import logging                  # Logs
 import time                     # sleep
 import string                   # split strings
+import socket                  # client/server TCP networking
 
 import os,sys,inspect           # for the following
 cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"modules")))
 if cmd_subfolder not in sys.path:
   sys.path.insert(0, cmd_subfolder)
 
-import lockfile                # daemonization
-from daemon import runner      # daemonization
+import lockfile             # daemonization
+from daemon import runner   # daemonization
+from pydes import pyDes     # DES encryption
 
 from multiprocessing import Process
 #import subprocess
 #import shlex
 
 #    logger.debug("Debug message")
-#    logger.info("Info message")
 #    logger.warn("Warning message")
 #    logger.error("Error message")
+#    logger.info("Info message")
 
 class App():
 
@@ -30,31 +32,63 @@ class App():
     self.stdin_path = "/dev/null"
     self.stdout_path = "/home/lionel/work/python/rome/log/stdout.log"
     self.stderr_path = "/home/lionel/work/python/rome/log/stderr.log"
-    self.pidfile_path = "/home/lionel/work/python/rome/pid/horusd.pid"
+    self.pidfile_path = "/home/lionel/work/python/rome/pid/romulus.pid"
     self.pidfile_timeout = 5
 
   def run(self):
 # We must put all functions before the 'if __name__ blabla'
 
-    def heartbeat(min):
-      seconds = int(min) * 60
+    def heartbeat(void):
+# Heartbeat is also responsible of verifying the network link between the two
+      #seconds = int(min) * 60
+      MY_TCP_IP = '192.168.0.250'
+      MY_TCP_PORT = 5005
+      MY_BUFFER_SIZE = 1024
+
+      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+      s.bind((MY_TCP_IP, MY_TCP_PORT))
+      s.listen(1)
       while True:
-        logger.info("HB : alive")
-        time.sleep(seconds)
+        time.sleep(3)
+        conn, addr = s.accept()
+        if not conn: break
+        data = conn.recv(MY_BUFFER_SIZE)
+        if not data: break
+        logger.info("Received HB from " + str(addr) + " : " + str(data))
+        k = pyDes.des(b"DEADJACK", pyDes.CBC, b"\2\2\2\2\2\2\2\2", pad=None, padmode=2)
+        try:
+            #logger.info("HB packet content is : " + str(data))
+            #logger.info("decrypting")
+            data = k.decrypt(data)
+            #logger.info("HB packet content is : " + str(data))
+            data = data.decode('UTF-8')
+            #logger.info("decode from UTF-8")
+            #logger.info("HB packet content is : " + str(data))
+            if data == "HB":
+              DEST_MESSAGE = bytes("Received", 'UTF-8')
+              DEST_MESSAGE = k.encrypt(DEST_MESSAGE)
+              # Sending
+              conn.send(DEST_MESSAGE)
+              logger.info("HB packet sent back : Received")
+              conn.close()
+        except:
+            logger.info("Received an invalid HB packet")
+            time.sleep(1)
+      logger.error("I am out of the infinit HB loop !")
       return False
 
 
 # All functions definitions took place above
     if __name__ == '__main__':
-      logger.info("Rome test started")
+      logger.info("Romulus (test version) started")
 
       '''
       Launching the heartbeat function
       '''
-      p = Process(target=heartbeat, args=(2,))
-      p.daemon = True
-      p.start()
-      heartbeat_pid = p.pid
+      hbp = Process(target=heartbeat, args=(False,))
+      hbp.daemon = True
+      hbp.start()
+      heartbeat_pid = hbp.pid
       logger.info("heartbeat loop launched (pid " + str(heartbeat_pid) + ")")
 
       '''
@@ -65,14 +99,15 @@ class App():
         while True:
             time.sleep(30)
       except:
-        logger.error("While loop interrupted : aborting")
+        logger.error("Main infinit while loop interrupted : aborting")
+      hbp.stop()
         # Faut arrêter les Process ...
 
 # Invocation of the daemon part
 # Means : if it's working : DON'T TOUCH ANYTHING
 if __name__ == "__main__":
   app = App()
-  logger = logging.getLogger("horusd")
+  logger = logging.getLogger("romulus")
   loglevel = "debug"
   if (loglevel=="DEBUG") or (loglevel=="debug"):
     logger.setLevel(logging.DEBUG)
@@ -85,7 +120,7 @@ if __name__ == "__main__":
   else:
     logger.setLevel(logging.DEBUG)
   formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
-  handler = logging.FileHandler("log/horusd.log")
+  handler = logging.FileHandler("log/romulus.log")
   handler.setFormatter(formatter)
   logger.addHandler(handler)