FileDocCategorySizeDatePackage
DHTTransportLoopbackImpl.javaAPI DocAzureus 3.0.3.414196Thu Mar 16 13:50:34 GMT 2006com.aelitis.azureus.core.dht.transport.loopback

DHTTransportLoopbackImpl

public class DHTTransportLoopbackImpl extends Object implements DHTTransport
author
parg

Fields Summary
public static byte
VERSION
public static int
LATENCY
public static int
FAIL_PERCENTAGE
private static long
node_id_seed_next
private static Map
node_map
private static List
dispatch_queue
private static AESemaphore
dispatch_queue_sem
private static AEMonitor
class_mon
private byte[]
node_id
private DHTTransportContact
local_contact
private int
id_byte_length
private DHTTransportRequestHandler
request_handler
private com.aelitis.azureus.core.dht.transport.util.DHTTransportStatsImpl
stats
private List
listeners
Constructors Summary
public DHTTransportLoopbackImpl(int _id_byte_length)

	
		id_byte_length	= _id_byte_length;
		
		try{
			class_mon.enter();
			
			byte[]	temp = new SHA1Simple().calculateHash( ( "" + ( node_id_seed_next++ )).getBytes());
			
			node_id	= new byte[id_byte_length];
			
			System.arraycopy( temp, 0, node_id, 0, id_byte_length );
			
			node_map.put( new HashWrapper( node_id ), this );
			
			local_contact	= new DHTTransportLoopbackContactImpl( this, node_id );
		}finally{
			
			class_mon.exit();
		}
	
Methods Summary
public voidaddListener(DHTTransportListener l)

		listeners.add(l);
	
public voidexportContact(DHTTransportContact contact, java.io.DataOutputStream os)

		os.writeInt( VERSION );
		
		os.writeInt( id_byte_length );
		
		os.write( contact.getID());
	
protected com.aelitis.azureus.core.dht.transport.loopback.DHTTransportLoopbackImplfindTarget(byte[] id)

		try{
			class_mon.enter();
			
			return((DHTTransportLoopbackImpl)node_map.get( new HashWrapper( id )));
		}finally{
			
			class_mon.exit();
		}
	
public DHTTransportContactgetLocalContact()

		return( local_contact );
	
public intgetNetwork()

		return( DHT.NW_MAIN );
	
public static DHTTransportStatsgetOverallStats()

	
	  
	
	
		try{
			class_mon.enter();
		
			DHTTransportStatsImpl	overall_stats = new DHTTransportLoopbackStatsImpl( VERSION );
			
			Iterator	it = node_map.values().iterator();
			
			while( it.hasNext()){
				
				overall_stats.add((DHTTransportStatsImpl)((DHTTransportLoopbackImpl)it.next()).getStats());
			}
			
			return( overall_stats );
			
		}finally{
			
			class_mon.exit();
		}
	
public intgetPort()

		return( 0 );
	
public bytegetProtocolVersion()

	
	 
	
	
		return( VERSION );
	
public DHTTransportContact[]getReachableContacts()

		return( new DHTTransportContact[0] );
	
protected DHTTransportRequestHandlergetRequestHandler()

		return( request_handler );
	
public DHTTransportStatsgetStats()

		return( stats );
	
public DHTTransportContactimportContact(java.io.DataInputStream is)

		int	version = is.readInt();
		
		if ( version != VERSION ){
			
			throw( new IOException( "Unsuported version" ));

		}
		int	id_len	= is.readInt();
		
		if ( id_len != id_byte_length ){
			
			throw( new IOException( "Imported contact has incorrect ID length" ));
		}
		
		byte[]	id = new byte[id_byte_length];
		
		is.read( id );
		
		DHTTransportContact contact = new DHTTransportLoopbackContactImpl( this, id );
		
		request_handler.contactImported( contact );
		
		return( contact );
	
public booleanisReachable()

		return( true );
	
public byte[]readTransfer(DHTTransportProgressListener listener, DHTTransportContact target, byte[] handler_key, byte[] key, long timeout)

		throw( new DHTTransportException("not imp"));
	
