FileDocCategorySizeDatePackage
ExternalIPCheckerServiceImpl.javaAPI DocAzureus 3.0.3.47507Sun Jan 23 13:21:54 GMT 2005org.gudy.azureus2.core3.ipchecker.extipchecker.impl

ExternalIPCheckerServiceImpl

public abstract class ExternalIPCheckerServiceImpl extends Object implements ExternalIPCheckerService, Cloneable
author
parg

Fields Summary
private static final int
MAX_PAGE_SIZE
private static final String
MSG_KEY_ROOT
private String
name
private String
description
private String
url
private boolean
completed
private Vector
listeners
private AEMonitor
this_mon
private AESemaphore
timeout_sem
Constructors Summary
protected ExternalIPCheckerServiceImpl(String name_key)

	
	
	
			 
	
		name 		= MessageText.getString( name_key + ".name" );
		description = MessageText.getString( name_key + ".description" );
		url			= MessageText.getString( name_key + ".url" );
	
Methods Summary
protected org.gudy.azureus2.core3.ipchecker.extipchecker.impl.ExternalIPCheckerServiceImpl_clone()

		try{
			return((ExternalIPCheckerServiceImpl)clone());
			
		}catch( CloneNotSupportedException e){
			
			Debug.printStackTrace( e );
			
			return( null );
		}
	
public voidaddListener(ExternalIPCheckerServiceListener l)

		try{
			this_mon.enter();
	
			listeners.addElement( l );
			
		}finally{
			
			this_mon.exit();
		}
	
protected java.lang.StringextractIPAddress(java.lang.String str)

		int		pos = 0;
		
		while(pos < str.length()){
			
			int	p1 = str.indexOf( '.", pos );
			
			if ( p1 == -1 ){
				
				informFailure( "ipnotfound"  );
				
				return( null );
			}
			
			if ( p1 > 0 ){
				
				if ( Character.isDigit(str.charAt(p1-1))){
					
					int	p2 = p1-1;
					
					while(p2>=0&&Character.isDigit(str.charAt(p2))){
						
						p2--;
					}
					
					p2++;
					
					int	p3 = p2+1;
					
					int	dots = 0;
					
					while(p3<str.length()){
						
						char	c = str.charAt(p3);
						
						if ( c == '." ){
					
							dots++;
									
						}else if ( Character.isDigit( c )){
					
						}else{
							
							break;
						}
					
						p3++;
					}
					
					if ( dots == 3 ){
						
						return( str.substring(p2,p3));
					}
				}
			}
			
			pos	= p1+1;
		}
		
		informFailure( "ipnotfound"  );

		return( null );
	
public java.lang.StringgetDescription()

		return( description );
	
public java.lang.StringgetName()

		return( name );
	
public java.lang.StringgetURL()

		return( url );
	
protected voidinformFailure(java.lang.String msg_key)

		try{
			this_mon.enter();
	
			informFailure( msg_key, null );
			
		}finally{
			
			this_mon.exit();
		}
	
protected voidinformFailure(java.lang.String msg_key, java.lang.String extra)

		try{
			this_mon.enter();
	
			if ( !completed ){
			
				timeout_sem.releaseForever();
				
				String	message = MessageText.getString( MSG_KEY_ROOT + "." + msg_key );
				
				if ( extra != null ){
					
					message += ": " + extra;
				}
				
				for ( int i=0;i<listeners.size();i++){
					
					((ExternalIPCheckerServiceListener)listeners.elementAt(i)).checkFailed( this, message );
				}
			}
		}finally{
			
			this_mon.exit();
		}
	
protected voidinformSuccess(java.lang.String ip)

		try{
			this_mon.enter();			
			
			if ( !completed ){
				
				timeout_sem.releaseForever();

				for ( int i=0;i<listeners.size();i++){
					
					((ExternalIPCheckerServiceListener)listeners.elementAt(i)).checkComplete( this, ip );
				}
			}
		}finally{
			
			this_mon.exit();
		}
	
public voidinitiateCheck(long timeout)

		_clone().initiateCheckSupport(timeout);
	
protected voidinitiateCheckSupport(long timeout)

	
		Thread	t = 
			new AEThread("IPChecker")
			{
				public void
				runSupport()
				{
					try{
						
						initiateCheckSupport();
						
					}finally{
						
						setComplete();
					}
				}
			};
			
		t.setDaemon( true );
		
		t.start();
		
		if ( timeout > 0 ){
			
			Thread	t2 = 
				new AEThread("IPChecker2")
				{
					public void
					runSupport()
					{
						try{
							
							if ( !timeout_sem.reserve( timeout )){
							
								if ( !completed ){
									
									informFailure( "timeout" );
									
									setComplete();
								}
							}
						}catch( Throwable e ){
								
							Debug.printStackTrace( e );
						}
					}
				};
			
			t2.setDaemon( true );
		
			t2.start();
			
		}
	
protected abstract voidinitiateCheckSupport()

protected java.lang.StringloadPage(java.lang.String url_string)

		try{
		
			URL	url = new URL( url_string );

			HttpURLConnection	connection 	= null;
			InputStream			is			= null;
			
			try{
				connection = (HttpURLConnection)url.openConnection();
				
				int	response = connection.getResponseCode();
				
				if( response == HttpURLConnection.HTTP_ACCEPTED || response == HttpURLConnection.HTTP_OK ){
								
					is = connection.getInputStream();
					
					String	page = "";
					
					while( page.length() < MAX_PAGE_SIZE ){
						
						byte[]	buffer = new byte[2048];
						
						int	len = is.read( buffer );
						
						if ( len < 0 ){
							
							break;
						}
						
						page += new String(buffer, 0, len);
					}
					
					return( page );
					
				}else{
					
					informFailure( "httpinvalidresponse", "" + response );
					
					return( null );
				}
			}finally{
				
				try{
				
					if ( is != null ){
						
						is.close();
					}
					
					if ( connection != null ){
						
						connection.disconnect();
					}
				}catch( Throwable e){
					
					Debug.printStackTrace( e );
				}
			}
		}catch( Throwable e ){
			
			informFailure( "httploadfail", e.toString());
			
			return( null );
		}
	
public voidremoveListener(ExternalIPCheckerServiceListener l)

		try{
			this_mon.enter();
	
			listeners.removeElement( l );
			
		}finally{
			
			this_mon.exit();
		}
	
protected voidreportProgress(java.lang.String msg_key)

		try{
			this_mon.enter();
	
			reportProgress( msg_key,  null );
			
		}finally{
			
			this_mon.exit();
		}
	
protected voidreportProgress(java.lang.String msg_key, java.lang.String extra)

		try{
			this_mon.enter();
	
			if ( !completed ){
			
				String	message = MessageText.getString( MSG_KEY_ROOT.concat(".").concat(msg_key) );
				
				if ( extra != null ){
					
					message = message.concat(": ").concat(extra);
				}
				for ( int i=0;i<listeners.size();i++){
					
					((ExternalIPCheckerServiceListener)listeners.elementAt(i)).reportProgress( this, message );
				}
			}
		}finally{
			
			this_mon.exit();
		}
	
protected voidsetComplete()

		completed = true;