From 16f725d27aecd3a98f28d7e995758dfd6535420b Mon Sep 17 00:00:00 2001 From: Adriana Draghici Date: Fri, 30 Apr 2010 09:33:08 +0000 Subject: [PATCH] autorun: added exceptions in Server --- autorun/server/BitTorrentClientRun.py | 60 ++++++++++++++----------- autorun/server/Server.py | 65 +++++++++++++++++---------- 2 files changed, 76 insertions(+), 49 deletions(-) diff --git a/autorun/server/BitTorrentClientRun.py b/autorun/server/BitTorrentClientRun.py index 6bab9c5..aadc798 100644 --- a/autorun/server/BitTorrentClientRun.py +++ b/autorun/server/BitTorrentClientRun.py @@ -61,31 +61,41 @@ class BitTorrentClientRun: 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; - #self.my_logger.debug(" BitTorrentClientRun: command =" + str(args)) - - log_redirect = open(self.log_dir+"/"+self.log_file,"w") - output_redirect = open(self.output_dir+"/"+self.output_file,"w") - - self.my_logger.debug(" output redirect to file " + self.output_dir+"/"+self.output_file) - self.my_logger.debug(" log redirect to file " + self.log_dir+"/"+self.log_file) - - p=subprocess.Popen(args, shell=False, #does not create sh process - stdout=output_redirect, - stderr=log_redirect) - pid = p.pid - self.my_logger.debug(" BitTorrentClientRun: pid =" + str(pid)) - return [pid, log_redirect, output_redirect] - + try: + # 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; + #self.my_logger.debug(" BitTorrentClientRun: command =" + str(args)) + + log_redirect = open(self.log_dir+"/"+self.log_file,"w") + output_redirect = open(self.output_dir+"/"+self.output_file,"w") + + self.my_logger.debug(" output redirect to file " + self.output_dir+"/"+self.output_file) + self.my_logger.debug(" log redirect to file " + self.log_dir+"/"+self.log_file) + + p=subprocess.Popen(args, shell=False, #does not create sh process + stdout=output_redirect, + stderr=log_redirect) + pid = p.pid + self.my_logger.debug(" BitTorrentClientRun: pid =" + str(pid)) + return [pid, log_redirect, output_redirect] + + except Exception, ex: + if log_redirect != None: + log_redirect.close() + if output_redirect != None: + output_redirect.close() + if pid != None: + os.kill(int_pid, signal.SIGKILL) # kill generates zombies + os.wait() + raise Exception + def main(): diff --git a/autorun/server/Server.py b/autorun/server/Server.py index 8608d31..49366f0 100644 --- a/autorun/server/Server.py +++ b/autorun/server/Server.py @@ -28,6 +28,9 @@ class MyDaemon(Daemon): processes_fd = {} # keeps lists of file descriptors for each process processes_info = {} # keeps lists of BT clients name, torrent name and status log name for each process logger = None + + START_ERR1 = -1 # returned when starting a client, if that client is not supported + START_ERR2 = -2 # an exception occured when creating a client process def __init__(self, pidfile, ip='', stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): Daemon.__init__(self, pidfile, stdin, stdout, stderr) @@ -190,7 +193,7 @@ class MyDaemon(Daemon): def get_client_status(self, transfer_id, line_parts = []): """ - Runs a parsers for the obtaining the last line from the + Runs a parser for the obtaining the last line from the torrent transfer's status file (output file). @param transfer_id identifier for the transfer @param line_parts list with line's components @@ -226,23 +229,27 @@ class MyDaemon(Daemon): elif bt_client_data[CLIENT] == HRKTORRENT: btcr = HrktorrentRun(bt_client_data[BASE_DIR]) else: - return -1 - - btcr.config_run(bt_client_data[DL_DIR], bt_client_data[OUT_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_fd[pid] = [log_fd, output_fd] - self.processes_info[pid] = [bt_client_data[CLIENT], - bt_client_data[TORRENT], - bt_client_data[OUT_DIR] + "/" + bt_client_data[OUT_FILE]] - - self.logger.debug(" started client with pid = " + str(pid)) - return pid + return START_ERR1 + try: + btcr.config_run(bt_client_data[DL_DIR], bt_client_data[OUT_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_fd[pid] = [log_fd, output_fd] + self.processes_info[pid] = [bt_client_data[CLIENT], + bt_client_data[TORRENT], + bt_client_data[OUT_DIR] + "/" + bt_client_data[OUT_FILE]] + + self.logger.debug(" started client with pid = " + str(pid)) + return pid + except Exception, ex: + self.logger.error(" Exception occured: " + str(ex)) + return START_ERR2 + def stop_bt_client(self, pid): """ Stops a BT client by killing it.""" @@ -269,12 +276,16 @@ class MyDaemon(Daemon): def do_Server(self, ip, port): """ Accepts socket connections and receives messages from commander.""" - self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.logger.debug( " 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, 0) + try: + self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.logger.debug( " 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, 0) + except Exception, e: + self.logger.error("Exception occured while connecting: " + e) + return while(1): self.logger.debug(" accepting connections") (clientsock, address) = self.serversocket.accept(); @@ -287,7 +298,12 @@ class MyDaemon(Daemon): if msg[0] == START_MSG: bt_client_data = msg[1] response = self.start_bt_client(bt_client_data) #returns client_pid - self.save_download_info(bt_client_data) + if response == self.START_ERR1: + err_msg = "Error occurred while starting client" + elif response == self.START_ERR2: + err_msg = "BitTorrent client " + msg[1][CLIENT] +" not supported." + else: + self.save_download_info(bt_client_data) elif msg[0] == STOP_MSG: client_pid = msg[1] @@ -336,6 +352,7 @@ class MyDaemon(Daemon): if err_msg != '': self.send_pickled_data(clientsock,(ERROR_MSG,err_msg)) else: + self.logger.debug(" Sending error message: " + err_msg) self.send_pickled_data(clientsock, (ACK_MSG, response)) clientsock.close() -- 2.20.1