FileDocCategorySizeDatePackage
PHETester.javaAPI DocAzureus 3.0.3.49974Thu Jan 11 07:45:38 GMT 2007com.aelitis.azureus.core.networkmanager.impl.test

PHETester

public class PHETester extends Object

Fields Summary
private final com.aelitis.azureus.core.networkmanager.VirtualChannelSelector
connect_selector
private byte[]
TEST_HEADER
private static boolean
OUTGOING_PLAIN
private static byte[]
shared_secret
Constructors Summary
public PHETester()

	
	
	
	
		ProtocolDecoder.addSecrets( new byte[][]{ shared_secret });
		
		VirtualServerChannelSelector
			accept_server = VirtualServerChannelSelectorFactory.createNonBlocking( 
					new InetSocketAddress( 8765 ), 
					0, 
					new VirtualServerChannelSelector.SelectListener() 
					{
						public void 
						newConnectionAccepted( 
							ServerSocketChannel	server,
							SocketChannel 		channel ) 
						{      
							incoming( channel );
						}
					});
		
		accept_server.start();
	
		new Thread()
		{
			public void
			run()
			{
				while( true ){
					try{
						connect_selector.select( 100 );
					}
					catch( Throwable t ) {
					  Debug.out( "connnectSelectLoop() EXCEPTION: ", t );
					}
				}
			}
		}.start();
		
		outgoings();
	
Methods Summary
protected voidincoming(java.nio.channels.SocketChannel channel)

		try{
			TransportHelper	helper = new TCPTransportHelper( channel );
			
			final ProtocolDecoderInitial	decoder = 
				new ProtocolDecoderInitial( 
						helper, 
						null,
						false,
						null,
						new ProtocolDecoderAdapter()
						{
							public void
							decodeComplete(
								ProtocolDecoder	decoder,
								ByteBuffer		remaining_initial_data )
							{
								System.out.println( "incoming decode complete: " +  decoder.getFilter().getName());
																
								readStream( "incoming", decoder.getFilter() );
								
								writeStream( "ten fat monkies", decoder.getFilter() );
							}
							
							public void
							decodeFailed(
								ProtocolDecoder	decoder,
								Throwable			cause )
							{
								System.out.println( "incoming decode failed: " + Debug.getNestedExceptionMessage(cause));
							}
							
							public void
							gotSecret(
								byte[]				session_secret )
							{
							}

							public int
							getMaximumPlainHeaderLength()
							{
								return( TEST_HEADER.length );
							}
							
							public int
							matchPlainHeader(
								ByteBuffer			buffer )
							{
								int	pos = buffer.position();
								int lim = buffer.limit();
								
								buffer.flip();
								
								boolean	match = buffer.compareTo( ByteBuffer.wrap( TEST_HEADER )) == 0;
								
								buffer.position( pos );
								buffer.limit( lim );
								
								System.out.println( "Match - " + match );
								
								return( match?ProtocolDecoderAdapter.MATCH_CRYPTO_NO_AUTO_FALLBACK:ProtocolDecoderAdapter.MATCH_NONE );
							}
						});
		}catch( Throwable e ){
			
			e.printStackTrace();
		}
	
public static voidmain(java.lang.String[] args)

		AEDiagnostics.startup();
		
		// OUTGOING_PLAIN	= true;
		
		COConfigurationManager.setParameter( "network.transport.encrypted.require", true );
		COConfigurationManager.setParameter( "network.transport.encrypted.min_level", "Plain" );
						
		new PHETester();
		
		try{
			Thread.sleep(10000000);
			
		}catch( Throwable e ){
			
		}
	
protected voidoutgoing()

		try{			
			final SocketChannel	channel = SocketChannel.open();
			
			channel.configureBlocking( false );
		
			if ( channel.connect( new InetSocketAddress("localhost", 8765 ))){
							
				outgoing( channel );
				
			}else{
				
				connect_selector.register(
					channel,
					new VirtualSelectorListener()
					{
						public boolean 
						selectSuccess(
							VirtualChannelSelector selector, SocketChannel sc, Object attachment)
						{
							try{
								if ( channel.finishConnect()){
									
									outgoing( channel );
									
									return( true );
								}else{
									
									throw( new IOException( "finishConnect failed" ));
								}
							}catch( Throwable e ){
								
								e.printStackTrace();
								
								return( false );
							}
						}
	
						public void 
						selectFailure(
								VirtualChannelSelector selector, SocketChannel sc, Object attachment, Throwable msg)
						{
							msg.printStackTrace();
						}
						 					
					},
					null );
			}
		}catch( Throwable e ){
			
			e.printStackTrace();
		}
	
