server: fixed bugs.
authorAdriana Draghici <adriana008@gmail.com>
Fri, 19 Mar 2010 13:38:30 +0000 (13:38 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Fri, 19 Mar 2010 13:38:30 +0000 (13:38 +0000)
Util.py [deleted file]
bt_comm/server/Server.py

diff --git a/Util.py b/Util.py
deleted file mode 100644 (file)
index b69ad72..0000000
--- a/Util.py
+++ /dev/null
@@ -1,28 +0,0 @@
-
-#!/usr/bin/env python
-
-""" Message types: client -> server """
-
-START_MSG = "1";
-STOP_MSG = "2";
-STATUS_MSG = "3";
-
-ACK_MSG = "ACK" # send by server to the client after receiving a message if everything was ok
-ERROR_MSG = "ERROR " # send by server to the client if the parameters were incorrect 
-                       # or the client could not be started etc
-
-#xml tags from swarm.xml
-CLIENT = "client"
-BASE_DIR = "base"
-UP_LIMIT = "upload_limit"
-DL_LIMIT = "download_limit"
-DL_DIR = "download_dir"
-LOG_DIR = "logging_dir"
-OUT_FILE = "output_file"
-LOG_FILE = "log_file"
-PORT = "port"
-
-
-#clients
-TRIBLER = tribler
-TRANSMISSION = transmission
index 57e9421..2cc964b 100644 (file)
@@ -9,163 +9,183 @@ from Util import SERVER_HOST, SERVER_PORT
 from BitTorrentClientRun import *
 from TransmissionRun import *
 from TriblerRun import *
-BUFFER_SIZE = 4096
-
-states = {} # keeps track of what kind of message was previously receveid on a socket.
-processes = {}
-WAITING_MSG_TYPE = 0
-WAITING_START_DATA = 3
-WAITING_STOP_DATA = 4
-WAITING_STATUS_DATA = 5
 
 DEBUG = True
 
 
+class MyDaemon(Daemon):
 
-def recv_pickled_data(clientsock):
-    #  while chunk:            
-    #          chunk = clientsock.recv(BUFFER_SIZE)
-    #          data += chunk
-    data = clientsock.recv(BUFFER_SIZE)
-    dd = pickle.loads(data)    
-    return dd
-
-
-""" Starts a process for a BitTorrent client and returns its pid.
-    @return: -1, if any error is encountered
-"""
-def start_bt_client(bt_client_data):
-
-    btcr = None
+    ip = ""
+    port = 0
+    BUFFER_SIZE = 4096
 
-    if bt_client_data[CLIENT] == TRANSMISSION:
-        btcr = TransmissionRun(bt_client_data[BASE_DIR])
-    elif bt_client_data[CLIENT] == TRIBLER:
-        btcr = TriblerRun(bt_client_data[BASE_DIR])
+    states = {} # keeps track of what kind of message was previously receveid on a socket.
+    processes = {}
+    WAITING_MSG_TYPE = 0
+    WAITING_START_DATA = 3
+    WAITING_STOP_DATA = 4
+    WAITING_STATUS_DATA = 5
     
-    else:
-        return -1
+    def __init__(self, pidfile, ip='', port = 0, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
+        Daemon.__init__(self, pidfile, stdin, stdout, stderr)
+        self.ip = ip
+        self.port = port
+
+    def set_address(self, ip):
+        self.ip = ip
+        print "am setat ip ", ip
     
-    btcr.config_run(bt_client_data[DL_DIR], bt_client_data[LOG_DIR], 
-         bt_client_data[OUT_FILE], bt_client_data[LOG_DIR],
-         bt_client_data[LOG_FILE], bt_client_data[PORT], 
-         bt_client_data[TORRENT])
-
-    btcr.start()
-    [pid, log_fd, output_fd] = btcr.run_client(btcr.simple_run_command)
-    processes[pid] = (log_fd, output_fd)
-    print processes[pid]
-    if(DEBUG):
-        print "Server: started client with pid = ", pid
-    return pid
-
-    """Simple test 
-
-    btcr = TransmissionRun("/usr/bin/transmissioncli")
-    btcr.config_run("/home/adriana/p2p/p2p-dld/transmission", 
-                    "/home/adriana/p2p/p2p-log/transmission", 
-                    "scrubs.out", "/home/adriana/p2p/", 
-                    "transmission-scrubs.log", 10150, 
-                    "/home/adriana/p2p/p2p-meta/scrubs.torrent")
+    def set_port(self, port):
+        self.port = port
+        print "am setat port ", port
     
-    btcr.start()
-    btcr.run_client(btcr.simple_run_command)
+    def run(self):
+        self.doServer(self.ip, self.port)
+    
+    def recv_pickled_data(self, clientsock):
+        #      while chunk:            
+        #              chunk = clientsock.recv(BUFFER_SIZE)
+        #              data += chunk
+        data = clientsock.recv(BUFFER_SIZE)
+        dd = pickle.loads(data)        
+        return dd
+
+
+    """ 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):
 
-def stop_bt_client(pid):
-    
-    int_pid = int(pid)
-    os.kill(int_pid, signal.SIGKILL) # kill generates zombies
-    os.wait()
-    processes[int_pid][0].close() 
-    processes[int_pid][1].close()
-    del processes[int_pid] 
-    if(DEBUG):
-        print "Server: killed process with pid = ", pid
-
-def doServer(ip, port):
-
-    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-    if(DEBUG):
-        print "Server: host ip = %s, port = %d"%(SERVER_HOST,SERVER_PORT)
-    SERVER_HOST = ip
-    SERVER_PORT = port
-    serversocket.bind((SERVER_HOST, SERVER_PORT));
-    serversocket.listen(10) #max 10 requests
-    clientsocks = []
-    while(1):
-        if(DEBUG):
-            print "Server: accepting connections"
-        (clientsock, address) = serversocket.accept();
+        btcr = None
+
+        if bt_client_data[CLIENT] == TRANSMISSION:
+            btcr = TransmissionRun(bt_client_data[BASE_DIR])
+        elif bt_client_data[CLIENT] == TRIBLER:
+            btcr = TriblerRun(bt_client_data[BASE_DIR])
+        
+        else:
+            return -1
+        
+        btcr.config_run(bt_client_data[DL_DIR], bt_client_data[LOG_DIR], 
+             bt_client_data[OUT_FILE], bt_client_data[LOG_DIR],
+             bt_client_data[LOG_FILE], bt_client_data[PORT], 
+             bt_client_data[TORRENT])
+
+        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]
         if(DEBUG):
