private long[] getTotals(int day, long start_offset) {
      if (start_offset == 0) {

        return (getTotals(day));

      } else {

        List<Long> records = getContents().get(day + "." + start_offset);

        if (records != null) {

          long[] result = new long[STAT_ENTRY_COUNT];

          if (records.size() == STAT_ENTRY_COUNT) {

            for (int i = 0; i < STAT_ENTRY_COUNT; i++) {

              result[i] = (Long) records.get(i);
            }
          }

          return (result);
        }

        return (null);
      }
    }
Beispiel #2
0
  public void setPersistentStringListProperty(String prop, String[] values) {
    boolean dirty = false;

    synchronized (persistent_properties) {
      try {
        List<byte[]> values_list = new ArrayList<byte[]>();

        for (String value : values) {

          values_list.add(value.getBytes("UTF-8"));
        }

        persistent_properties.put(prop, values_list);

        dirty = true;

      } catch (Throwable e) {

        Debug.printStackTrace(e);
      }
    }

    if (dirty) {

      setDirty();
    }
  }
  public boolean verifyParameter(String parameter, String value) {
    List verifiers = ConfigurationDefaults.getInstance().getVerifiers(parameter);

    if (verifiers != null) {
      try {
        for (int i = 0; i < verifiers.size(); i++) {

          ParameterVerifier verifier = (ParameterVerifier) verifiers.get(i);

          if (verifier != null) {

            try {
              if (!verifier.verify(parameter, value)) {

                return (false);
              }
            } catch (Throwable e) {

              Debug.printStackTrace(e);
            }
          }
        }
      } catch (Throwable e) {

        // we're not synchronized so possible but unlikely error here

        Debug.printStackTrace(e);
      }
    }

    return (true);
  }
Beispiel #4
0
  protected void createDirs(File target) throws FMFileManagerException {

    if (clone) {

      return;
    }

    deleteDirs();

    File parent = target.getParentFile();

    if (!parent.exists()) {

      List new_dirs = new ArrayList();

      File current = parent;

      while (current != null && !current.exists()) {

        new_dirs.add(current);

        current = current.getParentFile();
      }

      created_dirs_leaf = target;
      created_dirs = new ArrayList();

      if (FileUtil.mkdirs(parent)) {

        created_dirs = new_dirs;

        /*
        for (int i=created_dirs.size()-1;i>=0;i--){

        	System.out.println( "created " + created_dirs.get(i));
        }
        */
      } else {
        // had some reports of this exception being thrown when starting a torrent
        // double check in case there's some parallel creation being triggered somehow

        try {
          Thread.sleep(RandomUtils.nextInt(1000));

        } catch (Throwable e) {
        }

        FileUtil.mkdirs(parent);

        if (parent.isDirectory()) {

          created_dirs = new_dirs;

        } else {

          throw (new FMFileManagerException("Failed to create parent directory '" + parent + "'"));
        }
      }
    }
  }
  private void doReadAheadLoads() {
    List to_submit = null;
    try {
      lock_mon.enter();

      while (loading_messages.size() + queued_messages.size() < request_read_ahead
          && !requests.isEmpty()
          && !destroyed) {
        DiskManagerReadRequest dmr = (DiskManagerReadRequest) requests.removeFirst();
        loading_messages.add(dmr);
        if (to_submit == null) to_submit = new ArrayList();
        to_submit.add(dmr);
      }
    } finally {
      lock_mon.exit();
    }

    /*
    if ( peer.getIp().equals( "64.71.5.2")){

    	TimeFormatter.milliTrace( "obt read_ahead: -> " + (to_submit==null?0:to_submit.size()) +
    			" [lo=" + loading_messages.size() + ",qm=" + queued_messages.size() + ",re=" + requests.size() + ",rl=" + request_read_ahead + "]");
    }
    */

    if (to_submit != null) {
      for (int i = 0; i < to_submit.size(); i++) {
        peer.getManager()
            .getAdapter()
            .enqueueReadRequest(peer, (DiskManagerReadRequest) to_submit.get(i), read_req_listener);
      }
    }
  }
