Search in sources :

Example 1 with TorrentMetadata

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

the class TorrentArtifactCacheListener method onAfterAddOrUpdate.

public void onAfterAddOrUpdate(@NotNull File file) {
    if (isTorrentFile(file))
        return;
    if (isTeamcityIVYFile(file))
        return;
    final String absolutePath = file.getAbsolutePath();
    if (!myTorrentsManager.isTransportEnabled()) {
        LOG.debug("Torrent plugin disabled. Won't seed " + absolutePath);
        return;
    }
    if (!TorrentUtil.shouldCreateTorrentFor(file.length(), myConfiguration)) {
        LOG.debug("Won't create torrent for " + absolutePath + ". Artifact is too small: " + file.length());
        return;
    }
    String announceUrl = myConfiguration.getAnnounceUrl();
    if (announceUrl == null)
        return;
    TorrentMetadata metadata;
    try {
        metadata = TorrentCreator.create(file, URI.create(announceUrl), "TeamCity Torrent Plugin");
    } catch (Exception e) {
        return;
    }
    if (myTorrentsSeeder.getClient().isSeeding(metadata)) {
        LOG.debug("Already seeding " + absolutePath);
        return;
    }
    File cacheCurrentBuildDir;
    try {
        cacheCurrentBuildDir = getCurrentBuildFolderCache();
    } catch (NoRunningBuildException e) {
        logWarningThatCacheCurrentBuildNotFound(absolutePath);
        return;
    }
    if (cacheCurrentBuildDir == null) {
        logWarningThatCacheCurrentBuildNotFound(absolutePath);
        return;
    }
    if (!isChildFile(cacheCurrentBuildDir, file)) {
        // reference to parent in relative path means that it is artifact from other build. We can skip it
        return;
    }
    File createdTorrentFile = publishTorrentFileAndStartSeeding(file);
    if (createdTorrentFile == null) {
        return;
    }
    createTorrentFileCopyAndAddAsArtifact(createdTorrentFile, file, cacheCurrentBuildDir);
}
Also used : NoRunningBuildException(jetbrains.buildServer.agent.NoRunningBuildException) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata) IOException(java.io.IOException) NoRunningBuildException(jetbrains.buildServer.agent.NoRunningBuildException)

Example 2 with TorrentMetadata

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

the class TorrentFilesFactoryImpl method createTorrentFile.

@Nullable
@Override
public File createTorrentFile(@NotNull File srcFile) {
    final String announceUrl = myConfiguration.getAnnounceUrl();
    if (announceUrl == null)
        return null;
    try {
        File torrentFile = getTorrentFile();
        TorrentMetadata torrent = TorrentCreator.create(srcFile, URI.create(announceUrl), "TeamCity Torrent Plugin");
        TorrentUtil.saveTorrentToFile(torrent, torrentFile);
        return torrentFile;
    } catch (Exception e) {
        LOG.warnAndDebugDetails("Failed to create torrent for source file: " + srcFile.getAbsolutePath(), e);
    }
    return null;
}
Also used : File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with TorrentMetadata

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

the class AgentTorrentsManagerTest method testAnnounceAllOnAgentStarted.

