def list_command(conn, addr, command): print "Received list command from {}".format(addr[0]) tracker_files = get_tracker_files() num_files = len(tracker_files) conn.sendall("REP LIST {0}".format(str(num_files))) count = 1 # Obtains filename, filesize, and md5 for each tracker file to send to client for tracker in tracker_files: tracker_file = tracker_parser.TrackerFile() if not tracker_file.parseTrackerFile(tracker): file_size = tracker_file.get_file_size() md5 = tracker_file.get_md5() file_name = tracker_file.get_filename() data = "{0} {1} {2} {3}".format(str(count), file_name, str(file_size), md5) conn.sendall(data) # print "+=+=+=data {0}".format(data) count += 1 else: print 'Error parsing tracker file' conn.sendall('ERROR') conn.sendall("REP LIST END") conn.close()
def update_command(start_byte, end_byte): error = False sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((server_address, config["SERVER_PORT"])) except socket.error: sock.close() return True # instantiate TrackerFile object tracker_file = tracker_parser.TrackerFile() # Generate update command string command = tracker_file.updateCommand(config["TARGET_FILE"], PEER_PORT, start_byte, end_byte) sock.send(command) try: response = sock.recv(config["CHUNK_SIZE"]) if not response: error = True elif response.find('succ') == -1: error = True sock.close() return error except: sock.close() return True
def create_command(): error = False sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((server_address, config["SERVER_PORT"])) except socket.error: sock.close() return True # instantiate TrackerFile object tracker_file = tracker_parser.TrackerFile() # Generate creation command string command = tracker_file.createCommand(config["TARGET_FILE"], PEER_PORT, '_') sock.send(command) try: response = sock.recv(config["CHUNK_SIZE"]) if not response: error = True elif response.find('succ') == -1: error = True sock.close() # print "Client {0}, [create] error status = {1}".format(client_num, error) return error except socket.error: sock.close() return True
def updatetracker(conn, addr, command): print "Received updatetracker command from {}".format(addr[0]) tracker_file = tracker_parser.TrackerFile() status = tracker_parser.FILE_FAIL # Update tracker file or send error message back to client try: status = tracker_file.update(command) except: pass if status == tracker_parser.FILE_SUCC: conn.send('updatetracker succ') elif status == tracker_parser.FILE_ERR: conn.send('updatetracker ferr') else: conn.send('updatetracker fail')
def get_file(): while get_tracker_file(): pass # loop until successfully have tracker file # Create file handle for target file target_file = open(config["TARGET_FILE"], 'wb+') target_file.truncate(config["TARGET_FILE_SIZE"] + 1) # Create memory mapped writer to target file of specified size # For details, see: http://pymotw.com/2/mmap/ writer = mmap.mmap(target_file.fileno(), config["TARGET_FILE_SIZE"] + 1) tracker_file = tracker_parser.TrackerFile() threads = [] while True: if tracker_file.parseTrackerFile('{0}.track'.format( config["TARGET_FILE"])): # true if error return False for host in tracker_file.hosts: # spawn a new thread for each host (start_byte, num_bytes) = get_bytes_to_req(host) # print "//////// Client {0}, [get_file] num_bytes = {1}".format(client_num, num_bytes) if num_bytes > 0 and len(threads) < 6: thread = threading.Thread(target=thread_handler, args=(host.ip_addr, host.port, start_byte, num_bytes, writer)) try: thread.start() threads.append((thread, 0)) # thread, timeout except: pass # wait until all threads complete or timeout occurs timeout = 0 for (thread, timeout) in [ (thread, timeout) for thread, timeout in threads if not thread.is_alive() or timeout > config["THREAD_TIMEOUT"] ]: threads.remove((thread, timeout)) for (thread, timeout) in threads: timeout += 1 """ while len([thread for thread in threads if thread.is_alive()]) != 0 and timeout < config["THREAD_TIMEOUT"]: timeout += 1 time.sleep(1) """ if len(unwritten_bytes ) == 0: # no bytes left to be written - we're done! break # time.sleep(1) target_file.truncate(config["TARGET_FILE_SIZE"]) # TODO: check md5 calc_md5 = hashlib.md5() target_file.seek(0) while True: data = target_file.read(config["CHUNK_SIZE"]) if not data: break calc_md5.update(data) if tracker_file.md5 == str(calc_md5.hexdigest()): return True else: return False