use of com.turn.ttorrent.common.TorrentMetadata 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<CommunicationManager> communicationManagers = new ArrayList<CommunicationManager>();
for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
final ExecutorService es = Executors.newFixedThreadPool(2);
communicationManagers.add(createClientWithClosingExecutorServiceOnStop());
}
try {
tracker.start(true);
mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
final TorrentMetadata 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 (CommunicationManager communicationManager : communicationManagers) {
communicationManager.start(InetAddress.getLocalHost());
communicationManager.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath());
}
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 (CommunicationManager communicationManager : communicationManagers) {
communicationManager.stop();
}
tracker.stop();
}
}
use of com.turn.ttorrent.common.TorrentMetadata in project teamcity-torrent-plugin by JetBrains.
the class ServerTorrentsSeederTest method new_file_seedeed_old_removed.
public void new_file_seedeed_old_removed() throws IOException, InterruptedException, NoSuchAlgorithmException {
System.setProperty(SeedSettings.MAX_NUMBER_OF_SEEDED_TORRENTS, "3");
System.setProperty(TorrentConfiguration.ANNOUNCE_URL, "http://localhost:6969/announce");
System.setProperty(TorrentConfiguration.FILE_SIZE_THRESHOLD, "1");
System.setProperty(TorrentConfiguration.TRACKER_ENABLED, "true");
System.setProperty(TorrentConfiguration.USER_DOWNLOAD_ENABLED, "true");
myConfigurator.getConfigurationWatcher().checkForModifications();
myDispatcher.getMulticaster().serverStartup();
final File artifactsDir = createTempDir();
final File torrentsDir = createTempDir();
final int fileSize = 1 * 1024 * 1024;
final Queue<String> filesQueue = new ArrayDeque<String>();
final Queue<String> hashesQueue = new ArrayDeque<String>();
final List<File> allArtifacts = new ArrayList<File>();
final List<File> allTorrents = new ArrayList<File>();
for (int i = 0; i < 5; i++) {
// move to artifacts dir;
final File srcFile = createTmpFileWithTS(artifactsDir, fileSize);
allArtifacts.add(srcFile);
File torrentFile = new File(torrentsDir, srcFile.getName() + ".torrent");
assertFalse(torrentFile.exists());
TorrentMetadata torrentMetaInfo = TorrentCreator.create(srcFile, URI.create(""), "");
TorrentUtil.saveTorrentToFile(torrentMetaInfo, torrentFile);
BuildArtifact buildArtifact = new DummyBuildArtifactAdapter() {
@Override
public boolean isFile() {
return true;
}
@Override
public long getSize() {
return fileSize;
}
@NotNull
@Override
public String getName() {
return srcFile.getName();
}
@NotNull
@Override
public String getRelativePath() {
return srcFile.getName();
}
};
new ArtifactProcessorImpl(torrentsDir.toPath(), artifactsDir.toPath(), myTorrentsSeeder.getTorrentsSeeder(), myConfigurator).processArtifacts(Collections.singletonList(buildArtifact));
allTorrents.add(torrentFile);
filesQueue.add(srcFile.getName());
hashesQueue.add(torrentMetaInfo.getHexInfoHash());
if (filesQueue.size() > 3) {
filesQueue.poll();
}
if (hashesQueue.size() > 3) {
hashesQueue.poll();
}
new WaitFor(5 * 1000) {
@Override
protected boolean condition() {
final Collection<LoadedTorrent> torrents = myTorrentsSeeder.getLoadedTorrents();
if (torrents.size() <= 3) {
for (LoadedTorrent torrent : torrents) {
if (torrent.getTorrentHash().getHexInfoHash().equals(torrentMetaInfo.getHexInfoHash())) {
return true;
}
}
}
return false;
}
}.assertCompleted("should have completed in 5 sec");
assertTrue(myTorrentsSeeder.getSharedTorrents().size() <= 3);
Collection<String> torrentsHashes = new ArrayList<String>();
for (LoadedTorrent torrent : myTorrentsSeeder.getLoadedTorrents()) {
torrentsHashes.add(torrent.getTorrentHash().getHexInfoHash());
}
// checking currently seeded torrents
assertEquals(filesQueue.size(), torrentsHashes.size());
assertContains(hashesQueue, torrentsHashes.toArray(new String[0]));
// checking removed ones;
assertThat(allArtifacts, new Constraint() {
public boolean eval(Object o) {
for (File artifact : (List<File>) o) {
if (!artifact.exists()) {
return false;
}
}
return true;
}
public StringBuffer describeTo(StringBuffer buffer) {
return null;
}
});
assertThat(allTorrents, new Constraint() {
public boolean eval(Object o) {
for (File link : (List<File>) o) {
if (link.exists() != filesQueue.contains(link.getName().replace(".torrent", ""))) {
return false;
}
}
return true;
}
public StringBuffer describeTo(StringBuffer buffer) {
return null;
}
});
}
assertEquals(3, myTorrentsSeeder.getNumberOfSeededTorrents());
}
use of com.turn.ttorrent.common.TorrentMetadata 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) {
File torrentFile = new File(torrentsStore, relativePath + TORRENT_FILE_SUFFIX);
if (torrentFile.isFile()) {
try {
TorrentMetadata t = loadTorrent(torrentFile);
List<List<String>> announceList = t.getAnnounceList() == null ? Collections.singletonList(Collections.singletonList(t.getAnnounce())) : t.getAnnounceList();
for (List<String> uris : announceList) {
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 TorrentMetadata torrent = createTorrent(srcFile, torrentFile, announceURI);
return torrent != null ? torrentFile : null;
}
use of com.turn.ttorrent.common.TorrentMetadata 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 TorrentMetadata createTorrent(@NotNull File srcFile, @NotNull File torrentFile, @NotNull URI announceURI) {
try {
TorrentMetadata 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;
}
use of com.turn.ttorrent.common.TorrentMetadata 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("src/test/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<CommunicationManager> communicationManagers = new ArrayList<CommunicationManager>();
for (int i = 0; i < myLeechSettings.getMinSeedersForDownload(); i++) {
communicationManagers.add(createClientWithClosingExecutorServiceOnStop());
}
try {
tracker.start(true);
mySeeder.start(new InetAddress[] { InetAddress.getLocalHost() }, tracker.getAnnounceURI(), 5);
final TorrentMetadata 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 (CommunicationManager communicationManager : communicationManagers) {
communicationManager.start(InetAddress.getLocalHost());
communicationManager.addTorrent(torrentFile.getAbsolutePath(), storageDir.getAbsolutePath());
}
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 (CommunicationManager communicationManager : communicationManagers) {
communicationManager.stop();
}
tracker.stop();
}
}
Aggregations