Esempio n. 1
0
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
Esempio n. 2
0
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