Methods Summary |
---|
public void | abort()
abort( MessageText.getString("SpeedTestWizard.abort.message.manual.abort") );
|
public void | abort(java.lang.String reason)
if ( !aborted ){
aborted = true;
tester.abort( reason );
}
|
public void | addListener(com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTestScheduledTestListener listener)
listeners.add( listener );
|
private java.util.Map | getBEncodedMapFromRequest(java.net.URL url)Read from URL and return byte array.
ResourceDownloader rd = ResourceDownloaderFactoryImpl.getSingleton().create( url );
InputStream is=null;
Map reply = new HashMap();
try
{
is = rd.download();
reply = BDecoder.decode( new BufferedInputStream(is) );
//all replys of this type contains a "result"
Long res = (Long) reply.get("result");
if(res==null)
throw new IllegalStateException("No result parameter in the response!! reply="+reply);
if(res.intValue()==0){
StringBuffer msg = new StringBuffer("Server failed. ");
String error = new String( (byte[]) reply.get("error") );
String errDetail = new String( (byte[]) reply.get("error_detail") );
msg.append("Error: ").append(error);
// detail is of no interest to the user
// msg.append(" ,error detail: ").append(errDetail);
Debug.outNoStack( "SpeedCheck server returned an error: " + error + ", details=" + errDetail );
throw new IOException( msg.toString() );
}
}catch(IOException ise){
//rethrow this type of exception.
throw ise;
}catch(Throwable t){
Debug.out(t);
Debug.printStackTrace(t);
}finally{
try{
if(is!=null)
is.close();
}catch(Throwable e){
Debug.printStackTrace(e);
}
}
return reply;
|
public long | getMaxDownBytePerSec()
return( max_speed );
|
public long | getMaxUpBytePerSec()
return( max_speed );
|
public com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTester | getTester()
return( tester );
|
private java.lang.String | getVersionFromJAR(java.io.File jar_file)
try{
// force the URLClassLoader to load from the URL and not delegate and find the currently
// jar's Constants
ClassLoader parent = new ClassLoader()
{
protected synchronized Class
loadClass(
String name,
boolean resolve )
throws ClassNotFoundException
{
if ( name.equals( "org.gudy.azureus2.core3.util.Constants")){
throw( new ClassNotFoundException());
}
return( super.loadClass( name, resolve ));
}
};
ClassLoader cl = new URLClassLoader(new URL[]{jar_file.toURI().toURL()}, parent);
Class c = cl.loadClass( "org.gudy.azureus2.core3.util.Constants");
Field field = c.getField( "AZUREUS_VERSION" );
return((String)field.get( null ));
}catch( Throwable e){
return( null );
}
|
private java.util.Map | handleChallengeFromSpeedTestService(java.io.File jar_file, java.util.Map result)
//verify the following items are in the response.
//size (in bytes)
//offset (in bytes)
//challenge_id
Map retVal = new HashMap();
RandomAccessFile raf=null;
try{
Long size = (Long) result.get("size");
Long offset = (Long) result.get("offset");
if( size==null || offset==null )
throw new IllegalStateException("scheduleTestWithSpeedTestService had a null parameter.");
//read the bytes
raf = new RandomAccessFile( jar_file, "r" );
byte[] jarBytes = new byte[size.intValue()];
raf.seek(offset.intValue());
raf.read( jarBytes );
//Build the URL.
Map request = new HashMap();
request.put("request_type", new Long(CHALLENGE_REPLY) );
request.put("challenge_id", challenge_id );
request.put("data",jarBytes);
retVal = sendRequest( request );
}finally{
//close
try{
if(raf!=null)
raf.close();
}catch(Throwable t){
Debug.printStackTrace(t);
}
}
return retVal;
|
public void | removeListener(com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTestScheduledTestListener listener)
listeners.remove( listener );
|
protected void | reportComplete()
resetSpeedLimits();
Iterator it = listeners.iterator();
while( it.hasNext()){
try{
((NetworkAdminSpeedTestScheduledTestListener)it.next()).complete( this );
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
|
protected void | reportStage(java.lang.String str)
Iterator it = listeners.iterator();
while( it.hasNext()){
try{
((NetworkAdminSpeedTestScheduledTestListener)it.next()).stage( this, str );
}catch( Throwable e ){
Debug.printStackTrace( e );
}
}
|
protected synchronized void | resetSpeedLimits()Restore all the downloads the state before the speed test started.
if ( preTestSettings != null ){
preTestSettings.restoreLimits();
preTestSettings = null;
}
|
private boolean | schedule()Request a test from the speed testing service, handle the "challenge" if request and then get
the id for the test.
Per spec all request are BEncoded maps.
try{
//lookup UPnP devices found. One might be a router.
//Send "schedule test" request.
Map request = new HashMap();
request.put("request_type", new Long(REQUEST_TEST) );
String id = COConfigurationManager.getStringParameter("ID","unknown");
// get jar file its version for the test
File jar_file = null;
String jar_version = null;
String explicit_path = System.getProperty( "azureus.speed.test.challenge.jar.path", null );
if ( explicit_path != null ){
File f = new File( explicit_path );
if ( f.exists()){
String v = getVersionFromJAR( f );
if ( v != null ){
jar_file = f;
jar_version = v;
System.out.println( "SpeedTest: using explicit challenge jar " + jar_file.getAbsolutePath() + ", version " + jar_version );
}
}
}
if ( jar_file == null ){
String debug = System.getProperty("debug.speed.test.challenge","n");
if( !debug.equals( "n" )){
//over-ride the jar version, and location for debugging.
File f = new File( "C:\\test\\azureus\\Azureus3.0.1.2.jar" ); //ToDo: make this a -D option with this default.
if ( f.exists()){
jar_file = f;
jar_version = "3.0.1.2";
System.out.println( "SpeedTest: using old spec challenge jar " + jar_file.getAbsolutePath() + ", version " + jar_version );
}
}
}
if ( jar_file == null ){
jar_file = FileUtil.getJarFileFromClass( getClass());
if ( jar_file != null ){
jar_version = Constants.AZUREUS_VERSION;
// System.out.println( "SpeedTest: using class-based challenge jar " + jar_file.getAbsolutePath() + ", version " + jar_version );
}else{
File f = new File( SystemProperties.getAzureusJarPath());
if ( f.exists()){
jar_version = Constants.AZUREUS_VERSION;
jar_file = f;
// System.out.println( "SpeedTest: using config-based challenge jar " + jar_file.getAbsolutePath() + ", version " + jar_version );
}
}
}
if ( jar_file == null ){
throw( new Exception( "Failed to locate an 'Azureus2.jar' to use for the challenge protocol" ));
}
//ToDo: remove once challenge testing is done.
request.put("az-id",id); //Where to I get the AZ-ID and client version from the Configuration?
request.put("type","both");
request.put("jar_ver",jar_version);
if ( detectedRouter != null ){
request.put( "router", detectedRouter );
}
Map result = sendRequest( request );
challenge_id = (byte[]) result.get("challenge_id");
if( challenge_id == null ){
throw new IllegalStateException("No challenge returned from speed test scheduling service");
}
Long responseType = (Long) result.get("reply_type");
if( responseType.intValue()==1 ){
//a challenge has occured.
result = handleChallengeFromSpeedTestService( jar_file, result );
responseType = (Long) result.get("reply_type");
}
if( responseType == null ){
throw new IllegalStateException("No challenge response returned from speed test scheduling service");
}
if( responseType.intValue()==0 ){
//a test has been scheduled.
//set the Map properly.
Long time = (Long) result.get("time");
Long limit = (Long) result.get("limit");
if( time==null || limit==null ){
throw new IllegalArgumentException("Returned time or limit parameter is null");
}
delay_millis = time.longValue();
max_speed = limit.longValue();
// this is test-specific data
Map torrentMap = (Map)result.get("torrent");
test_torrent = TOTorrentFactory.deserialiseFromMap(torrentMap);
return( true );
}else{
throw new IllegalStateException( "Unrecognized response from speed test scheduling servcie." );
}
}catch( Throwable t ){
Debug.printStackTrace(t);
tester.abort( MessageText.getString("SpeedTestWizard.abort.message.scheduling.failed"), t );
return( false );
}
|
private java.util.Map | sendRequest(java.util.Map request)
request.put( "ver", new Long(1) );//request version
request.put( "locale", MessageText.getCurrentLocale().toString());
String speedTestServiceName = System.getProperty( "speedtest.service.ip.address", Constants.SPEED_TEST_SERVER );
URL urlRequestTest = new URL("http://"+speedTestServiceName+":60000/scheduletest?request="
+ URLEncoder.encode( new String(BEncoder.encode(request),"ISO-8859-1"),"ISO-8859-1"));
return( getBEncodedMapFromRequest( urlRequestTest ));
|
private void | sendResult(com.aelitis.azureus.core.networkmanager.admin.NetworkAdminSpeedTesterResult result)
try{
if ( challenge_id != null ){
Map request = new HashMap();
request.put("request_type", new Long(TEST_RESULT) );
request.put("challenge_id", challenge_id );
request.put( "type", new Long( tester.getTestType()));
request.put( "mode", new Long( tester.getMode()));
request.put( "crypto", new Long( tester.getUseCrypto()?1:0));
if ( result.hadError()){
request.put( "result", new Long(0));
request.put( "error", result.getLastError());
}else{
request.put( "result", new Long(1));
request.put( "maxup", new Long(result.getUploadSpeed()));
request.put( "maxdown", new Long(result.getDownloadSpeed()));
}
sendRequest( request );
}
}catch( Throwable e ){
Debug.printStackTrace(e);
}
|
protected synchronized void | setSpeedLimits()Preserve all the data about the downloads while the test is running.
// in case we've already saved limits
resetSpeedLimits();
preTestSettings = new SpeedTestDownloadState();
preTestSettings.saveLimits();
|
public boolean | start()
if ( schedule()){
new AEThread( "NetworkAdminSpeedTestScheduledTest:delay", true )
{
public void
runSupport()
{
long delay_ticks = delay_millis/1000;
for (int i=0;i<delay_ticks;i++){
if ( aborted ){
break;
}
String testScheduledIn = MessageText.getString( "SpeedTestWizard.abort.message.scheduled.in"
, new String[]{""+(delay_ticks - i)} );
reportStage( testScheduledIn );
try{
Thread.sleep(1000);
}catch( InterruptedException e ){
e.printStackTrace();
}
}
if ( !aborted ){
setSpeedLimits();
if ( tester.getTestType() == NetworkAdminSpeedTestScheduler.TEST_TYPE_BT ){
((NetworkAdminSpeedTesterBTImpl)tester).start( test_torrent );
}else{
String unsupportedType = MessageText.getString("SpeedTestWizard.abort.message.unsupported.type");
tester.abort(unsupportedType);
}
}
}
}.start();
return( true );
}else{
return( false );
}
|