FileDocCategorySizeDatePackage
DHTUDPPacketHelper.javaAPI DocAzureus 3.0.3.47878Thu Jan 11 07:45:38 GMT 2007com.aelitis.azureus.core.dht.transport.udp.impl

DHTUDPPacketHelper

public class DHTUDPPacketHelper extends Object
author
parg

Fields Summary
public static final int
PACKET_MAX_BYTES
public static final int
ACT_REQUEST_PING
public static final int
ACT_REPLY_PING
public static final int
ACT_REQUEST_STORE
public static final int
ACT_REPLY_STORE
public static final int
ACT_REQUEST_FIND_NODE
public static final int
ACT_REPLY_FIND_NODE
public static final int
ACT_REQUEST_FIND_VALUE
public static final int
ACT_REPLY_FIND_VALUE
public static final int
ACT_REPLY_ERROR
public static final int
ACT_REPLY_STATS
public static final int
ACT_REQUEST_STATS
public static final int
ACT_DATA
public static final int
ACT_REQUEST_KEY_BLOCK
public static final int
ACT_REPLY_KEY_BLOCK
private static boolean
registered
Constructors Summary
Methods Summary
protected static voidregisterCodecs()

	
	  
	
	
		if ( registered ){
			
			return;
		}
	
		registered	= true;
					
		PRUDPPacketRequestDecoder	request_decoder =
			new PRUDPPacketRequestDecoder()
			{
				public PRUDPPacketRequest
				decode(
					PRUDPPacketHandler	handler,
					DataInputStream		is,
					long				connection_id,
					int					action,
					int					transaction_id )
				
					throws IOException
				{
					if ( handler == null ){
					
							// most likely cause is DHT packet ending up on the UDP tracker as it'll get
							// router here but with a null-handler
						
						throw( new IOException( "No handler available for DHT packet decode" ));
					}
					
					DHTUDPPacketNetworkHandler	network_handler = (DHTUDPPacketNetworkHandler)handler.getRequestHandler();

					if ( network_handler == null ){
						
							// we an get this after a port change and the old port listener is still running (e.g.
							// its still doing UDP tracker)
						
						throw( new IOException( "No network handler available for DHT packet decode" ));
					}
					
					switch( action ){
						case ACT_REQUEST_PING:
						{
							return( new DHTUDPPacketRequestPing(network_handler,is, connection_id,transaction_id));
						}
						case ACT_REQUEST_STORE:
						{
							return( new DHTUDPPacketRequestStore(network_handler,is, connection_id,transaction_id));
						}
						case ACT_REQUEST_FIND_NODE:
						{
							return( new DHTUDPPacketRequestFindNode(network_handler,is, connection_id,transaction_id));
						}
						case ACT_REQUEST_FIND_VALUE:
						{
							return( new DHTUDPPacketRequestFindValue(network_handler,is, connection_id,transaction_id));
						}
						case ACT_REQUEST_STATS:
						{
							return( new DHTUDPPacketRequestStats(network_handler,is, connection_id, transaction_id));
						}
						case ACT_DATA:
						{
							return( new DHTUDPPacketData(network_handler,is, connection_id, transaction_id));
						}
						case ACT_REQUEST_KEY_BLOCK:
						{
							return( new DHTUDPPacketRequestKeyBlock(network_handler,is, connection_id, transaction_id));
						}
						default:
						{
							throw( new IOException( "Unknown action type" ));
						}
					}
				}
			};
			
		Map	request_decoders = new HashMap();
		
		request_decoders.put( new Integer( ACT_REQUEST_PING ), request_decoder );
		request_decoders.put( new Integer( ACT_REQUEST_STORE ), request_decoder );
		request_decoders.put( new Integer( ACT_REQUEST_FIND_NODE ), request_decoder );
		request_decoders.put( new Integer( ACT_REQUEST_FIND_VALUE ), request_decoder );
		request_decoders.put( new Integer( ACT_REQUEST_STATS ), request_decoder );		
		request_decoders.put( new Integer( ACT_DATA ), request_decoder );
		request_decoders.put( new Integer( ACT_REQUEST_KEY_BLOCK ), request_decoder );
		
		PRUDPPacketRequest.registerDecoders( request_decoders );	
			
		
		
		PRUDPPacketReplyDecoder	reply_decoder =
			new PRUDPPacketReplyDecoder()
			{
				public PRUDPPacketReply
				decode(
					PRUDPPacketHandler	handler,
					DataInputStream		is,
					int					action,
					int					transaction_id )
				
					throws IOException
				{
					if ( handler == null ){
						
							// most likely cause is DHT packet ending up on the UDP tracker as it'll get
							// router here but with a null-handler
					
						throw( new IOException( "No handler available for DHT packet decode" ));
					}
					
					DHTUDPPacketNetworkHandler	network_handler = (DHTUDPPacketNetworkHandler)handler.getRequestHandler();
					
					if ( network_handler == null ){
						
						// we an get this after a port change and the old port listener is still running (e.g.
						// its still doing UDP tracker)
					
						throw( new IOException( "No network handler available for DHT packet decode" ));
					}
					
					switch( action ){
					
						case ACT_REPLY_PING:
						{
							return( new DHTUDPPacketReplyPing(network_handler,is, transaction_id));
						}
						case ACT_REPLY_STORE:
						{
							return( new DHTUDPPacketReplyStore(network_handler,is, transaction_id));
						}
						case ACT_REPLY_FIND_NODE:
						{
							return( new DHTUDPPacketReplyFindNode(network_handler, is, transaction_id));
						}
						case ACT_REPLY_FIND_VALUE:
						{
							return( new DHTUDPPacketReplyFindValue(network_handler, is, transaction_id));
						}
						case ACT_REPLY_ERROR:
						{
							return( new DHTUDPPacketReplyError(network_handler, is, transaction_id));
						}
						case ACT_REPLY_STATS:
						{
							return( new DHTUDPPacketReplyStats( network_handler, is, transaction_id));
						}
						case ACT_REPLY_KEY_BLOCK:
						{
							return( new DHTUDPPacketReplyKeyBlock( network_handler, is, transaction_id));
						}
						default:
						{
							throw( new IOException( "Unknown action type" ));
						}
					}
				}
			};
			
		Map	reply_decoders = new HashMap();
		
		reply_decoders.put( new Integer( ACT_REPLY_PING ), reply_decoder );
		reply_decoders.put( new Integer( ACT_REPLY_STORE ), reply_decoder );
		reply_decoders.put( new Integer( ACT_REPLY_FIND_NODE ), reply_decoder );
		reply_decoders.put( new Integer( ACT_REPLY_FIND_VALUE ), reply_decoder );
		reply_decoders.put( new Integer( ACT_REPLY_ERROR ), reply_decoder );
		reply_decoders.put( new Integer( ACT_REPLY_STATS ), reply_decoder );
		reply_decoders.put( new Integer( ACT_REPLY_KEY_BLOCK ), reply_decoder );
		
		PRUDPPacketReply.registerDecoders( reply_decoders );