use of com.biglybt.core.lws.LightWeightSeed 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.core.lws.LightWeightSeed in project BiglyBT by BiglySoftware.
the class SubscriptionImpl method destroy.
protected void destroy() {
LightWeightSeed l;
synchronized (this) {
destroyed = true;
l = lws;
}
if (l != null) {
l.remove();
}
}
Aggregations