directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r151548 - in incubator/directory/network/trunk/mina/src: java/org/apache/mina/io/datagram/DatagramAcceptor.java java/org/apache/mina/io/socket/SocketAcceptor.java test/org/apache/mina/examples/echoserver/Test.java
Date Sun, 06 Feb 2005 06:16:46 GMT
Author: trustin
Date: Sat Feb  5 22:16:44 2005
New Revision: 151548

URL: http://svn.apache.org/viewcvs?view=rev&rev=151548
Log:
 * Fixed: File descriptor leakage on bind failure
 * Fixed: Echo server test fails on machines whose port 8080 is not availble.

Modified:
    incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
    incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketAcceptor.java
    incubator/directory/network/trunk/mina/src/test/org/apache/mina/examples/echoserver/Test.java

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java?view=diff&r1=151547&r2=151548
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
Sat Feb  5 22:16:44 2005
@@ -52,7 +52,7 @@
 
     private final IoHandlerFilterManager filterManager = new IoHandlerFilterManager();
 
-    private final int id = nextId++;
+    private final int id = nextId ++ ;
 
     private final Selector selector;
 
@@ -86,13 +86,25 @@
         if( handler == null )
             throw new NullPointerException( "handler" );
 
-        if( ! ( address instanceof InetSocketAddress ) )
+        if( !( address instanceof InetSocketAddress ) )
             throw new IllegalArgumentException( "Unexpected address type: "
                                                 + address.getClass() );
 
         DatagramChannel ch = DatagramChannel.open();
-        ch.configureBlocking( false );
-        ch.socket().bind( address );
+        boolean bound = false;
+        try
+        {
+            ch.configureBlocking( false );
+            ch.socket().bind( address );
+            bound = true;
+        }
+        finally
+        {
+            if( !bound )
+            {
+                ch.close();
+            }
+        }
 
         synchronized( this )
         {
@@ -196,8 +208,8 @@
                 }
                 catch( IOException e )
                 {
-                    exceptionMonitor
-                            .exceptionCaught( DatagramAcceptor.this, e );
+                    exceptionMonitor.exceptionCaught( DatagramAcceptor.this,
+                            e );
 
                     try
                     {
@@ -222,11 +234,8 @@
             DatagramChannel ch = ( DatagramChannel ) key.channel();
 
             DatagramSession session = new DatagramSession(
-                                                           DatagramAcceptor.this,
-                                                           filterManager,
-                                                           ch,
-                                                           ( IoHandler ) key
-                                                                   .attachment() );
+                    DatagramAcceptor.this, filterManager, ch,
+                    ( IoHandler ) key.attachment() );
             session.setSelectionKey( key );
 
             if( key.isReadable() )
@@ -247,8 +256,8 @@
         ByteBuffer readBuf = ByteBuffer.allocate( 2048 );
         try
         {
-            SocketAddress remoteAddress = session.getChannel()
-                    .receive( readBuf.buf() );
+            SocketAddress remoteAddress = session.getChannel().receive(
+                    readBuf.buf() );
             if( remoteAddress != null )
             {
                 readBuf.flip();
@@ -334,7 +343,8 @@
                 continue;
             }
 
-            int writtenBytes = ch.send( buf.buf(), session.getRemoteAddress() );
+            int writtenBytes = ch
+                    .send( buf.buf(), session.getRemoteAddress() );
 
             SelectionKey key = session.getSelectionKey();
             if( writtenBytes == 0 )
@@ -344,9 +354,8 @@
             }
             else
             {
-                key
-                        .interestOps( key.interestOps()
-                                      & ( ~SelectionKey.OP_WRITE ) );
+                key.interestOps( key.interestOps()
+                                 & ( ~SelectionKey.OP_WRITE ) );
 
                 // pop and fire event
                 synchronized( writeBufferQueue )
@@ -377,7 +386,8 @@
             if( req == null )
                 break;
 
-            req.channel.register( selector, SelectionKey.OP_READ, req.handler );
+            req.channel
+                    .register( selector, SelectionKey.OP_READ, req.handler );
         }
     }
 
@@ -416,12 +426,12 @@
 
     public void removeAllFilters()
     {
-    	filterManager.removeAllFilters();
+        filterManager.removeAllFilters();
     }
-    
+
     public List getAllFilters()
     {
-    	return filterManager.filters();
+        return filterManager.filters();
     }
 
     private static class RegistrationRequest
