if ( interesting_pub_max > 0 && interesting_published > interesting_pub_max ){
continue;
}
DownloadScrapeResult scrape = (DownloadScrapeResult)scrapes.get( download );
if ( scrape == null ){
// catch it next time round
continue;
}
if ( scrape.getSeedCount() + scrape.getNonSeedCount() > NUM_WANT ){
continue;
}
}
long target = ((Long)interesting_downloads.get( download )).longValue();
long check_period = TorrentUtils.isDecentralised( torrent.getAnnounceURL())?INTERESTING_DHT_CHECK_PERIOD:INTERESTING_CHECK_PERIOD;
if ( target <= now ){
ready_download = download;
ready_download_next_check = now + check_period;
interesting_downloads.put( download, new Long( ready_download_next_check ));
}else if ( target - now > check_period ){
interesting_downloads.put( download, new Long( now + (target%check_period)));
}
}
}
}finally{
this_mon.exit();
}
if ( ready_download != null ){
final Download f_ready_download = ready_download;
final Torrent torrent = ready_download.getTorrent();
if ( dht.isDiversified( torrent.getHash())){
// System.out.println( "presence query for " + f_ready_download.getName() + "-> diversified pre start" );
try{
this_mon.enter();
interesting_downloads.remove( f_ready_download );
}finally{
this_mon.exit();
}
}else{
//System.out.println( "presence query for " + ready_download.getName());
final long start = now;
final long f_next_check = ready_download_next_check;
dht.get( torrent.getHash(),
"Presence query for '" + ready_download.getName() + "'",
(byte)0,
INTERESTING_AVAIL_MAX,
ANNOUNCE_TIMEOUT,
false, false,
new DHTPluginOperationListener()
{
private boolean diversified;
private int leechers = 0;
private int seeds = 0;
public void
diversified()
{
diversified = true;
}
public void
starts(
byte[] key )
{
}
public void
valueRead(
DHTPluginContact originator,
DHTPluginValue value )
{
if (( value.getFlags() & DHTPlugin.FLAG_DOWNLOADING ) == 1 ){
leechers++;
}else{
seeds++;
}
}
public void
valueWritten(
DHTPluginContact target,
DHTPluginValue value )
{
}
public void
complete(
byte[] key,
boolean timeout_occurred )
{
// System.out.println( " presence query for " + f_ready_download.getName() + "->" + total + "/div = " + diversified );
int total = leechers + seeds;
log( torrent,
"Presence query: availability="+
(total==INTERESTING_AVAIL_MAX?(INTERESTING_AVAIL_MAX+"+"):(total+"")) + ",div=" + diversified +
" (elapsed=" + TimeFormatter.formatColonMillis(SystemTime.getCurrentTime() - start) + ")");
if ( diversified ){
try{
this_mon.enter();
interesting_downloads.remove( f_ready_download );
}finally{
this_mon.exit();
}
}else if ( total < INTERESTING_AVAIL_MAX ){
// once we're registered we don't need to process this download any
// more unless it goes active and then inactive again
try{
this_mon.enter();
interesting_downloads.remove( f_ready_download );
}finally{
this_mon.exit();
}
interesting_published++;
if ( !disable_put ){
dht.put(
torrent.getHash(),
"Presence store '" + f_ready_download.getName() + "'",
"0".getBytes(), // port 0, no connections
(byte)0,
new DHTPluginOperationListener()
{
public void
diversified()
{
}
public void
starts(
byte[] key )
{
}
public void
valueRead(
DHTPluginContact originator,
DHTPluginValue value )
{
}
public void
valueWritten(
DHTPluginContact target,
DHTPluginValue value )
{
}
public void
complete(
byte[] key,
boolean timeout_occurred )
{
}
});
}
}
try{
this_mon.enter();
int[] run_data = running_downloads.get( f_ready_download );
if ( run_data == null ){
run_data = run_data_cache.get( f_ready_download );
}
if ( run_data != null ){
if ( total < INTERESTING_AVAIL_MAX ){
run_data[1] = seeds;
run_data[2] = leechers;
run_data[3] = total;
}else{
run_data[1] = Math.max( run_data[1], seeds );
run_data[2] = Math.max( run_data[2], leechers );
}
}
}finally{
this_mon.exit();
}
f_ready_download.setScrapeResult(
new DownloadScrapeResult()
{
public Download
getDownload()
{
return( null );