FileDocCategorySizeDatePackage
DHTUDPPacketHandler.javaAPI DocAzureus 3.0.3.47491Thu Feb 09 19:42:44 GMT 2006com.aelitis.azureus.core.dht.transport.udp.impl.packethandler

DHTUDPPacketHandler

public class DHTUDPPacketHandler extends Object

Fields Summary
private DHTUDPPacketHandlerFactory
factory
private int
network
private com.aelitis.net.udp.uc.PRUDPPacketHandler
packet_handler
private DHTUDPRequestHandler
request_handler
private DHTUDPPacketHandlerStats
stats
private boolean
test_network_alive
private int
BLOOM_FILTER_SIZE
private static final int
BLOOM_ROTATION_PERIOD
private com.aelitis.azureus.core.util.bloom.BloomFilter
bloom1
private com.aelitis.azureus.core.util.bloom.BloomFilter
bloom2
private long
last_bloom_rotation_time
Constructors Summary
protected DHTUDPPacketHandler(DHTUDPPacketHandlerFactory _factory, int _network, com.aelitis.net.udp.uc.PRUDPPacketHandler _packet_handler, DHTUDPRequestHandler _request_handler)

	
	
	 
			
									
					
				 
	
		factory			= _factory;
		network			= _network;
		packet_handler	= _packet_handler;
		request_handler	= _request_handler;
		
		bloom1	= BloomFilterFactory.createAddOnly( BLOOM_FILTER_SIZE );
		bloom2	= BloomFilterFactory.createAddOnly( BLOOM_FILTER_SIZE );
		
		stats = new DHTUDPPacketHandlerStats( packet_handler );
	
Methods Summary
public voiddestroy()

		factory.destroy( this );
	
protected intgetNetwork()

		return( network );
	
protected com.aelitis.net.udp.uc.PRUDPPacketHandlergetPacketHandler()

		return( packet_handler );
	
protected DHTUDPRequestHandlergetRequestHandler()

		return( request_handler );
	
public DHTUDPPacketHandlerStatsgetStats()

		return( stats );
	
protected voidreceive(com.aelitis.azureus.core.dht.transport.udp.impl.DHTUDPPacketRequest request)

			// incoming request
		
		if ( test_network_alive ){
		
			request.setAddress( AddressUtils.adjustDHTAddress( request.getAddress(), false ));

				// an alien request is one that originates from a peer that we haven't recently
				// talked to
			
			boolean	alien = !bloom1.contains( request.getAddress().getAddress().getAddress());
			
			stats.packetReceived( request.getSerialisedSize());
		
			request_handler.process( request, alien );
		}
	
public voidsend(com.aelitis.azureus.core.dht.transport.udp.impl.DHTUDPPacketRequest request, java.net.InetSocketAddress destination_address)

		destination_address	= AddressUtils.adjustDHTAddress( destination_address, true );
		
			// one way send (no matching reply expected )
		
		try{
			
			request.setNetwork( network );
			
			if ( test_network_alive ){
				
				packet_handler.send( request, destination_address );
			}
			
		}catch( PRUDPPacketHandlerException e ){
			
			throw( new DHTUDPPacketHandlerException( e ));
			
		}finally{
			
			stats.packetSent( request.getSerialisedSize() );
		}
	
public voidsend(com.aelitis.azureus.core.dht.transport.udp.impl.DHTUDPPacketReply reply, java.net.InetSocketAddress destination_address)

		destination_address	= AddressUtils.adjustDHTAddress( destination_address, true );

			// send reply to a request
		
		try{
			reply.setNetwork( network );
			
				// outgoing request
					
			if ( test_network_alive ){
				
				packet_handler.send( reply, destination_address );
			}
				
		}catch( PRUDPPacketHandlerException e ){
			
			throw( new DHTUDPPacketHandlerException( e ));
		
		}finally{
			
			stats.packetSent( reply.getSerialisedSize());
		}	
	
public voidsendAndReceive(com.aelitis.azureus.core.dht.transport.udp.impl.DHTUDPPacketRequest request, java.net.InetSocketAddress destination_address, DHTUDPPacketReceiver receiver, long timeout, int priority)

			// send and receive pair
		
		destination_address	= AddressUtils.adjustDHTAddress( destination_address, true );
		
		try{
			request.setNetwork( network );
			
			if ( test_network_alive ){
				
			    long diff = SystemTime.getCurrentTime() - last_bloom_rotation_time;
			    
			    if( diff < 0 || diff > BLOOM_ROTATION_PERIOD ) {
			    
			    	// System.out.println( "bloom rotate: entries = " + bloom1.getEntryCount() + "/" + bloom2.getEntryCount());
			    	
			    	bloom1 = bloom2;
			    	
			    	bloom2 = BloomFilterFactory.createAddOnly( BLOOM_FILTER_SIZE );
			        
			        last_bloom_rotation_time = SystemTime.getCurrentTime();
			    }

			    byte[]	address_bytes = destination_address.getAddress().getAddress();
			    
			    bloom1.add( address_bytes );
			    bloom2.add( address_bytes );
			    
				packet_handler.sendAndReceive( 
					request, 
					destination_address, 
					new PRUDPPacketReceiver()
					{
						public void
						packetReceived(
							PRUDPPacketHandlerRequest	request,
							PRUDPPacket					packet,
							InetSocketAddress			from_address )
						{
							DHTUDPPacketReply	reply = (DHTUDPPacketReply)packet;
							
							stats.packetReceived( reply.getSerialisedSize() );
							
							if ( reply.getNetwork() == network ){
								
								receiver.packetReceived(reply, from_address, request.getElapsedTime());
								
							}else{
								
								Debug.out( "Non-matching network reply received" );
								
								receiver.error( new DHTUDPPacketHandlerException( new Exception( "Non-matching network reply received" )));
							}
						}
			
						public void
						error(
							PRUDPPacketHandlerException	e )
						{
							receiver.error( new DHTUDPPacketHandlerException( e ));
						}
					}, 
					timeout, 
					priority );
			}else{
				
				receiver.error( new DHTUDPPacketHandlerException( new Exception( "Test network disabled" )));
			}
			
		}catch( PRUDPPacketHandlerException e ){
			
			throw( new DHTUDPPacketHandlerException(e ));
			
		}finally{
			
			stats.packetSent( request.getSerialisedSize() );
		}
	
public voidsetDelays(int send_delay, int receive_delay, int queued_request_timeout)

			// TODO: hmm
		
		packet_handler.setDelays( send_delay, receive_delay, queued_request_timeout );
	
public voidtestNetworkAlive(boolean alive)

		test_network_alive	= alive;