observer.complete();
}else{
new AEThread2( "RCM:search", true )
{
public void
run()
{
final Set<String> hashes = new HashSet<String>();
try{
List<RelatedContent> matches = matchContent( term );
for ( final RelatedContent c: matches ){
final byte[] hash = c.getHash();
if ( hash == null ){
continue;
}
hashes.add( Base32.encode( hash ));
SearchResult result =
new SearchResult()
{
public Object
getProperty(
int property_name )
{
if ( property_name == SearchResult.PR_NAME ){
return( c.getTitle());
}else if ( property_name == SearchResult.PR_SIZE ){
return( c.getSize());
}else if ( property_name == SearchResult.PR_HASH ){
return( hash );
}else if ( property_name == SearchResult.PR_RANK ){
// this rank isn't that accurate, scale down
return( new Long( c.getRank() / 4 ));
}else if ( property_name == SearchResult.PR_SEED_COUNT ){
return( new Long( c.getSeeds()));
}else if ( property_name == SearchResult.PR_LEECHER_COUNT ){
return( new Long( c.getLeechers()));
}else if ( property_name == SearchResult.PR_SUPER_SEED_COUNT ){
if ( c.getContentNetwork() != ContentNetwork.CONTENT_NETWORK_UNKNOWN ){
return( new Long( 1 ));
}else{
return( new Long( 0 ));
}
}else if ( property_name == SearchResult.PR_PUB_DATE ){
long date = c.getPublishDate();
if ( date <= 0 ){
return( null );
}
return( new Date( date ));
}else if ( property_name == SearchResult.PR_DOWNLOAD_LINK ||
property_name == SearchResult.PR_DOWNLOAD_BUTTON_LINK ){
byte[] hash = c.getHash();
if ( hash != null ){
return( UrlUtils.getMagnetURI( hash ));
}
}
return( null );
}
};
observer.resultReceived( si, result );
}
}finally{
try{
DHT[] dhts = dht_plugin.getDHTs();
Set<InetSocketAddress> addresses = new HashSet<InetSocketAddress>();
for ( DHT dht: dhts ){
DHTTransportContact[] contacts = dht.getTransport().getReachableContacts();
for ( DHTTransportContact c: contacts ){
if ( c.getProtocolVersion() >= DHTTransportUDP.PROTOCOL_VERSION_REPLICATION_CONTROL ){
addresses.add( c.getAddress());
}
}
}
if ( addresses.size() < MAX_REMOTE_SEARCH_CONTACTS ){
for ( DHT dht: dhts ){
DHTTransportContact[] contacts = dht.getTransport().getRecentContacts();
for ( DHTTransportContact c: contacts ){
if ( c.getProtocolVersion() >= DHTTransportUDP.PROTOCOL_VERSION_REPLICATION_CONTROL ){
addresses.add( c.getAddress());
if ( addresses.size() >= MAX_REMOTE_SEARCH_CONTACTS ){
break;
}
}
}
if ( addresses.size() >= MAX_REMOTE_SEARCH_CONTACTS ){
break;
}
}
}
List<InetSocketAddress> list = new ArrayList<InetSocketAddress>( addresses );
Collections.shuffle( list );
List<DistributedDatabaseContact> ddb_contacts = new ArrayList<DistributedDatabaseContact>();
for (int i=0;i<Math.min( list.size(), MAX_REMOTE_SEARCH_CONTACTS );i++){
try{
ddb_contacts.add( ddb.importContact( list.get(i), DHTTransportUDP.PROTOCOL_VERSION_REPLICATION_CONTROL ));
}catch( Throwable e ){
}
}
long start = SystemTime.getMonotonousTime();
long max = MAX_REMOTE_SEARCH_MILLIS;
final AESemaphore sem = new AESemaphore( "RCM:rems" );
int sent = 0;
final int[] done = {0};
for (int i=0;i<ddb_contacts.size();i++){
final DistributedDatabaseContact c = ddb_contacts.get( i );
new AEThread2( "RCM:rems", true )
{
public void
run()
{
try{