Beispiel #6
0
  public String[] getPersistentStringListProperty(String prop) {
    synchronized (persistent_properties) {
      try {
        List<byte[]> values = (List<byte[]>) persistent_properties.get(prop);

        if (values == null) {

          return (new String[0]);
        }

        String[] res = new String[values.size()];

        int pos = 0;

        for (byte[] value : values) {

          res[pos++] = new String(value, "UTF-8");
        }

        return (res);

      } catch (Throwable e) {

        Debug.printStackTrace(e);

        return (new String[0]);
      }
    }
  }
  public static void addPlatformHost(String host) {
    List platformHosts = getPlatformHosts();
    host = host.toLowerCase();

    if (!platformHosts.contains(host)) {
      platformHosts.add(host);
      mapPlatformTrackerTorrents.clear();
    }
  }
  protected void report(String resource_key, String additional_text) {
    if (progress_listeners.size() > 0) {

      String prefix = MessageText.getString(resource_key);

      for (int i = 0; i < progress_listeners.size(); i++) {

        ((TOTorrentProgressListener) progress_listeners.get(i))
            .reportCurrentTask(prefix + (additional_text == null ? "" : additional_text));
      }
    }
  }
    private void setTotals(int day, long[] totals) {
      List<Long> records = new ArrayList<Long>();

      for (Long l : totals) {

        records.add(l);
      }

      getContents().put(String.valueOf(day), records);

      dirty = true;
    }
Beispiel #10
0
  protected static void generateEvidence(IndentWriter writer) {
    writer.println(file_map.size() + " FMFile Reservations");

    try {
      writer.indent();

      try {
        file_map_mon.enter();

        Iterator it = file_map.keySet().iterator();

        while (it.hasNext()) {

          String key = (String) it.next();

          List owners = (List) file_map.get(key);

          Iterator it2 = owners.iterator();

          String str = "";

          while (it2.hasNext()) {

            Object[] entry = (Object[]) it2.next();

            FMFileOwner owner = (FMFileOwner) entry[0];
            Boolean write = (Boolean) entry[1];
            String reason = (String) entry[2];

            str +=
                (str.length() == 0 ? "" : ", ")
                    + owner.getName()
                    + "["
                    + (write.booleanValue() ? "write" : "read")
                    + "/"
                    + reason
                    + "]";
          }

          writer.println(Debug.secretFileName(key) + " -> " + str);
        }
      } finally {

        file_map_mon.exit();
      }

      FMFileManagerImpl.generateEvidence(writer);

    } finally {

      writer.exdent();
    }
  }
  public void save(String filename) {
    if (propertiesMap == null) {

      // nothing to save, initialisation not complete

      return;
    }

    /**
     * Note - propertiesMap isn't synchronised! We'll clone the map now, because we need to modify
     * it. The BEncoding code will create a new map object (TreeMap) because it needs to be sorted,
     * so we might as well do it here too.
     */
    TreeMap<String, Object> properties_clone = propertiesMap.toTreeMap();

    // Remove any transient parameters.
    if (!this.transient_properties.isEmpty()) {
      properties_clone.keySet().removeAll(this.transient_properties);
    }

    FileUtil.writeResilientConfigFile(filename, properties_clone);

    List<COConfigurationListener> listeners_copy;

    synchronized (listenerz) {
      listeners_copy = new ArrayList<COConfigurationListener>(listenerz);
    }

    for (int i = 0; i < listeners_copy.size(); i++) {

      COConfigurationListener l = (COConfigurationListener) listeners_copy.get(i);

      if (l != null) {

        try {
          l.configurationSaved();

        } catch (Throwable e) {

          Debug.printStackTrace(e);
        }
      } else {

        Debug.out("COConfigurationListener is null");
      }
    }

    if (exported_parameters_dirty) {

      exportParameters();
    }
  }
Beispiel #12
0
  public void pieceHashed(int piece_number) {
    for (int i = 0; i < progress_listeners.size(); i++) {

      int this_progress = (int) ((piece_number * 100) / piece_count);

      if (this_progress != reported_progress) {

        reported_progress = this_progress;

        ((TOTorrentProgressListener) progress_listeners.get(i)).reportProgress(reported_progress);
      }
    }
  }
