use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.
the class SubscriptionManagerImpl method downloadSubscription.
private void downloadSubscription(final String description, final TOTorrent torrent, final InetSocketAddress peer, byte[] subs_id, int version, String name, final downloadListener listener) {
try {
// testing purposes, see if local exists
LightWeightSeed lws = LightWeightSeedManager.getSingleton().get(new HashWrapper(torrent.getHash()));
if (lws != null) {
log("Light weight seed found");
listener.complete(lws.getDataLocation());
} else {
String sid = ByteFormatter.encodeString(subs_id);
File dir = getSubsDir();
dir = new File(dir, "temp");
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw (new IOException("Failed to create dir '" + dir + "'"));
}
}
final File torrent_file = new File(dir, sid + "_" + version + ".torrent");
final File data_file = new File(dir, VuzeFileHandler.getVuzeFileName(sid + "_" + version));
PluginInterface pi = PluginInitializer.getDefaultInterface();
final DownloadManager dm = pi.getDownloadManager();
Download download = dm.getDownload(torrent.getHash());
if (download == null) {
log("Adding download for subscription '" + new String(torrent.getName()) + "'");
boolean is_update = getSubscriptionFromSID(subs_id) != null;
PlatformTorrentUtils.setContentTitle(torrent, "Subscription " + (is_update ? "Update" : "Download") + ": " + description + "(" + name + ")");
// PlatformTorrentUtils.setContentThumbnail(torrent, thumbnail);
TorrentUtils.setFlag(torrent, TorrentUtils.TORRENT_FLAG_LOW_NOISE, true);
Torrent t = new TorrentImpl(torrent);
t.setDefaultEncoding();
t.writeToFile(torrent_file);
download = dm.addDownload(t, torrent_file, data_file);
download.setFlag(Download.FLAG_DISABLE_AUTO_FILE_MOVE, true);
download.setBooleanAttribute(ta_subs_download, true);
Map rd = listener.getRecoveryData();
if (rd != null) {
download.setMapAttribute(ta_subs_download_rd, rd);
}
} else {
log("Existing download found for subscription '" + new String(torrent.getName()) + "'");
}
final Download f_download = download;
final TimerEventPeriodic[] event = { null };
event[0] = SimpleTimer.addPeriodicEvent("SM:cancelTimer", 10 * 1000, new TimerEventPerformer() {
private long start_time = SystemTime.getMonotonousTime();
@Override
public void perform(TimerEvent ev) {
boolean kill = false;
try {
Download download = dm.getDownload(torrent.getHash());
if (listener.isCancelled() || download == null) {
kill = true;
} else {
int state = download.getState();
if (state == Download.ST_ERROR) {
log("Download entered error state, removing");
kill = true;
} else {
long now = SystemTime.getMonotonousTime();
long running_for = now - start_time;
if (running_for > 10 * 60 * 1000) {
log("Download hasn't completed in permitted time, removing");
kill = true;
} else if (running_for > 4 * 60 * 1000) {
if (download.getStats().getDownloaded() == 0) {
log("Download has zero downloaded, removing");
kill = true;
}
} else if (running_for > 2 * 60 * 1000) {
DownloadScrapeResult scrape = download.getLastScrapeResult();
if (scrape == null || scrape.getSeedCount() <= 0) {
log("Download has no seeds, removing");
kill = true;
}
}
}
}
} catch (Throwable e) {
log("Download failed", e);
kill = true;
}
if (kill && event[0] != null) {
try {
event[0].cancel();
if (!listener.isCancelled()) {
listener.failed(new SubscriptionException("Download abandoned"));
}
} finally {
removeDownload(f_download, true);
torrent_file.delete();
}
}
}
});
download.addCompletionListener(new DownloadCompletionListener() {
@Override
public void onCompletion(Download d) {
listener.complete(d, torrent_file);
}
});
if (download.isComplete()) {
listener.complete(download, torrent_file);
} else {
download.setForceStart(true);
if (peer != null) {
download.addPeerListener(new DownloadPeerListener() {
@Override
public void peerManagerAdded(Download download, PeerManager peer_manager) {
InetSocketAddress tcp = AddressUtils.adjustTCPAddress(peer, true);
InetSocketAddress udp = AddressUtils.adjustUDPAddress(peer, true);
log(" Injecting peer into download: " + tcp);
peer_manager.addPeer(tcp.getAddress().getHostAddress(), tcp.getPort(), udp.getPort(), true);
}
@Override
public void peerManagerRemoved(Download download, PeerManager peer_manager) {
}
});
}
}
}
} catch (Throwable e) {
log("Failed to add download", e);
listener.failed(e);
}
}
use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.
the class ShareManagerImpl method readTorrent.
protected void readTorrent(ShareItemImpl item) throws ShareException {
try {
TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedFile(getTorrentFile(item));
item.setTorrent(new TorrentImpl(torrent));
} catch (TOTorrentException e) {
throw (new ShareException("ShareManager: Torrent read fails", e));
}
}
use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.
the class DownloadImpl method getTorrent.
@Override
public Torrent getTorrent() {
if (this.torrent != null) {
return this.torrent;
}
TOTorrent torrent = download_manager.getTorrent();
if (torrent == null) {
return null;
}
this.torrent = new TorrentImpl(torrent);
return this.torrent;
}
use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.
the class ExternalSeedReaderGetRight method setupDownloaders.
private void setupDownloaders() {
synchronized (this) {
if (http_downloaders != null) {
return;
}
TOTorrent to_torrent = ((TorrentImpl) getTorrent()).getTorrent();
String ua = getUserAgent();
if (to_torrent.isSimpleTorrent()) {
http_downloaders = new ExternalSeedHTTPDownloader[] { linear_download ? new ExternalSeedHTTPDownloaderLinear(url, ua) : new ExternalSeedHTTPDownloaderRange(url, ua) };
downloader_offsets = new long[] { 0 };
downloader_lengths = new long[] { to_torrent.getSize() };
} else {
TOTorrentFile[] files = to_torrent.getFiles();
http_downloaders = new ExternalSeedHTTPDownloader[files.length];
downloader_offsets = new long[files.length];
downloader_lengths = new long[files.length];
long offset = 0;
// encoding is a problem, assume ISO-8859-1
String base_url = url.toString();
if (base_url.endsWith("/")) {
base_url = base_url.substring(0, base_url.length() - 1);
}
try {
base_url += "/" + URLEncoder.encode(new String(to_torrent.getName(), "ISO-8859-1"), "ISO-8859-1").replaceAll("\\+", "%20");
for (int i = 0; i < files.length; i++) {
TOTorrentFile file = files[i];
long length = file.getLength();
String file_url_str = base_url;
byte[][] bits = file.getPathComponents();
for (int j = 0; j < bits.length; j++) {
file_url_str += "/" + URLEncoder.encode(new String(bits[j], "ISO-8859-1"), "ISO-8859-1").replaceAll("\\+", "%20");
}
http_downloaders[i] = linear_download ? new ExternalSeedHTTPDownloaderLinear(new URL(file_url_str), ua) : new ExternalSeedHTTPDownloaderRange(new URL(file_url_str), ua);
downloader_offsets[i] = offset;
downloader_lengths[i] = length;
offset += length;
}
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
use of com.biglybt.pifimpl.local.torrent.TorrentImpl in project BiglyBT by BiglySoftware.
the class NetworkAdminSpeedTesterBTImpl method start.
/**
* The downloads have been stopped just need to do the testing.
* @param tot - Torrent recieved from testing service.
*/
public synchronized void start(TOTorrent tot) {
if (test_started) {
Debug.out("Test already started!");
return;
}
test_started = true;
// OK lets start the test.
try {
TorrentUtils.setFlag(tot, TorrentUtils.TORRENT_FLAG_LOW_NOISE, true);
Torrent torrent = new TorrentImpl(tot);
String fileName = torrent.getName();
sendStageUpdateToListeners(MessageText.getString("SpeedTestWizard.stage.message.preparing"));
// create a blank file of specified size. (using the temporary name.)
File saveLocation = AETemporaryFileHandler.createTempFile();
File baseDir = saveLocation.getParentFile();
File blankFile = new File(baseDir, fileName);
File blankTorrentFile = new File(baseDir, "speedTestTorrent.torrent");
torrent.writeToFile(blankTorrentFile);
URL announce_url = torrent.getAnnounceURL();
if (announce_url.getProtocol().equalsIgnoreCase("https")) {
SESecurityManager.setCertificateHandler(announce_url, new SECertificateListener() {
@Override
public boolean trustCertificate(String resource, X509Certificate cert) {
return (true);
}
});
}
Download speed_download = plugin.getDownloadManager().addDownloadStopped(torrent, blankTorrentFile, blankFile);
speed_download.setBooleanAttribute(speedTestAttrib, true);
DownloadManager core_download = PluginCoreUtils.unwrap(speed_download);
core_download.setPieceCheckingEnabled(false);
// make sure we've got a bunch of upload slots
core_download.getDownloadState().setIntParameter(DownloadManagerState.PARAM_MAX_UPLOADS, 32);
core_download.getDownloadState().setIntParameter(DownloadManagerState.PARAM_MAX_UPLOADS_WHEN_SEEDING, 32);
if (use_crypto) {
core_download.setCryptoLevel(NetworkManager.CRYPTO_OVERRIDE_REQUIRED);
}
core_download.addPeerListener(new DownloadManagerPeerListener() {
@Override
public void peerManagerWillBeAdded(PEPeerManager peer_manager) {
DiskManager disk_manager = peer_manager.getDiskManager();
DiskManagerPiece[] pieces = disk_manager.getPieces();
int startPiece = setStartPieceBasedOnMode(testMode, pieces.length);
for (int i = startPiece; i < pieces.length; i++) {
pieces[i].setDone(true);
}
}
@Override
public void peerManagerAdded(PEPeerManager peer_manager) {
}
@Override
public void peerManagerRemoved(PEPeerManager manager) {
}
@Override
public void peerAdded(PEPeer peer) {
}
@Override
public void peerRemoved(PEPeer peer) {
}
});
speed_download.moveTo(1);
speed_download.setFlag(Download.FLAG_DISABLE_AUTO_FILE_MOVE, true);
core_download.initialize();
core_download.setForceStart(true);
TorrentSpeedTestMonitorThread monitor = new TorrentSpeedTestMonitorThread(speed_download);
monitor.start();
// The test has now started!!
} catch (Throwable e) {
test_completed = true;
abort("Could not start test", e);
}
}
Aggregations