Ejemplo n.º 1
0
  public Download(
      DownloadManager parent,
      Hash root,
      BlockStorage storage,
      String filename,
      ArrayList<Integer> guids)
      throws IOException {
    this.manager = parent;
    this.root = root;
    this.storage = storage;
    this.auxInfoFilename = filename;
    this.auxInfoGuids = guids;

    setFd(
        manager
            .getCore()
            .getFileManager()
            .getFd(root)); // not sure there is one, but when resuming there is

    if (fd != null) {
      BlockFile bf = storage.getBlockFile(fd.getRootHash());
      if (bf != null) {
        bytesComplete = ((long) bf.getNumberOfBlocksComplete()) * BLOCK_SIZE;
      }
    }
  }
Ejemplo n.º 2
0
 public boolean isBlockComplete(int blockNumber) throws IOException {
   BlockFile bf = storage.getBlockFile(fd.getRootHash());
   if (bf == null) {
     return false;
   }
   return bf.isBlockComplete(blockNumber);
 }
Ejemplo n.º 3
0
 public void signalBlockComplete(int blockNumber) throws IOException {
   signalBlockCompleteCounter++;
   if (signalBlockCompleteCounter > SEND_BLOCKMASK_UPDATE_INTERVAL_IN_BLOCKS) {
     signalBlockCompleteCounter = 0;
     BlockFile bf = storage.getBlockFile(fd.getRootHash());
     if (bf == null) {
       return;
     }
     List<Friend> list = manager.getFriendsInterestedIn(root);
     if (list != null) {
       for (Friend f : list) {
         FriendConnection fc = f.getFriendConnection();
         if (fc != null) {
           if (T.t) {
             T.info(
                 "Friend "
                     + f
                     + " is interested in file "
                     + root
                     + ". Sending updated block mask to him.");
           }
           fc.send(new BlockMaskResult(root, bf.getBlockMask()));
         }
       }
     }
   }
 }
Ejemplo n.º 4
0
  public int selectBestBlockForDownload(Friend remoteFriend) throws IOException {
    if (T.t) {
      T.debug("Selecting best block for download. Remote: " + remoteFriend);
    }
    BlockMask bm = blockMasks.get(remoteFriend.getGuid());
    if (bm == null) {
      if (T.t) {
        T.info("Ehh. Don't know anything about this friends block mask. Can't download.");
      }
      return -1;
    }

    BitSet interestingBlocks = getInterestingBlocks(bm);

    // remove bocks in progress from interesting blocks:
    BlockFile bf = storage.getBlockFile(root);
    BitSet blocksInProgress = bf == null ? new BitSet() : bf.getBlocksInProgress();
    blocksInProgress.flip(0, fd.getNumberOfBlocks());
    blocksInProgress.and(interestingBlocks);

    if (blocksInProgress.cardinality() > 0) {
      // there are blocks of interest that are NOT in progress. Take one of these
      interestingBlocks = blocksInProgress;
    } // else there are only blocks in progress. Use any of them

    int highestBlockNumber = 0;
    if (bf != null) {
      highestBlockNumber = bf.getHighestCompleteBlock();
    }
    highestBlockNumber += manager.getCore().getSettings().getInternal().getMaxfileexpandinblocks();
    // we prefer to load blocks below highestBlockNumber
    if (interestingBlocks.nextSetBit(0) < highestBlockNumber) {
      // we're good - there are interesting blocks below the highest block number.

      // remove all blocks above highest block number:
      if (highestBlockNumber + 1 < fd.getNumberOfBlocks()) {
        interestingBlocks.clear(highestBlockNumber + 1, fd.getNumberOfBlocks());
      }
    }

    // select a random block of the ones we're interested in - change this to rarest first in the
    // future
    int c = interestingBlocks.cardinality();
    int n = (int) (Math.random() * c);
    for (int i = interestingBlocks.nextSetBit(0), j = 0;
        i >= 0;
        i = interestingBlocks.nextSetBit(i + 1), j++) {
      if (j == n) {
        return i;
      }
    }

    if (T.t) {
      T.trace("Can't find any block to download from " + remoteFriend);
    }
    return -1;
  }