from BitTorrentClientRun import *
from TransmissionRun import *
from TriblerRun import *
+from HrktorrentRun import *
from TriblerStatusParser import *
class MyDaemon(Daemon):
def save_download_info(self, data):
""" Stores data about log files in a text file. """
+ try:
+ f = open(SESSIONS_FILE, 'a')
+ #f.write ("# session " + strftime("%d-%m-%Y %H:%M:%S", localtime())+ "\n")
+
+ num_lines = len(data.keys())
+ if PORT in data:
+ num_lines = num_lines -1;
+ if BASE_DIR in data:
+ num_lines = num_lines -1;
+
+ f.write(str(num_lines)+"\n")
+ for k in data.keys():
+ if k == PORT or k == BASE_DIR:
+ continue
+ f.write(str(k)+":"+str(data[k])+"\n")
- f = open(SESSIONS_FILE, 'a')
- #f.write ("# session " + strftime("%d-%m-%Y %H:%M:%S", localtime())+ "\n")
-
- num_lines = len(data.keys())
- if PORT in data:
- num_lines = num_lines -1;
- if BASE_DIR in data:
- num_lines = num_lines -1;
-
- f.write(str(num_lines)+"\n")
- for k in data.keys():
- if k == PORT or k == BASE_DIR:
- continue
- f.write(str(k)+":"+str(data[k])+"\n")
-
- f.close()
+ f.close()
+ except Exception, ex:
+ self.logger(" Exception while saving transfer info: " + str(ex))
+ raise Exception
def add_to_output_msg(self, file_list, info_dict):
""" Constructs a list with self.logger file info - paths, client, metafile
log_path = ""
out_path = ""
err_msg = ""
- 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]
+ 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
else: archives_list.append(file_path)
+ if err_msg != "":
+ self.logger.error(" " + err_msg)
+ except Exception, ex:
+ self.logger.error("Exception while archiving files: " + str(ex))
+ raise Exception
- 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
- else: archives_list.append(file_path)
-
- self.logger.error(" " + err_msg)
def read_download_info(self, file_list, create_archive = False, archives_list = []):
""" Reads all the contents of the file that stores info about
- self.logger files and folders.
+ self.logger files and folders.
"""
if not os.path.exists(SESSIONS_FILE):
self.logger.error(" No sessions_file")
self.archive_files(info_dict, archives_list)
except Exception, e:
- self.logger.error(" wrong file type for sessions file\n" + e)
+ self.logger.error(" wrong file type for sessions file\n" + str(e))
f.close()
return -1
elif bt_client_data[CLIENT] == HRKTORRENT:
btcr = HrktorrentRun(bt_client_data[BASE_DIR])
else:
- return START_ERR1
+ return self.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],
except Exception, ex:
self.logger.error(" Exception occured: " + str(ex))
- return START_ERR2
+ return self.START_ERR2
def stop_bt_client(self, pid):
""" Stops a BT client by killing it."""
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)
+ self.logger.error("Exception occured while connecting: " + str(e))
return
while(1):
self.logger.debug(" accepting connections")
elif response == self.START_ERR2:
err_msg = "BitTorrent client " + msg[1][CLIENT] +" not supported."
else:
- self.save_download_info(bt_client_data)
+ 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)
- response = ""
elif msg[0] == GET_OUTPUT:
response = []
elif msg[0] == ARCHIVE:
response = []
- 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()
-
+ 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:
+ 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)
- 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]
+ 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";
if err_msg != '':
self.send_pickled_data(clientsock,(ERROR_MSG,err_msg))
- else:
self.logger.debug(" Sending error message: " + err_msg)
+ else:
self.send_pickled_data(clientsock, (ACK_MSG, response))
clientsock.close()
sys.exit(2)
sys.exit(0)
else:
- print "usage:\n\t %s start <host_ip> <port>" % sys.argv[0]
+ print "usage:\n\t %s start <host_ip> " % sys.argv[0]
print "\t%s stop|restart " % sys.argv[0]
sys.exit(2)