public void awaitDownload()

in common/src/main/java/jetbrains/buildServer/torrent/torrent/TorrentDownloader.java [82:119]


  public void awaitDownload() throws InterruptedException, DownloadException {

    //wait setup connection with peers
    if (mySemaphore.tryAcquire(myTimeoutForFindingPeers, TimeUnit.MILLISECONDS)) {
      //download was finished in this timeout
      return;
    }

    int downloadedPieces = myDownloadedPiecesCount.get();
    while (true) {
      int connectedPeers = myConnectedPeersCount.get();
      boolean allPiecesReceived = myReceivedPiecesCount.get() == myTorrentMetadata.getPiecesCount();
      if (connectedPeers < myMinPeersCount && !allPiecesReceived) {
        throw new DownloadException("Need " + myMinPeersCount +
                " peers but right now only " + connectedPeers + " are connected");
      }
      if (mySemaphore.tryAcquire(myIdleTimeout, TimeUnit.MILLISECONDS)) {
        return;
      }
      int newDownloadedPieces = myDownloadedPiecesCount.get();
      if (newDownloadedPieces <= downloadedPieces) {
        //no pieces were downloaded
        throw new DownloadException(String.format(
                "No pieces were downloaded in %dms. Downloaded pieces %d/%d, connected peers %d",
                myIdleTimeout,
                downloadedPieces,
                myTorrentMetadata.getPiecesCount(),
                connectedPeers));
      }
      Throwable failedException = myFailedExceptionHolder.get();
      if (failedException != null) {
        throw new DownloadException("Downloading was failed, problem: " + failedException.getMessage(),
                failedException);
      }
      downloadedPieces = newDownloadedPieces;
      //continue waiting
    }
  }