Exemplo n.º 1
0
  private TrackerInfo doRequest(
      String announce,
      String infoHash,
      String peerID,
      long uploaded,
      long downloaded,
      long left,
      String event)
      throws IOException {
    String s =
        announce
            + "?info_hash="
            + infoHash
            + "&peer_id="
            + peerID
            + "&port="
            + port
            + "&uploaded="
            + uploaded
            + "&downloaded="
            + downloaded
            + "&left="
            + left
            + ((event != NO_EVENT) ? ("&event=" + event) : "");
    URL u = new URL(s);
    if (Snark.debug >= Snark.INFO) Snark.debug("Sending TrackerClient request: " + u, Snark.INFO);

    URLConnection c = u.openConnection();
    c.connect();
    InputStream in = c.getInputStream();

    if (c instanceof HttpURLConnection) {
      // Check whether the page exists
      int code = ((HttpURLConnection) c).getResponseCode();
      if (code / 100 != 2) // We can only handle 200 OK responses
      throw new IOException(
            "Loading '" + s + "' gave error code " + code + ", it probably doesn't exists");
    }

    TrackerInfo info = new TrackerInfo(in, coordinator.getID(), coordinator.getMetaInfo());
    if (Snark.debug >= Snark.INFO) Snark.debug("TrackerClient response: " + info, Snark.INFO);
    lastRequestTime = System.currentTimeMillis();

    String failure = info.getFailureReason();
    if (failure != null) throw new IOException(failure);

    interval = info.getInterval() * 1000;
    return info;
  }