-            print "Server: accepted connection from ", address
+            print "Server: started client with pid = ", pid
+        return pid
+
+        """Simple test 
+
+        btcr = TransmissionRun("/usr/bin/transmissioncli")
+        btcr.config_run("/home/adriana/p2p/p2p-dld/transmission", 
+                        "/home/adriana/p2p/p2p-log/transmission", 
+                        "scrubs.out", "/home/adriana/p2p/", 
+                        "transmission-scrubs.log", 10150, 
+                        "/home/adriana/p2p/p2p-meta/scrubs.torrent")
         
-        if clientsock not in states:
-            states[clientsock] = WAITING_MSG_TYPE;
+        btcr.start()
+        btcr.run_client(btcr.simple_run_command)
+        """
 
-        if states[clientsock] == WAITING_MSG_TYPE:
-            print states[clientsock]
-            msg = clientsock.recv(BUFFER_SIZE)
-            if(DEBUG):
-                print "Server: received message:\n", msg
-            msg_types = {
-                START_MSG: WAITING_START_DATA,
-                STOP_MSG: WAITING_STOP_DATA,
-                STATUS_MSG: WAITING_STATUS_DATA
-            }
-            if msg not in  msg_types:
-                clientsock.send(ERROR_MSG +"wrong message type " + msg)
-            else:
-                states[clientsock] = msg_types[msg]
-                clientsock.send(ACK_MSG)
-        #else: 
-        print states[clientsock]
+    def stop_bt_client(self, pid):
+        
+        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] 
+        if(DEBUG):
+            print "Server: killed process with pid = ", pid
 
