print "am primit ", dd
return dd
"""
+
def recv_pickled_data(self):
msg = ''
while 1:
#print "am primit mesajul: ", dd
return dd
-
# send a pickled dictionary
def send_dict(self, data):
dumped_data = pickle.dumps(data)
}
- print s.send_command(GET_OUTPUT, "")
- """response = s.send_command(START_MSG, start_data_tribler)
+ #print s.send_command(GET_OUTPUT, "")
+ response = s.send_command(START_MSG, start_data_tribler)
+ print response
+ pid1 = response[1]
+ print pid1
+ s = MySocket()
+ s.connect(SERVER_HOST, SERVER_PORT)
+ response = s.send_command(START_MSG, start_data_transmission)
+ print response
+ pid2 = response[1]
+ print pid2
+ s = MySocket()
+ s.connect(SERVER_HOST, SERVER_PORT)
+ response = s.send_command(GET_CLIENTS,"")
+ print response
+
+ time.sleep(30)
+
+ s = MySocket()
+ s.connect(SERVER_HOST, SERVER_PORT)
+ response = s.send_command(STOP_MSG, pid2)
print response
- pid = response[1]
- print pid
s = MySocket()
s.connect(SERVER_HOST, SERVER_PORT)
- time.sleep(20)
- response = s.send_command(STOP_MSG, pid)
+ response = s.send_command(STOP_MSG, pid1)
print response
- """
+
def test_send_recv():
# test 1
port = 0
DEBUG = True
- processes = {} # keeps lists of file descriptors for each process
-
+ processes_fd = {} # keeps lists of file descriptors for each process
+ processes_info = {} # keeps lists of BT clients name and torrent name for each process
def __init__(self, pidfile, ip='', stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
Daemon.__init__(self, pidfile, stdin, stdout, stderr)
self.ip = ip
print "Server: sent message: ", data
- """
- Stores data about log files in a text file.
- """
def save_download_info(self, data):
- f = open(SESSIONS_FILE, 'a')
+ """ Stores data about log files in a text file. """
+ f = open(SESSIONS_FILE, 'a')
#f.write ("# session " + strftime("%d-%m-%Y %H:%M:%S", localtime())+ "\n")
num_lines = len(data.keys())
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):
+ """ Constructs a list with logging file info - paths, client, metafile
+ @param file_list list to which the created logging info list in appended. """
+
if CLIENT not in info_dict and TORRENT not in info_dict:
return -1
info_list = [info_dict[CLIENT],info_dict[TORRENT]]
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):
+ """ Reads all the contents of the file that stores info about
+ logging files and folders.
+ """
import os.path
if not os.path.exists(SESSIONS_FILE):
print "Server Error: No sessions_file"
if size == 0 and info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
raise Exception
- except TypeError:
+ except Exception:
print "Server Error: wrong file type for sessions file"
err_msg = "Error encountered while reading file"
f.close()
f.close()
return 0
-
-
-
- """ Starts a process for a BitTorrent client and returns its pid.
- @return: -1, if any error is encountered
- """
def start_bt_client(self, bt_client_data):
+ """ Starts a process for a BitTorrent client and returns its pid.
+ @return: -1, if any error is encountered
+ """
btcr = None
btcr = TriblerRun(bt_client_data[BASE_DIR])
elif bt_client_data[CLIENT] == HRKTORRENT:
btcr = HrktorrentRun(bt_client_data[BASE_DIR])
-
else:
return -1
btcr.start()
[pid, log_fd, output_fd] = btcr.run_client(btcr.simple_run_command)
- self.processes[pid] = (log_fd, output_fd)
- print self.processes[pid]
+ self.processes_fd[pid] = [log_fd, output_fd]
+ self.processes_info[pid] = [bt_client_data[CLIENT], bt_client_data[TORRENT]]
+
if(self.DEBUG):
print "Server: started client with pid = ", pid
return pid
-
- """
- Stops a BT client by killing it.
- """
def stop_bt_client(self, pid):
+ """ Stops a BT client by killing it."""
int_pid = int(pid)
os.kill(int_pid, signal.SIGKILL) # kill generates zombies
os.wait()
- self.processes[int_pid][0].close()
- self.processes[int_pid][1].close()
- del self.processes[int_pid]
+ self.processes_fd[int_pid][0].close()
+ self.processes_fd[int_pid][1].close()
+ del self.processes_fd[int_pid]
if(self.DEBUG):
print "Server: killed process with pid = ", pid
-
- """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))
- """
- Accepts socket connections and receives messages from commander.
- """
def do_Server(self, ip, port):
+ """ 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.serversocket.bind((ip,SERVER_PORT));
self.serversocket.listen(10) #max 10 requests
self.set_linger(self.serversocket,1, 0)
msg = self.recv_pickled_data(clientsock)
err_msg = ''
+ response = ''
if msg[0] == START_MSG:
bt_client_data = msg[1]
- client_pid = self.start_bt_client(bt_client_data)
+ response = self.start_bt_client(bt_client_data) #returns client_pid
self.save_download_info(bt_client_data)
- self.send_pickled_data(clientsock, (ACK_MSG, client_pid))
elif msg[0] == STOP_MSG:
client_pid = msg[1]
self.stop_bt_client(client_pid)
- self.send_pickled_data(clientsock, (ACK_MSG,""))
+ response = ""
elif msg[0] == GET_OUTPUT:
- file_list = []
- if self.read_download_info(file_list) < 0:
+ response = []
+ if self.read_download_info(response) < 0:
err_msg = "Error encountered while reading file"
- else:
- self.send_pickled_data(clientsock, (ACK_MSG, file_list))
elif msg[0] == GET_CLIENTS:
- print "la get output"
+ 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"
+
else:
err_msg = "Error: wrong message type";
if err_msg != '':
self.send_pickled_data(clientsock,(ERROR_MSG,err_msg))
+ else:
+ self.send_pickled_data(clientsock, (ACK_MSG, response))
clientsock.close()