final int RES_CONNECT_FAILED = 0;
final int RES_SOCKS_FAILED = 1;
final int RES_OK = 3;
final AESemaphore sem = new AESemaphore( "NetworkAdminSocksProxy:test" );
final int[] result = { RES_CONNECT_FAILED };
final NetworkAdminException[] error = { null };
try{
InetSocketAddress socks_address = new InetSocketAddress( InetAddress.getByName( host ), Integer.parseInt(port));
final InetSocketAddress target_address = new InetSocketAddress( TARGET_HOST, TARGET_PORT );
TCPConnectionManager.ConnectListener connect_listener =
new TCPConnectionManager.ConnectListener()
{
public void
connectAttemptStarted()
{
}
public void
connectSuccess(
SocketChannel channel )
{
final TCPTransportImpl transport =
new TCPTransportImpl(
new ProtocolEndpointTCP( target_address ), false, false, null );
transport.setFilter( TCPTransportHelperFilterFactory.createTransparentFilter( channel ));
new ProxyLoginHandler(
transport,
target_address,
new ProxyLoginHandler.ProxyListener()
{
public void
connectSuccess()
{
transport.close( "Done" );
result[0] = RES_OK;
sem.release();
}
public void
connectFailure(
Throwable failure_msg )
{
transport.close( "Proxy login failed" );
result[0] = RES_SOCKS_FAILED;
error[0] = new NetworkAdminException( "Proxy connect failed", failure_msg );
sem.release();
}
},
version,
user,
password );
}
public void
connectFailure(
Throwable failure_msg )
{
result[0] = RES_CONNECT_FAILED;
error[0] = new NetworkAdminException( "Connect failed", failure_msg );
sem.release();
}
};
TCPNetworkManager.getSingleton().getConnectDisconnectManager().requestNewConnection(
socks_address, connect_listener );
}catch( Throwable e ){
result[0] = RES_CONNECT_FAILED;
error[0] = new NetworkAdminException( "Connect failed", e );
sem.release();
}
if ( !sem.reserve(10000)){
result[0] = RES_CONNECT_FAILED;
error[0] = new NetworkAdminException( "Connect timeout" );
}
if ( result[0] != RES_OK ){
throw( error[0] );
}