public boolean startUpload(final FrostUploadItem ulItem) { if (ulItem == null || ulItem.getState() != FrostUploadItem.STATE_WAITING) { return false; } ulItem.setUploadStartedMillis(System.currentTimeMillis()); ulItem.setState(FrostUploadItem.STATE_PROGRESS); // start the upload final boolean doMime; final boolean setTargetFileName; if (ulItem.isSharedFile()) { doMime = false; setTargetFileName = false; } else { doMime = true; setTargetFileName = true; } // try to start using DDA boolean isDda = fcpTools.startPersistentPutUsingDda( ulItem.getGqIdentifier(), ulItem.getFile(), ulItem.getFileName(), doMime, setTargetFileName, ulItem.getCompress(), ulItem.getFreenetCompatibilityMode(), ulItem.getPriority()); if (!isDda) { // upload was not startet because DDA is not allowed... // if UploadManager selected this file then it is not already in progress! directTransferQueue.appendItemToQueue(ulItem); } return true; }
@Override public void run() { final int maxAllowedExceptions = 5; int catchedExceptions = 0; while (true) { try { // if there is no work in queue this call waits for a new queue item final ModelItem<?> item = directTransferQueue.getItemFromQueue(); if (item == null) { // paranoia, should never happen Mixed.wait(5 * 1000); continue; } if (item instanceof FrostUploadItem) { // transfer bytes to node final FrostUploadItem ulItem = (FrostUploadItem) item; // FIXME: provide item, state=Transfer to node, % shows progress final String gqid = ulItem.getGqIdentifier(); final boolean doMime; final boolean setTargetFileName; if (ulItem.isSharedFile()) { doMime = false; setTargetFileName = false; } else { doMime = true; setTargetFileName = true; } final NodeMessage answer = fcpTools.startDirectPersistentPut( gqid, ulItem.getFile(), ulItem.getFileName(), doMime, setTargetFileName, ulItem.getCompress(), ulItem.getFreenetCompatibilityMode(), ulItem.getPriority()); if (answer == null) { final String desc = "Could not open a new FCP2 socket for direct put!"; final FcpResultPut result = new FcpResultPut(FcpResultPut.Error, -1, desc, false); FileTransferManager.inst().getUploadManager().notifyUploadFinished(ulItem, result); logger.severe(desc); } else { // wait for an answer, don't start request again directPUTsWithoutAnswer.add(gqid); } directPUTsInProgress.remove(gqid); } else if (item instanceof FrostDownloadItem) { // transfer bytes from node final FrostDownloadItem dlItem = (FrostDownloadItem) item; // FIXME: provide item, state=Transfer from node, % shows progress final String gqid = dlItem.getGqIdentifier(); final File targetFile = new File(dlItem.getDownloadFilename()); final boolean retryNow; NodeMessage answer = null; try { answer = fcpTools.startDirectPersistentGet(gqid, targetFile); } catch (final FileNotFoundException e) { final String msg = "Could not write to " + dlItem.getDownloadFilename() + ": " + e.getMessage(); System.out.println(msg); logger.severe(msg); } if (answer != null) { final FcpResultGet result = new FcpResultGet(true); FileTransferManager.inst() .getDownloadManager() .notifyDownloadFinished(dlItem, result, targetFile); retryNow = false; } else { logger.severe("Could not open a new fcp socket for direct get!"); final FcpResultGet result = new FcpResultGet(false); retryNow = FileTransferManager.inst() .getDownloadManager() .notifyDownloadFinished(dlItem, result, targetFile); } directGETsInProgress.remove(gqid); if (retryNow) { startDownload(dlItem); } } } catch (final Throwable t) { logger.log(Level.SEVERE, "Exception catched", t); catchedExceptions++; } if (catchedExceptions > maxAllowedExceptions) { logger.log(Level.SEVERE, "Stopping DirectTransferThread because of too much exceptions"); break; } } }
/** Apply the states of FcpRequestPut to the FrostUploadItem. */ private void applyState(final FrostUploadItem ulItem, final FcpPersistentPut putReq) { // when cancelled and we expect this, don't set failed; don't even set the old priority! if (ulItem.isInternalRemoveExpected() && putReq.isFailed()) { final int returnCode = putReq.getCode(); if (returnCode == 25) { return; } } if (directPUTsWithoutAnswer.contains(ulItem.getGqIdentifier())) { // we got an answer directPUTsWithoutAnswer.remove(ulItem.getGqIdentifier()); } // apply externally changed priority if (ulItem.getPriority() != putReq.getPriority()) { if (Core.frostSettings.getBoolValue(SettingsClass.FCP2_ENFORCE_FROST_PRIO_FILE_UPLOAD)) { // reset priority with our current value fcpTools.changeRequestPriority(putReq.getIdentifier(), ulItem.getPriority()); } else { // apply to uploaditem ulItem.setPriority(putReq.getPriority()); } } if (!putReq.isProgressSet() && !putReq.isSuccess() && !putReq.isFailed()) { if (ulItem.getState() == FrostUploadItem.STATE_WAITING) { ulItem.setState(FrostUploadItem.STATE_PROGRESS); } return; } if (putReq.isProgressSet()) { final int doneBlocks = putReq.getDoneBlocks(); final int totalBlocks = putReq.getTotalBlocks(); final boolean isFinalized = putReq.isFinalized(); if (totalBlocks > 0) { ulItem.setDoneBlocks(doneBlocks); ulItem.setTotalBlocks(totalBlocks); ulItem.setFinalized(isFinalized); ulItem.fireValueChanged(); } if (ulItem.getState() != FrostUploadItem.STATE_PROGRESS) { ulItem.setState(FrostUploadItem.STATE_PROGRESS); } } if (putReq.isSuccess()) { // maybe progress was not completely sent ulItem.setFinalized(true); if (ulItem.getTotalBlocks() > 0 && ulItem.getDoneBlocks() != ulItem.getTotalBlocks()) { ulItem.setDoneBlocks(ulItem.getTotalBlocks()); } final String chkKey = putReq.getUri(); if (ulItem.isExternal()) { ulItem.setState(FrostUploadItem.STATE_DONE); ulItem.setKey(chkKey); } else { final FcpResultPut result = new FcpResultPut(FcpResultPut.Success, chkKey); FileTransferManager.inst().getUploadManager().notifyUploadFinished(ulItem, result); } } if (putReq.isFailed()) { final String desc = putReq.getCodeDesc(); if (ulItem.isExternal()) { ulItem.setState(FrostUploadItem.STATE_FAILED); ulItem.setErrorCodeDescription(desc); } else { final int returnCode = putReq.getCode(); final boolean isFatal = putReq.isFatal(); final FcpResultPut result; if (returnCode == 9) { result = new FcpResultPut(FcpResultPut.KeyCollision, returnCode, desc, isFatal); } else if (returnCode == 5) { result = new FcpResultPut(FcpResultPut.Retry, returnCode, desc, isFatal); } else { result = new FcpResultPut(FcpResultPut.Error, returnCode, desc, isFatal); } FileTransferManager.inst().getUploadManager().notifyUploadFinished(ulItem, result); } } }