use of com.biglybt.core.tracker.TrackerPeerSourceAdapter in project BiglyBT by BiglySoftware.
the class DownloadManagerImpl method getTrackerPeerSources.
@Override
public List<TrackerPeerSource> getTrackerPeerSources() {
try {
this_mon.enter();
Object[] tps_data = (Object[]) getUserData(TPS_Key);
List<TrackerPeerSource> tps;
if (tps_data == null) {
tps = new ArrayList<>();
TOTorrentListener tol = new TOTorrentListener() {
@Override
public void torrentChanged(TOTorrent torrent, int type) {
if (type == TOTorrentListener.CT_ANNOUNCE_URLS) {
List<DownloadManagerTPSListener> to_inform = null;
try {
this_mon.enter();
torrent.removeListener(this);
setUserData(TPS_Key, null);
if (tps_listeners != null) {
to_inform = new ArrayList<>(tps_listeners);
}
} finally {
this_mon.exit();
}
if (to_inform != null) {
for (DownloadManagerTPSListener l : to_inform) {
try {
l.trackerPeerSourcesChanged();
} catch (Throwable e) {
Debug.out(e);
}
}
}
}
}
};
setUserData(TPS_Key, new Object[] { tps, tol });
Download plugin_download = PluginCoreUtils.wrap(this);
if (isDestroyed() || plugin_download == null) {
return (tps);
}
// tracker peer sources
final TOTorrent t = getTorrent();
if (t != null) {
t.addListener(tol);
TOTorrentAnnounceURLSet[] sets = t.getAnnounceURLGroup().getAnnounceURLSets();
if (sets.length == 0) {
sets = new TOTorrentAnnounceURLSet[] { t.getAnnounceURLGroup().createAnnounceURLSet(new URL[] { torrent.getAnnounceURL() }) };
}
for (final TOTorrentAnnounceURLSet set : sets) {
final URL[] urls = set.getAnnounceURLs();
if (urls.length == 0 || TorrentUtils.isDecentralised(urls[0])) {
continue;
}
tps.add(new TrackerPeerSource() {
private TrackerPeerSource _delegate;
private TRTrackerAnnouncer ta;
private long ta_fixup;
private long last_scrape_fixup_time;
private Object[] last_scrape;
private TrackerPeerSource fixup() {
long now = SystemTime.getMonotonousTime();
if (now - ta_fixup > 1000) {
TRTrackerAnnouncer current_ta = getTrackerClient();
if (current_ta == ta) {
if (current_ta != null && _delegate == null) {
_delegate = current_ta.getTrackerPeerSource(set);
}
} else {
if (current_ta == null) {
_delegate = null;
} else {
_delegate = current_ta.getTrackerPeerSource(set);
}
ta = current_ta;
}
ta_fixup = now;
}
return (_delegate);
}
protected Object[] getScrape() {
long now = SystemTime.getMonotonousTime();
if (now - last_scrape_fixup_time > 30 * 1000 || last_scrape == null) {
TRTrackerScraper scraper = globalManager.getTrackerScraper();
int max_peers = -1;
int max_seeds = -1;
int max_comp = -1;
int max_time = 0;
int min_scrape = Integer.MAX_VALUE;
String status_str = null;
boolean found_usable = false;
for (URL u : urls) {
TRTrackerScraperResponse resp = scraper.peekScrape(torrent, u);
if (resp != null) {
if (!resp.isDHTBackup()) {
found_usable = true;
int peers = resp.getPeers();
int seeds = resp.getSeeds();
int comp = resp.getCompleted();
if (peers > max_peers) {
max_peers = peers;
}
if (seeds > max_seeds) {
max_seeds = seeds;
}
if (comp > max_comp) {
max_comp = comp;
}
if (resp.getStatus() != TRTrackerScraperResponse.ST_INITIALIZING) {
status_str = resp.getStatusString();
int time = resp.getScrapeTime();
if (time > max_time) {
max_time = time;
}
long next_scrape = resp.getNextScrapeStartTime();
if (next_scrape > 0) {
int ns = (int) (next_scrape / 1000);
if (ns < min_scrape) {
min_scrape = ns;
}
}
}
}
}
}
if (found_usable || last_scrape == null) {
last_scrape = new Object[] { max_seeds, max_peers, max_time, min_scrape, max_comp, status_str };
}
last_scrape_fixup_time = now;
}
return (last_scrape);
}
@Override
public int getType() {
return (TrackerPeerSource.TP_TRACKER);
}
@Override
public String getName() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (urls[0].toExternalForm());
}
return (delegate.getName());
}
@Override
public int getStatus() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (ST_STOPPED);
}
return (delegate.getStatus());
}
@Override
public String getStatusString() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((String) getScrape()[5]);
}
return (delegate.getStatusString());
}
@Override
public int getSeedCount() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[0]);
}
int seeds = delegate.getSeedCount();
if (seeds < 0) {
seeds = (Integer) getScrape()[0];
}
return (seeds);
}
@Override
public int getLeecherCount() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[1]);
}
int leechers = delegate.getLeecherCount();
if (leechers < 0) {
leechers = (Integer) getScrape()[1];
}
return (leechers);
}
@Override
public int getCompletedCount() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[4]);
}
int comp = delegate.getCompletedCount();
if (comp < 0) {
comp = (Integer) getScrape()[4];
}
return (comp);
}
@Override
public int getPeers() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
}
return (delegate.getPeers());
}
@Override
public int getInterval() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
Object[] si = getScrape();
int last = (Integer) si[2];
int next = (Integer) si[3];
if (last > 0 && next < Integer.MAX_VALUE && last < next) {
return (next - last);
}
return (-1);
}
return (delegate.getInterval());
}
@Override
public int getMinInterval() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
}
return (delegate.getMinInterval());
}
@Override
public boolean isUpdating() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (false);
}
return (delegate.isUpdating());
}
@Override
public int getLastUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ((Integer) getScrape()[2]);
}
return (delegate.getLastUpdate());
}
@Override
public int getSecondsToUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
}
return (delegate.getSecondsToUpdate());
}
@Override
public boolean canManuallyUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (false);
}
return (delegate.canManuallyUpdate());
}
@Override
public void manualUpdate() {
TrackerPeerSource delegate = fixup();
if (delegate != null) {
delegate.manualUpdate();
}
}
@Override
public boolean canDelete() {
return (true);
}
@Override
public void delete() {
List<List<String>> lists = TorrentUtils.announceGroupsToList(t);
List<String> rem = new ArrayList<>();
for (URL u : urls) {
rem.add(u.toExternalForm());
}
lists = TorrentUtils.removeAnnounceURLs2(lists, rem, false);
TorrentUtils.listToAnnounceGroups(lists, t);
}
});
}
// cache peer source
tps.add(new TrackerPeerSourceAdapter() {
private TrackerPeerSource _delegate;
private TRTrackerAnnouncer ta;
private boolean enabled;
private long ta_fixup;
private TrackerPeerSource fixup() {
long now = SystemTime.getMonotonousTime();
if (now - ta_fixup > 1000) {
TRTrackerAnnouncer current_ta = getTrackerClient();
if (current_ta == ta) {
if (current_ta != null && _delegate == null) {
_delegate = current_ta.getCacheTrackerPeerSource();
}
} else {
if (current_ta == null) {
_delegate = null;
} else {
_delegate = current_ta.getCacheTrackerPeerSource();
}
ta = current_ta;
}
enabled = controller.isPeerSourceEnabled(PEPeerSource.PS_BT_TRACKER);
ta_fixup = now;
}
return (_delegate);
}
@Override
public int getType() {
return (TrackerPeerSource.TP_TRACKER);
}
@Override
public String getName() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (MessageText.getString("tps.tracker.cache"));
}
return (delegate.getName());
}
@Override
public int getStatus() {
TrackerPeerSource delegate = fixup();
if (!enabled) {
return (ST_DISABLED);
}
if (delegate == null) {
return (ST_STOPPED);
}
return (ST_ONLINE);
}
@Override
public int getPeers() {
TrackerPeerSource delegate = fixup();
if (delegate == null || !enabled) {
return (-1);
}
return (delegate.getPeers());
}
});
}
try {
ExternalSeedPlugin esp = DownloadManagerController.getExternalSeedPlugin();
if (esp != null) {
tps.add(esp.getTrackerPeerSource(plugin_download));
}
} catch (Throwable e) {
}
try {
PluginInterface dht_pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(DHTTrackerPlugin.class);
if (dht_pi != null) {
tps.add(((DHTTrackerPlugin) dht_pi.getPlugin()).getTrackerPeerSource(plugin_download));
}
} catch (Throwable e) {
}
try {
PluginInterface lt_pi = CoreFactory.getSingleton().getPluginManager().getPluginInterfaceByClass(LocalTrackerPlugin.class);
if (lt_pi != null) {
tps.add(((LocalTrackerPlugin) lt_pi.getPlugin()).getTrackerPeerSource(plugin_download));
}
} catch (Throwable e) {
}
try {
tps.add(((DownloadImpl) plugin_download).getTrackerPeerSource());
} catch (Throwable e) {
}
// PEX...
tps.add(new TrackerPeerSourceAdapter() {
private PEPeerManager _pm;
private TrackerPeerSource _delegate;
private TrackerPeerSource fixup() {
PEPeerManager pm = getPeerManager();
if (pm == null) {
_delegate = null;
_pm = null;
} else if (pm != _pm) {
_pm = pm;
_delegate = pm.getTrackerPeerSource();
}
return (_delegate);
}
@Override
public int getType() {
return (TP_PEX);
}
@Override
public int getStatus() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (ST_STOPPED);
} else {
return (delegate.getStatus());
}
}
@Override
public String getName() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return ("");
} else {
return (delegate.getName());
}
}
@Override
public int getPeers() {
TrackerPeerSource delegate = fixup();
if (delegate == null) {
return (-1);
} else {
return (delegate.getPeers());
}
}
});
// incoming
tps.add(new TrackerPeerSourceAdapter() {
private long fixup_time;
private PEPeerManager _pm;
private int tcp;
private int udp;
private int utp;
private int total;
private boolean enabled;
private PEPeerManager fixup() {
long now = SystemTime.getMonotonousTime();
if (now - fixup_time > 1000) {
PEPeerManager pm = _pm = getPeerManager();
if (pm != null) {
tcp = pm.getNbRemoteTCPConnections();
udp = pm.getNbRemoteUDPConnections();
utp = pm.getNbRemoteUTPConnections();
total = pm.getStats().getTotalIncomingConnections();
}
enabled = controller.isPeerSourceEnabled(PEPeerSource.PS_INCOMING);
fixup_time = now;
}
return (_pm);
}
@Override
public int getType() {
return (TP_INCOMING);
}
@Override
public int getStatus() {
PEPeerManager delegate = fixup();
if (delegate == null) {
return (ST_STOPPED);
} else if (!enabled) {
return (ST_DISABLED);
} else {
return (ST_ONLINE);
}
}
@Override
public String getName() {
PEPeerManager delegate = fixup();
if (delegate == null || !enabled) {
return ("");
} else {
return (MessageText.getString("tps.incoming.details", new String[] { String.valueOf(tcp), String.valueOf(udp + utp), String.valueOf(total) }));
}
}
@Override
public int getPeers() {
PEPeerManager delegate = fixup();
if (delegate == null || !enabled) {
return (-1);
} else {
return (tcp + udp);
}
}
});
} else {
tps = (List<TrackerPeerSource>) tps_data[0];
}
return (tps);
} finally {
this_mon.exit();
}
}
use of com.biglybt.core.tracker.TrackerPeerSourceAdapter in project BiglyBT by BiglySoftware.
the class DHTTrackerPlugin method getTrackerPeerSources.
public TrackerPeerSource[] getTrackerPeerSources(final Torrent torrent) {
TrackerPeerSource vuze_dht = new TrackerPeerSourceAdapter() {
private volatile boolean query_done;
private volatile int status = TrackerPeerSource.ST_INITIALISING;
private volatile int seeds = 0;
private volatile int leechers = 0;
private void fixup() {
if (initialised_sem.isReleasedForever()) {
synchronized (this) {
if (query_done) {
return;
}
query_done = true;
status = TrackerPeerSource.ST_UPDATING;
}
dht.get(torrent.getHash(), "Availability lookup for '" + torrent.getName() + "'", DHTPlugin.FLAG_DOWNLOADING, NUM_WANT, ANNOUNCE_DERIVED_TIMEOUT, false, true, new DHTPluginOperationListener() {
@Override
public void starts(byte[] key) {
}
@Override
public boolean diversified() {
return (true);
}
@Override
public void valueRead(DHTPluginContact originator, DHTPluginValue value) {
if ((value.getFlags() & DHTPlugin.FLAG_DOWNLOADING) == 1) {
seeds++;
} else {
leechers++;
}
}
@Override
public void valueWritten(DHTPluginContact target, DHTPluginValue value) {
}
@Override
public void complete(byte[] key, boolean timeout_occurred) {
status = TrackerPeerSource.ST_ONLINE;
}
});
}
}
@Override
public int getType() {
return (TrackerPeerSource.TP_DHT);
}
@Override
public String getName() {
return ("Vuze DHT");
}
@Override
public int getStatus() {
fixup();
return (status);
}
@Override
public int getSeedCount() {
fixup();
int result = seeds;
if (result == 0 && status != TrackerPeerSource.ST_ONLINE) {
return (-1);
}
return (result);
}
@Override
public int getLeecherCount() {
fixup();
int result = leechers;
if (result == 0 && status != TrackerPeerSource.ST_ONLINE) {
return (-1);
}
return (result);
}
@Override
public int getPeers() {
return (-1);
}
@Override
public boolean isUpdating() {
return (status == TrackerPeerSource.ST_UPDATING);
}
};
if (alt_lookup_handler != null) {
TrackerPeerSource alt_dht = new TrackerPeerSourceAdapter() {
private volatile int status = TrackerPeerSource.ST_UPDATING;
private volatile int peers = 0;
{
alt_lookup_handler.get(torrent.getHash(), false, new DHTTrackerPluginAlt.LookupListener() {
@Override
public void foundPeer(InetSocketAddress address) {
peers++;
}
@Override
public boolean isComplete() {
return (false);
}
@Override
public void completed() {
status = TrackerPeerSource.ST_ONLINE;
}
});
}
@Override
public int getType() {
return (TrackerPeerSource.TP_DHT);
}
@Override
public String getName() {
return ("Mainline DHT");
}
@Override
public int getStatus() {
return (status);
}
@Override
public int getPeers() {
int result = peers;
if (result == 0 && status != TrackerPeerSource.ST_ONLINE) {
return (-1);
}
return (result);
}
@Override
public boolean isUpdating() {
return (status == TrackerPeerSource.ST_UPDATING);
}
};
return (new TrackerPeerSource[] { vuze_dht, alt_dht });
} else {
return (new TrackerPeerSource[] { vuze_dht });
}
}
Aggregations