f.close()
except Exception, ex:
self.logger(" Exception while saving transfer info: " + str(ex))
- raise Exception
+ raise
def add_to_output_msg(self, file_list, info_dict):
""" Constructs a list with self.logger file info - paths, client, metafile
def archive_files(self, info_dict, archives_list):
""" Archives status and verbose message log files. """
- log_path = ""
- out_path = ""
err_msg = ""
try:
if LOG_DIR in info_dict:
- log_path = info_dict[LOG_DIR]
- if LOG_FILE in info_dict:
- file_path = log_path + "/" + info_dict[LOG_FILE]
- if create_archive_file(file_path) < -1:
- err_msg = "File does not exist: ", file_path
- else: archives_list.append(file_path)
-
- if OUT_DIR in info_dict:
- out_path = info_dict[OUT_DIR]
- else:
- out_path = log_path
-
- if OUT_FILE in info_dict:
- file_path = out_path + "/" + info_dict[OUT_FILE]
- if create_archive_file(file_path) < -1:
- err_msg = "File does not exist: ", file_path
+ if info_dict[CLIENT] == HRKTORRENT:
+ if create_archive_dir_logs(info_dict[DL_DIR]) < 0:
+ err_msg = "Could not archive files from dir " + file_path
+ else: archives_list.append(info_dict[DL_DIR] + "/*" )
+ else:
+ if LOG_FILE in info_dict:
+ file_path = info_dict[LOG_DIR] + "/" + info_dict[LOG_FILE]
+ if create_archive_file(file_path) < 0:
+ err_msg = "File does not exist: " + file_path
+ else: archives_list.append(file_path)
+
+ if OUT_DIR in info_dict and OUT_FILE in info_dict:
+ file_path = info_dict[OUT_DIR] + "/" + info_dict[OUT_FILE]
+ if create_archive_file(file_path) < 0:
+ err_msg = "File does not exist: " + file_path
else: archives_list.append(file_path)
+
if err_msg != "":
- self.logger.error(" " + err_msg)
+ self.logger.error(err_msg)
+
except Exception, ex:
self.logger.error("Exception while archiving files: " + str(ex))
- raise Exception
-
+ raise
def read_download_info(self, file_list, create_archive = False, archives_list = []):
""" Reads all the contents of the file that stores info about
dicts = [] #list with all info_dict dictionaries
try:
while line != '':
- print line
if size == 0 :
if info_dict != {} and info_dict not in dicts:
if self.add_to_output_msg(file_list, info_dict) < 0:
def stop_bt_client(self, pid):
""" Stops a BT client by killing it."""
-
- int_pid = int(pid)
- if int_pid == -1 or int_pid not in self.processes_fd.keys():
- return "Invalid pid " + str(pid)
- os.kill(int_pid, signal.SIGKILL) # kill generates zombies
- os.wait()
- self.processes_fd[int_pid][0].close()
- self.processes_fd[int_pid][1].close()
-
- del self.processes_fd[int_pid]
- del self.processes_info[int_pid]
+ try:
+ int_pid = int(pid)
+ if int_pid == -1 or int_pid not in self.processes_fd.keys():
+ return "Invalid pid " + str(pid)
+ os.kill(int_pid, signal.SIGKILL) # kill generates zombies
+ os.wait()
+ self.processes_fd[int_pid][0].close()
+ self.processes_fd[int_pid][1].close()
+
+ del self.processes_fd[int_pid]
+ del self.processes_info[int_pid]
- self.logger.debug(" killed process with pid = " + str(pid))
- return ""
+ self.logger.debug(" killed process with pid = " + str(pid))
+ return ""
+
+ except Exception ,ex:
+ self.logger.error(" Exception occured: " + str(ex))
+ return "Could not kill process with pid " + str(pid)
def set_linger(self,sock, l_onoff, l_linger):
"""Sets the SO_LINGER value on a socket."""
self.logger.error("Exception occured while connecting: " + str(e))
return
while(1):
- self.logger.debug(" accepting connections")
- (clientsock, address) = self.serversocket.accept();
- self.logger.debug(" accepted connection from " +str(address))
-
- msg = self.recv_pickled_data(clientsock)
+ try:
+ self.logger.debug(" accepting connections")
+ (clientsock, address) = self.serversocket.accept();
+ self.logger.debug(" accepted connection from " +str(address))
+
+ msg = self.recv_pickled_data(clientsock)
+
+ err_msg = ''
+ response = ''
+
+ if msg[0] == START_MSG:
+ bt_client_data = msg[1]
+ response = self.start_bt_client(bt_client_data) #returns client_pid
+ if response == self.START_ERR2:
+ err_msg = "Error occurred while starting client"
+ elif response == self.START_ERR1:
+ err_msg = "BitTorrent client " + msg[1][CLIENT] +" not supported."
+ else:
+ try:
+ self.save_download_info(bt_client_data)
+ except Exception:
+ err_msg = "Client started but transfer info could not be saved."
+
+ elif msg[0] == STOP_MSG:
+ client_pid = msg[1]
+ err_msg = self.stop_bt_client(client_pid)
- err_msg = ''
- response = ''
- if msg[0] == START_MSG:
- bt_client_data = msg[1]
- response = self.start_bt_client(bt_client_data) #returns client_pid
- 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:
+ elif msg[0] == GET_OUTPUT:
+ response = []
+ if self.read_download_info(response) < 0:
+ err_msg = "Error encountered while reading file"
+
+ elif msg[0] == GET_CLIENTS:
+ response = []
+ print self.processes_info
try:
- self.save_download_info(bt_client_data)
+ for k in self. processes_info.keys():
+ response.append([self.processes_info[k][0], k, self.processes_info[k][1]]) # client_name, pid, torrent file
except Exception:
- err_msg = "Client started but transfer info could not be saved."
-
- elif msg[0] == STOP_MSG:
- client_pid = msg[1]
- err_msg = self.stop_bt_client(client_pid)
+ err_msg = "Error: could not retrive running clients list"
- elif msg[0] == GET_OUTPUT:
- response = []
- if self.read_download_info(response) < 0:
- err_msg = "Error encountered while reading file"
-
- elif msg[0] == GET_CLIENTS:
- response = []
- print self.processes_info
- try:
- for k in self. processes_info.keys():
- response.append([self.processes_info[k][0], k, self.processes_info[k][1]]) # client_name, pid, torrent file
- except Exception:
- err_msg = "Error: could not retrive running clients list"
-
- elif msg[0] == ARCHIVE:
- response = []
- if len(self.processes_fd) == 0: # ARCHIVE ONLY NO CLIENTS ARE RUNNING
- try :
- if self.read_download_info([], True, response) < 0: # read file path and create archives
+ elif msg[0] == ARCHIVE:
+ response = []
+ if len(self.processes_fd) == 0: # ARCHIVE ONLY NO CLIENTS ARE RUNNING
+ try :
+ if self.read_download_info([], True, response) < 0: # read file path and create archives
+ err_msg = "Error: could not archive log files."
+ else: # erase sessions file
+ f = open(SESSIONS_FILE, "w")
+ f.close()
+ except Exception , ex:
err_msg = "Error: could not archive log files."
- else: # erase sessions file
- f = open(SESSIONS_FILE, "w")
- f.close()
- except Exception , ex:
- err_msg = "Error: could not archive log files."
+ else:
+ err_msg = "Cannot archive logs while clients are running"
+
+ elif msg[0] == GET_STATUS:
+ line_parts = []
+ err_msg = self.get_client_status(msg[1], line_parts)
+ if len(err_msg) == 0:
+ response = {}
+
+ print len(line_parts)
+ if len(line_parts[0]) >= 7:
+ response[TIMESTAMP] = line_parts[0][0]
+ response[NUM_PEERS] = line_parts[0][1]
+ response[DHT] = line_parts[0][2]
+ response[DL_SPEED] = line_parts[0][3]
+ response[UP_SPEED] = line_parts[0][4]
+ response[DL_SIZE] = line_parts[0][5]
+ response[ETA] = line_parts[0][6]
+
else:
- err_msg = "Cannot archive logs while clients are running"
- elif msg[0] == GET_STATUS:
- line_parts = []
- err_msg = self.get_client_status(msg[1], line_parts)
- if len(err_msg) == 0:
- response = {}
-
- print len(line_parts)
- if len(line_parts[0]) >= 7:
- response[TIMESTAMP] = line_parts[0][0]
- response[NUM_PEERS] = line_parts[0][1]
- response[DHT] = line_parts[0][2]
- response[DL_SPEED] = line_parts[0][3]
- response[UP_SPEED] = line_parts[0][4]
- response[DL_SIZE] = line_parts[0][5]
- response[ETA] = line_parts[0][6]
-
- else:
- err_msg = "Error: wrong message type";
+ err_msg = "Error: wrong message type";
- if err_msg != '':
- self.send_pickled_data(clientsock,(ERROR_MSG,err_msg))
- self.logger.debug(" Sending error message: " + err_msg)
- else:
- self.send_pickled_data(clientsock, (ACK_MSG, response))
-
- clientsock.close()
+ if err_msg != '':
+ self.send_pickled_data(clientsock,(ERROR_MSG,err_msg))
+ self.logger.debug(" Sending error message: " + err_msg)
+ else:
+ self.send_pickled_data(clientsock, (ACK_MSG, response))
+ clientsock.close()
+ except Exception, ex:
+ self.logger.error("Exception occured: "+ str(ex))
if __name__ == "__main__":
sys.exit(2)
+