Exemplo n.º 1
0
  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;
  }
Exemplo n.º 2
0
    @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;
        }
      }
    }