FileDocCategorySizeDatePackage
DHTUDPPacketRequest.javaAPI DocAzureus 3.0.3.47939Tue Aug 01 14:20:14 BST 2006com.aelitis.azureus.core.dht.transport.udp.impl

DHTUDPPacketRequest

public class DHTUDPPacketRequest extends com.aelitis.net.udp.uc.PRUDPPacketRequest implements DHTUDPPacket
author
parg

Fields Summary
public static final int
DHT_HEADER_SIZE
private DHTTransportUDPImpl
transport
private byte
protocol_version
private byte
vendor_id
private int
network
private byte
originator_version
private long
originator_time
private InetSocketAddress
originator_address
private int
originator_instance_id
private long
skew
Constructors Summary
public DHTUDPPacketRequest(DHTTransportUDPImpl _transport, int _type, long _connection_id, DHTTransportUDPContactImpl _local_contact, DHTTransportUDPContactImpl _remote_contact)

	
	
	
							
										
									
				
				 
	
		super( _type, _connection_id );
		
		transport	= _transport;
		
			// serialisation constructor
		
		protocol_version		= _remote_contact.getProtocolVersion();		
		
			// the target might be at a higher protocol version that us, so trim back if necessary
			// as we obviously can't talk a higher version than what we are!
		
		if ( protocol_version > _transport.getProtocolVersion() ){
			
			protocol_version = _transport.getProtocolVersion();
		}
		
		originator_address		= _local_contact.getExternalAddress();
		originator_instance_id	= _local_contact.getInstanceID();
		originator_time			= SystemTime.getCurrentTime();
	
protected DHTUDPPacketRequest(com.aelitis.azureus.core.dht.transport.udp.impl.packethandler.DHTUDPPacketNetworkHandler network_handler, DataInputStream is, int type, long con_id, int trans_id)

		super( type, con_id, trans_id );
		
			// deserialisation constructor
		
		protocol_version	= is.readByte();
		
		if ( protocol_version < DHTTransportUDP.PROTOCOL_VERSION_MIN ){
			
			throw( new IOException( "Invalid DHT protocol version, please update Azureus" ));
		}
		
		if ( protocol_version >= DHTTransportUDP.PROTOCOL_VERSION_VENDOR_ID ){
			
			vendor_id	= is.readByte();			
		}
		
		if ( protocol_version >= DHTTransportUDP.PROTOCOL_VERSION_NETWORKS ){
			
			network	= is.readInt();
		}

			// we can only get the correct transport after decoding the network...
	
		transport = network_handler.getTransport( this );

		if ( protocol_version >= DHTTransportUDP.PROTOCOL_VERSION_FIX_ORIGINATOR ){
			
			originator_version = is.readByte();
			
		}else{
			
				// this should be set correctly in the post-deserialise code, however default
				// it for now
			
			originator_version = protocol_version;
		}
		
		originator_address		= DHTUDPUtils.deserialiseAddress( is );
		
		originator_instance_id	= is.readInt();
		
		originator_time			= is.readLong();
		
			// We maintain a rough view of the clock diff between them and us,
			// times are then normalised appropriately. 
			// If the skew is positive then this means our clock is ahead of their
			// clock. Thus any times they send us will need to have the skew added in
			// so that they're correct relative to us.
			// For example: X has clock = 01:00, they create a value that expires at
			// X+8 hours 09:00. They send X to us. Our clock is an hour ahead (skew=+1hr)
			// We receive it at 02:00 (our time) and therefore time it out an hour early.
			// We therefore need to adjust the creation time to be 02:00.
		
			// Likewise, when we return a time to a caller we need to adjust by - skew to
			// put the time into their frame of reference.
		
		skew = SystemTime.getCurrentTime() - originator_time;
	
Methods Summary
protected longgetClockSkew()

		return( skew );
	
public intgetNetwork()

		return( network );
	
protected java.net.InetSocketAddressgetOriginatorAddress()

		return( originator_address );
	
protected intgetOriginatorInstanceID()

		return( originator_instance_id );
	
protected bytegetOriginatorVersion()

		return( originator_version );
	
public bytegetProtocolVersion()

		return( protocol_version );
	
public java.lang.StringgetString()

		return( super.getString() + ",[prot=" + protocol_version + ",ven=" + vendor_id + ",net="+network+",ov=" + originator_version + "]");
	
public DHTTransportUDPImplgetTransport()

		return( transport );
	
protected bytegetVendorID()

		return( vendor_id );
	
protected voidpostDeserialise(java.io.DataInputStream is)

		if ( protocol_version < DHTTransportUDP.PROTOCOL_VERSION_FIX_ORIGINATOR ){

			if ( is.available() > 0 ){
				
				originator_version	= is.readByte();
							
			}else{
				
				originator_version = protocol_version;
			}
			
				// if the originator is a higher version than us then we can't do anything sensible
				// working at their version (e.g. we can't reply to them using that version). 
				// Therefore trim their perceived version back to something we can deal with
			
			if ( originator_version > getTransport().getProtocolVersion() ){
				
				originator_version = getTransport().getProtocolVersion();
			}
		}
	
protected voidpostSerialise(java.io.DataOutputStream os)

		if ( protocol_version < DHTTransportUDP.PROTOCOL_VERSION_FIX_ORIGINATOR ){
			
				// originator version is at tail so it works with older versions
			
			os.writeByte( getTransport().getProtocolVersion());
		}
	
public voidserialise(java.io.DataOutputStream os)

		super.serialise(os);

			// add to this and you need to amend HEADER_SIZE above
		
		os.writeByte( protocol_version );		
		
		if ( protocol_version >= DHTTransportUDP.PROTOCOL_VERSION_VENDOR_ID ){
						
			os.writeByte( DHTTransportUDP.VENDOR_ID_ME );
		}
		
		if ( protocol_version >= DHTTransportUDP.PROTOCOL_VERSION_NETWORKS ){
			
			os.writeInt( network );
		}

		if ( protocol_version >= DHTTransportUDP.PROTOCOL_VERSION_FIX_ORIGINATOR ){
		
				// originator version
			
			os.writeByte( getTransport().getProtocolVersion());
		}
		
		try{
			DHTUDPUtils.serialiseAddress( os, originator_address );
			
		}catch( DHTTransportException	e ){
			
			throw( new IOException( e.getMessage()));
		}
		
		os.writeInt( originator_instance_id );
		
		os.writeLong( originator_time );
	
public voidsetNetwork(int _network)

		network	= _network;
	
protected voidsetOriginatorAddress(java.net.InetSocketAddress address)

		originator_address	= address;