autorun: server stored transfers info in a sessions files
authorAdriana Draghici <adriana008@gmail.com>
Thu, 15 Apr 2010 12:36:04 +0000 (12:36 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Thu, 15 Apr 2010 12:36:12 +0000 (12:36 +0000)
autorun/Util.py
autorun/server/Server.py

index 186ebc8..5a4b83c 100644 (file)
@@ -3,9 +3,11 @@
 SERVER_DIR = "/home/p2p/cs-p2p-next/autorun/server/"
 SERVER_FILE = "Server.py"
 SERVER_TYPE = "python"
-SERVER_PORT = 10004 
+SERVER_PORT = 10003 
 SERVER_HOST = "127.0.0.1"
 
+SESSIONS_FILE = "/home/p2p/sessions.txt" # TO CHANGE 
+
 """ Message types: client -> server """
 
 START_MSG = 1
index 66cbb11..296e456 100644 (file)
@@ -1,8 +1,10 @@
 #!/usr/bin/env python
 
 import sys, os, socket 
+import time
 import pickle
 import signal
+
 from daemon import Daemon
 from Util import *
 from Util import SERVER_HOST, SERVER_PORT
@@ -50,8 +52,30 @@ class MyDaemon(Daemon):
                 if sent == 0:
                     raise RuntimeError,        "socket connection broken"
                 totalsent = totalsent + sent
+    
+    """
+        Stores  data about log files in a text file.
+    """
+    def save_download_info(self, data):
+        f = open(SESSIONS_FILE, 'a')
+
+        #f.write ("# session " + strftime("%d-%m-%Y %H:%M:%S", localtime())+ "\n")
+        
+        num_lines = len(data.keys())
+        if PORT in data:
+            num_lines = num_lines -1;
+        if BASE_DIR in data:
+            num_lines = num_lines -1;
+
+        f.write(str(num_lines)+"\n")
+        for k in data.keys():
+            if k == PORT or k ==  BASE_DIR:
+                continue
+            f.write(str(k)+":"+str(data[k])+"\n")
 
 
+        f.close()
+
     """ Starts a process for a BitTorrent client and returns its pid.
         @return: -1, if any error is encountered
     """
@@ -126,6 +150,7 @@ class MyDaemon(Daemon):
             if msg[0] == START_MSG:
                 bt_client_data = msg[1]
                 client_pid = self.start_bt_client(bt_client_data)
+                self.save_download_info(bt_client_data)
                 self.send_pickled_data(clientsock, (ACK_MSG, client_pid))
 
             elif msg[0] == STOP_MSG:
@@ -134,7 +159,13 @@ class MyDaemon(Daemon):
                     print "Server: received message:\n", msg
                 self.stop_bt_client(client_pid)
                 self.send_pickled_data(clientsock, (ACK_MSG,""))
-            
+        
+            elif msg[0] == GET_CLIENTS:
+                pass
+            elif msg[0] == GET_OUTPUT:
+
+                pass
+
             else:
 
                 self.send_pickled_data(clientsock,( ERROR_MSG,"Error: wrong message type "))
@@ -147,6 +178,7 @@ class MyDaemon(Daemon):
 if __name__ == "__main__":
 
     if len(sys.argv) >= 2:
+       
         
         if 'start' == sys.argv[1]:
             #if(len(sys.argv) != 4):
@@ -162,6 +194,9 @@ if __name__ == "__main__":
         elif 'restart' == sys.argv[1]:
             daemon = MyDaemon('/tmp/daemon-example.pid') 
             daemon.restart()
+        elif 'no' == sys.argv[1]: #no daemon
+            daemon = MyDaemon('/tmp/daemon-example.pid', SERVER_HOST, stdout = '/home/p2p/out', stderr = '/home/p2p/err') 
+            daemon.doServer("","")
         else:
             print "Unknown command"
             sys.exit(2)