public void testAnnounceAllOnAgentStarted() throws IOException, URISyntaxException, InterruptedException, NoSuchAlgorithmException {
    Tracker tracker = new Tracker(6969);
    try {
        final List<String> torrentHashes = new ArrayList<String>();
        final List<File> createdFiles = new ArrayList<File>();
        final int torrentsCount = 10;
        tracker.start(true);
        for (int i = 0; i < torrentsCount; i++) {
            final File artifactFile = createTempFile(65535);
            createdFiles.add(artifactFile);
            File torrentDir = createTempDir();
            final TorrentMetadata torrent = TorrentCreator.create(artifactFile, tracker.getAnnounceURI(), "tc-plugin-test");
            final File torrentFile = new File(torrentDir, artifactFile.getName() + ".torrent");
            torrentHashes.add(torrent.getHexInfoHash());
            TorrentUtil.saveTorrentToFile(torrent, torrentFile);
            myTorrentsManager.getTorrentsSeeder().registerSrcAndTorrentFile(artifactFile, torrentFile, false);
        }
        Mock buildAgentMock = mock(BuildAgent.class);
        myTorrentsManager.agentStarted((BuildAgent) buildAgentMock.proxy());
        new WaitFor(3 * 1000) {

            @Override
            protected boolean condition() {
                return myTorrentsManager.getTorrentsSeeder().getNumberOfSeededTorrents() == torrentsCount;
            }
        };
        List<String> seededHashes = new ArrayList<String>();
        List<File> seededFiles = new ArrayList<File>();
        List<LoadedTorrent> loadedTorrents = myTorrentsManager.getTorrentsSeeder().getClient().getLoadedTorrents();
        for (int i = 0; i < loadedTorrents.size(); i++) {
            LoadedTorrent st = loadedTorrents.get(i);
            File artifact = createdFiles.get(i);
            seededHashes.add(st.getTorrentHash().getHexInfoHash());
            TorrentMetadata metadata = st.getMetadata();
            seededFiles.add(new File(artifact.getParentFile(), metadata.getFiles().get(0).getRelativePathAsString()));
        }
        assertSameElements(torrentHashes, seededHashes);
        assertSameElements(createdFiles, seededFiles);
    } finally {
        myTorrentsManager.agentShutdown();
        tracker.stop();
    }
}
Also used : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) Tracker(com.turn.ttorrent.tracker.Tracker) WaitFor(jetbrains.buildServer.util.WaitFor) ArrayList(java.util.ArrayList) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata) Mock(org.jmock.Mock)

Example 4 with TorrentMetadata

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

the class TorrentArtifactCacheListenerTest method test_seed_when_file_appear.

public void test_seed_when_file_appear() throws IOException, NoSuchAlgorithmException {
    File file = createTempFile(1024 * 1025);
    myCacheListener.onAfterAddOrUpdate(file);
    waitForSeededTorrents(1);
    assertEquals(1, mySeeder.getNumberOfSeededTorrents());
    final LoadedTorrent loadedTorrent = mySeeder.getTorrentsSeeder().getClient().getLoadedTorrents().iterator().next();
    TorrentMetadata metadata = loadedTorrent.getMetadata();
    assertEquals(file.getAbsolutePath(), file.getParent() + File.separatorChar + metadata.getFiles().get(0).getRelativePathAsString());
}
Also used : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata)

Example 5 with TorrentMetadata

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

the class TorrentArtifactCacheListenerTest method test_stop_seed_when_delete.

public void test_stop_seed_when_delete() throws IOException, NoSuchAlgorithmException {
    File file = createTempFile(1024 * 1025);
    myCacheListener.onAfterAddOrUpdate(file);
    waitForSeededTorrents(1);
    final LoadedTorrent loadedTorrentTorrent = mySeeder.getTorrentsSeeder().getClient().getLoadedTorrents().iterator().next();
    TorrentMetadata metadata = loadedTorrentTorrent.getMetadata();
    assertEquals(file.getAbsolutePath(), file.getParent() + File.separatorChar + metadata.getFiles().get(0).getRelativePathAsString());
    myCacheListener.onBeforeDelete(file);
    assertEquals(0, mySeeder.getNumberOfSeededTorrents());
}
Also used : LoadedTorrent(com.turn.ttorrent.client.LoadedTorrent) File(java.io.File) TorrentMetadata(com.turn.ttorrent.common.TorrentMetadata)

Aggregations

TorrentMetadata (com.turn.ttorrent.common.TorrentMetadata)12 File (java.io.File)9 IOException (java.io.IOException)6 LoadedTorrent (com.turn.ttorrent.client.LoadedTorrent)4 Tracker (com.turn.ttorrent.tracker.Tracker)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 CommunicationManager (com.turn.ttorrent.client.CommunicationManager)2 WaitFor (com.intellij.util.WaitFor)1 FileCollectionStorage (com.turn.ttorrent.client.storage.FileCollectionStorage)1 PieceStorage (com.turn.ttorrent.client.storage.PieceStorage)1 FileNotFoundException (java.io.FileNotFoundException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 NoRunningBuildException (jetbrains.buildServer.agent.NoRunningBuildException)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