import pickle
import signal
import struct
+import logging
from daemon import Daemon
from Util import *
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
+
def __init__(self, pidfile, ip='', stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
Daemon.__init__(self, pidfile, stdin, stdout, stderr)
self.ip = ip
self.port = SERVER_PORT
-
+ logging.basicConfig(level = logging.DEBUG)
+ self.logger = logging.getLogger('autorun.Server')
+ self.logger.setLevel(logging.DEBUG)
def set_address(self, ip):
self.ip = ip
break
dd = pickle.loads(msg)
- if self.DEBUG:
- print "Server: received message: ", dd
+ self.logger.debug(" received message: ", dd)
return dd
def send_pickled_data (self, clientsock, data):
if sent == 0:
raise RuntimeError, "socket connection broken"
totalsent = totalsent + sent
- if self.DEBUG:
- print "Server: sent message: ", data
+ self.logger.debug(" sent message: ", data)
def save_download_info(self, data):
f.close()
def add_to_output_msg(self, file_list, info_dict):
- """ Constructs a list with logging file info - paths, client, metafile
- @param file_list list to which the created logging info list in appended. """
+ """ Constructs a list with self.logger file info - paths, client, metafile
+ @param file_list list to which the created self.logger info list in appended. """
if CLIENT not in info_dict and TORRENT not in info_dict:
return -1
if LOG_FILE in info_dict:
info_list.append((info_dict[LOG_FILE], LOG_FILE))
- if self.DEBUG:
- print "Server: read transfer log info: ", info_list
+ self.logger.debug(" read transfer log info: ", info_list)
file_list.append (info_list)
return 0 # success
err_msg = "File does not exist: ", file_path
else: archives_list.append(file_path)
- print "Server Error: ", err_msg
+ 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
- logging files and folders.
+ self.logger files and folders.
"""
if not os.path.exists(SESSIONS_FILE):
- print "Server Error: No sessions_file"
+ self.logger.error(" No sessions_file")
return -1
else:
f = open(SESSIONS_FILE,"r")
self.archive_files(info_dict, archives_list)
except Exception, e:
- print "Server Error: wrong file type for sessions file"
- print e
+ self.logger.error(" wrong file type for sessions file\n" + e)
f.close()
return -1
line_parts.append(parser.parse_last_status_line(line))
if line_parts == "":
return "Error occured while reading status file " + status_file_path
- print line_parts
- print line
+ self.logger.debug(" status_line = " + line)
return ""
def start_bt_client(self, bt_client_data):
bt_client_data[TORRENT],
bt_client_data[OUT_DIR] + "/" + bt_client_data[OUT_FILE]]
- if(self.DEBUG):
- print "Server: started client with pid = ", pid
+ self.logger.debug(" started client with pid = ", pid)
return pid
def stop_bt_client(self, pid):
del self.processes_fd[int_pid]
del self.processes_info[int_pid]
- if(self.DEBUG):
- print "Server: killed process with pid = ", pid
+ self.logger.debug(" killed process with pid = ", pid)
return ""
def set_linger(self,sock, l_onoff, l_linger):
""" Accepts socket connections and receives messages from commander."""
self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- if(self.DEBUG):
- print "Server: host ip = %s, port = %s"%(ip,SERVER_PORT)
+ 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)
while(1):
- if(self.DEBUG):
- print "Server: accepting connections"
+ self.logger.debug(" accepting connections")
(clientsock, address) = self.serversocket.accept();
- if(self.DEBUG):
- print "Server: accepted connection from ", address
+ self.logger.debug(" accepted connection from ", address)
msg = self.recv_pickled_data(clientsock)