autorun: modified server arguments
authorAdriana Draghici <adriana008@gmail.com>
Fri, 16 Apr 2010 12:51:58 +0000 (12:51 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Fri, 16 Apr 2010 12:52:05 +0000 (12:52 +0000)
autorun/Util.py
autorun/server/Server.py

index 1108b7e..bc00d66 100644 (file)
@@ -4,7 +4,7 @@ SERVER_DIR = "/home/p2p/cs-p2p-next/autorun/server/"
 SERVER_FILE = "Server.py"
 SERVER_TYPE = "python"
 SERVER_PORT = 10004 
-SERVER_HOST = "127.0.0.1"
+SERVER_HOST = "172.16.20.3"
 
 SESSIONS_FILE = "/home/p2p/sessions.txt" # TO CHANGE 
 BUFFER_SIZE = 4096
index 31378e2..33d9583 100644 (file)
@@ -37,10 +37,6 @@ class MyDaemon(Daemon):
         self.doServer(self.ip, self.port)
     
     def recv_pickled_data(self, clientsock):
-        #      while chunk:            
-        #              chunk = clientsock.recv(self.BUFFER_SIZE)
-        #              data += chunk
-
         msg = ''
         while 1:
             chunk = clientsock.recv(BUFFER_SIZE)
@@ -85,9 +81,10 @@ class MyDaemon(Daemon):
                 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
     """
@@ -99,7 +96,9 @@ class MyDaemon(Daemon):
             btcr = TransmissionRun(bt_client_data[BASE_DIR])
         elif bt_client_data[CLIENT] == TRIBLER:
             btcr = TriblerRun(bt_client_data[BASE_DIR])
-        
+        elif bt_client_data[CLIENT] == HRKTORRENT:
+            btcr = HrktorrentRun(bt_client_data[BASE_DIR])
+
         else:
             return -1
         
@@ -129,6 +128,9 @@ class MyDaemon(Daemon):
         btcr.run_client(btcr.simple_run_command)
         """
 
+    """
+        Stops a BT client by killing it.
+    """
     def stop_bt_client(self, pid):
         
         int_pid = int(pid)
@@ -136,16 +138,41 @@ class MyDaemon(Daemon):
         os.wait()
         self.processes[int_pid][0].close() 
         self.processes[int_pid][1].close()
-        del self.processes[int_pid] 
+        del self.processes[int_pid]
+
         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)
+        
+        file_list.append (info_list)
+
+        return 0 # success
+
+
+    """
+        Accepts socket connections and receives messages from commander.
+    """
     def doServer(self, ip, port):
 
         serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         if(self.DEBUG):
-            print "Server: host ip = %s, port = %d"%(SERVER_HOST,SERVER_PORT)
-        serversocket.bind((SERVER_HOST, SERVER_PORT));
+            print "Server: host ip = %s, port = %s"%(ip,SERVER_PORT)
+        serversocket.bind((ip,SERVER_PORT));
         serversocket.listen(10) #max 10 requests
         while(1):
             if(self.DEBUG):
@@ -158,7 +185,7 @@ class MyDaemon(Daemon):
             if(self.DEBUG):
                 print "Server: received message:\n", msg
             
-
+            err_msg = ''
             if msg[0] == START_MSG:
                 bt_client_data = msg[1]
                 client_pid = self.start_bt_client(bt_client_data)
@@ -172,12 +199,48 @@ class MyDaemon(Daemon):
                 self.stop_bt_client(client_pid)
                 self.send_pickled_data(clientsock, (ACK_MSG,""))
         
-            elif msg[0] == GET_CLIENTS:
-                pass
             elif msg[0] == GET_OUTPUT:
-                #f = open(SESSIONS_FILE, )
-                pass
 
+                print "LA get output\n"
+                import os.path
+                if not os.path.exists(SESSIONS_FILE):
+                    print "Error: No sessions_file"
+                    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 != '':
+                            print line 
+                            if size == 0:
+                                    print "aici la size = 0"
+                                    if info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
+                                        print "aici"
+                                        raise Exception
+                                    print "aici3ia"
+                                    size = int(line)
+                                    print "aici3"
+                                    info_dict = {}
+                            else:
+                                parts = line.strip().split(':')
+                                if len(parts) != 2:
+                                    print "aici2"
+                                    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
+            
+            elif msg[0] == GET_CLIENTS:
+                pass
             else:
 
                 self.send_pickled_data(clientsock,( ERROR_MSG,"Error: wrong message type "))
@@ -198,7 +261,7 @@ if __name__ == "__main__":
             #    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', SERVER_HOST, 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]:
             daemon = MyDaemon('/tmp/daemon-example.pid') 
@@ -207,8 +270,8 @@ if __name__ == "__main__":
             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("","")
+            daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], stdout = '/home/p2p/out', stderr = '/home/p2p/err') 
+            daemon.doServer(sys.argv[2],"")
         else:
             print "Unknown command"
             sys.exit(2)