autorun: server receives GET_CLIENTS msg, sends back clients list
authorAdriana Draghici <adriana008@gmail.com>
Sat, 17 Apr 2010 11:14:44 +0000 (11:14 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Sat, 17 Apr 2010 11:14:44 +0000 (11:14 +0000)
autorun/PROTOCOL
autorun/server/Client.py
autorun/server/Server.py

index d677070..14e6c5e 100644 (file)
@@ -29,7 +29,7 @@ Serverul pastreaza un fisier cu info despre toate fisierele downloadate:
 
 1. Mesaje GET_CLIENTS
  -----> tipul mesajului GET_CLIENTS
- <----- lista tupluri (client_name, pid)   
+ <----- lista de liste [client_name, pid, torrent_file]   
 
 
 2. Mesaje GET_OUTPUT
index a29924c..c5c6614 100644 (file)
@@ -54,6 +54,7 @@ class MySocket:
         print "am primit ", dd
         return dd
     """
+
     def recv_pickled_data(self):
         msg = ''
         while 1:
@@ -70,7 +71,6 @@ class MySocket:
         #print "am primit mesajul: ", dd
         return dd
 
-
     # send a pickled dictionary
     def send_dict(self, data):
         dumped_data = pickle.dumps(data)       
@@ -114,17 +114,33 @@ def test_all_commands(torrent_file):
 
 
             }
-    print s.send_command(GET_OUTPUT, "")
-    """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
+    s = MySocket()
+    s.connect(SERVER_HOST, SERVER_PORT)
+    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)
+
+    s = MySocket()
+    s.connect(SERVER_HOST, SERVER_PORT)
+    response = s.send_command(STOP_MSG, pid2)
     print response
-    pid = response[1]
-    print pid
     s = MySocket()
     s.connect(SERVER_HOST, SERVER_PORT)
-    time.sleep(20)
-    response = s.send_command(STOP_MSG, pid)
+    response = s.send_command(STOP_MSG, pid1)
     print response
