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())); } } } } }
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; } } }
public boolean isBlockComplete(int blockNumber) throws IOException { BlockFile bf = storage.getBlockFile(fd.getRootHash()); if (bf == null) { return false; } return bf.isBlockComplete(blockNumber); }
public void abortAndRemovePerfmanently() throws IOException { for (DownloadConnection c : connections.values()) { if (c.isConnected()) { c.sendGracefulClose(); } } storage.removePermanently(root); }
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; }
public static Download createFrom(ObjectInputStream in, DownloadManager m) throws IOException { try { Hash h = new Hash(); int r = in.read(h.array()); String fn = in.readUTF(); ArrayList<Integer> guids = (ArrayList<Integer>) in.readObject(); if (T.t) { T.ass(r == h.array().length, "Incorrect length when deserializing download"); } BlockStorage bs = BlockStorage.getById(m.getCore(), in.readInt()); Download d = new Download(m, h, bs, fn, guids); d.fd = d.manager.getCore().getFileManager().getFd(h); return d; } catch (ClassNotFoundException e) { throw new IOException("Could not find class while deserializing: " + e); } }
public void serializeTo(ObjectOutputStream out) throws IOException { out.write(root.array()); out.writeUTF(auxInfoFilename); out.writeObject(auxInfoGuids); out.writeInt(storage.getStorageTypeId()); }