public voidregisterTransferHandler(byte[] handler_key, DHTTransportTransferHandler handler)

	
public voidremoveContact(DHTTransportContact contact)

	
public voidremoveListener(DHTTransportListener l)

		listeners.remove(l);
	
protected voidrun(AERunnable r)

		try{
			class_mon.enter();
			
			dispatch_queue.add( r );
			
		}finally{
			
			class_mon.exit();
		}
		
		dispatch_queue_sem.release();
	
public voidsendFindNode(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[] nid)

		AERunnable	runnable = 
			new AERunnable()
			{
				public void
				runSupport()
				{
					sendFindNodeSupport( contact, handler, nid );
				}
			};
		
		run( runnable );
	
public voidsendFindNodeSupport(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[] nid)

		DHTTransportLoopbackImpl	target = findTarget( contact.getID());
		
		stats.findNodeSent(null);
		
		if ( target == null  || triggerFailure() ){
		
			stats.findNodeFailed();
			
			handler.failed(contact,new Exception( "failed"));
			
		}else{
			
			stats.findNodeOK();
			
			DHTTransportContact temp = new DHTTransportLoopbackContactImpl( target, node_id );
			
			DHTTransportContact[] res =
				target.getRequestHandler().findNodeRequest( 
					temp,
					nid );
			
			contact.setRandomID( temp.getRandomID());
			
			DHTTransportContact[] trans_res = new DHTTransportContact[res.length];
																	  														  
			for (int i=0;i<res.length;i++){
				
				trans_res[i] = new DHTTransportLoopbackContactImpl( this, res[i].getID());
			}
			
			handler.findNodeReply( contact, trans_res );
		}
	
public voidsendFindValue(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[] key, int max, byte flags)

		AERunnable	runnable = 
			new AERunnable()
			{
				public void
				runSupport()
				{
					sendFindValueSupport( contact, handler, key, max, flags );
				}
			};
		
		run( runnable );
	
public voidsendFindValueSupport(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[] key, int max, byte flags)

		DHTTransportLoopbackImpl	target = findTarget( contact.getID());
		
		stats.findValueSent(null);
		
		if ( target == null  || triggerFailure()){
		
			stats.findValueFailed();
			
			handler.failed(contact,new Exception( "failed"));
			
		}else{
			
			stats.findValueOK();
			
			DHTTransportFindValueReply find_res =
				target.getRequestHandler().findValueRequest( 
					new DHTTransportLoopbackContactImpl( target, node_id ),
					key, max, flags );
			
			if ( find_res.hit()){
				
				handler.findValueReply( contact, find_res.getValues(), find_res.getDiversificationType(), false );

			}else if ( find_res.blocked()){
				
				handler.keyBlockRequest( contact, find_res.getBlockedKey(), find_res.getBlockedSignature() );
				
				handler.failed( contact, new Throwable( "key blocked" ));
				
			}else{
				
				DHTTransportContact[]	res  = find_res.getContacts();
				
				DHTTransportContact[] trans_res = new DHTTransportContact[res.length];
				  
				for (int i=0;i<res.length;i++){
				
					trans_res[i] = new DHTTransportLoopbackContactImpl( this, res[i].getID());
				}

				handler.findValueReply( contact, trans_res );
				
			}
		}
	
public voidsendKeyBlock(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[] request, byte[] sig)

		AERunnable	runnable = 
			new AERunnable()
			{
				public void
				runSupport()
				{
					sendKeyBlockSupport( contact, handler, request, sig );
				}
			};
		
		run( runnable );
	
public voidsendKeyBlockSupport(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[] request, byte[] sig)

		DHTTransportLoopbackImpl	target = findTarget( contact.getID());
		
		stats.keyBlockSent(null);
		
		if ( target == null || triggerFailure()){
		
			stats.keyBlockFailed();
			
			handler.failed(contact, new Exception( "failed" ));
			
		}else{
			
			stats.keyBlockOK();
			
			target.getRequestHandler().keyBlockRequest(
						new DHTTransportLoopbackContactImpl( target, node_id ),
						request, sig );
			
			handler.keyBlockReply(contact);
		}
	
