if not chunk:
break
#raise RuntimeError, "socket connection broken"
-
msg = msg + chunk
if len(chunk) < BUFFER_SIZE:
break
dd = pickle.loads(msg)
- #print "am primit mesajul: ", dd
+ if self.DEBUG:
+ print "Server: received message: ", dd
return dd
def send_pickled_data (self, clientsock, data):
- dumped_data = pickle.dumps(data)
- totalsent = 0
- while totalsent < len(dumped_data):
- sent = clientsock.send(dumped_data[totalsent:])
- if sent == 0:
- raise RuntimeError, "socket connection broken"
- totalsent = totalsent + sent
-
+ dumped_data = pickle.dumps(data)
+ totalsent = 0
+ while totalsent < len(dumped_data):
+ sent = clientsock.send(dumped_data[totalsent:])
+ if sent == 0:
+ raise RuntimeError, "socket connection broken"
+ totalsent = totalsent + sent
+ if self.DEBUG:
+ print "Server: sent message: ", data
+
+
"""
Stores data about log files in a text file.
"""
f.close()
+ """
+ Constructs a list with logging file info - paths, client, metafile
+ file_list: list to which the created logging info list in appended.
+ """
+ def add_to_output_msg(self, file_list, info_dict):
+ if CLIENT not in info_dict and TORRENT not in info_dict:
+ return -1
+ info_list = [info_dict[CLIENT],info_dict[TORRENT]]
+ if LOG_DIR in info_dict:
+ info_list.append((info_dict[LOG_DIR], LOG_DIR))
+ if OUT_FILE in info_dict:
+ info_list.append((info_dict[OUT_FILE], OUT_FILE))
+ 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
+
+ file_list.append (info_list)
+ return 0 # success
+
+ """
+ Reads all the contents of the file that stores info about
+ logging files and folders.
+ """
+ def read_download_info(self, file_list):
+ import os.path
+ if not os.path.exists(SESSIONS_FILE):
+ print "Server Error: No sessions_file"
+ return -1
+ else:
+ f = open(SESSIONS_FILE,"r")
+ line = f.readline()
+ size = 0
+ info_dict = {}
+ try:
+ while line != '':
+ if size == 0 :
+ if info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
+ raise Exception
+ size = int(line)
+ info_dict = {}
+ else:
+ parts = line.strip().split(':')
+ if len(parts) != 2:
+ raise Exception
+ info_dict[parts[0]] = parts[1]
+ size = size - 1
+ line = f.readline()
+
+ if size == 0 and info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
+ raise Exception
+
+ except TypeError:
+ print "Server Error: wrong file type for sessions file"
+ err_msg = "Error encountered while reading file"
+ f.close()
+ return -1
+
+ f.close()
+ return 0
+
+
""" Starts a process for a BitTorrent client and returns its pid.
if(self.DEBUG):
print "Server: killed process with pid = ", pid
- """
- Constructs a list with logging file info - paths, client, metafile
- file_list: list to which the created logging info list in appended.
- """
-
- def add_to_output_msg(self, file_list, info_dict):
- if CLIENT not in info_dict and TORRENT not in info_dict:
- return -1
- info_list = [info_dict[CLIENT],info_dict[TORRENT]]
- if LOG_DIR in info_dict:
- info_list.append((info_dict[LOG_DIR], LOG_DIR))
- if OUT_FILE in info_dict:
- info_list.append((info_dict[OUT_FILE], OUT_FILE))
- if LOG_FILE in info_dict:
- info_list.append((info_dict[LOG_FILE], LOG_FILE))
- print info_list
-
- file_list.append (info_list)
-
- return 0 # success
-
+ """Sets the SO_LINGER value on a socket."""
def set_linger(self,sock, l_onoff, l_linger):
- """Sets the SO_LINGER value on a socket."""
sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', l_onoff, l_linger))
print "Server: 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, 2)
+ self.set_linger(self.serversocket,1, 0)
while(1):
if(self.DEBUG):
print "Server: accepting connections"
print "Server: accepted connection from ", address
msg = self.recv_pickled_data(clientsock)
- if(self.DEBUG):
- print "Server: received message:\n", msg
err_msg = ''
if msg[0] == START_MSG:
elif msg[0] == STOP_MSG:
client_pid = msg[1]
- if(self.DEBUG):
- print "Server: received message:\n", msg
self.stop_bt_client(client_pid)
self.send_pickled_data(clientsock, (ACK_MSG,""))
elif msg[0] == GET_OUTPUT:
-
- import os.path
- if not os.path.exists(SESSIONS_FILE):
- print "Error: No sessions_file"
+ file_list = []
+ if self.read_download_info(file_list) < 0:
err_msg = "Error encountered while reading file"
- else:
- f = open(SESSIONS_FILE,"r")
- line = f.readline()
- size = 0
- info_dict = {}
- file_list = []
- try:
- while line != '':
- if size == 0:
- if info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
- raise Exception
- size = int(line)
- info_dict = {}
- else:
- parts = line.strip().split(':')
- if len(parts) != 2:
- raise Exception
- info_dict[parts[0]] = parts[1]
- size = size - 1
- line = f.readline()
-
- except TypeError:
- print "Server: wrong file type for sessions file"
- err_msg = "Error encountered while reading file"
-
- print file_list
+ else:
+ self.send_pickled_data(clientsock, (ACK_MSG, file_list))
elif msg[0] == GET_CLIENTS:
- pass
+ print "la get output"
else:
+ err_msg = "Error: wrong message type";
- self.send_pickled_data(clientsock,( ERROR_MSG,"Error: wrong message type "))
+ if err_msg != '':
+ self.send_pickled_data(clientsock,(ERROR_MSG,err_msg))
- # clientsock.recv(BUFFER_SIZE)
- # recv_pickled_data(clientsock)
clientsock.close()
if 'start' == sys.argv[1]:
- #if(len(sys.argv) != 4):
- # print "usage:\n\t %s start <host_ip> <port>" % sys.argv[0]
- # sys.exit(2)
+ if(len(sys.argv) != 3):
+ print "usage:\n\t %s start <host_ip> " % sys.argv[0]
+ sys.exit(2)
- #daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], int(sys.argv[3]), stdout = '/home/p2p/out', stderr = 'home/p2p/err')
daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], stdout = '/home/p2p/out', stderr = '/home/p2p/err')
daemon.start()
elif 'stop' == sys.argv[1]: