You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/10/31 12:23:15 UTC
svn commit: r329799 - in /directory/network:
branches/0.8/src/test/org/apache/mina/examples/echoserver/
trunk/src/examples/org/apache/mina/examples/echoserver/
trunk/src/java/org/apache/mina/filter/
trunk/src/java/org/apache/mina/filter/support/ trunk/...
Author: trustin
Date: Mon Oct 31 03:23:00 2005
New Revision: 329799
URL: http://svn.apache.org/viewcvs?rev=329799&view=rev
Log:
* Fixed more concurrency issues in SSLFilter
* SSLFilter now provides a way to notify security information to IoHandler.
* Removed unused comments.
Modified:
directory/network/branches/0.8/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java
directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java
directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
Modified: directory/network/branches/0.8/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.8/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java?rev=329799&r1=329798&r2=329799&view=diff
==============================================================================
--- directory/network/branches/0.8/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java (original)
+++ directory/network/branches/0.8/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java Mon Oct 31 03:23:00 2005
@@ -54,9 +54,6 @@
testConnector( connector );
}
- /**
- * Client-side SSL doesn't work for now.
- */
public void testTCPWithSSL() throws Exception
{
// Add an SSL filter to acceptor
@@ -150,7 +147,7 @@
session.write( buf, marker );
// This will align message arrival order in UDP
- for( int j = 0; j < 30; j ++ )
+ for( int j = 0; j < 100; j ++ )
{
if( readBuf.position() == ( i + 1 ) * 16 )
{
@@ -160,14 +157,14 @@
}
}
- for( int i = 0; i < 30; i++ ) {
+ for( int i = 0; i < 100; i++ ) {
if( readBuf.position() == 160 )
{
break;
}
else
{
- Thread.sleep( 100 );
+ Thread.sleep( 10 );
}
}
Modified: directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java?rev=329799&r1=329798&r2=329799&view=diff
==============================================================================
--- directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java (original)
+++ directory/network/trunk/src/examples/org/apache/mina/examples/echoserver/EchoProtocolHandler.java Mon Oct 31 03:23:00 2005
@@ -23,6 +23,7 @@
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.SSLFilter;
import org.apache.mina.transport.socket.nio.SocketSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -45,6 +46,9 @@
}
session.setIdleTime( IdleStatus.BOTH_IDLE, 10 );
+
+ // We're going to use SSL negotiation notification.
+ session.setAttribute( SSLFilter.USE_NOTIFICATION );
}
public void sessionIdle( IoSession session, IdleStatus status )
@@ -57,11 +61,17 @@
public void exceptionCaught( IoSession session, Throwable cause )
{
+ cause.printStackTrace();
session.close();
}
public void messageReceived( IoSession session, Object message ) throws Exception
{
+ if( !( message instanceof ByteBuffer ) )
+ {
+ return;
+ }
+
ByteBuffer rb = ( ByteBuffer ) message;
// Write the received data back to remote peer
ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
Modified: directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java?rev=329799&r1=329798&r2=329799&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/SSLFilter.java Mon Oct 31 03:23:00 2005
@@ -29,6 +29,7 @@
import org.apache.mina.common.CloseFuture;
import org.apache.mina.common.IoFilterAdapter;
import org.apache.mina.common.IoFilterChain;
+import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.WriteFuture;
import org.apache.mina.filter.support.SSLHandler;
@@ -90,6 +91,30 @@
* StartTLS.
*/
public static final String DISABLE_ENCRYPTION_ONCE = SSLFilter.class.getName() + ".DisableEncryptionOnce";
+
+ /**
+ * A session attribute key that makes this filter to emit a
+ * {@link IoHandler#messageReceived(IoSession, Object)} event with a
+ * special message ({@link #SESSION_SECURED} or {@link #SESSION_UNSECURED}).
+ * This is a marker attribute, which means that you can put whatever as its
+ * value. ({@link Boolean#TRUE} is preferred.) By default, this filter
+ * doesn't emit any events related with SSL session flow control.
+ */
+ public static final String USE_NOTIFICATION = SSLFilter.class.getName() + ".UseNotification";
+
+ /**
+ * A special message object which is emitted with a {@link IoHandler#messageReceived(IoSession, Object)}
+ * event when the session is secured and its {@link #USE_NOTIFICATION}
+ * attribute is set.
+ */
+ public static final Object SESSION_SECURED = "SESSION_SECURED";
+
+ /**
+ * A special message object which is emitted with a {@link IoHandler#messageReceived(IoSession, Object)}
+ * event when the session is not secure anymore and its {@link #USE_NOTIFICATION}
+ * attribute is set.
+ */
+ public static final Object SESSION_UNSECURED = "SESSION_UNSECURED";
private static final String SSL_HANDLER = SSLFilter.class.getName() + ".SSLHandler";
@@ -139,26 +164,29 @@
*/
public boolean startSSL( IoSession session ) throws SSLException
{
- SSLHandler handler = getSSLSessionHandler( session );
- if( handler != null )
+ synchronized( session )
{
- if( handler.getParent() != this )
+ SSLHandler handler = getSSLSessionHandler( session );
+ if( handler != null )
{
- throw new IllegalArgumentException( "Not managed by this filter." );
- }
-
- if( handler.isOutboundDone() )
- {
- session.removeAttribute( SSL_HANDLER );
- }
- else
- {
- return false;
+ if( handler.getParent() != this )
+ {
+ throw new IllegalArgumentException( "Not managed by this filter." );
+ }
+
+ if( handler.isOutboundDone() )
+ {
+ session.removeAttribute( SSL_HANDLER );
+ }
+ else
+ {
+ return false;
+ }
}
+
+ createSSLSessionHandler( parent.getNextFilter( this ), session );
+ return true;
}
-
- createSSLSessionHandler( parent.getNextFilter( this ), session );
- return true;
}
/**
@@ -169,13 +197,16 @@
*/
public boolean isSSLStarted( IoSession session )
{
- SSLHandler handler = getSSLSessionHandler( session );
- if( handler == null )
+ synchronized( session )
{
- return true;
+ SSLHandler handler = getSSLSessionHandler( session );
+ if( handler == null )
+ {
+ return true;
+ }
+
+ return !handler.isOutboundDone();
}
-
- return !handler.isOutboundDone();
}
/**
@@ -188,20 +219,23 @@
*/
public WriteFuture stopSSL( IoSession session ) throws SSLException
{
- SSLHandler handler = getSSLSessionHandler( session );
- if( handler == null )
+ synchronized( session )
{
- // Return a dummy future to prevent NFE.
- return WriteFuture.newNotWrittenFuture();
- }
-
- if( handler.getParent() != this )
- {
- throw new IllegalArgumentException( "Not managed by this filter." );
+ SSLHandler handler = getSSLSessionHandler( session );
+ if( handler == null )
+ {
+ // Return a dummy future to prevent NFE.
+ return WriteFuture.newNotWrittenFuture();
+ }
+
+ if( handler.getParent() != this )
+ {
+ throw new IllegalArgumentException( "Not managed by this filter." );
+ }
+
+ NextFilter nextFilter = parent.getNextFilter( this );
+ return initiateClosure( nextFilter, session );
}
-
- NextFilter nextFilter = parent.getNextFilter( this );
- return initiateClosure( nextFilter, session );
}
/**
@@ -321,66 +355,73 @@
public void sessionOpened( NextFilter nextFilter, IoSession session ) throws SSLException
{
- if( !isSSLStarted( session ) )
+ synchronized( session )
{
+ if( !isSSLStarted( session ) )
+ {
+ nextFilter.sessionOpened( session );
+ return;
+ }
+
+ // Create an SSL handler
+ createSSLSessionHandler( nextFilter, session );
nextFilter.sessionOpened( session );
- return;
}
-
- // Create an SSL handler
- createSSLSessionHandler( nextFilter, session );
- nextFilter.sessionOpened( session );
}
public void sessionClosed( NextFilter nextFilter, IoSession session ) throws SSLException
{
- if( !isSSLStarted( session ) )
- {
- nextFilter.sessionClosed( session );
- return;
- }
-
- if( log.isDebugEnabled() )
- {
- log.debug( session + " Closed: " + getSSLSessionHandler( session ) );
- }
-
- // release resources
- try
+ synchronized( session )
{
- releaseSSLSessionHandler( session );
- }
- finally
- {
- // notify closed session
- nextFilter.sessionClosed( session );
+ if( !isSSLStarted( session ) )
+ {
+ nextFilter.sessionClosed( session );
+ return;
+ }
+
+ if( log.isDebugEnabled() )
+ {
+ log.debug( session + " Closed: " + getSSLSessionHandler( session ) );
+ }
+
+ // release resources
+ try
+ {
+ releaseSSLSessionHandler( session );
+ }
+ finally
+ {
+ // notify closed session
+ nextFilter.sessionClosed( session );
+ }
}
}
public void messageReceived( NextFilter nextFilter, IoSession session,
Object message ) throws SSLException
{
- SSLHandler sslHandler = createSSLSessionHandler( nextFilter, session );
-
- if( !isSSLStarted( session ) )
+ synchronized( session )
{
- if( sslHandler != null && sslHandler.isInboundDone() )
- {
- nextFilter.messageReceived( session, message );
- return;
- }
- }
-
- ByteBuffer buf = ( ByteBuffer ) message;
- sslHandler = createSSLSessionHandler( nextFilter, session );
- if( sslHandler != null )
- {
- if( log.isDebugEnabled() )
+ SSLHandler sslHandler = createSSLSessionHandler( nextFilter, session );
+
+ if( !isSSLStarted( session ) )
{
- log.debug( session + " Data Read: " + sslHandler + " (" + buf+ ')' );
+ if( sslHandler != null && sslHandler.isInboundDone() )
+ {
+ nextFilter.messageReceived( session, message );
+ return;
+ }
}
- synchronized( sslHandler )
+
+ ByteBuffer buf = ( ByteBuffer ) message;
+ sslHandler = createSSLSessionHandler( nextFilter, session );
+ if( sslHandler != null )
{
+ if( log.isDebugEnabled() )
+ {
+ log.debug( session + " Data Read: " + sslHandler + " (" + buf+ ')' );
+ }
+
try
{
// forward read encrypted data to SSL handler
@@ -425,10 +466,10 @@
throw ssle;
}
}
- }
- else
- {
- nextFilter.messageReceived( session, buf );
+ else
+ {
+ nextFilter.messageReceived( session, buf );
+ }
}
}
@@ -449,32 +490,33 @@
public void filterWrite( NextFilter nextFilter, IoSession session, WriteRequest writeRequest ) throws SSLException
{
- if( !isSSLStarted( session ) )
- {
- nextFilter.filterWrite( session, writeRequest );
- return;
- }
-
- // Don't encrypt the data if encryption is disabled.
- if( session.containsAttribute( DISABLE_ENCRYPTION_ONCE ) )
+ synchronized( session )
{
- // Remove the marker attribute because it is temporary.
- session.removeAttribute( DISABLE_ENCRYPTION_ONCE );
- nextFilter.filterWrite( session, writeRequest );
- return;
- }
+ if( !isSSLStarted( session ) )
+ {
+ nextFilter.filterWrite( session, writeRequest );
+ return;
+ }
+
+ // Don't encrypt the data if encryption is disabled.
+ if( session.containsAttribute( DISABLE_ENCRYPTION_ONCE ) )
+ {
+ // Remove the marker attribute because it is temporary.
+ session.removeAttribute( DISABLE_ENCRYPTION_ONCE );
+ nextFilter.filterWrite( session, writeRequest );
+ return;
+ }
+
+ // Otherwise, encrypt the buffer.
+ ByteBuffer buf = ( ByteBuffer ) writeRequest.getMessage();
- // Otherwise, encrypt the buffer.
- ByteBuffer buf = ( ByteBuffer ) writeRequest.getMessage();
+ SSLHandler handler = createSSLSessionHandler( nextFilter, session );
- SSLHandler handler = createSSLSessionHandler( nextFilter, session );
- if( log.isDebugEnabled() )
- {
- log.debug( session + " Filtered Write: " + handler );
- }
+ if( log.isDebugEnabled() )
+ {
+ log.debug( session + " Filtered Write: " + handler );
+ }
- synchronized( handler )
- {
if( handler.isWritingEncryptedData() )
{
// data already encrypted; simply return buffer
@@ -530,16 +572,19 @@
public void filterClose( NextFilter nextFilter, IoSession session, CloseFuture closeFuture ) throws SSLException
{
- try
+ synchronized( session )
{
- if( isSSLStarted( session ) )
+ try
{
- initiateClosure( nextFilter, session ).join();
+ if( isSSLStarted( session ) )
+ {
+ initiateClosure( nextFilter, session ).join();
+ }
+ }
+ finally
+ {
+ nextFilter.filterClose( session, closeFuture );
}
- }
- finally
- {
- nextFilter.filterClose( session, closeFuture );
}
}
@@ -551,24 +596,26 @@
return WriteFuture.newNotWrittenFuture();
}
- synchronized( handler )
+ // shut down
+ if( !handler.closeOutbound() )
{
- // shut down
- if( !handler.closeOutbound() )
- {
- return WriteFuture.newNotWrittenFuture();
- }
-
- // there might be data to write out here?
- WriteFuture future = handler.writeNetBuffer( nextFilter );
-
- if( handler.isInboundDone() )
- {
- releaseSSLSessionHandler( session );
- }
+ return WriteFuture.newNotWrittenFuture();
+ }
+
+ // there might be data to write out here?
+ WriteFuture future = handler.writeNetBuffer( nextFilter );
+
+ if( handler.isInboundDone() )
+ {
+ releaseSSLSessionHandler( session );
+ }
- return future;
+ if( session.containsAttribute( USE_NOTIFICATION ) )
+ {
+ nextFilter.messageReceived( session, SESSION_UNSECURED );
}
+
+ return future;
}
// Utiliities
@@ -617,27 +664,20 @@
SSLHandler handler = getSSLSessionHandler( session );
if( handler == null )
{
- synchronized( session )
+ boolean done = false;
+ try
{
- handler = getSSLSessionHandler( session );
- if( handler == null )
+ handler =
+ new SSLHandler( this, sslContext, session );
+ session.setAttribute( SSL_HANDLER, handler );
+ handler.handshake( nextFilter );
+ done = true;
+ }
+ finally
+ {
+ if( !done )
{
- boolean done = false;
- try
- {
- handler =
- new SSLHandler( this, sslContext, session );
- session.setAttribute( SSL_HANDLER, handler );
- handler.doHandshake( nextFilter );
- done = true;
- }
- finally
- {
- if( !done )
- {
- session.removeAttribute( SSL_HANDLER );
- }
- }
+ session.removeAttribute( SSL_HANDLER );
}
}
}
@@ -659,7 +699,7 @@
sslHandler.release();
}
}
-
+
private static class EncryptedBuffer extends ByteBufferProxy
{
private final ByteBuffer originalBuffer;
Modified: directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java?rev=329799&r1=329798&r2=329799&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/filter/support/SSLHandler.java Mon Oct 31 03:23:00 2005
@@ -148,7 +148,7 @@
/**
* Check we are writing encrypted data.
*/
- public synchronized boolean isWritingEncryptedData()
+ public boolean isWritingEncryptedData()
{
return writingEncryptedData;
}
@@ -156,17 +156,17 @@
/**
* Check if initial handshake is completed.
*/
- public synchronized boolean isInitialHandshakeComplete()
+ public boolean isInitialHandshakeComplete()
{
return initialHandshakeComplete;
}
- public synchronized boolean isInboundDone()
+ public boolean isInboundDone()
{
return sslEngine.isInboundDone();
}
- public synchronized boolean isOutboundDone()
+ public boolean isOutboundDone()
{
return sslEngine.isOutboundDone();
}
@@ -174,17 +174,17 @@
/**
* Check if there is any need to complete initial handshake.
*/
- public synchronized boolean needToCompleteInitialHandshake()
+ public boolean needToCompleteInitialHandshake()
{
return ( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.NEED_WRAP && !isInboundDone() );
}
- public synchronized void scheduleWrite( NextFilter nextFilter, WriteRequest writeRequest )
+ public void scheduleWrite( NextFilter nextFilter, WriteRequest writeRequest )
{
scheduledWrites.push( new ScheduledWrite( nextFilter, writeRequest ) );
}
- public synchronized void flushScheduledWrites() throws SSLException
+ public void flushScheduledWrites() throws SSLException
{
ScheduledWrite scheduledWrite;
@@ -206,7 +206,7 @@
* @param buf buffer to decrypt
* @throws SSLException on errors
*/
- public synchronized void messageReceived( NextFilter nextFilter, ByteBuffer buf ) throws SSLException
+ public void messageReceived( NextFilter nextFilter, ByteBuffer buf ) throws SSLException
{
if ( buf.limit() > inNetBuffer.remaining() ) {
// We have to expand inNetBuffer
@@ -229,7 +229,7 @@
inNetBuffer.put( buf );
if( !initialHandshakeComplete )
{
- doHandshake( nextFilter );
+ handshake( nextFilter );
}
else
{
@@ -245,20 +245,6 @@
}
/**
- * Continue initial SSL handshake.
- *
- * @throws SSLException on errors
- */
- public synchronized void continueHandshake( NextFilter nextFilter ) throws SSLException
- {
- if( log.isDebugEnabled() )
- {
- log.debug( session + " continueHandshake()" );
- }
- doHandshake( nextFilter );
- }
-
- /**
* Get decrypted application data.
*
* @return buffer with data
@@ -284,7 +270,7 @@
* @param src data to encrypt
* @throws SSLException on errors
*/
- public synchronized void encrypt( ByteBuffer src ) throws SSLException
+ public void encrypt( ByteBuffer src ) throws SSLException
{
if( !initialHandshakeComplete )
{
@@ -337,7 +323,7 @@
*
* @throws SSLException on errors
*/
- public synchronized boolean closeOutbound() throws SSLException
+ public boolean closeOutbound() throws SSLException
{
if( sslEngine.isOutboundDone() )
{
@@ -361,7 +347,7 @@
/**
* Release allocated ByteBuffers.
*/
- public synchronized void release()
+ public void release()
{
SSLByteBufferPool.release( appBuffer );
SSLByteBufferPool.release( inNetBuffer );
@@ -414,15 +400,19 @@
/**
* Perform any handshaking processing.
*/
- public synchronized void doHandshake( NextFilter nextFilter ) throws SSLException
+ public void handshake( NextFilter nextFilter ) throws SSLException
{
-
if( log.isDebugEnabled() )
{
log.debug( session + " doHandshake()" );
}
+
+ if( initialHandshakeComplete )
+ {
+ return;
+ }
- while( !initialHandshakeComplete )
+ for( ;; )
{
if( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.FINISHED )
{
@@ -434,7 +424,11 @@
log.debug( session + " sslSession CipherSuite used " + sslSession.getCipherSuite() );
}
initialHandshakeComplete = true;
- return;
+ if( session.containsAttribute( SSLFilter.USE_NOTIFICATION ) )
+ {
+ nextFilter.messageReceived( session, SSLFilter.SESSION_SECURED );
+ }
+ break;
}
else if( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.NEED_TASK )
{
@@ -454,11 +448,11 @@
}
SSLEngineResult.Status status = unwrapHandshake();
if( ( initialHandshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED
- && status == SSLEngineResult.Status.BUFFER_UNDERFLOW )
+ && status == SSLEngineResult.Status.BUFFER_UNDERFLOW )
|| isInboundDone() )
{
// We need more data or the session is closed
- return;
+ break;
}
}
else if( initialHandshakeStatus == SSLEngineResult.HandshakeStatus.NEED_WRAP )
@@ -475,7 +469,7 @@
{
log.debug( session + " Still data in out buffer!" );
}
- return;
+ break;
}
outNetBuffer.clear();
SSLEngineResult result = sslEngine.wrap( hsBB, outNetBuffer );
@@ -498,7 +492,7 @@
}
}
- public synchronized WriteFuture writeNetBuffer( NextFilter nextFilter ) throws SSLException
+ public WriteFuture writeNetBuffer( NextFilter nextFilter ) throws SSLException
{
// Check if any net data needed to be writen
if( !getOutNetBuffer().hasRemaining() )
@@ -536,7 +530,7 @@
{
try
{
- continueHandshake( nextFilter );
+ handshake( nextFilter );
}
catch( SSLException ssle )
{
@@ -698,7 +692,7 @@
/*
* We could run this in a separate thread, but I don't see the need
- * for this when used from IoSSLFilter.Use thread filters in Mina instead?
+ * for this when used from SSLFilter. Use thread filters in MINA instead?
*/
Runnable runnable;
while( ( runnable = sslEngine.getDelegatedTask() ) != null )
Modified: directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java?rev=329799&r1=329798&r2=329799&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java Mon Oct 31 03:23:00 2005
@@ -104,6 +104,11 @@
// This is for TLS reentrance test
public void messageReceived( IoSession session, Object message ) throws Exception
{
+ if( !( message instanceof ByteBuffer ) )
+ {
+ return;
+ }
+
ByteBuffer buf = ( ByteBuffer ) message;
if( buf.remaining() == 1 && buf.get() == ( byte ) '.' )
{
Modified: directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java?rev=329799&r1=329798&r2=329799&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java Mon Oct 31 03:23:00 2005
@@ -48,6 +48,10 @@
*/
public class ConnectorTest extends AbstractTest
{
+ private static final int TIMEOUT = 10000; // 10 seconds
+ private final int COUNT = 10;
+ private final int DATA_SIZE = 16;
+
public ConnectorTest()
{
}
@@ -162,14 +166,7 @@
session.write( buf ).join();
//// Wait for StartTLS response.
- for( int j = 0; j < 30; j ++ )
- {
- if( handler.readBuf.position() >= 1 )
- {
- break;
- }
- Thread.sleep( 10 );
- }
+ waitForResponse( handler, 1 );
handler.readBuf.flip();
Assert.assertEquals( 1, handler.readBuf.remaining() );
@@ -183,11 +180,9 @@
session.close().join();
}
-
+
private void testConnector0( IoSession session ) throws InterruptedException
{
- final int COUNT = 10;
- final int DATA_SIZE = 16;
EchoConnectorHandler handler = ( EchoConnectorHandler ) session.getHandler();
ByteBuffer readBuf = handler.readBuf;
readBuf.clear();
@@ -204,29 +199,13 @@
if( session.getTransportType().isConnectionless() )
{
// This will align message arrival order in connectionless transport types
- for( int j = 0; j < 30; j ++ )
- {
- if( readBuf.position() == ( i + 1 ) * DATA_SIZE )
- {
- break;
- }
- Thread.sleep( 10 );
- }
+ waitForResponse( handler, ( i + 1 ) * DATA_SIZE );
}
}
writeFuture.join();
- for( int i = 0; i < 30; i++ ) {
- if( readBuf.position() >= DATA_SIZE * COUNT )
- {
- break;
- }
- else
- {
- Thread.sleep( 100 );
- }
- }
+ waitForResponse( handler, DATA_SIZE * COUNT );
// Assert data
//// Please note that BufferOverflowException can be thrown
@@ -245,6 +224,20 @@
assertEquals(expectedBuf, readBuf);
}
+ private void waitForResponse( EchoConnectorHandler handler, int bytes ) throws InterruptedException
+ {
+ for( int j = 0; j < TIMEOUT / 10; j ++ )
+ {
+ if( handler.readBuf.position() >= bytes )
+ {
+ break;
+ }
+ Thread.sleep( 10 );
+ }
+
+ Assert.assertEquals( bytes, handler.readBuf.position() );
+ }
+
private void fillWriteBuffer( ByteBuffer writeBuf, int i )
{
while( writeBuf.remaining() > 0 )
@@ -260,7 +253,12 @@
private static class EchoConnectorHandler extends IoHandlerAdapter
{
- private ByteBuffer readBuf = ByteBuffer.allocate( 8192 );
+ private ByteBuffer readBuf = ByteBuffer.allocate( 1024 );
+
+ private EchoConnectorHandler()
+ {
+ readBuf.setAutoExpand( true );
+ }
public void messageReceived( IoSession session, Object message )
{