private void startNewDownloads() { boolean isLimited = true; int currentAllowedDownloadCount = 0; { final int allowedConcurrentDownloads = Core.frostSettings.getIntValue(SettingsClass.DOWNLOAD_MAX_THREADS); if (allowedConcurrentDownloads <= 0) { isLimited = false; } else { int runningDownloads = 0; for (final FrostDownloadItem dlItem : downloadModelItems.values()) { if (!dlItem.isExternal() && dlItem.getState() == FrostDownloadItem.STATE_PROGRESS) { runningDownloads++; } } currentAllowedDownloadCount = allowedConcurrentDownloads - runningDownloads; if (currentAllowedDownloadCount < 0) { currentAllowedDownloadCount = 0; } } } { while (!isLimited || currentAllowedDownloadCount > 0) { final FrostDownloadItem dlItem = FileTransferManager.inst().getDownloadManager().selectNextDownloadItem(); if (dlItem == null) { break; } // start the download if (startDownload(dlItem)) { currentAllowedDownloadCount--; } } } }
public void persistentRequestRemoved(final FcpPersistentGet downloadRequest) { if (downloadModelItems.containsKey(downloadRequest.getIdentifier())) { final FrostDownloadItem dlItem = downloadModelItems.get(downloadRequest.getIdentifier()); if (dlItem.isExternal()) { SwingUtilities.invokeLater( new Runnable() { public void run() { List<FrostDownloadItem> itemList = new ArrayList<FrostDownloadItem>(); itemList.add(dlItem); downloadModel.removeItems(itemList); } }); } else { if (dlItem.isInternalRemoveExpected()) { dlItem.setInternalRemoveExpected(false); // clear flag } else if (dlItem.getState() != FrostDownloadItem.STATE_DONE) { dlItem.setEnabled(false); dlItem.setState(FrostDownloadItem.STATE_FAILED); dlItem.setErrorCodeDescription("Disappeared from global queue"); } } } }
/** Apply the states of FcpRequestGet to the FrostDownloadItem. */ private void applyState(final FrostDownloadItem dlItem, final FcpPersistentGet getReq) { // when cancelled and we expect this, don't set failed; don't even set the old priority! if (dlItem.isInternalRemoveExpected() && getReq.isFailed()) { final int returnCode = getReq.getCode(); if (returnCode == 25) { return; } } applyPriority(dlItem, getReq); if (dlItem.isDirect() != getReq.isDirect()) { dlItem.setDirect(getReq.isDirect()); } if (!getReq.isProgressSet() && !getReq.isSuccess() && !getReq.isFailed()) { if (dlItem.getState() == FrostDownloadItem.STATE_WAITING) { dlItem.setState(FrostDownloadItem.STATE_PROGRESS); } return; } if (getReq.isProgressSet()) { final int doneBlocks = getReq.getDoneBlocks(); final int requiredBlocks = getReq.getRequiredBlocks(); final int totalBlocks = getReq.getTotalBlocks(); final boolean isFinalized = getReq.isFinalized(); if (totalBlocks > 0) { dlItem.setDoneBlocks(doneBlocks); dlItem.setRequiredBlocks(requiredBlocks); dlItem.setTotalBlocks(totalBlocks); dlItem.setFinalized(isFinalized); dlItem.fireValueChanged(); } if (dlItem.getState() != FrostDownloadItem.STATE_PROGRESS) { dlItem.setState(FrostDownloadItem.STATE_PROGRESS); } } if (getReq.isSuccess()) { // maybe progress was not completely sent dlItem.setFinalized(true); if (dlItem.getTotalBlocks() > 0 && dlItem.getDoneBlocks() < dlItem.getRequiredBlocks()) { dlItem.setDoneBlocks(dlItem.getRequiredBlocks()); dlItem.fireValueChanged(); } if (dlItem.isExternal()) { dlItem.setFileSize(getReq.getFilesize()); dlItem.setState(FrostDownloadItem.STATE_DONE); } else { if (dlItem.isDirect()) { maybeEnqueueDirectGet(dlItem, getReq.getFilesize()); } else { final FcpResultGet result = new FcpResultGet(true); final File targetFile = new File(dlItem.getDownloadFilename()); FileTransferManager.inst() .getDownloadManager() .notifyDownloadFinished(dlItem, result, targetFile); } } } if (getReq.isFailed()) { final String desc = getReq.getCodeDesc(); if (dlItem.isExternal()) { dlItem.setState(FrostDownloadItem.STATE_FAILED); dlItem.setErrorCodeDescription(desc); } else { final int returnCode = getReq.getCode(); final boolean isFatal = getReq.isFatal(); final String redirectURI = getReq.getRedirectURI(); final FcpResultGet result = new FcpResultGet(false, returnCode, desc, isFatal, redirectURI); final File targetFile = new File(dlItem.getDownloadFilename()); final boolean retry = FileTransferManager.inst() .getDownloadManager() .notifyDownloadFinished(dlItem, result, targetFile); if (retry) { fcpTools.removeRequest(getReq.getIdentifier()); startDownload(dlItem); // restart immediately } } } }