def findRequestableBlocks(classifiedPeers, numToReturn = nil)
      # Have a list of the current pieces we are working on. Each time this method is 
      # called, check the blocks in the pieces in list order to find the blocks to return
      # for requesting. If a piece is completed, remove it from this list. If we need more blocks
      # than there are available in the list, add more pieces to the end of the list (in rarest-first
      # order).
      result = []

      # Update requestable peers to only be those that we can still request pieces from.
      peersHavingPiece = computePeersHavingPiece(classifiedPeers)
      requestable = @completeBlocks.union(@requestedBlocks).compliment!
      rarityOrder = nil

      currentPiece = 0
      while true
        if currentPiece >= @currentPieces.length
          # Add more pieces in rarest-first order. If there are no more pieces, break.
          rarityOrder = computeRarity(classifiedPeers) if ! rarityOrder
          added = false
          rarityOrder.each do |pair|
            pieceIndex = pair[1]
            peersWithPiece = peersHavingPiece[pieceIndex]
            if peersWithPiece && peersWithPiece.size > 0 && !@currentPieces.index(pieceIndex) && ! pieceCompleted?(pieceIndex)
              @logger.debug "Adding piece #{pieceIndex} to the current downloading list"
              @currentPieces.push pieceIndex
              added = true
              break
            end
          end
          if ! added          
            @logger.debug "There are no more pieces to add to the current downloading list"
            break
          end
        end        
    
        currentPieceIndex = @currentPieces[currentPiece]

        if pieceCompleted?(currentPieceIndex)
          @logger.debug "Piece #{currentPieceIndex} complete so removing it from the current downloading list" 
          @currentPieces.delete_at(currentPiece)
          next
        end

        peersWithPiece = peersHavingPiece[currentPieceIndex]
        if !peersWithPiece || peersWithPiece.size == 0
          @logger.debug "No peers have piece #{currentPieceIndex}" 
          currentPiece += 1
          next
        end

        eachBlockInPiece(currentPieceIndex) do |blockIndex|
          if requestable.set?(blockIndex)
            result.push createBlockinfoByPieceAndBlockIndex(currentPieceIndex, peersWithPiece, blockIndex)
            break if numToReturn && result.size >= numToReturn
          end
        end           

        break if numToReturn && result.size >= numToReturn
        currentPiece += 1
      end

      result
    end
def findRequestableBlocks(classifiedPeers, numToReturn = nil)
      # Have a list of the current pieces we are working on. Each time this method is 
      # called, check the blocks in the pieces in list order to find the blocks to return
      # for requesting. If a piece is completed, remove it from this list. If we need more blocks
      # than there are available in the list, add more pieces to the end of the list (in rarest-first
      # order).
      result = []

      # Update requestable peers to only be those that we can still request pieces from.
      peersHavingPiece = computePeersHavingPiece(classifiedPeers)
      requestable = @completeBlocks.union(@requestedBlocks).compliment!
      rarityOrder = nil

      currentPiece = 0
      while true
        if currentPiece >= @currentPieces.length
          # Add more pieces in rarest-first order. If there are no more pieces, break.
          rarityOrder = computeRarity(classifiedPeers) if ! rarityOrder
          added = false
          rarityOrder.each do |pair|
            pieceIndex = pair[1]
            peersWithPiece = peersHavingPiece[pieceIndex]
            if peersWithPiece && peersWithPiece.size > 0 && !@currentPieces.index(pieceIndex) && ! pieceCompleted?(pieceIndex)
              @logger.debug "Adding piece #{pieceIndex} to the current downloading list"
              @currentPieces.push pieceIndex
              added = true
              break
            end
          end
          if ! added          
            @logger.debug "There are no more pieces to add to the current downloading list"
            break
          end
        end        
    
        currentPieceIndex = @currentPieces[currentPiece]

        if pieceCompleted?(currentPieceIndex)
          @logger.debug "Piece #{currentPieceIndex} complete so removing it from the current downloading list" 
          @currentPieces.delete_at(currentPiece)
          next
        end

        peersWithPiece = peersHavingPiece[currentPieceIndex]
        if !peersWithPiece || peersWithPiece.size == 0
          @logger.debug "No peers have piece #{currentPieceIndex}" 
          currentPiece += 1
          next
        end

        eachBlockInPiece(currentPieceIndex) do |blockIndex|
          if requestable.set?(blockIndex)
            result.push createBlockinfoByPieceAndBlockIndex(currentPieceIndex, peersWithPiece, blockIndex)
            break if numToReturn && result.size >= numToReturn
          end
        end           

        break if numToReturn && result.size >= numToReturn
        currentPiece += 1
      end

      result
    end
Return a list of BlockInfo objects representing blocjs that should be requested from peers.