public voidsendPing(DHTTransportContact contact, DHTTransportReplyHandler handler)

		AERunnable	runnable = 
			new AERunnable()
			{
				public void
				runSupport()
				{
					sendPingSupport( contact, handler );
				}
			};
		
		run( runnable );
	
public voidsendPingSupport(DHTTransportContact contact, DHTTransportReplyHandler handler)

		DHTTransportLoopbackImpl	target = findTarget( contact.getID());
		
		stats.pingSent(null);
		
		if ( target == null || triggerFailure()){
		
			stats.pingFailed();
			
			handler.failed(contact, new Exception( "failed" ));
			
		}else{
			
			stats.pingOK();
			
			target.getRequestHandler().pingRequest( new DHTTransportLoopbackContactImpl( target, node_id ));
			
			handler.pingReply(contact,0);
		}
	
public voidsendStats(DHTTransportContact contact, DHTTransportReplyHandler handler)

		AERunnable	runnable = 
			new AERunnable()
			{
				public void
				runSupport()
				{
					sendStatsSupport( contact, handler );
				}
			};
		
		run( runnable );
	
public voidsendStatsSupport(DHTTransportContact contact, DHTTransportReplyHandler handler)

		DHTTransportLoopbackImpl	target = findTarget( contact.getID());
		
		stats.statsSent(null);
		
		if ( target == null || triggerFailure()){
		
			stats.statsFailed();
			
			handler.failed(contact, new Exception( "failed"));
			
		}else{
			
			stats.statsOK();
			
			DHTTransportFullStats res = target.getRequestHandler().statsRequest( new DHTTransportLoopbackContactImpl( target, node_id ));
			
			handler.statsReply(contact,res);
		}
	
public voidsendStore(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[][] keys, DHTTransportValue[][] value_sets)

		AERunnable	runnable = 
			new AERunnable()
			{
				public void
				runSupport()
				{
					sendStoreSupport( contact, handler, keys, value_sets );
				}
			};
		
		run( runnable );
	
public voidsendStoreSupport(DHTTransportContact contact, DHTTransportReplyHandler handler, byte[][] keys, DHTTransportValue[][] value_sets)

		DHTTransportLoopbackImpl	target = findTarget( contact.getID());
		
		stats.storeSent(null);
		
		if ( target == null  || triggerFailure()){
		
			stats.storeFailed();
			
			handler.failed(contact,new Exception( "failed"));
			
		}else{
			
			stats.storeOK();
			
			DHTTransportContact	temp = new DHTTransportLoopbackContactImpl( target, node_id );
			
			temp.setRandomID( contact.getRandomID());
			
			DHTTransportStoreReply	rep = 
				target.getRequestHandler().storeRequest( 
					temp,
					keys, value_sets );
			
			if ( rep.blocked()){
				
				handler.keyBlockRequest( contact, rep.getBlockRequest(), rep.getBlockSignature());
				
				handler.failed( contact, new Throwable( "key blocked" ));
				
			}else{
				
				handler.storeReply( contact, rep.getDiversificationTypes());
			}
		}
	
public static voidsetFailPercentage(int p)

		FAIL_PERCENTAGE	= p;
	
public static voidsetLatency(int _latency)

		LATENCY	= _latency;
	
public voidsetPort(int port)

	
	
public voidsetRequestHandler(DHTTransportRequestHandler _request_handler)

		request_handler = new DHTTransportRequestCounter( _request_handler, stats );

	
public booleansupportsStorage()

		return( true );
	
protected booleantriggerFailure()

		return( Math.random()*100 < FAIL_PERCENTAGE );
	
public byte[]writeReadTransfer(DHTTransportProgressListener listener, DHTTransportContact target, byte[] handler_key, byte[] data, long timeout)

		throw( new DHTTransportException("not imp"));
	
public voidwriteTransfer(DHTTransportProgressListener listener, DHTTransportContact target, byte[] handler_key, byte[] key, byte[] data, long timeout)

		throw( new DHTTransportException("not imp"));