use of com.biglybt.pif.peers.PeerManager 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.pif.peers.PeerManager in project BiglyBT by BiglySoftware.
the class DownloadImpl method addPeerListener.
@Override
public void addPeerListener(final DownloadPeerListener listener) {
DownloadManagerPeerListener delegate = new DownloadManagerPeerListener() {
@Override
public void peerManagerAdded(PEPeerManager manager) {
PeerManager pm = PeerManagerImpl.getPeerManager(manager);
listener.peerManagerAdded(DownloadImpl.this, pm);
}
@Override
public void peerManagerRemoved(PEPeerManager manager) {
PeerManager pm = PeerManagerImpl.getPeerManager(manager);
listener.peerManagerRemoved(DownloadImpl.this, pm);
}
@Override
public void peerManagerWillBeAdded(PEPeerManager manager) {
}
@Override
public void peerAdded(PEPeer peer) {
}
@Override
public void peerRemoved(PEPeer peer) {
}
};
try {
peer_listeners_mon.enter();
peer_listeners.put(listener, delegate);
} finally {
peer_listeners_mon.exit();
}
download_manager.addPeerListener(delegate);
}
use of com.biglybt.pif.peers.PeerManager in project BiglyBT by BiglySoftware.
the class SpeedLimitHandler method checkIPSetsSupport.
private synchronized void checkIPSetsSupport() {
final com.biglybt.pif.download.DownloadManager download_manager = plugin_interface.getDownloadManager();
if (current_dml != null) {
current_dml.destroy();
current_dml = null;
}
Download[] downloads = download_manager.getDownloads();
for (Download dm : downloads) {
PeerManager pm = dm.getPeerManager();
if (pm != null) {
Peer[] peers = pm.getPeers();
for (Peer peer : peers) {
RateLimiter[] lims = peer.getRateLimiters(false);
for (RateLimiter l : lims) {
if (ip_set_rate_limiters_down.containsValue(l)) {
synchronized (RL_TO_BE_REMOVED_LOCK) {
List<RateLimiter> to_be_removed = (List<RateLimiter>) peer.getUserData(RLD_TO_BE_REMOVED_KEY);
if (to_be_removed == null) {
to_be_removed = new ArrayList<>();
peer.setUserData(RLD_TO_BE_REMOVED_KEY, to_be_removed);
}
to_be_removed.add(l);
}
// defer as removing the rate limiter and then re-adding it gives time for
// quite a lot to happen in between
// peer.removeRateLimiter( l , false );
}
}
lims = peer.getRateLimiters(true);
for (RateLimiter l : lims) {
if (ip_set_rate_limiters_up.containsValue(l)) {
synchronized (RL_TO_BE_REMOVED_LOCK) {
List<RateLimiter> to_be_removed = (List<RateLimiter>) peer.getUserData(RLU_TO_BE_REMOVED_KEY);
if (to_be_removed == null) {
to_be_removed = new ArrayList<>();
peer.setUserData(RLU_TO_BE_REMOVED_KEY, to_be_removed);
}
to_be_removed.add(l);
}
// peer.removeRateLimiter( l , true );
}
}
}
}
}
ip_set_rate_limiters_down.clear();
ip_set_rate_limiters_up.clear();
boolean has_cats_or_tags = false;
for (IPSet set : current_ip_sets.values()) {
ip_set_rate_limiters_down.put(set.getName(), set.getDownLimiter());
ip_set_rate_limiters_up.put(set.getName(), set.getUpLimiter());
if (set.getCategoriesOrTags() != null) {
has_cats_or_tags = true;
}
set.removeAllPeers();
}
if (current_ip_sets.size() == 0) {
if (ip_set_event != null) {
ip_set_event.cancel();
ip_set_event = null;
}
} else {
if (ip_set_event == null) {
ip_set_event = SimpleTimer.addPeriodicEvent("speed handler ip set scheduler", 1000, new TimerEventPerformer() {
private int tick_count;
@Override
public void perform(TimerEvent event) {
tick_count++;
synchronized (SpeedLimitHandler.this) {
for (IPSet set : current_ip_sets.values()) {
set.updateStats(tick_count);
}
/*
if ( tick_count % 30 == 0 ){
String str = "";
for ( IPSet set: current_ip_sets.values()){
str += (str.length()==0?"":", ") + set.getString();
}
logger.log( str );
}
*/
}
}
});
}
current_dml = new DML(download_manager, has_cats_or_tags);
}
}
use of com.biglybt.pif.peers.PeerManager in project BiglyBT by BiglySoftware.
the class TranscodeJobImpl method updateStatus.
protected void updateStatus(boolean report_change) {
synchronized (this) {
if (download_ok) {
return;
}
long downloaded = file.getDownloaded();
long length = file.getLength();
if (download == null || downloaded == length) {
download_ok = true;
} else {
int percent_done = (int) (100 * downloaded / length);
if (percent_done >= TRANSCODE_OK_DL_PERCENT) {
download_ok = true;
} else {
PeerManager pm = download.getPeerManager();
if (pm != null) {
PeerManagerStats stats = pm.getStats();
int connected_seeds = stats.getConnectedSeeds();
int connected_leechers = stats.getConnectedLeechers();
if (connected_seeds > 10 && connected_seeds > connected_leechers) {
download_ok = true;
}
} else {
int state = download.getState();
if (state == Download.ST_STOPPED) {
try {
download.restart();
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
}
}
if (download_ok && report_change) {
queue.jobChanged(this, true, false);
}
}
use of com.biglybt.pif.peers.PeerManager in project BiglyBT by BiglySoftware.
the class DHTPluginImpl method checkForReSeed.
public void checkForReSeed(boolean force) {
int seed_limit = 32;
try {
long[] router_stats = dht.getRouter().getStats().getStats();
if (router_stats[DHTRouterStats.ST_CONTACTS_LIVE] < seed_limit || force) {
if (force) {
log.log("Reseeding");
} else {
log.log("Less than 32 live contacts, reseeding");
}
int peers_imported = 0;
if (network == DHT.NW_AZ_MAIN || network == DHT.NW_AZ_MAIN_V6) {
// first look for peers to directly import
Download[] downloads = plugin_interface.getDownloadManager().getDownloads();
outer: for (int i = 0; i < downloads.length; i++) {
Download download = downloads[i];
PeerManager pm = download.getPeerManager();
if (pm == null) {
continue;
}
Peer[] peers = pm.getPeers();
for (int j = 0; j < peers.length; j++) {
Peer p = peers[j];
int peer_udp_port = p.getUDPNonDataListenPort();
if (peer_udp_port != 0) {
boolean is_v6 = p.getIp().contains(":");
if (is_v6 == v6) {
String ip = p.getIp();
if (AENetworkClassifier.categoriseAddress(ip) == AENetworkClassifier.AT_PUBLIC) {
if (importSeed(ip, peer_udp_port) != null) {
peers_imported++;
if (peers_imported > seed_limit) {
break outer;
}
}
}
}
}
}
}
if (peers_imported < 16) {
List<InetSocketAddress> list = VersionCheckClient.getSingleton().getDHTBootstrap(network != DHT.NW_AZ_MAIN_V6);
for (InetSocketAddress address : list) {
if (importSeed(address) != null) {
peers_imported++;
if (peers_imported > seed_limit) {
break;
}
}
}
}
} else if (network == DHT.NW_BIGLYBT_MAIN) {
List<InetSocketAddress> list = VersionCheckClient.getSingleton().getDHTBootstrap(true);
for (InetSocketAddress address : list) {
if (importSeed(address) != null) {
peers_imported++;
if (peers_imported > seed_limit) {
break;
}
}
}
}
DHTTransportContact root_to_remove = null;
if (peers_imported == 0) {
root_to_remove = importRootSeed();
if (root_to_remove != null) {
peers_imported++;
}
}
if (peers_imported > 0) {
integrateDHT(false, root_to_remove);
} else {
log.log("No valid peers found to reseed from");
}
}
} catch (Throwable e) {
log.log(e);
}
}
Aggregations