FileDocCategorySizeDatePackage
SSDPIGDImpl.javaAPI DocAzureus 3.0.3.47366Fri May 12 10:02:56 BST 2006com.aelitis.net.upnp.impl.ssdp

SSDPIGDImpl

public class SSDPIGDImpl extends Object implements SSDPIGD, UPnPSSDPListener
author
parg

Fields Summary
private UPnPImpl
upnp
private SSDPCore
ssdp_core
private boolean
first_result
private long
last_explicit_search
private List
listeners
protected AEMonitor
this_mon
Constructors Summary
public SSDPIGDImpl(UPnPImpl _upnp, String[] _selected_interfaces)


	
	
	
				
				 
	
		 
		
		upnp	= _upnp;
		
		ssdp_core	= 
			SSDPCore.getSingleton( 
				upnp.getAdapter(),
				UPnPSSDP.SSDP_GROUP_ADDRESS,
				UPnPSSDP.SSDP_GROUP_PORT,
				UPnPSSDP.SSDP_CONTROL_PORT,
				_selected_interfaces );
		
		ssdp_core.addListener( this );
	
Methods Summary
public voidaddListener(SSDPIGDListener l)

		listeners.add( l );
	
protected boolean[]bytesToBits(byte[] bytes)

		boolean[]	res = new boolean[bytes.length*8];
		
		for (int i=0;i<bytes.length;i++){
			
			byte	b = bytes[i];
			
			for (int j=0;j<8;j++){
				
				res[i*8+j] = (b&(byte)(0x01<<(7-j))) != 0;
			}
		}
				
		return( res );
	
protected voidgotAlive(java.lang.String usn, java.net.URL location)

		for (int i=0;i<listeners.size();i++){
			
			try{
				((SSDPIGDListener)listeners.get(i)).rootAlive( usn, location );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
protected voidgotRoot(java.net.NetworkInterface network_interface, java.net.InetAddress local_address, java.lang.String usn, java.net.URL location)

		for (int i=0;i<listeners.size();i++){
			
			try{
				((SSDPIGDListener)listeners.get(i)).rootDiscovered( network_interface, local_address, usn, location );
				
			}catch( Throwable e ){
			
				Debug.printStackTrace(e);
			}
		}
	
public voidinterfaceChanged(java.net.NetworkInterface network_interface)

		for (int i=0;i<listeners.size();i++){
			
			try{
				((SSDPIGDListener)listeners.get(i)).interfaceChanged( network_interface );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
protected voidlostRoot(java.net.InetAddress local_address, java.lang.String usn)

		for (int i=0;i<listeners.size();i++){
		
			try{
				((SSDPIGDListener)listeners.get(i)).rootLost( local_address, usn );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace(e);
			}
		}
	
protected voidqueryLoop()

		while(true){
			
			try{
				search();
				
				Thread.sleep( 60000 );
				
			}catch( Throwable e ){
				
				Debug.printStackTrace( e );
			}
			
		}
	
public voidreceivedNotify(java.net.NetworkInterface network_interface, java.net.InetAddress local_address, java.net.InetAddress originator, java.lang.String usn, java.net.URL location, java.lang.String nt, java.lang.String nts)

		try{
			this_mon.enter();

			if ( nt.indexOf( "upnp:rootdevice" ) != -1 ){
				
				if ( nts.indexOf("alive") != -1 ){
					
						// alive can be reported on any interface
					
					try{

						InetAddress	dev = InetAddress.getByName( location.getHost());
						
						byte[]	dev_bytes = dev.getAddress();
														
						boolean[]	dev_bits = bytesToBits( dev_bytes );
						
							// try and work out what bind address this location corresponds to
						
						NetworkInterface	best_ni 	= null;
						InetAddress			best_addr	= null;
						
						int	best_prefix	= 0;
						
						Enumeration network_interfaces = NetworkInterface.getNetworkInterfaces();
						
						while (network_interfaces.hasMoreElements()){
							
							NetworkInterface this_ni = (NetworkInterface)network_interfaces.nextElement();
													
							Enumeration ni_addresses = this_ni.getInetAddresses();
							
							while (ni_addresses.hasMoreElements()){
								
								InetAddress this_address = (InetAddress)ni_addresses.nextElement();
								
								byte[]	this_bytes = this_address.getAddress();
								
								if ( dev_bytes.length == this_bytes.length ){
									
									boolean[]	this_bits = bytesToBits( this_bytes );

									for (int i=0;i<this_bits.length;i++){
										
										if ( dev_bits[i] != this_bits[i] ){
											
											break;
										}
										
										if ( i > best_prefix ){
											
											best_prefix	= i;
											
											best_ni		= this_ni;
											best_addr	= this_address;
										}
									}
								}
							}
						}
						
						if ( best_ni != null ){
							
							if ( first_result ){
								
								upnp.log( location + " -> " + best_ni.getDisplayName() + "/" + best_addr + " (prefix=" + (best_prefix + 1 ) + ")");
							}
							
							gotRoot( best_ni, best_addr, usn, location );
							
						}else{
							
							gotAlive( usn, location );
						}
					}catch( Throwable e ){
						
						gotAlive( usn, location );
					}
				}else if ( nts.indexOf( "byebye") != -1 ){
						
					lostRoot( local_address, usn );
				}
			}
		}finally{

			first_result	= false;
			
			this_mon.exit();
		}
	
public voidreceivedResult(java.net.NetworkInterface network_interface, java.net.InetAddress local_address, java.net.InetAddress originator, java.lang.String usn, java.net.URL location, java.lang.String st, java.lang.String al)

		try{
			this_mon.enter();

			if ( st.equalsIgnoreCase( "upnp:rootdevice" )){
				
				gotRoot( network_interface, local_address, usn, location );
			}
		}finally{
			
			first_result	= false;
			
			this_mon.exit();
		}
	
public java.lang.String[]receivedSearch(java.net.NetworkInterface network_interface, java.net.InetAddress local_address, java.net.InetAddress originator, java.lang.String ST)

		// not interested, loopback or other search
		
		return( null );
	
public voidremoveListener(SSDPIGDListener l)

		listeners.remove(l);
	
protected voidsearch()

		ssdp_core.search( "upnp:rootdevice" );
	
public voidsearchNow()

		long	now = SystemTime.getCurrentTime();
		
		if ( now - last_explicit_search < 10000 ){
			
			return;
		}
		
		last_explicit_search	= now;
		
		search();
	
public voidstart()

		try{	
			upnp.getAdapter().createThread(
					"SSDP:queryLoop",
					new AERunnable()
					{
						public void
						runSupport()
						{
							queryLoop();
						}	
					});
			
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			
			throw( new UPnPException( "Failed to initialise SSDP", e ));
		}