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