bt_comm - testing tribler start.
authorAdriana Draghici <adriana008@gmail.com>
Thu, 11 Mar 2010 16:57:57 +0000 (16:57 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Thu, 11 Mar 2010 16:57:57 +0000 (16:57 +0000)
bt_comm/client/Client.py
bt_comm/server/BitTorrentClientRun.py [new file with mode: 0644]
bt_comm/server/Client.py
bt_comm/server/Server_NO_DAEMON.py

index 02719f3..80c5c21 100644 (file)
@@ -9,6 +9,7 @@ from threading import Thread
 paramiko.util.log_to_file('/tmp/paramiko.log')
 
 MSGLEN = 1024
+DEBUG = True
 
 class Commander(Thread):
        def __init__(self, nodes_xml, swarm_xml):
@@ -47,7 +48,8 @@ class Commander(Thread):
                        if response == ACK_MSG:
                                self.sendMsg(pickle.dumps(config_data))
                                response = self.recv_msg()
-                               print response
+                           if (DEBUG):
+                    print response
                except Exception as e:
                        print e
                finally:
diff --git a/bt_comm/server/BitTorrentClientRun.py b/bt_comm/server/BitTorrentClientRun.py
new file mode 100644 (file)
index 0000000..cfab24a
--- /dev/null
@@ -0,0 +1,102 @@
+#!/usr/bin/env python
+
+"""
+ Base class for running BitTorrent client instances
+ 2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+"""
+
+import sys
+import os
+import os.path
+import subprocess
+import shlex
+from string import Template
+
+DEBUG = True
+
+class BitTorrentClientRun:
+    def __init__(self, base_path, simple_run_expr, logging_run_expr):
+        self.base_path = base_path
+        self.simple_run_expr = simple_run_expr
+        self.logging_run_expr = logging_run_expr
+
+    def config_run(self, download_dir, output_dir, output_file, log_dir, log_file, port, torrent_file):
+        self.download_dir = download_dir
+        self.output_dir = output_dir
+        self.output_file = output_file # output from stdout
+        self.log_dir = log_dir
+        self.log_file = log_file # output from stderr
+        self.port = port
+        self.torrent_file = torrent_file
+
+    def start(self):
+        t = Template(self.simple_run_expr)
+        self.simple_run_command = t.substitute(
+                base_path = self.base_path,
+                download_dir = self.download_dir,
+                output_dir = self.output_dir,
+                output_file = self.output_file,
+                log_dir = self.log_dir,
+                log_file = self.log_file,
+                port = str(self.port),
+                torrent_file = self.torrent_file
+                )
+
+        t = Template(self.logging_run_expr)
+        self.logging_run_command = t.substitute(
+                base_path = self.base_path,
+                download_dir = self.download_dir,
+                output_dir = self.output_dir,
+                output_file = self.output_file,
+                log_dir = self.log_dir,
+                log_file = self.log_file,
+                port = str(self.port),
+                torrent_file = self.torrent_file
+                )
+
+        print self.simple_run_command
+        print self.logging_run_command
+       
+    def run_client(self, command):
+
+        # split command
+        args = shlex.split(command)
+      
+        # remove redirectation parameters
+        for i in range(0, len(args)):
+            if args[i].find(">") > -1 :
+                for j in range(i, len(args)):
+                    args.pop(i)
+                break;
+        if(DEBUG):
+            print "BitTorrentClientRun: command =", args
+        
+
+        log_redirect = open(self.log_dir+"/"+self.log_file,"w")
+        output_redirect = open(self.output_dir+"/"+self.output_file,"w")
+        p=subprocess.Popen(args, shell=False, #does not create sh process
+                            stdout=log_redirect,
+                            stderr=output_redirect)
+        pid = p.pid
+        if(DEBUG):
+            print "BitTorrentClientRun: pid =", pid
+        return [pid, log_redirect, output_redirect]
+        
+
+def main():
+
+    """
+    Test case
+
+    btcr = BitTorrentClientRun("/home/p2p/p2p-clients/transmission",
+            "$base_path/cli/transmissioncli --download-dir $download_dir --port $port $torrent_file > $output_dir/$output_file",
+            "TR_DEBUG=2 $base_path/cli/transmissioncli --download-dir $download_dir --port $port $torrent_file 2> $log_dir/$log_file > $output_dir/$output_file")
+    btcr.config_run("/home/p2p/p2p-dld/transmission", "/home/p2p/p2p-log/transmission", "transmission-fedora.out", "/home/p2p/p2p-log/transmission", "transmission-fedora.log", 10150, "/home/p2p/p2p-meta/fedora.torrent")
+    btcr.start()
+
+       
+    """
+
+
+if __name__ == "__main__":
+    sys.exit(main())
index 3eccafb..1783458 100644 (file)
@@ -52,15 +52,26 @@ def test_all_commands(torrent_file):
     s = MySocket()
     s.connect(SERVER_HOST, SERVER_PORT)
     torrent_name = torrent_file[(torrent_file.rfind("/")+1):torrent_file.find(".torrent")];
-    start_data = {CLIENT: TRANSMISSION, PORT:10150, 
-            TORRENT: "/home/p2p/p2p-meta/" + torrent_file,
+
+    start_data_tribler = {
+            CLIENT: TRIBLER, PORT:10150,
+            TORRENT:"/home/p2p/p2p-meta/" + torrent_name+".torrent",
+            DL_DIR: "/home/p2p/p2p-dld/tribler",
+            LOG_DIR: "/home/p2p/p2p-log/tribler",
+            OUT_FILE: torrent_name + ".out",
+            LOG_FILE: "tribler-" + torrent_name + ".log",
+
+            }
+    start_data_transmission = {
+            CLIENT: TRANSMISSION, PORT:10150,
+            TORRENT: "/home/p2p/p2p-meta/" + torrent_name+".torrent",
             DL_DIR: "/home/p2p/p2p-dld/transmission",
             LOG_DIR: "/home/p2p/p2p-log/transmission",
             OUT_FILE: torrent_name + ".out",
             LOG_FILE: "transmission-" + torrent_name + ".log",
 
             }
-    response = s.send_command(START_MSG, start_data)
+    response = s.send_command(START_MSG, start_data_tribler)
     print response
     pid = (response.split(" "))[1]
     print pid
index da051d4..8512735 100644 (file)
@@ -8,6 +8,7 @@ from Util import *
 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.
@@ -30,23 +31,30 @@ def recv_pickled_data(clientsock):
 
 def start_bt_client(bt_client_data):
 
+    btcr = None
+
     if bt_client_data[CLIENT] == TRANSMISSION:
-        
         btcr = TransmissionRun("/usr/bin/transmissioncli")
-        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
+    
+    elif bt_client_data[CLIENT] == TRIBLER:
+        btcr = TriblerRun("/home/p2p/p2p-clients/tribler")
+    
+    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)
+    processes[pid] = (log_fd, output_fd)
+    print processes[pid]
+    if(DEBUG):
+        print "Server: started client with pid = ", pid
+    return pid
 
-    return -1
     """Simple test 
 
     btcr = TransmissionRun("/usr/bin/transmissioncli")
@@ -65,8 +73,6 @@ def stop_bt_client(pid):
     int_pid = int(pid)
     os.kill(int_pid, signal.SIGKILL) # kill generates zombies
     os.wait()
-    print int_pid
-    print processes 
     processes[int_pid][0].close() 
     processes[int_pid][1].close()
     del processes[int_pid]