Search in sources :

Example 6 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentTransportTest method testInterrupt.

public void testInterrupt() throws IOException, InterruptedException, NoSuchAlgorithmException {
    setTorrentTransportEnabled();
    setDownloadHonestly(true);
    final File storageDir = new File(myTempDir, "storageDir");
    storageDir.mkdir();
    final File downloadDir = new File(myTempDir, "downloadDir");
    downloadDir.mkdir();
    final File torrentsDir = new File(myTempDir, "torrentsDir");
    torrentsDir.mkdir();
    final String fileName = "MyBuild.31.zip";
    final File artifactFile = new File(storageDir, fileName);
    createTempFile(25 * 1024 * 1025).renameTo(artifactFile);
    final File teamcityIvyFile = new File("agent/tests/resources/" + Constants.TEAMCITY_IVY);
    myDownloadMap.put("/" + Constants.TEAMCITY_IVY, teamcityIvyFile);
    final String ivyUrl = SERVER_PATH + Constants.TEAMCITY_IVY;
    final File ivyFile = new File(myTempDir, Constants.TEAMCITY_IVY);
    myTorrentTransport.downloadUrlTo(ivyUrl, ivyFile);
    Tracker tracker = new Tracker(6969);
    List<Client> clientList = new ArrayList<Client>();
    for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
        final ExecutorService es = Executors.newFixedThreadPool(2);
        clientList.add(createClientWithClosingExecutorServiceOnStop());
    }
    try {
        tracker.start(true);
        mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
        final Torrent torrent = TorrentCreator.create(artifactFile, tracker.getAnnounceURI(), "testplugin");
        final File torrentFile = new File(torrentsDir, fileName + ".torrent");
        TorrentUtil.saveTorrentToFile(torrent, torrentFile);
        myDownloadMap.put("/.teamcity/torrents/" + fileName + ".torrent", torrentFile);
        for (Client client : clientList) {
            client.start(InetAddress.getLocalHost());
            client.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath(), true, false);
        }
        final File targetFile = new File(downloadDir, fileName);
        new Thread() {

            @Override
            public void run() {
                try {
                    sleep(200);
                    myTorrentTransport.interrupt();
                } catch (InterruptedException e) {
                    fail("Must not fail here: " + e);
                }
            }
        }.start();
        String digest = null;
        try {
            digest = myTorrentTransport.downloadUrlTo(SERVER_PATH + fileName, targetFile);
        } catch (IOException ex) {
            assertNull(digest);
            assertTrue(ex.getCause() instanceof InterruptedException);
        }
        assertFalse(targetFile.exists());
    } finally {
        for (Client client : clientList) {
            client.stop();
        }
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) Torrent(com.turn.ttorrent.common.Torrent) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ExecutorService(java.util.concurrent.ExecutorService) Client(com.turn.ttorrent.client.Client) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File)

Example 7 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TeamcityTorrentClient method stopSeeding.