@@ -430,7 +440,8 @@
 
         private final IoHandler handler;
 
-        private RegistrationRequest( DatagramChannel channel, IoHandler handler )
+        private RegistrationRequest( DatagramChannel channel,
+                                    IoHandler handler )
         {
             this.channel = channel;
             this.handler = handler;
@@ -451,4 +462,4 @@
 
         this.exceptionMonitor = monitor;
     }
-}
\ No newline at end of file
+}

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketAcceptor.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketAcceptor.java?view=diff&r1=151547&r2=151548
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketAcceptor.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/SocketAcceptor.java
Sat Feb  5 22:16:44 2005
@@ -51,7 +51,7 @@
 
     private final IoHandlerFilterManager filterManager = new IoHandlerFilterManager();
 
-    private final int id = nextId++;
+    private final int id = nextId ++ ;
 
     private final Selector selector;
 
@@ -96,13 +96,25 @@
         if( handler == null )
             throw new NullPointerException( "handler" );
 
-        if( ! ( address instanceof InetSocketAddress ) )
+        if( !( address instanceof InetSocketAddress ) )
             throw new IllegalArgumentException( "Unexpected address type: "
                                                 + address.getClass() );
 
         ServerSocketChannel ssc = ServerSocketChannel.open();
-        ssc.configureBlocking( false );
-        ssc.socket().bind( address, backlog );
+        boolean bound = false;
+        try
+        {
+            ssc.configureBlocking( false );
+            ssc.socket().bind( address, backlog );
+            bound = true;
+        }
+        finally
+        {
+            if( !bound )
+            {
+                ssc.close();
+            }
+        }
 
         synchronized( this )
         {
@@ -209,10 +221,8 @@
                             continue;
 
                         SocketSession session = new SocketSession(
-                                                                   filterManager,
-                                                                   ch,
-                                                                   ( IoHandler ) key
-                                                                           .attachment()
);
+                                filterManager, ch, ( IoHandler ) key
+                                        .attachment() );
                         SocketIoProcessor.getInstance().addSession( session );
                     }
                 }
@@ -249,7 +259,7 @@
                 break;
 
             req.channel.register( selector, SelectionKey.OP_ACCEPT,
-                                  req.handler );
+                    req.handler );
         }
     }
 
@@ -285,15 +295,15 @@
     {
         filterManager.removeFilter( filter );
     }
-    
+
     public void removeAllFilters()
     {
-    	filterManager.removeAllFilters();
+        filterManager.removeAllFilters();
     }
 
     public List getAllFilters()
     {
-    	return filterManager.filters();
+        return filterManager.filters();
     }
 
     private static class RegistrationRequest
@@ -324,4 +334,4 @@
 
         this.exceptionMonitor = monitor;
     }
-}
\ No newline at end of file
+}

Modified: incubator/directory/network/trunk/mina/src/test/org/apache/mina/examples/echoserver/Test.java
URL: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/test/org/apache/mina/examples/echoserver/Test.java?view=diff&r1=151547&r2=151548
==============================================================================
--- incubator/directory/network/trunk/mina/src/test/org/apache/mina/examples/echoserver/Test.java
(original)
+++ incubator/directory/network/trunk/mina/src/test/org/apache/mina/examples/echoserver/Test.java
Sat Feb  5 22:16:44 2005
@@ -3,6 +3,7 @@
  */
 package org.apache.mina.examples.echoserver;
 
