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()));
      }
    }
  }
  protected long allocateConnectionId(String client_address) {
    try {
      random_mon.enter();

      long id = random.nextLong();

      Long new_key = new Long(id);

      connectionData new_data = new connectionData(client_address, id);

      // check for timeouts

      if (new_data.getTime() - last_timeout_check > 500) {

        last_timeout_check = new_data.getTime();

        Iterator<Long> it = connection_id_map.keySet().iterator();

        while (it.hasNext()) {

          Long key = it.next();

          connectionData data = connection_id_map.get(key);

          if (new_data.getTime() - data.getTime() > CONNECTION_ID_LIFETIME) {

            // System.out.println( "TRTrackerServerProcessorUDP: connection id timeout" );

            it.remove();

            List<connectionData> cds = connection_ip_map.get(client_address);

            if (cds != null) {

              Iterator<connectionData> it2 = cds.iterator();

              while (it2.hasNext()) {

                if (it2.next().getID() == key) {

                  it2.remove();

                  break;
                }
              }

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

                connection_ip_map.remove(client_address);
              }
            }

          } else {
            // insertion order into map is time based - LinkedHashMap returns keys in same order

            break;
          }
        }
      }

      List<connectionData> cds = connection_ip_map.get(client_address);

      if (cds == null) {

        cds = new ArrayList<connectionData>();

        connection_ip_map.put(client_address, cds);
      }

      cds.add(new_data);

      if (cds.size() > 512) {

        connectionData dead = cds.remove(0);

        connection_id_map.remove(dead.getID());
      }

      connection_id_map.put(new_key, new_data);

      // System.out.println( "TRTrackerServerProcessorUDP: allocated:" + id + ", connection id map
      // size = " + connection_id_map.size());

      return (id);

    } finally {

      random_mon.exit();
    }
  }
 public void removeListener(COConfigurationListener listener) {
   synchronized (listenerz) {
     listenerz.remove(listener);
   }
 }
 protected void removeListener(TOTorrentProgressListener listener) {
   progress_listeners.remove(listener);
 }
 public void removeVersionCheckClientListener(VersionCheckClientListener l) {
   synchronized (listeners) {
     listeners.remove(l);
   }
 }