Beispiel #13
0
  private void reserveFile() throws FMFileManagerException {

    if (clone) {

      return;
    }

    try {
      file_map_mon.enter();

      // System.out.println( "FMFile::reserveFile:" + canonical_path + "("+ owner.getName() + ")" +
      // " - " + Debug.getCompressedStackTrace() );

      List owners = (List) file_map.get(canonical_path);

      if (owners == null) {

        owners = new ArrayList();

        // System.out.println( "    creating new owners entr" );

        file_map.put(canonical_path, owners);
      }

      for (Iterator it = owners.iterator(); it.hasNext(); ) {

        Object[] entry = (Object[]) it.next();

        String entry_name = ((FMFileOwner) entry[0]).getName();

        // System.out.println( "    existing entry: " + entry_name );

        if (owner.getName().equals(entry_name)) {

          // already present, start off read-access

          Debug.out("reserve file - entry already present");

          entry[1] = new Boolean(false);

          return;
        }
      }

      owners.add(new Object[] {owner, new Boolean(false), "<reservation>"});

    } finally {

      file_map_mon.exit();
    }
  }
  public boolean setParameter(String parameter, StringList value) {
    try {
      List encoded = new ArrayList();

      List l = ((StringListImpl) value).getList();

      for (int i = 0; i < l.size(); i++) {

        encoded.add(stringToBytes((String) l.get(i)));
      }
      propertiesMap.put(parameter, encoded);
      notifyParameterListeners(parameter);
    } catch (Exception e) {
      Debug.printStackTrace(e);
      return false;
    }
    return true;
  }
Beispiel #15
0
  static {
    try {
      device_renames.add(Pattern.compile("TV\\s*+\\(([^\\)]*)\\)", Pattern.CASE_INSENSITIVE));

    } catch (Throwable e) {

      Debug.out(e);
    }
  }
    private void setTotals(int day, long start_offset, long[] totals) {
      if (start_offset == 0) {

        setTotals(day, totals);

      } else {

        List<Long> records = new ArrayList<Long>();

        for (Long l : totals) {

          records.add(l);
        }

        getContents().put(day + "." + start_offset, records);

        dirty = true;
      }
    }
  protected void closedownComplete() {
    Iterator it = listeners.iterator();

    while (it.hasNext()) {

      try {
        ((PluginListener) it.next()).closedownComplete();

      } catch (Throwable e) {

        Debug.printStackTrace(e);
      }
    }

    for (int i = 0; i < children.size(); i++) {

      ((PluginInterfaceImpl) children.get(i)).closedownComplete();
    }
  }
  protected void initialisationComplete() {
    Iterator<PluginListener> it = listeners.iterator();

    while (it.hasNext()) {

      try {
        fireInitComplete(it.next());

      } catch (Throwable e) {

        Debug.printStackTrace(e);
      }
    }

    for (int i = 0; i < children.size(); i++) {

      ((PluginInterfaceImpl) children.get(i)).initialisationComplete();
    }
  }
Beispiel #19
0
  protected void addDP(List<String[]> dp, String name, String[] values) {
    String value = "";

    for (String v : values) {

      value += (value.length() == 0 ? "" : ",") + v;
    }

    dp.add(new String[] {name, value});
  }
Beispiel #20
0
  public String[][] getDisplayProperties() {
    List<String[]> dp = new ArrayList<String[]>();

    getDisplayProperties(dp);

    String[][] res = new String[2][dp.size()];

    int pos = 0;

    for (String[] entry : dp) {

      res[0][pos] = entry[0];
      res[1][pos] = entry[1];

      pos++;
    }

    return (res);
  }
  public String[] getRecommendedPlugins() {
    Map reply = getVersionCheckInfo(REASON_RECOMMENDED_PLUGINS, AT_EITHER);

    List l = (List) reply.get("recommended_plugins");

    if (l == null) {

      return (new String[0]);
    }

    String[] res = new String[l.size()];

    for (int i = 0; i < l.size(); i++) {

      res[i] = new String((byte[]) l.get(i));
    }

    return (res);
  }
    private long[] getTotals(int day) {
      List<Long> records = getContents().get(String.valueOf(day));

      if (records != null) {

        long[] result = new long[STAT_ENTRY_COUNT];

        if (records.size() == STAT_ENTRY_COUNT) {

          for (int i = 0; i < STAT_ENTRY_COUNT; i++) {

            result[i] = (Long) records.get(i);
          }
        }

        return (result);
      }

      return (null);
    }
Beispiel #23
0
  private TranscodeProfile[] getTranscodeProfiles(String classification) {
    List<TranscodeProfile> profiles = new ArrayList<TranscodeProfile>();

    DeviceManagerImpl dm = getManager();

    TranscodeProvider[] providers = dm.getProviders();

    for (TranscodeProvider provider : providers) {

      TranscodeProfile[] ps = provider.getProfiles(classification);

      if (providers.length == 1) {

        return (ps);
      }

      profiles.addAll(Arrays.asList(ps));
    }

    return (profiles.toArray(new TranscodeProfile[profiles.size()]));
  }
  protected void firePluginEventSupport(PluginEvent event) {
    Iterator<PluginEventListener> it = event_listeners.iterator();

    while (it.hasNext()) {

      try {
        PluginEventListener listener = it.next();

        listener.handleEvent(event);

      } catch (Throwable e) {

        Debug.printStackTrace(e);
      }
    }

    for (int i = 0; i < children.size(); i++) {

      ((PluginInterfaceImpl) children.get(i)).firePluginEvent(event);
    }
  }
  public void addAndFireListener(COConfigurationListener listener) {
    synchronized (listenerz) {
      listenerz.add(listener);
    }

    try {
      listener.configurationSaved();

    } catch (Throwable e) {

      Debug.printStackTrace(e);
    }
  }
