--- /dev/null
+#!/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())
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
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.
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")
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]