def connect_peer_server(threadname, relevant_path, downloadedFiles, downloadingFiles, listOfFiles, allFilesList, ip_address, peer_server_port, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer): while (1): #print " Connect Peer Server " #list all the files available in the tracker server params = "GET command=list" #urllib.urlencode({'command':'list'}) #TODO: uncomment this line to get server response listString = connect_tracker_server(params, socket, 'list', ip_address, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer) #Below list of files should be downloaded at this peer allTrackerFilesList = listString.split("\n") #print " All Tracker Files Obtained: ", allTrackerFilesList toBeDownloadedFileList = removeOriginallySharedFiles( relevant_path, allTrackerFilesList) #print "downloading Files" , downloadingFiles, " toBeDownloadedFileList ", toBeDownloadedFileList #if (len(downloadedFiles) < len(toBeDownloadedFileList)): #print "Peer 10: All tracker files lists that need to be downloaded: ", toBeDownloadedFileList, "\n\n" if len(toBeDownloadedFileList) > 0: # last one is always empty for index in range(len(toBeDownloadedFileList) - 1): trackerFile = toBeDownloadedFileList[index] #print " To be downloaded List in : ", trackerFile #downloadedFiles.append(trackerFile) #get the tracker file for the this file #params = urllib.urlencode({'command':'get','filenametrack':trackerFile}) params = "GET command=get" + "&filenametrack=" + trackerFile #uncomment this line to get response from the server getTrackerString = connect_tracker_server( params, socket, 'get', ip_address, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer) if os.path.isfile(relevant_path + trackerFile) == True: lines = [ line.rstrip('\n') for line in open(relevant_path + trackerFile).readlines() ] for line in lines: if line not in getTrackerString: getTrackerString += line + "\n" print "Peer 10 : GET Tracker File: \n ", getTrackerString, "\n\n" try: #print "END HERE " # try downloading the files as per the tracker file #pass the contents of tracker file thread.start_new_thread( process_data, ("Thread-3", 5, getTrackerString, trackerFile, relevant_path, maxSegmentSize, ip_address, peer_server_port)) except: print "Error: unable to start thread - process_data"
def connect_peer_server(threadname, relevant_path, downloadedFiles, downloadingFiles, listOfFiles, allFilesList, ip_address, peer_server_port, tracker_server_port, maxSegmentSize,maxFileSizeFromTrackerServer ): while(1): #print " Connect Peer Server " #list all the files available in the tracker server params = "GET command=list" #urllib.urlencode({'command':'list'}) #TODO: uncomment this line to get server response listString=connect_tracker_server(params, socket, 'list', ip_address, tracker_server_port,maxSegmentSize,maxFileSizeFromTrackerServer) #Below list of files should be downloaded at this peer allTrackerFilesList = listString.split("\n") #print " All Tracker Files Obtained: ", allTrackerFilesList toBeDownloadedFileList = removeOriginallySharedFiles(relevant_path,allTrackerFilesList) #print "downloading Files" , downloadingFiles, " toBeDownloadedFileList ", toBeDownloadedFileList #if (len(downloadedFiles) < len(toBeDownloadedFileList)): #print "Peer 2: All tracker files lists that need to be downloaded: ", toBeDownloadedFileList, "\n\n" if len(toBeDownloadedFileList) > 0: # last one is always empty for index in range(len(toBeDownloadedFileList)-1): trackerFile = toBeDownloadedFileList[index] #print " To be downloaded List in : ", trackerFile #downloadedFiles.append(trackerFile) #get the tracker file for the this file #params = urllib.urlencode({'command':'get','filenametrack':trackerFile}) params = "GET command=get"+"&filenametrack="+trackerFile #uncomment this line to get response from the server getTrackerString = connect_tracker_server(params, socket , 'get',ip_address, tracker_server_port, maxSegmentSize,maxFileSizeFromTrackerServer) if os.path.isfile(relevant_path + trackerFile) == True: lines = [line.rstrip('\n') for line in open(relevant_path + trackerFile).readlines()] for line in lines: if line not in getTrackerString: getTrackerString += line +"\n" print "Peer 2 : GET Tracker File: \n " , getTrackerString, "\n\n" try: #print "END HERE " # try downloading the files as per the tracker file #pass the contents of tracker file thread.start_new_thread( process_data, ("Thread-3", 5, getTrackerString, trackerFile, relevant_path, maxSegmentSize, ip_address, peer_server_port) ) except: print "Error: unable to start thread - process_data"
def handle_tracker_server( threadname, socket, delay, relevant_path, included_extenstions, listOfFiles, sharedFiles, updateTrackerFilesList, updatedListOfFiles, ip_address, PORT, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer, ): while 1: # keep track of segment count in a particular tracker file # so that inform the peer not to send the update request for another tracker file # unless current tracker file is done updating allSegmentUpdatedCount = 0 # create tracker files for all the files that peer wants to share if len(sharedFiles) < len(listOfFiles): # print "Shared Files: " , sharedFiles , " List of Files: ", listOfFiles for file in listOfFiles: if file not in sharedFiles: currentFile = file # print currentFile params = createTrackerFile(relevant_path + file, "Hello", ip_address, PORT) # isShared = 1 # print "Tracker file being Created for : ", currentFile ,"\n" # uncomment this line to get server response recvCode = connect_tracker_server( params, socket, "createTracker", ip_address, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer, ) # print " Tracker file has been created: Code - " , code if recvCode == "200": sharedFiles.append(currentFile) recvCode = 404 else: time.sleep(delay) # print "here I am in Update Tracker " allowed_extensions = ["track"] updateTrackerFilesList = [ fn for fn in os.listdir(relevant_path) if any(fn.endswith(ext) for ext in allowed_extensions) ] updateTrackerFilesList = removeOriginallySharedFiles(relevant_path, updateTrackerFilesList) # maintain a local copy and updated tracker copy - once updated to tracker file # overwrite the local copy with the updated tracker copy # update tracker for all downloaded files # TODO: should be greater # if(len(updatedListOfFiles) < len(updateTrackerFilesList)): # print (delay , updatedListOfFiles) for file in updateTrackerFilesList: updatedFile = file # TODO: uncomment this line # if(file not in listOfFiles): listOfSegments = parseTrackerFile(relevant_path + file) # print " Update Tracker File : List of segments: \n ", listOfSegments for index in range(len(listOfSegments) - 1): segmentLine = listOfSegments[index].split(":") # print "Peer 7: Update the tracker server file with ", segmentLine,"\n\n" params = updateTrackerFile(relevant_path + file, segmentLine) # print " Update Tracker Params: ", params # print "Updated file Tracker for : ", updatedFile # uncomment this line to get server response recvCode = connect_tracker_server( params, socket, "updateTracker", ip_address, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer, ) if recvCode == "200": allSegmentUpdatedCount += 1 recvCode = 404 else: index = index - 1
def handle_tracker_server(threadname, socket, delay, relevant_path, included_extenstions, listOfFiles, sharedFiles, updateTrackerFilesList, updatedListOfFiles, ip_address, PORT, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer): while (1): # keep track of segment count in a particular tracker file # so that inform the peer not to send the update request for another tracker file # unless current tracker file is done updating allSegmentUpdatedCount = 0 #create tracker files for all the files that peer wants to share if (len(sharedFiles) < len(listOfFiles)): #print "Shared Files: " , sharedFiles , " List of Files: ", listOfFiles for file in listOfFiles: if (file not in sharedFiles): currentFile = file #print currentFile params = createTrackerFile(relevant_path + file, "Hello", ip_address, PORT) #isShared = 1 #print "Tracker file being Created for : ", currentFile ,"\n" #uncomment this line to get server response recvCode = connect_tracker_server( params, socket, 'createTracker', ip_address, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer) #print " Tracker file has been created: Code - " , code if recvCode == '200': sharedFiles.append(currentFile) recvCode = 404 else: time.sleep(delay) #print "here I am in Update Tracker " allowed_extensions = ["track"] updateTrackerFilesList = [ fn for fn in os.listdir(relevant_path) if any( fn.endswith(ext) for ext in allowed_extensions) ] updateTrackerFilesList = removeOriginallySharedFiles( relevant_path, updateTrackerFilesList) # maintain a local copy and updated tracker copy - once updated to tracker file # overwrite the local copy with the updated tracker copy #update tracker for all downloaded files # TODO: should be greater #if(len(updatedListOfFiles) < len(updateTrackerFilesList)): #print (delay , updatedListOfFiles) for file in updateTrackerFilesList: updatedFile = file # TODO: uncomment this line #if(file not in listOfFiles): listOfSegments = parseTrackerFile(relevant_path + file) #print " Update Tracker File : List of segments: \n ", listOfSegments for index in range(len(listOfSegments) - 1): segmentLine = listOfSegments[index].split(":") #print "Peer 10: Update the tracker server file with ", segmentLine,"\n\n" params = updateTrackerFile(relevant_path + file, segmentLine) #print " Update Tracker Params: ", params #print "Updated file Tracker for : ", updatedFile #uncomment this line to get server response recvCode = connect_tracker_server( params, socket, 'updateTracker', ip_address, tracker_server_port, maxSegmentSize, maxFileSizeFromTrackerServer) if recvCode == '200': allSegmentUpdatedCount += 1 recvCode = 404 else: index = index - 1