autorun: implemented GET_STATUS in server
authorAdriana Draghici <adriana008@gmail.com>
Thu, 29 Apr 2010 14:17:30 +0000 (14:17 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Thu, 29 Apr 2010 14:17:30 +0000 (14:17 +0000)
autorun/Util.py
autorun/server/Client.py
autorun/server/Server.py
ppf/log-parser/generic/GenericStatusParser.py

index 36b4476..967ec55 100644 (file)
@@ -49,6 +49,16 @@ TRIBLER = "tribler"
 TRANSMISSION = "transmission"
 HRKTORRENT = "hrktorrent"
 
+#status line
+TIMESTAMP = "timestamp"
+DL_SIZE = "download_size"
+UP_SIZE = "upload_size"
+UP_SPEED = "upload_speed"
+DL_SPEED = "download_speed"
+ETA = "eta"
+DHT = "dht"
+NUM_PEERS = "num_peers"
+
 import os.path
 import time
 def create_archive_file(file_path):
index f34f0fa..e139ba4 100644 (file)
@@ -117,33 +117,41 @@ def test_all_commands(torrent_file):
 
 
             }
-    print s.send_command(ARCHIVE, "")
-    """response = s.send_command(START_MSG, start_data_tribler)
+    #print s.send_command(GET_OUTPUT, "")
+   
+    response = s.send_command(START_MSG, start_data_tribler)
     print response
     pid1 = response[1]
     print pid1
+    
+    time.sleep(7)
     s = MySocket()
     s.connect(SERVER_HOST, SERVER_PORT)
-    response = s.send_command(START_MSG, start_data_transmission)
+    response = s.send_command(GET_STATUS, pid1)
+    print response
+
+    """response = s.send_command(START_MSG, start_data_transmission)
     print response
     pid2 = response[1]
     print pid2
+    
     s = MySocket()
     s.connect(SERVER_HOST, SERVER_PORT)
     response = s.send_command(GET_CLIENTS,"")
     print response
-    
-    time.sleep(30)
-
+    """
+    time.sleep(5)
+    """
     s = MySocket()
     s.connect(SERVER_HOST, SERVER_PORT)
     response = s.send_command(STOP_MSG, pid2)
     print response
+    """
     s = MySocket()
     s.connect(SERVER_HOST, SERVER_PORT)
     response = s.send_command(STOP_MSG, pid1)
     print response
-    """   
+    
 def test_send_recv():
     # test 1
 
index 4609625..1c58a4e 100644 (file)
@@ -13,6 +13,7 @@ from Util import SERVER_HOST, SERVER_PORT
 from BitTorrentClientRun import *
 from TransmissionRun import *
 from TriblerRun import *
+from TriblerStatusParser import *
 
 class MyDaemon(Daemon):
 
@@ -21,7 +22,8 @@ class MyDaemon(Daemon):
     DEBUG = True
 
     processes_fd = {}   # keeps lists of file descriptors for each process
-    processes_info = {} # keeps lists of BT clients name and torrent name for each process
+    processes_info = {} # keeps lists of BT clients name, torrent name and status log name for each process
+   
     def __init__(self, pidfile, ip='', stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
         Daemon.__init__(self, pidfile, stdin, stdout, stderr)
         self.ip = ip
@@ -183,7 +185,25 @@ class MyDaemon(Daemon):
             return 0
 
 
+    def get_client_status(self, transfer_id, line_parts = []):
 
+        if int(transfer_id) not in self.processes_info:
+            return "Invalid transfer id: " + transfer_id
+        status_file_path = self.processes_info[transfer_id][2]
+        client = self.processes_info[transfer_id][0]
+        line = ""
+        if client == TRIBLER:
+            parser = TriblerStatusParser(status_file_path)
+        elif client == HRKTORRENT:
+            parser = TriblerStatusParser(status_file_path)
+        else:
+            return "Functionality not supported for client "+ client
+        line_parts.append(parser.parse_last_status_line(line))
+        if line_parts == "":
+            return "Error occured while reading status file " + status_file_path
+        print line_parts
+        print line
+        return ""
 
     def start_bt_client(self, bt_client_data):
         """ Starts a process for a BitTorrent client and returns its pid.
@@ -209,7 +229,9 @@ class MyDaemon(Daemon):
         btcr.start()
         [pid, log_fd, output_fd] = btcr.run_client(btcr.simple_run_command)
         self.processes_fd[pid] = [log_fd, output_fd]
-        self.processes_info[pid] = [bt_client_data[CLIENT], bt_client_data[TORRENT]]
+        self.processes_info[pid] = [bt_client_data[CLIENT], 
+                                    bt_client_data[TORRENT], 
+                                    bt_client_data[OUT_DIR] + "/" + bt_client_data[OUT_FILE]]
         
         if(self.DEBUG):
             print "Server: started client with pid = ", pid
@@ -293,10 +315,18 @@ class MyDaemon(Daemon):
                     f.close()
             
             elif msg[0] == GET_STATUS:
-                #TODO: current ideea: call parse_status_line from GenericStatusParser
-                # create a dictionary from the returned list, its better to use a dictionary 
-                # because it is position independent
-                pass
+                line_parts = []
+                err_msg = self.get_client_status(msg[1], line_parts)
+                response = {}
+                print len(line_parts) 
+                if len(line_parts[0]) >= 6:
+                    #response[TIMESTAMP] = line_parts[0]
+                    response[NUM_PEERS] = line_parts[0][0]
+                    response[DHT] = line_parts[0][1]
+                    response[DL_SPEED] = line_parts[0][2]
+                    response[UP_SPEED] = line_parts[0][3]
+                    response[DL_SIZE] = line_parts[0][4]
+                    response[ETA] = line_parts[0][5]
             
             else:
                 err_msg = "Error: wrong message type";
index 5100f7e..a0444f6 100644 (file)
@@ -76,26 +76,32 @@ class GenericStatusParser:
         upload_size = 0
         eta = 0
 
-        return (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta)
+        return (num_peers, dht, download_speed, upload_speed, 
+                download_size, upload_size, eta)
 
     def parse_last_status_line(self, line):
-        # read last line from status log file
-        f = open(self.filename, "r")
-        f.seek(-1, os.SEEK_END)
-
-        # seek before the beginning of the last line
-
-        while f.read(1) != '\n' :
-            f.seek(-2, os.SEEK_CUR)
-        line = f.readline()
-        f.close()
+        try:
+            # read last line from status log file
+            f = open(self.filename, "r")
+            f.seek(-1, os.SEEK_END)
+
+            # seek before the beginning of the last line
+
+            while f.read(1) != '\n' :
+                f.seek(-2, os.SEEK_CUR)
+            line = f.readline()
+            f.close()
+        except Exception, e:
+            print e
+            return []
         return self.parse_status_line(line)
 
     def cb_print(self, num_peers, dht,
             download_speed, upload_speed,
             download_size, upload_size,
             eta_seconds):
-        print "ps = %d, dht = %d, ds = %d kb/s, us = %d kb/s, dsize = %d bytes, usize = %d bytes" % (num_peers, dht, download_speed, upload_speed, download_size, upload_size)
+        print "ps = %d, dht = %d, ds = %d kb/s, us = %d kb/s, dsize = %d bytes, usize = %d bytes" % (num_peers, dht, 
+                                                            download_speed, upload_speed, download_size, upload_size)
 
     def parse(self, callback_func, callback_arg = None):
         try: