FileDocCategorySizeDatePackage
NetworkAdminSpeedTesterBTImpl.javaAPI DocAzureus 3.0.3.430446Tue Jun 12 16:32:08 BST 2007com.aelitis.azureus.core.networkmanager.admin.impl

NetworkAdminSpeedTesterBTImpl

public class NetworkAdminSpeedTesterBTImpl extends NetworkAdminSpeedTesterImpl implements com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTester
Created on Apr 17, 2007 Created by Alan Snyder Copyright (C) 2007 Aelitis, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. AELITIS, SAS au capital de 63.529,40 euros 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.

Fields Summary
public static final String
DOWNLOAD_AVE
public static final String
UPLOAD_AVE
public static final String
DOWNLOAD_STD_DEV
public static final String
UPLOAD_STD_DEV
private static int
testMode
private static org.gudy.azureus2.plugins.torrent.TorrentAttribute
speedTestAttrib
private static com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTesterResult
lastResult
private org.gudy.azureus2.plugins.PluginInterface
plugin
private boolean
test_started
private boolean
test_completed
private boolean
use_crypto
private volatile boolean
aborted
private String
deferred_abort
Constructors Summary
public NetworkAdminSpeedTesterBTImpl(org.gudy.azureus2.plugins.PluginInterface pi)

param
pi - PluginInterface is used to get Manager classes.

        plugin = pi;
    
Methods Summary
protected voidabort(java.lang.String reason, java.lang.Throwable cause)

    	String	msg;
    	
    	if ( cause instanceof RuntimeException ){
    		
    		msg = Debug.getNestedExceptionMessageAndStack( cause );
    			
    	}else{
    		
    		msg = Debug.getNestedExceptionMessage( cause );
    	}
    	
    	abort( reason + ": " + msg );
    
public voidabort(java.lang.String reason)

		reason = "Test aborted: " + reason;
		
		synchronized( this ){
			
			if ( aborted ){
				
				return;
			}
			
			aborted = true;
		
				// we need to defer the reporting of a failure until the test is complete
				// as this prevents us from starting another test while the current one is
				// terminating
			
			if ( test_started && !test_completed ){
				
				deferred_abort = reason;
				
				return;
			}
		}
		
        sendResultToListeners( new BitTorrentResult( reason ));
    
private static java.lang.DoubleautoboxDouble(double d)

        return new Double(d);
    
private static longautoboxLong(java.lang.Object o)

        return autoboxLong( (Long) o );
    
private static longautoboxLong(java.lang.Long l)

        return l.longValue();
    
private static java.lang.LongautoboxLong(long l)

        return new Long(l);
    
public voidcomplete(com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTesterResult result)

    	sendResultToListeners( result );
    
protected static com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTesterResultgetLastResult()

    	return( lastResult );
    
public intgetMode()

    	return( testMode );
    
public com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTesterResultgetResult()
Get the result for

return
Result object of speed test.

        return lastResult;
    
public intgetTestType()

    	return( NetworkAdminSpeedTestScheduler.TEST_TYPE_BT );
    
public booleangetUseCrypto()

    	return( use_crypto );
    
public voidsetMode(int mode)

        testMode = mode;
    
private static intsetStartPieceBasedOnMode(int mode, int totalPieces)
Depending on the mode we want to upload all the set all, none or only half the pieces to done.

param
mode - int that maps to NetworkAdminSpeedTestScheduler.TEST_TYPE...
param
totalPieces - total pieces in this test torrent.
return
- int - the starting piece number to setDone to true.


        //if(mode==TEST_TYPE_UPLOAD_AND_DOWNLOAD){
        //    //upload half the pieces
        //    return totalPieces/2;
        //}else
        if(mode==TEST_TYPE_UPLOAD_ONLY){
            //upload all the pieces
            return 0;
        }else if(mode==TEST_TYPE_DOWNLOAD_ONLY){
            //download all the pieces
            return totalPieces;
        }
        else
            throw new IllegalStateException("Did not recognize the NetworkAdmin Speed Test type. mode="+mode);
    
public voidsetUseCrypto(boolean _use_crypto)

    	use_crypto = _use_crypto;
    
public synchronized voidstart(org.gudy.azureus2.core3.torrent.TOTorrent tot)
The downloads have been stopped just need to do the testing.

param
tot - Torrent recieved from testing service.

    	if ( test_started ){
    		
    		Debug.out( "Test already started!" );
    		
    		return;
    	}
    	
    	test_started = true;
    	
        //OK lets start the test.
        try{
            TorrentUtils.setFlag( tot, TorrentUtils.TORRENT_FLAG_LOW_NOISE, true );
            
            Torrent torrent = new TorrentImpl(tot);
            String fileName = torrent.getName();

            sendStageUpdateToListeners(MessageText.getString("SpeedTestWizard.stage.message.preparing"));
            	
            //create a blank file of specified size. (using the temporary name.)
            File saveLocation = AETemporaryFileHandler.createTempFile();
            File baseDir = saveLocation.getParentFile();
            File blankFile = new File(baseDir,fileName);
            File blankTorrentFile = new File( baseDir, "speedTestTorrent.torrent" );
            torrent.writeToFile(blankTorrentFile);

            URL	announce_url = torrent.getAnnounceURL();
            
            if ( announce_url.getProtocol().equalsIgnoreCase( "https" )){
            	
            	SESecurityManager.setCertificateHandler( 
            			announce_url,
            			new SECertificateListener()
            			{
            				public boolean
            				trustCertificate(
            					String			resource,
            					X509Certificate	cert )
            				{
            					return( true );
            				}
            			});
            }
            
            Download speed_download = plugin.getDownloadManager().addDownloadStopped( torrent, blankTorrentFile ,blankFile);

            speed_download.setBooleanAttribute(speedTestAttrib,true);

            DownloadManager core_download = PluginCoreUtils.unwrap( speed_download );
            
            core_download.setPieceCheckingEnabled( false );
            
            	// make sure we've got a bunch of upload slots
            
            core_download.getDownloadState().setIntParameter( DownloadManagerState.PARAM_MAX_UPLOADS, 32 );
            core_download.getDownloadState().setIntParameter( DownloadManagerState.PARAM_MAX_UPLOADS_WHEN_SEEDING, 32 );
            
            if ( use_crypto ){
            	
            	core_download.setCryptoLevel( NetworkManager.CRYPTO_OVERRIDE_REQUIRED );
            }
            
            core_download.addPeerListener(
            		new DownloadManagerPeerListener()
            		{
            			public void
            			peerManagerWillBeAdded(
            				PEPeerManager	peer_manager )
            			{
              				DiskManager	disk_manager = peer_manager.getDiskManager();
              				
                			DiskManagerPiece[]	pieces = disk_manager.getPieces();

                            int startPiece = setStartPieceBasedOnMode(testMode,pieces.length);
                            
                            for ( int i=startPiece; i<pieces.length; i++ ){
                                pieces[i].setDone( true );
                			}
            			}
            			
            			public void
            			peerManagerAdded( PEPeerManager	peer_manager )
            			{
            			}
            			
            			public void
            			peerManagerRemoved(PEPeerManager	manager )
            			{    				
            			}
            			
            			public void
            			peerAdded(PEPeer 	peer )
            			{	
            			}
            				
            			public void
            			peerRemoved(PEPeer	peer )
            			{	
            			}
            				
            			public void
            			pieceAdded(PEPiece 	piece )
            			{	
            			}
            				
            			public void
            			pieceRemoved(PEPiece		piece )
            			{	
            			}
                	});
 
            speed_download.moveTo( 1 );
            
            speed_download.setFlag( Download.FLAG_DISABLE_AUTO_FILE_MOVE, true );
            
            core_download.initialize();
            
            core_download.setForceStart( true );
            
            TorrentSpeedTestMonitorThread monitor = new TorrentSpeedTestMonitorThread( speed_download );
            
            monitor.start();

            //The test has now started!!

        }catch( Throwable e){
        	
        	test_completed = true;
        	
            abort( "Could not start test", e );
        }
    
protected static voidstartUp(org.gudy.azureus2.plugins.PluginInterface plugin)


   
      
    
    		 
    
    	speedTestAttrib = plugin.getTorrentManager().getPluginAttribute(NetworkAdminSpeedTesterBTImpl.class.getName()+".test.attrib");
  
    	org.gudy.azureus2.plugins.download.DownloadManager dm = plugin.getDownloadManager();
    	Download[] downloads = dm.getDownloads();

    	if(downloads!=null){
    		int num = downloads.length;
    		for(int i=0; i<num; i++){
    			Download	download = downloads[i];
    			if( download.getBooleanAttribute(speedTestAttrib) ){
    				try{
    					if (download.getState() != Download.ST_STOPPED ){
    						try{
    							download.stop();
    						}catch( Throwable e ){
    							Debug.out(e);
    						}
    					}
    					download.remove(true,true);
     				}catch(Throwable e ){
    					Debug.out("Had "+e.getMessage()+" while trying to remove "+downloads[i].getName());
    				}
    			}
    		}
    	}