public void stopSeeding(@NotNull File torrentFile) {
    try {
        Torrent t = loadTorrent(torrentFile);
        myClient.removeTorrent(t);
    } catch (IOException e) {
        LOG.warn(e.toString());
    } catch (NoSuchAlgorithmException e) {
        LOG.warn(e.toString());
    }
}
Also used : Torrent(com.turn.ttorrent.common.Torrent) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) SharedTorrent(com.turn.ttorrent.client.SharedTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 8 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentUtil method createTorrent.

/**
 * Creates the torrent file for the specified <code>srcFile</code> and announce URI.
 */
@Nullable
public static Torrent createTorrent(@NotNull File srcFile, @NotNull File torrentFile, @NotNull URI announceURI) {
    setHashingThreadsCount();
    try {
        Torrent t = TorrentCreator.create(srcFile, announceURI, "TeamCity");
        saveTorrentToFile(t, torrentFile);
        return t;
    } catch (Exception e) {
        LOG.warnAndDebugDetails(String.format("Unable to create torrent file from %s: %s", srcFile.getPath(), e.toString()), e);
    }
    return null;
}
Also used : Torrent(com.turn.ttorrent.common.Torrent) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentUtil method getOrCreateTorrent.

/**
 * Creates the torrent file for the specified <code>srcFile</code> and announce URI.
 * If such torrent already exists, loads and returns it.
 */
@Nullable
public static File getOrCreateTorrent(@NotNull final File srcFile, @NotNull final String relativePath, @NotNull final File torrentsStore, @NotNull final URI announceURI) {
    setHashingThreadsCount();
    File torrentFile = new File(torrentsStore, relativePath + TORRENT_FILE_SUFFIX);
    if (torrentFile.isFile()) {
        try {
            Torrent t = loadTorrent(torrentFile);
            for (List<String> uris : t.getAnnounceList()) {
                if (uris.contains(announceURI.toString()))
                    return torrentFile;
            }
        } catch (IOException e) {
            LOG.warn("Failed to load existing torrent file: " + torrentFile.getAbsolutePath() + ", error: " + e.toString() + ". Will create new torrent file instead.");
        }
    }
    final Torrent torrent = createTorrent(srcFile, torrentFile, announceURI);
    return torrent != null ? torrentFile : null;
}
Also used : Torrent(com.turn.ttorrent.common.Torrent) IOException(java.io.IOException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with Torrent

use of com.turn.ttorrent.common.Torrent in project teamcity-torrent-plugin by JetBrains.

the class TorrentTransportTest method testDownloadAndSeed.

public void testDownloadAndSeed() throws IOException, NoSuchAlgorithmException, InterruptedException {
    setTorrentTransportEnabled();
    setDownloadHonestly(true);
    final File storageDir = new File(myTempDir, "storageDir");
    storageDir.mkdir();
    final File downloadDir = new File(myTempDir, "downloadDir");
    downloadDir.mkdir();
    final File torrentsDir = new File(myTempDir, "torrentsDir");
    torrentsDir.mkdir();
    final String fileName = "MyBuild.31.zip";
    final File artifactFile = new File(storageDir, fileName);
    createTempFile(20250).renameTo(artifactFile);
    final File teamcityIvyFile = new File("agent/tests/resources/" + Constants.TEAMCITY_IVY);
    myDownloadMap.put("/" + Constants.TEAMCITY_IVY, teamcityIvyFile);
    final String ivyUrl = SERVER_PATH + Constants.TEAMCITY_IVY;
    final File ivyFile = new File(myTempDir, Constants.TEAMCITY_IVY);
    myTorrentTransport.downloadUrlTo(ivyUrl, ivyFile);
    Tracker tracker = new Tracker(6969);
    List<Client> clientList = new ArrayList<Client>();
    for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
        clientList.add(createClientWithClosingExecutorServiceOnStop());
    }
    try {
        tracker.start(true);
        mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
        final Torrent torrent = TorrentCreator.create(artifactFile, tracker.getAnnounceURI(), "testplugin");
        final File torrentFile = new File(torrentsDir, fileName + ".torrent");
        TorrentUtil.saveTorrentToFile(torrent, torrentFile);
        myDownloadMap.put("/.teamcity/torrents/" + fileName + ".torrent", torrentFile);
        for (Client client : clientList) {
            client.start(InetAddress.getLocalHost());
            client.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath(), true, false);
        }
        final File targetFile = new File(downloadDir, fileName);
        final String digest = myTorrentTransport.downloadUrlTo(SERVER_PATH + fileName, targetFile);
        assertNotNull(digest);
        assertTrue(FileUtils.contentEquals(artifactFile, targetFile));
    } finally {
        for (Client client : clientList) {
            client.stop();
        }
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) Torrent(com.turn.ttorrent.common.Torrent) ArrayList(java.util.ArrayList) Client(com.turn.ttorrent.client.Client) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File)

Aggregations

Torrent (com.turn.ttorrent.common.Torrent)11 File (java.io.File)8 AnnounceableTorrent (com.turn.ttorrent.common.AnnounceableTorrent)5 IOException (java.io.IOException)5 AnnounceableFileTorrent (com.turn.ttorrent.common.AnnounceableFileTorrent)4 Tracker (com.turn.ttorrent.tracker.Tracker)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 Client (com.turn.ttorrent.client.Client)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 HttpClient (org.apache.commons.httpclient.HttpClient)2 WaitFor (com.intellij.util.WaitFor)1 SharedTorrent (com.turn.ttorrent.client.SharedTorrent)1 ExecutorService (java.util.concurrent.ExecutorService)1 BuildArtifact (jetbrains.buildServer.serverSide.artifacts.BuildArtifact)1 WaitFor (jetbrains.buildServer.util.WaitFor)1 Mock (org.jmock.Mock)1 Constraint (org.jmock.core.Constraint)1