protected voidoutgoing(java.nio.channels.SocketChannel channel)

		try{

			if ( OUTGOING_PLAIN ){
				
				writeStream( TEST_HEADER,  channel);
				
				writeStream( "two jolly porkers".getBytes(), channel );
				
			}else{			
				TransportHelper	helper = new TCPTransportHelper( channel );

				final ProtocolDecoderInitial decoder =
					new ProtocolDecoderInitial( 
						helper,
						new byte[][]{ shared_secret },
						true,
						null,
						new ProtocolDecoderAdapter()
						{
							public void
							decodeComplete(
								ProtocolDecoder	decoder,
								ByteBuffer		remaining_initial_data )
							{
								System.out.println( "outgoing decode complete: " +  decoder.getFilter().getName());
															
								readStream( "incoming", decoder.getFilter() );
								
								writeStream( TEST_HEADER,  decoder.getFilter());
								
								writeStream( "two jolly porkers", decoder.getFilter() );
							}
							
							public void
							decodeFailed(
								ProtocolDecoder	decoder,
								Throwable			cause )
							{
								System.out.println( "outgoing decode failed: " + Debug.getNestedExceptionMessage(cause));
	
							}
							
							public void
							gotSecret(
								byte[]				session_secret )
							{	
							}
							
							public int
							getMaximumPlainHeaderLength()
							{
								throw( new RuntimeException());
							}
							
							public int
							matchPlainHeader(
								ByteBuffer			buffer )
							{
								throw( new RuntimeException());
							}
						});
			}
		}catch( Throwable e ){
			
			e.printStackTrace();
		}
	
protected voidoutgoings()

		while( true ){
			
			outgoing();
			
			try{
				Thread.sleep(1000000);
				
			}catch( Throwable e ){
	
			}
		}
	
protected voidreadStream(java.lang.String str, com.aelitis.azureus.core.networkmanager.impl.TransportHelperFilter filter)

		try{
			TCPNetworkManager.getSingleton().getReadSelector().register(
				((TCPTransportHelper)filter.getHelper()).getSocketChannel(),
				new VirtualSelectorListener()
				{
					public boolean 
					selectSuccess(
						VirtualChannelSelector selector, SocketChannel sc, Object attachment)
					{
						ByteBuffer	buffer = ByteBuffer.allocate(1024);
						
						try{
							long	len = filter.read( new ByteBuffer[]{ buffer }, 0, 1 );
						
							byte[]	data = new byte[buffer.position()];
							
							buffer.flip();
							
							buffer.get( data );
							
							System.out.println( str + ": " + new String(data));
							
							return( len > 0 );
							
						}catch( Throwable e ){
							
							e.printStackTrace();
							
							return( false );
						}
					}

					public void 
					selectFailure(
							VirtualChannelSelector selector, SocketChannel sc, Object attachment, Throwable msg)
					{
						msg.printStackTrace();
					}
				},
				null );
			
		}catch( Throwable e ){
			
			e.printStackTrace();
		}
	
protected voidwriteStream(java.lang.String str, com.aelitis.azureus.core.networkmanager.impl.TransportHelperFilter filter)

		writeStream( str.getBytes(), filter );
	
protected voidwriteStream(byte[] data, com.aelitis.azureus.core.networkmanager.impl.TransportHelperFilter filter)

		try{
			filter.write( new ByteBuffer[]{ ByteBuffer.wrap(data)}, 0, 1 );
			
		}catch( Throwable e ){
			
			e.printStackTrace();
		}
	
protected voidwriteStream(byte[] data, java.nio.channels.SocketChannel channel)

		try{
			channel.write( new ByteBuffer[]{ ByteBuffer.wrap(data)}, 0, 1 );
			
		}catch( Throwable e ){
			
			e.printStackTrace();
		}