Example #1
0
  protected void fileDirty(TranscodeFileImpl file, int type, Object data) {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();

        } else {

          device_files_last_mod = SystemTime.getMonotonousTime();
        }
      }

      device_files_dirty = true;

    } catch (Throwable e) {

      Debug.out("Failed to load device file", e);
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileChanged(file, type, data);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }
  }
  protected boolean checkConnectionId(String client_address, long id) {
    try {
      random_mon.enter();

      Long key = new Long(id);

      connectionData data = (connectionData) connection_id_map.get(key);

      if (data == null) {

        // System.out.println( "TRTrackerServerProcessorUDP: rejected:" + id + ", data not found" );

        return (false);

      } else {

        if (SystemTime.getMonotonousTime() - data.getTime() > CONNECTION_ID_LIFETIME) {

          return (false);
        }
      }

      boolean ok = data.getAddress().equals(client_address);

      // System.out.println( "TRTrackerServerProcessorUDP: tested:" + id + "/" + client_address + "
      // -> " + ok );

      return (ok);

    } finally {

      random_mon.exit();
    }
  }
Example #3
0
  protected void loadDeviceFile() throws IOException {

    device_files_last_mod = SystemTime.getMonotonousTime();

    if (device_files_ref != null) {

      device_files = device_files_ref.get();
    }

    if (device_files == null) {

      Map map = FileUtil.readResilientFile(getDeviceFile());

      device_files = (Map<String, Map<String, ?>>) map.get("files");

      if (device_files == null) {

        device_files = new HashMap<String, Map<String, ?>>();
      }

      device_files_ref = new WeakReference<Map<String, Map<String, ?>>>(device_files);

      log("Loaded device file for " + getName() + ": files=" + device_files.size());
    }

    final int GC_TIME = 15000;

    new DelayedEvent(
        "Device:gc",
        GC_TIME,
        new AERunnable() {
          public void runSupport() {
            synchronized (DeviceImpl.this) {
              if (SystemTime.getMonotonousTime() - device_files_last_mod >= GC_TIME) {

                if (device_files_dirty) {

                  saveDeviceFile();
                }

                device_files = null;

              } else {

                new DelayedEvent("Device:gc2", GC_TIME, this);
              }
            }
          }
        });
  }
Example #4
0
  public TranscodeFileImpl allocateFile(
      TranscodeProfile profile, boolean no_xcode, DiskManagerFileInfo file, boolean for_job)
      throws TranscodeException {
    TranscodeFileImpl result = null;

    setError(KEY_FILE_ALLOC_ERROR, null);

    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        String key =
            ByteFormatter.encodeString(file.getDownloadHash())
                + ":"
                + file.getIndex()
                + ":"
                + profile.getUID();

        if (device_files.containsKey(key)) {

          try {
            result = new TranscodeFileImpl(this, key, device_files);

          } catch (Throwable e) {

            device_files.remove(key);

            log("Failed to deserialise transcode file", e);
          }
        }

        if (result == null) {

          String ext = profile.getFileExtension();

          String target_file = file.getFile(true).getName();

          if (ext != null && !no_xcode) {

            int pos = target_file.lastIndexOf('.');

            if (pos != -1) {

              target_file = target_file.substring(0, pos);
            }

            target_file += ext;
          }

          target_file = allocateUniqueFileName(target_file);

          File output_file = getWorkingDirectory(true);

          if (!output_file.canWrite()) {

            throw (new TranscodeException(
                "Can't write to transcode folder '" + output_file.getAbsolutePath() + "'"));
          }

          output_file = new File(output_file.getAbsoluteFile(), target_file);

          result =
              new TranscodeFileImpl(
                  this, key, profile.getName(), device_files, output_file, for_job);

          result.setSourceFile(file);

          device_files_last_mod = SystemTime.getMonotonousTime();

          device_files_dirty = true;

        } else {

          result.setSourceFile(file);

          result.setProfileName(profile.getName());
        }
      }
    } catch (Throwable e) {

      setError(KEY_FILE_ALLOC_ERROR, Debug.getNestedExceptionMessage(e));

      throw (new TranscodeException("File allocation failed", e));
    }

    for (TranscodeTargetListener l : listeners) {

      try {
        l.fileAdded(result);

      } catch (Throwable e) {

        Debug.out(e);
      }
    }

    return (result);
  }
Example #5
0
  protected void deleteFile(TranscodeFileImpl file, boolean delete_contents, boolean remove)
      throws TranscodeException {
    if (file.isDeleted()) {

      return;
    }

    if (delete_contents) {

      File f = file.getCacheFile();

      int time = 0;

      while (f.exists() && !f.delete()) {

        if (time > 3000) {

          log("Failed to remove file '" + f.getAbsolutePath() + "'");

          break;

        } else {

          try {
            Thread.sleep(500);

          } catch (Throwable e) {

          }

          time += 500;
        }
      }
    }

    if (remove) {

      try {
        // fire the listeners FIRST as this gives listeners a chance to extract data
        // from the file before it is deleted (otherwise operations fail with 'file has been
        // deleted'

        for (TranscodeTargetListener l : listeners) {

          try {
            l.fileRemoved(file);

          } catch (Throwable e) {

            Debug.out(e);
          }
        }

        synchronized (this) {
          if (device_files == null) {

            loadDeviceFile();

          } else {

            device_files_last_mod = SystemTime.getMonotonousTime();
          }

          device_files.remove(file.getKey());

          device_files_dirty = true;
        }

      } catch (Throwable e) {

        throw (new TranscodeException("Delete failed", e));
      }
    }
  }
    private connectionData(String _address, long _id) {
      address = _address;
      id = _id;

      time = SystemTime.getMonotonousTime();
    }