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);
}
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;
}
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();
}
}
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());
}
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());
}
Aggregations