Пример #1
0
  public void run() {
    // XXX - Support other IPs
    String announce = meta.getAnnounce();
    String infoHash = urlencode(meta.getInfoHash());
    String peerID = urlencode(coordinator.getID());

    long uploaded = coordinator.getUploaded();
    long downloaded = coordinator.getDownloaded();
    long left = coordinator.getLeft();

    boolean completed = (left == 0);

    try {
      boolean started = false;
      while (!started) {
        try {
          // Send start.
          TrackerInfo info =
              doRequest(announce, infoHash, peerID, uploaded, downloaded, left, STARTED_EVENT);
          Iterator it = info.getPeers().iterator();
          while (it.hasNext()) coordinator.addPeer((Peer) it.next());
          started = true;
        } catch (IOException ioe) {
          // Probably not fatal (if it doesn't last to long...)
          Snark.debug(
              "WARNING: Could not contact tracker at '" + announce + "': " + ioe, Snark.WARNING);
        }

        if (!started && !stop) {
          Snark.debug("         Retrying in one minute...", Snark.DEBUG);
          try {
            // Sleep one minutes...
            Thread.sleep(60 * 1000);
          } catch (InterruptedException interrupt) {
            // ignore
          }
        }
      }

      while (!stop) {
        try {
          // Sleep some minutes...
          Thread.sleep(SLEEP * 60 * 1000);
        } catch (InterruptedException interrupt) {
          // ignore
        }

        if (stop) break;

        uploaded = coordinator.getUploaded();
        downloaded = coordinator.getDownloaded();
        left = coordinator.getLeft();

        // First time we got a complete download?
        String event;
        if (!completed && left == 0) {
          completed = true;
          event = COMPLETED_EVENT;
        } else event = NO_EVENT;

        // Only do a request when necessary.
        if (event == COMPLETED_EVENT
            || coordinator.needPeers()
            || System.currentTimeMillis() > lastRequestTime + interval) {
          try {
            TrackerInfo info =
                doRequest(announce, infoHash, peerID, uploaded, downloaded, left, event);

            Iterator it = info.getPeers().iterator();
            while (it.hasNext()) coordinator.addPeer((Peer) it.next());
          } catch (IOException ioe) {
            // Probably not fatal (if it doesn't last to long...)
            Snark.debug(
                "WARNING: Could not contact tracker at '" + announce + "': " + ioe, Snark.WARNING);
          }
        }
      }
    } catch (Throwable t) {
      Snark.debug("TrackerClient: " + t, Snark.ERROR);
      t.printStackTrace();
    } finally {
      try {
        TrackerInfo info =
            doRequest(announce, infoHash, peerID, uploaded, downloaded, left, STOPPED_EVENT);
      } catch (IOException ioe) {
        /* ignored */
      }
    }
  }
Пример #2
0
 /**
  * Получить ReferenceType.name
  *
  * @param e класс справочника
  * @return ReferenceType.name
  */
 public static <E extends Enum> String name(Class<E> e) {
   return MetaInfo.getMetaInfo(e).getName();
 }