+import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketTimeoutException;
@@ -24,7 +25,7 @@
  */
 public class Test extends TestCase
 {
-    private static final int PORT = 8080;
+    private int port;
 
     private IoAcceptor acceptor;
 
@@ -40,7 +41,7 @@
     private static String toString( byte[] buf )
     {
         StringBuffer str = new StringBuffer( buf.length * 4 );
-        for( int i = 0; i < buf.length; i++ )
+        for( int i = 0; i < buf.length; i ++ )
         {
             str.append( buf[ i ] );
             str.append( ' ' );
@@ -50,36 +51,79 @@
 
     protected void setUp() throws Exception
     {
+        acceptor = new SocketAcceptor();
+        datagramAcceptor = new DatagramAcceptor();
+
+        // Find an availble test port and bind to it.
+        boolean socketBound = false;
+        boolean datagramBound = false;
+        for( port = 1024; port <= 65535; port ++ )
+        {
+            socketBound = false;
+            datagramBound = false;
+            try
+            {
+                acceptor.bind( new InetSocketAddress( port ),
+                        new EchoProtocolHandler() );
+                socketBound = true;
+
+                datagramAcceptor.bind( new InetSocketAddress( port ),
+                        new EchoProtocolHandler() );
+                datagramBound = true;
+
+                break;
+            }
+            catch( IOException e )
+            {
+            }
+            finally
+            {
+                if( !socketBound || !datagramBound )
+                {
+                    if( socketBound )
+                    {
+                        acceptor.unbind( new InetSocketAddress( port ) );
+                    }
+                    if( datagramBound )
+                    {
+                        datagramAcceptor
+                                .unbind( new InetSocketAddress( port ) );
+                    }
+                }
+            }
+        }
+
+        // If there is no port available, test fails.
+        if( !socketBound || !datagramBound )
+        {
+            throw new IOException( "Cannot bind any test port." );
+        }
+        
+        System.out.println("Using port " + port + " for testing.");
+
         threadPoolFilter = new IoThreadPoolFilter();
         threadPoolFilter.start();
 
-        acceptor = new SocketAcceptor();
-        acceptor.bind( new InetSocketAddress( PORT ),
-                       new EchoProtocolHandler() );
         acceptor.addFilter( Integer.MAX_VALUE, threadPoolFilter );
-
-        datagramAcceptor = new DatagramAcceptor();
         datagramAcceptor.addFilter( Integer.MAX_VALUE, threadPoolFilter );
-        datagramAcceptor.bind( new InetSocketAddress( PORT ),
-                               new EchoProtocolHandler() );
     }
 
     protected void tearDown() throws Exception
     {
-        acceptor.unbind( new InetSocketAddress( PORT ) );
-        datagramAcceptor.unbind( new InetSocketAddress( PORT ) );
+        acceptor.unbind( new InetSocketAddress( port ) );
+        datagramAcceptor.unbind( new InetSocketAddress( port ) );
         threadPoolFilter.stop();
     }
 
     public void testTCP() throws Exception
     {
         EchoTCPClient client = new EchoTCPClient();
-        client.connect( InetAddress.getLocalHost(), PORT );
+        client.connect( InetAddress.getLocalHost(), port );
         client.setSoTimeout( 3000 );
 
         byte[] writeBuf = new byte[ 16 ];
 
-        for( int i = 0; i < 10; i++ )
+        for( int i = 0; i < 10; i ++ )
         {
             fillWriteBuffer( writeBuf, i );
             client.getOutputStream().write( writeBuf );
@@ -87,15 +131,15 @@
 
         byte[] readBuf = new byte[ writeBuf.length ];
 
-        for( int i = 0; i < 10; i++ )
+        for( int i = 0; i < 10; i ++ )
         {
             fillWriteBuffer( writeBuf, i );
 
             int readBytes = 0;
             while( readBytes < readBuf.length )
             {
-                int nBytes = client.getInputStream()
-                        .read( readBuf, readBytes, readBuf.length - readBytes );
+                int nBytes = client.getInputStream().read( readBuf,
+                        readBytes, readBuf.length - readBytes );
 
                 if( nBytes < 0 )
                     fail( "Unexpected disconnection." );
@@ -131,14 +175,14 @@
 
         client.setSoTimeout( 500 );
 
-        for( int i = 0; i < 10; i++ )
+        for( int i = 0; i < 10; i ++ )
         {
             fillWriteBuffer( writeBuf, i );
-            client.send( writeBuf, writeBuf.length,
-                         InetAddress.getLocalHost(), PORT );
+            client.send( writeBuf, writeBuf.length, InetAddress
+                    .getLocalHost(), port );
 
             assertEquals( readBuf.length, client.receive( readBuf,
-                                                          readBuf.length ) );
+                    readBuf.length ) );
             assertEquals( writeBuf, readBuf );
         }
 
@@ -156,7 +200,7 @@
 
     private void fillWriteBuffer( byte[] writeBuf, int i )
     {
-        for( int j = writeBuf.length - 1; j >= 0; j-- )
+        for( int j = writeBuf.length - 1; j >= 0; j -- )
         {
             writeBuf[ j ] = ( byte ) ( j + i );
         }
@@ -166,4 +210,4 @@
     {
         junit.textui.TestRunner.run( Test.class );
     }
-}
\ No newline at end of file
+}



Mime
View raw message