📄 magnetplugin.java
字号:
public void
UIAttached(
UIInstance instance )
{
if ( instance instanceof UISWTInstance ){
UISWTInstance swt = (UISWTInstance)instance;
Image image = swt.loadImage( "com/aelitis/azureus/plugins/magnet/icons/magnet.gif" );
menu1.setGraphic( swt.createGraphic( image ));
menu2.setGraphic( swt.createGraphic( image ));
}
}
public void
UIDetached(
UIInstance instance )
{
}
});
}
public URL
getMagnetURL(
Download d )
{
Torrent torrent = d.getTorrent();
if ( torrent == null ){
return( null );
}
return( getMagnetURL( torrent.getHash()));
}
public URL
getMagnetURL(
byte[] hash )
{
try{
return( new URL( "magnet:?xt=urn:btih:" + Base32.encode(hash)));
}catch( Throwable e ){
Debug.printStackTrace(e);
return( null );
}
}
public byte[]
badge()
{
return( null );
}
public byte[]
download(
final MagnetPluginProgressListener listener,
final byte[] hash,
final InetSocketAddress[] sources,
final long timeout )
throws MagnetURIHandlerException
{
try{
listener.reportActivity( getMessageText( "report.waiting_ddb" ));
final DistributedDatabase db = plugin_interface.getDistributedDatabase();
final List potential_contacts = new ArrayList();
final AESemaphore potential_contacts_sem = new AESemaphore( "MagnetPlugin:liveones" );
final AEMonitor potential_contacts_mon = new AEMonitor( "MagnetPlugin:liveones" );
final int[] outstanding = {0};
listener.reportActivity( getMessageText( "report.searching" ));
DistributedDatabaseListener ddb_listener =
new DistributedDatabaseListener()
{
public void
event(
DistributedDatabaseEvent event )
{
int type = event.getType();
if ( type == DistributedDatabaseEvent.ET_VALUE_READ ){
contactFound( event.getValue().getContact());
}else if ( type == DistributedDatabaseEvent.ET_OPERATION_COMPLETE ||
type == DistributedDatabaseEvent.ET_OPERATION_TIMEOUT ){
// now inject any explicit sources
for (int i=0;i<sources.length;i++){
try{
contactFound( db.importContact(sources[i]));
}catch( Throwable e ){
Debug.printStackTrace(e);
}
}
potential_contacts_sem.release();
}
}
public void
contactFound(
final DistributedDatabaseContact contact )
{
listener.reportActivity( getMessageText( "report.found", contact.getName()));
outstanding[0]++;
Thread t =
new AEThread( "MagnetPlugin:HitHandler")
{
public void
runSupport()
{
try{
boolean alive = contact.isAlive(20*1000);
listener.reportActivity(
getMessageText( alive?"report.alive":"report.dead", contact.getName()));
try{
potential_contacts_mon.enter();
Object[] entry = new Object[]{ new Boolean( alive ), contact};
boolean added = false;
if ( alive ){
// try and place before first dead entry
for (int i=0;i<potential_contacts.size();i++){
if (!((Boolean)((Object[])potential_contacts.get(i))[0]).booleanValue()){
potential_contacts.add(i, entry );
added = true;
break;
}
}
}
if ( !added ){
potential_contacts.add( entry ); // dead at end
}
potential_contacts_sem.release();
}finally{
potential_contacts_mon.exit();
}
}finally{
try{
potential_contacts_mon.enter();
outstanding[0]--;
}finally{
potential_contacts_mon.exit();
}
}
}
};
t.setDaemon(true);
t.start();
}
};
db.read(
ddb_listener,
db.createKey( hash, "Torrent download lookup for '" + ByteFormatter.encodeString( hash ) + "'" ),
timeout,
DistributedDatabase.OP_EXHAUSTIVE_READ | DistributedDatabase.OP_PRIORITY_HIGH );
long remaining = timeout;
while( remaining > 0 ){
long start = SystemTime.getCurrentTime();
potential_contacts_sem.reserve( remaining );
remaining -= ( SystemTime.getCurrentTime() - start );
DistributedDatabaseContact contact;
boolean live_contact;
try{
potential_contacts_mon.enter();
if ( potential_contacts.size() == 0 ){
if ( outstanding[0] == 0 ){
break;
}else{
continue;
}
}else{
Object[] entry = (Object[])potential_contacts.remove(0);
live_contact = ((Boolean)entry[0]).booleanValue();
contact = (DistributedDatabaseContact)entry[1];
}
}finally{
potential_contacts_mon.exit();
}
// System.out.println( "magnetDownload: " + contact.getName() + ", live = " + live_contact );
if ( !live_contact ){
listener.reportActivity( getMessageText( "report.tunnel", contact.getName()));
contact.openTunnel();
}
try{
listener.reportActivity( getMessageText( "report.downloading", contact.getName()));
DistributedDatabaseValue value =
contact.read(
new DistributedDatabaseProgressListener()
{
public void
reportSize(
long size )
{
listener.reportSize( size );
}
public void
reportActivity(
String str )
{
listener.reportActivity( str );
}
public void
reportCompleteness(
int percent )
{
listener.reportCompleteness( percent );
}
},
db.getStandardTransferType( DistributedDatabaseTransferType.ST_TORRENT ),
db.createKey ( hash , "Torrent download content for '" + ByteFormatter.encodeString( hash ) + "'"),
timeout );
if ( value != null ){
return( (byte[])value.getValue(byte[].class));
}
}catch( Throwable e ){
listener.reportActivity( getMessageText( "report.error", Debug.getNestedExceptionMessage(e)));
Debug.printStackTrace(e);
}
}
return( null ); // nothing found
}catch( Throwable e ){
Debug.printStackTrace(e);
listener.reportActivity( getMessageText( "report.error", Debug.getNestedExceptionMessage(e)));
throw( new MagnetURIHandlerException( "MagnetURIHandler failed", e ));
}
}
protected String
getMessageText(
String resource )
{
return( plugin_interface.getUtilities().getLocaleUtilities().getLocalisedMessageText( "MagnetPlugin." + resource ));
}
protected String
getMessageText(
String resource,
String param )
{
return( plugin_interface.getUtilities().getLocaleUtilities().getLocalisedMessageText(
"MagnetPlugin." + resource, new String[]{ param }));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -