autorun: server receives GET_OUTPUT msg and sends back files' list
authorAdriana Draghici <adriana008@gmail.com>
Sat, 17 Apr 2010 10:27:13 +0000 (10:27 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Sat, 17 Apr 2010 10:27:13 +0000 (10:27 +0000)
autorun/server/Client.py
autorun/server/Server.py

index 0ec782e..a29924c 100644 (file)
@@ -44,7 +44,7 @@ class MySocket:
         msg = msg + chunk
         return msg
 
-    def recv_pickled_data(self):
+    """def recv_pickled_data(self):
         #      while chunk:            
         #              chunk = clientsock.recv(BUFFER_SIZE)
         #              data += chunk
@@ -53,6 +53,23 @@ class MySocket:
         dd = pickle.loads(data)        
         print "am primit ", dd
         return dd
+    """
+    def recv_pickled_data(self):
+        msg = ''
+        while 1:
+            chunk = self.sock.recv(BUFFER_SIZE)
+            if not chunk:
+                break
+                #raise RuntimeError, "socket connection broken"
+
+            msg = msg + chunk
+            if len(chunk) < BUFFER_SIZE:
+                break
+        
+        dd = pickle.loads(msg) 
+        #print "am primit mesajul: ", dd
+        return dd
+
 
     # send a pickled dictionary
     def send_dict(self, data):
@@ -97,7 +114,8 @@ def test_all_commands(torrent_file):
 
 
             }
-    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
     pid = response[1]
     print pid
@@ -106,7 +124,7 @@ def test_all_commands(torrent_file):
     time.sleep(20)
     response = s.send_command(STOP_MSG, pid)
     print response
-
+    """   
 def test_send_recv():
     # test 1
 
index 4fe6b60..75370e8 100644 (file)
@@ -42,24 +42,27 @@ class MyDaemon(Daemon):
             if not chunk:
                 break
                 #raise RuntimeError, "socket connection broken"
-
             msg = msg + chunk
             if len(chunk) < BUFFER_SIZE:
                 break
         
         dd = pickle.loads(msg) 
-        #print "am primit mesajul: ", dd
+        if self.DEBUG:
+            print "Server: received message: ", dd
         return dd
 
     def send_pickled_data (self, clientsock, data):
-            dumped_data = pickle.dumps(data)   
-            totalsent = 0
-            while totalsent < len(dumped_data):
-                sent = clientsock.send(dumped_data[totalsent:])
-                if sent == 0:
-                    raise RuntimeError,        "socket connection broken"
-                totalsent = totalsent + sent
-    
+        dumped_data = pickle.dumps(data)       
+        totalsent = 0
+        while totalsent < len(dumped_data):
+            sent = clientsock.send(dumped_data[totalsent:])
+            if sent == 0:
+                raise RuntimeError,    "socket connection broken"
+            totalsent = totalsent + sent
+        if self.DEBUG:
+            print "Server: sent message: ", data
+
+
     """
         Stores  data about log files in a text file.
     """
@@ -82,6 +85,69 @@ 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):
+        if CLIENT not in info_dict and TORRENT not in info_dict:
+            return -1
+        info_list = [info_dict[CLIENT],info_dict[TORRENT]]
+        if LOG_DIR in info_dict:
+            info_list.append((info_dict[LOG_DIR], LOG_DIR))
+        if OUT_FILE in info_dict:
+            info_list.append((info_dict[OUT_FILE], OUT_FILE))
+        if LOG_FILE in info_dict:
+            info_list.append((info_dict[LOG_FILE], LOG_FILE))
+        
+        if self.DEBUG:
+            print "Server: read transfer log info: ", info_list
+         
+        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):
+        import os.path
+        if not os.path.exists(SESSIONS_FILE):
+            print "Server Error: No sessions_file"
+            return -1
+        else:    
+            f = open(SESSIONS_FILE,"r")
+            line = f.readline()
+            size = 0
+            info_dict = {}
+            try:
+                while line != '':
+                    if size == 0 :
+                            if info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
+                                raise Exception
+                            size = int(line)
+                            info_dict = {}
+                    else:
+                        parts = line.strip().split(':')
+                        if len(parts) != 2:
+                            raise Exception
+                        info_dict[parts[0]] = parts[1]
+                        size = size - 1
+                    line = f.readline()
+               
+                if size == 0 and info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
+                    raise Exception
+            
+            except TypeError:
+                print "Server Error: wrong file type for sessions file"
+                err_msg = "Error encountered while reading file"
+                f.close()
+                return -1
+
+            f.close()
+            return 0
+
+
 
 
     """ Starts a process for a BitTorrent client and returns its pid.