-        if states[clientsock] == WAITING_START_DATA:
-            bt_client_data = recv_pickled_data(clientsock)
-            if(DEBUG):
-                print "Server: received message:\n", msg
-            client_pid = start_bt_client(bt_client_data)
-            clientsock.send(ACK_MSG +" "+ str(client_pid))
+    def doServer(self, ip, port):
 
-        elif states[clientsock] == WAITING_STOP_DATA:
-            client_pid = recv_pickled_data(clientsock)
+        serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        if(DEBUG):
+            print "Server: host ip = %s, port = %d"%(SERVER_HOST,SERVER_PORT)
+        serversocket.bind((ip,port));
+        serversocket.listen(10) #max 10 requests
+        clientsocks = []
+        while(1):
             if(DEBUG):
-                print "Server: received message:\n", msg
-            stop_bt_client(client_pid)
-            clientsock.send(ACK_MSG)
-
-        elif states[clientsock] == WAITING_STATUS_DATA:
-            config = recv_pickled_data(clientsock)
-            clientsock.send(ACK_MSG)
+                print "Server: accepting connections"
+            (clientsock, address) = serversocket.accept();
+            if(DEBUG):
+                print "Server: accepted connection from ", address
+            
+            if clientsock not in states:
+                states[clientsock] = WAITING_MSG_TYPE;
+
+            if states[clientsock] == WAITING_MSG_TYPE:
+                print states[clientsock]
+                msg = clientsock.recv(BUFFER_SIZE)
+                if(DEBUG):
+                    print "Server: received message:\n", msg
+                msg_types = {
+                    START_MSG: WAITING_START_DATA,
+                    STOP_MSG: WAITING_STOP_DATA,
+                    STATUS_MSG: WAITING_STATUS_DATA
+                }
+                if msg not in  msg_types:
+                    clientsock.send(ERROR_MSG +"wrong message type " + msg)
+                else:
+                    states[clientsock] = msg_types[msg]
+                    clientsock.send(ACK_MSG)
+            #else: 
+            print states[clientsock]
 
-        states[clientsock] = WAITING_MSG_TYPE
+            if states[clientsock] == WAITING_START_DATA:
+                bt_client_data = self.recv_pickled_data(clientsock)
+                if(DEBUG):
+                    print "Server: received message:\n", msg
+                client_pid = self.start_bt_client(bt_client_data)
+                clientsock.send(ACK_MSG +" "+ str(client_pid))
+
+            elif states[clientsock] == WAITING_STOP_DATA:
+                client_pid = self.recv_pickled_data(clientsock)
+                if(DEBUG):
+                    print "Server: received message:\n", msg
+                self.stop_bt_client(client_pid)
+                clientsock.send(ACK_MSG)
 
-        # clientsock.recv(BUFFER_SIZE)
-        recv_pickled_data(clientsock)
-        clientsock.close()
+            elif states[clientsock] == WAITING_STATUS_DATA:
+                config = self.recv_pickled_data(clientsock)
+                clientsock.send(ACK_MSG)
 
+            states[clientsock] = WAITING_MSG_TYPE
 
-class MyDaemon(Daemon):
-    def run(self, ip, port):
-        doServer(ip, port)
+            # clientsock.recv(BUFFER_SIZE)
+            # recv_pickled_data(clientsock)
+            clientsock.close()
 
 if __name__ == "__main__":
 
-    if len(sys.argv) == 4:
-        daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], int(sys.argv[3]))
+    if len(sys.argv) >= 2:
+        
         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)
+            daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], int(sys.argv[3]), stdout = '/home/p2p/out', stderr = 'home/p2p/err') 
             daemon.start()
         elif 'stop' == sys.argv[1]:
+            daemon = MyDaemon('/tmp/daemon-example.pid') 
             daemon.stop()
         elif 'restart' == sys.argv[1]:
+            daemon = MyDaemon('/tmp/daemon-example.pid') 
             daemon.restart()
         else:
             print "Unknown command"
             sys.exit(2)
         sys.exit(0)
     else:
-        print "usage: %s start|stop|restart <host_ip> <port>" % sys.argv[0]
+        print "usage:\n\t %s start <host_ip> <port>" % sys.argv[0]
+        print "\t%s stop|restart "  % sys.argv[0]
         sys.exit(2)