Exemplo n.º 1
0
  /** Close torrent and set final client state before signing off. */
  private void finish() {

    this.announce.stop();

    for (SharedTorrent torrent : this.torrents.values()) {
      torrent.close();
      if (torrent.isFinished()) {
        torrent.setClientState(ClientState.DONE);
      } else {
        torrent.setClientState(ClientState.ERROR);
      }
    }

    logger.info("BitTorrent client signing off.");
  }
Exemplo n.º 2
0
 public void removeTorrent(TorrentHash torrentHash) {
   this.announce.removeTorrent(torrentHash);
   SharedTorrent torrent = this.torrents.remove(torrentHash.getHexInfoHash());
   if (torrent != null) {
     torrent.setClientState(ClientState.DONE);
     torrent.close();
   }
 }
Exemplo n.º 3
0
  public void addTorrent(SharedTorrent torrent) throws IOException, InterruptedException {
    torrent.init();
    if (!torrent.isInitialized()) {
      torrent.close();
      return;
    }

    this.torrents.put(torrent.getHexInfoHash(), torrent);

    // Initial completion test
    if (torrent.isComplete()) {
      torrent.setClientState(ClientState.SEEDING);
    } else {
      torrent.setClientState(ClientState.SHARING);
    }

    this.announce.addTorrent(torrent, this);
  }
Exemplo n.º 4
0
  /**
   * Piece download completion handler.
   *
   * <p>When a piece is completed, and valid, we announce to all connected peers that we now have
   * this piece.
   *
   * <p>We use this handler to identify when all of the pieces have been downloaded. When that's the
   * case, we can start the seeding period, if any.
   *
   * @param peer The peer we got the piece from.
   * @param piece The piece in question.
   */
  @Override
  public void handlePieceCompleted(SharingPeer peer, Piece piece) throws IOException {
    final SharedTorrent torrent = peer.getTorrent();
    synchronized (torrent) {
      if (piece.isValid()) {
        // Make sure the piece is marked as completed in the torrent
        // Note: this is required because the order the
        // PeerActivityListeners are called is not defined, and we
        // might be called before the torrent's piece completion
        // handler is.
        torrent.markCompleted(piece);
        logger.debug(
            "Completed download of {}, now has {}/{} pieces.",
            new Object[] {
              piece, torrent.getCompletedPieces().cardinality(), torrent.getPieceCount()
            });

        // Send a HAVE message to all connected peers
        PeerMessage have = PeerMessage.HaveMessage.craft(piece.getIndex());
        for (SharingPeer remote : getConnectedPeers()) {
          remote.send(have);
        }
      }

      if (torrent.isComplete()) {
        logger.info("Last piece validated and completed, " + "download is complete.");
        torrent.finish();

        try {
          this.announce
              .getCurrentTrackerClient(torrent)
              .announce(
                  TrackerMessage.AnnounceRequestMessage.RequestEvent.COMPLETED, true, torrent);
        } catch (AnnounceException ae) {
          logger.warn("Error announcing completion event to " + "tracker: {}", ae.getMessage());
        }

        torrent.setClientState(ClientState.SEEDING);
        if (seed == 0) {
          peer.unbind(false);
          this.announce.removeTorrent(torrent);
        }
      }
    }
  }