self.doServer(self.ip, self.port)
def recv_pickled_data(self, clientsock):
- # while chunk:
- # chunk = clientsock.recv(self.BUFFER_SIZE)
- # data += chunk
-
msg = ''
while 1:
chunk = clientsock.recv(BUFFER_SIZE)
continue
f.write(str(k)+":"+str(data[k])+"\n")
-
f.close()
+
+
""" Starts a process for a BitTorrent client and returns its pid.
@return: -1, if any error is encountered
"""
btcr = TransmissionRun(bt_client_data[BASE_DIR])
elif bt_client_data[CLIENT] == TRIBLER:
btcr = TriblerRun(bt_client_data[BASE_DIR])
-
+ elif bt_client_data[CLIENT] == HRKTORRENT:
+ btcr = HrktorrentRun(bt_client_data[BASE_DIR])
+
else:
return -1
btcr.run_client(btcr.simple_run_command)
"""
+ """
+ Stops a BT client by killing it.
+ """
def stop_bt_client(self, pid):
int_pid = int(pid)
os.wait()
self.processes[int_pid][0].close()
self.processes[int_pid][1].close()
- del self.processes[int_pid]
+ del self.processes[int_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)
+
+ file_list.append (info_list)
+
+ return 0 # success
+
+
+ """
+ Accepts socket connections and receives messages from commander.
+ """
def doServer(self, ip, port):
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if(self.DEBUG):
- print "Server: host ip = %s, port = %d"%(SERVER_HOST,SERVER_PORT)
- serversocket.bind((SERVER_HOST, SERVER_PORT));
+ print "Server: host ip = %s, port = %s"%(ip,SERVER_PORT)
+ serversocket.bind((ip,SERVER_PORT));
serversocket.listen(10) #max 10 requests
while(1):
if(self.DEBUG):
if(self.DEBUG):
print "Server: received message:\n", msg
-
+ err_msg = ''
if msg[0] == START_MSG:
bt_client_data = msg[1]
client_pid = self.start_bt_client(bt_client_data)
self.stop_bt_client(client_pid)
self.send_pickled_data(clientsock, (ACK_MSG,""))
- elif msg[0] == GET_CLIENTS:
- pass
elif msg[0] == GET_OUTPUT:
- #f = open(SESSIONS_FILE, )
- pass
+ print "LA get output\n"
+ import os.path
+ if not os.path.exists(SESSIONS_FILE):
+ print "Error: No sessions_file"
+ 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 != '':
+ print line
+ if size == 0:
+ print "aici la size = 0"
+ if info_dict != {} and self.add_to_output_msg(file_list, info_dict) < 0:
+ print "aici"
+ raise Exception
+ print "aici3ia"
+ size = int(line)
+ print "aici3"
+ info_dict = {}
+ else:
+ parts = line.strip().split(':')
+ if len(parts) != 2:
+ print "aici2"
+ 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
+
+ elif msg[0] == GET_CLIENTS:
+ pass
else:
self.send_pickled_data(clientsock,( ERROR_MSG,"Error: wrong message type "))
# 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', SERVER_HOST, 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]:
daemon = MyDaemon('/tmp/daemon-example.pid')
daemon = MyDaemon('/tmp/daemon-example.pid')
daemon.restart()
elif 'no' == sys.argv[1]: #no daemon
- daemon = MyDaemon('/tmp/daemon-example.pid', SERVER_HOST, stdout = '/home/p2p/out', stderr = '/home/p2p/err')
- daemon.doServer("","")
+ daemon = MyDaemon('/tmp/daemon-example.pid', sys.argv[2], stdout = '/home/p2p/out', stderr = '/home/p2p/err')
+ daemon.doServer(sys.argv[2],"")
else:
print "Unknown command"
sys.exit(2)