Beispiel #26
0
  private void releaseFile() {
    if (clone) {

      return;
    }

    try {
      file_map_mon.enter();

      // System.out.println( "FMFile::releaseFile:" + canonical_path + "("+ owner.getName() + ")" +
      // " - " + Debug.getCompressedStackTrace());

      List owners = (List) file_map.get(canonical_path);

      if (owners != null) {

        for (Iterator it = owners.iterator(); it.hasNext(); ) {

          Object[] entry = (Object[]) it.next();

          if (owner.getName().equals(((FMFileOwner) entry[0]).getName())) {

            it.remove();

            break;
          }
        }

        if (owners.size() == 0) {

          file_map.remove(canonical_path);
        }
      }
    } finally {

      file_map_mon.exit();
    }
  }
Beispiel #27
0
  public TranscodeFileImpl[] getFiles() {
    try {
      synchronized (this) {
        if (device_files == null) {

          loadDeviceFile();
        }

        List<TranscodeFile> result = new ArrayList<TranscodeFile>();

        Iterator<Map.Entry<String, Map<String, ?>>> it = device_files.entrySet().iterator();

        while (it.hasNext()) {

          Map.Entry<String, Map<String, ?>> entry = it.next();

          try {
            TranscodeFileImpl tf = new TranscodeFileImpl(this, entry.getKey(), device_files);

            result.add(tf);

          } catch (Throwable e) {

            it.remove();

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

        return (result.toArray(new TranscodeFileImpl[result.size()]));
      }
    } catch (Throwable e) {

      Debug.out(e);

      return (new TranscodeFileImpl[0]);
    }
  }
  public void addVersionCheckClientListener(
      boolean triggerStartListener, VersionCheckClientListener l) {
    synchronized (listeners) {
      listeners.add(l);

      if (triggerStartListener && startCheckRan) {
        try {
          l.versionCheckStarted(REASON_UPDATE_CHECK_START);
        } catch (Exception e) {
          Debug.out(e);
        }
      }
    }
  }
  public ResourceDownloaderAlternateImpl(
      ResourceDownloaderBaseImpl _parent,
      ResourceDownloader[] _delegates,
      int _max_to_try,
      boolean _random) {
    super(_parent);

    delegates = _delegates;
    max_to_try = _max_to_try;
    random = _random;

    for (int i = 0; i < delegates.length; i++) {

      ((ResourceDownloaderBaseImpl) delegates[i]).setParent(this);
    }

    if (max_to_try < 0) {

      max_to_try = delegates.length;

    } else {

      max_to_try = Math.min(max_to_try, delegates.length);
    }

    if (random) {

      List l = new ArrayList(Arrays.asList(delegates));

      delegates = new ResourceDownloader[delegates.length];

      for (int i = 0; i < delegates.length; i++) {

        delegates[i] = (ResourceDownloader) l.remove((int) (Math.random() * l.size()));
      }
    }
  }
  public PluginInterface getLocalPluginInterface(Class plugin_class, String id)
      throws PluginException {
    try {
      Plugin p = (Plugin) plugin_class.newInstance();

      // Discard plugin.id from the properties, we want the
      // plugin ID we create to take priority - not a value
      // from the original plugin ID properties file.
      Properties local_props = new Properties(props);
      local_props.remove("plugin.id");

      if (id.endsWith("_v")) {

        throw (new Exception("Verified plugins must be loaded from a jar"));
      }

      PluginInterfaceImpl pi =
          new PluginInterfaceImpl(
              p,
              initialiser,
              initialiser_key,
              class_loader,
              null,
              key + "." + id,
              local_props,
              pluginDir,
              getPluginID() + "." + id,
              plugin_version);

      initialiser.fireCreated(pi);

      p.initialize(pi);

      children.add(pi);

      return (pi);

    } catch (Throwable e) {

      if (e instanceof PluginException) {

        throw ((PluginException) e);
      }

      throw (new PluginException("Local initialisation fails", e));
    }
  }