-    """   
+       
 def test_send_recv():
     # test 1
 
index 75370e8..a64c1cc 100644 (file)
@@ -19,8 +19,8 @@ class MyDaemon(Daemon):
     port = 0
     DEBUG = True
 
-    processes = {} # keeps lists of file descriptors for each process
-    
+    processes_fd = {}   # keeps lists of file descriptors for each process
+    processes_info = {} # keeps lists of BT clients name and torrent 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
@@ -63,12 +63,10 @@ class MyDaemon(Daemon):
             print "Server: sent message: ", data
 
 
-    """
-        Stores  data about log files in a text file.
-    """
     def save_download_info(self, data):
-        f = open(SESSIONS_FILE, 'a')
+        """ Stores  data about log files in a text file. """
 
+        f = open(SESSIONS_FILE, 'a')
         #f.write ("# session " + strftime("%d-%m-%Y %H:%M:%S", localtime())+ "\n")
         
         num_lines = len(data.keys())
@@ -85,11 +83,10 @@ class MyDaemon(Daemon):
 
         f.close()
 
-    """
-        Constructs a list with logging file info - paths, client, metafile
-        file_list: list to which the created logging info list in appended.
-    """
     def add_to_output_msg(self, file_list, info_dict):
+        """ Constructs a list with logging file info - paths, client, metafile
+        @param file_list list to which the created logging info list in appended. """
+        
         if CLIENT not in info_dict and TORRENT not in info_dict:
             return -1
         info_list = [info_dict[CLIENT],info_dict[TORRENT]]
@@ -106,11 +103,10 @@ class MyDaemon(Daemon):
         file_list.append (info_list)
         return 0 # success
 
-    """
-        Reads all the contents of the file that stores info about 
-        logging files and folders.
-    """
     def read_download_info(self, file_list):
+        """ Reads all the contents of the file that stores info about 
+        logging files and folders.
+        """
         import os.path
         if not os.path.exists(SESSIONS_FILE):
             print "Server Error: No sessions_file"
@@ -138,7 +134,7 @@ class MyDaemon(Daemon):
                 if size == 0 and info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
                     raise Exception
             
-            except TypeError:
+            except Exception:
                 print "Server Error: wrong file type for sessions file"
                 err_msg = "Error encountered while reading file"
                 f.close()
@@ -147,13 +143,10 @@ class MyDaemon(Daemon):
             f.close()
             return 0
 
-
-
-
-    """ Starts a process for a BitTorrent client and returns its pid.
-        @return: -1, if any error is encountered
-    """
     def start_bt_client(self, bt_client_data):
+        """ Starts a process for a BitTorrent client and returns its pid.
+            @return: -1, if any error is encountered
+        """
 
         btcr = None
 
@@ -163,7 +156,6 @@ class MyDaemon(Daemon):
             btcr = TriblerRun(bt_client_data[BASE_DIR])
         elif bt_client_data[CLIENT] == HRKTORRENT:
             btcr = HrktorrentRun(bt_client_data[BASE_DIR])
-
         else:
             return -1
         
@@ -174,43 +166,39 @@ class MyDaemon(Daemon):
 
         btcr.start()
         [pid, log_fd, output_fd] = btcr.run_client(btcr.simple_run_command)
-        self.processes[pid] = (log_fd, output_fd)
-        print self.processes[pid]
+        self.processes_fd[pid] = [log_fd, output_fd]
+        self.processes_info[pid] = [bt_client_data[CLIENT], bt_client_data[TORRENT]]
+        
         if(self.DEBUG):
             print "Server: started client with pid = ", pid
         return pid
 
-
-    """
-        Stops a BT client by killing it.
-    """
     def stop_bt_client(self, pid):
+        """ Stops a BT client by killing it."""
         
         int_pid = int(pid)
         os.kill(int_pid, signal.SIGKILL) # kill generates zombies
         os.wait()
-        self.processes[int_pid][0].close() 
-        self.processes[int_pid][1].close()
-        del self.processes[int_pid]
+        self.processes_fd[int_pid][0].close() 
+        self.processes_fd[int_pid][1].close()
+        del self.processes_fd[int_pid]
 
         if(self.DEBUG):
             print "Server: killed process with pid = ", pid
 
-
-    """Sets the SO_LINGER value on a socket."""
     def set_linger(self,sock, l_onoff, l_linger):
+        """Sets the SO_LINGER value on a socket."""
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                 struct.pack('ii', l_onoff, l_linger))
 
 
-    """
-        Accepts socket connections and receives messages from commander.
-    """
     def do_Server(self, ip, port):
+        """ Accepts socket connections and receives messages from commander."""
 
         self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         if(self.DEBUG):
             print "Server: host ip = %s, port = %s"%(ip,SERVER_PORT)
+        
         self.serversocket.bind((ip,SERVER_PORT));
         self.serversocket.listen(10) #max 10 requests
         self.set_linger(self.serversocket,1, 0)
@@ -224,31 +212,38 @@ class MyDaemon(Daemon):
             msg = self.recv_pickled_data(clientsock)
             
             err_msg = ''
+            response = ''
             if msg[0] == START_MSG:
                 bt_client_data = msg[1]
-                client_pid = self.start_bt_client(bt_client_data)
+                response = self.start_bt_client(bt_client_data) #returns client_pid
                 self.save_download_info(bt_client_data)
-                self.send_pickled_data(clientsock, (ACK_MSG, client_pid))
 
             elif msg[0] == STOP_MSG:
                 client_pid = msg[1]
                 self.stop_bt_client(client_pid)
-                self.send_pickled_data(clientsock, (ACK_MSG,""))
+                response = ""
         
             elif msg[0] == GET_OUTPUT:
-                file_list = []
-                if self.read_download_info(file_list) < 0:
+                response = []
+                if self.read_download_info(response) < 0:
                     err_msg = "Error encountered while reading file"
-                else:
-                    self.send_pickled_data(clientsock, (ACK_MSG, file_list))
             
             elif msg[0] == GET_CLIENTS:
-                print "la get output"
+                response = []
+                print self.processes_info
+                try:
+                    for k in self. processes_info.keys():
+                        response.append([self.processes_info[k][0], k, self.processes_info[k][1]]) # client_name, pid, torrent file
+                except Exception:
+                     err_msg = "Error: could not retrive running clients list"    
+
             else:
                 err_msg = "Error: wrong message type";
 
             if err_msg != '':
                 self.send_pickled_data(clientsock,(ERROR_MSG,err_msg))
+            else:
+                self.send_pickled_data(clientsock, (ACK_MSG, response))
 
             clientsock.close()