@@ -130,30 +196,9 @@ class MyDaemon(Daemon):
         if(self.DEBUG):
             print "Server: killed process with pid = ", pid
 
-    """
-        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):
-        if CLIENT not in info_dict and TORRENT not in info_dict:
-            return -1
-        info_list = [info_dict[CLIENT],info_dict[TORRENT]]
-        if LOG_DIR in info_dict:
-            info_list.append((info_dict[LOG_DIR], LOG_DIR))
-        if OUT_FILE in info_dict:
-            info_list.append((info_dict[OUT_FILE], OUT_FILE))
-        if LOG_FILE in info_dict:
-            info_list.append((info_dict[LOG_FILE], LOG_FILE))
-        print info_list
-        
-        file_list.append (info_list)
-
-        return 0 # success
-
 
+    """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))
 
@@ -168,7 +213,7 @@ class MyDaemon(Daemon):
             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, 2)
+        self.set_linger(self.serversocket,1, 0)
         while(1):
             if(self.DEBUG):
                 print "Server: accepting connections"
@@ -177,8 +222,6 @@ class MyDaemon(Daemon):
                 print "Server: accepted connection from ", address
             
             msg = self.recv_pickled_data(clientsock)
-            if(self.DEBUG):
-                print "Server: received message:\n", msg
             
             err_msg = ''
             if msg[0] == START_MSG:
@@ -189,52 +232,24 @@ class MyDaemon(Daemon):
 
             elif msg[0] == STOP_MSG:
                 client_pid = msg[1]
-                if(self.DEBUG):
-                    print "Server: received message:\n", msg
                 self.stop_bt_client(client_pid)
                 self.send_pickled_data(clientsock, (ACK_MSG,""))
         
             elif msg[0] == GET_OUTPUT:
-
-                import os.path
-                if not os.path.exists(SESSIONS_FILE):
-                    print "Error: No sessions_file"
+                file_list = []
+                if self.read_download_info(file_list) < 0:
                     err_msg = "Error encountered while reading file"
-                else:    
-                    f = open(SESSIONS_FILE,"r")
-                    line = f.readline()
-                    size = 0
-                    info_dict = {}
-                    file_list = []
-                    try:
-                        while line != '':
-                            if size == 0:
-                                    if info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
-                                        raise Exception
-                                    size = int(line)
-                                    info_dict = {}
-                            else:
-                                parts = line.strip().split(':')
-                                if len(parts) != 2:
-                                    raise Exception
-                                info_dict[parts[0]] = parts[1]
-                                size = size - 1
-                            line = f.readline()
-                    
-                    except TypeError:
-                        print "Server: wrong file type for sessions file"
-                        err_msg = "Error encountered while reading file"
-
-                    print file_list
+                else:
+                    self.send_pickled_data(clientsock, (ACK_MSG, file_list))
             
             elif msg[0] == GET_CLIENTS:
-                pass
+                print "la get output"
             else:
+                err_msg = "Error: wrong message type";
 
-                self.send_pickled_data(clientsock,( ERROR_MSG,"Error: wrong message type "))
+            if err_msg != '':
+                self.send_pickled_data(clientsock,(ERROR_MSG,err_msg))
 
-            # clientsock.recv(BUFFER_SIZE)
-            # recv_pickled_data(clientsock)
             clientsock.close()
 
 
@@ -244,11 +259,10 @@ if __name__ == "__main__":
        
         
         if 'start' == sys.argv[1]:
-            #if(len(sys.argv) != 4):
-            #    print "usage:\n\t %s start <host_ip> <port>" % sys.argv[0]
-            #    sys.exit(2)
+            if(len(sys.argv) != 3):
+                print "usage:\n\t %s start <host_ip> " % sys.argv[0]
+                sys.exit(2)
 
-            #daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], int(sys.argv[3]), stdout = '/home/p2p/out', stderr = 'home/p2p/err') 
             daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], stdout = '/home/p2p/out', stderr = '/home/p2p/err') 
             daemon.start()
         elif 'stop' == sys.argv[1]: