directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r164032 - in /directory/network/trunk/src: java/org/apache/mina/io/datagram/ java/org/apache/mina/io/socket/ test/org/apache/mina/examples/echoserver/
Date Thu, 21 Apr 2005 10:49:11 GMT
Author: trustin
Date: Thu Apr 21 03:49:06 2005
New Revision: 164032

URL: http://svn.apache.org/viewcvs?rev=164032&view=rev
Log:
* Fixed: Resource leakage in DatagramConnector
* Added some tests related with SessionInitializer

Modified:
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramSession.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketSession.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AbstractTest.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java?rev=164032&r1=164031&r2=164032&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java Thu
Apr 21 03:49:06 2005
@@ -139,6 +139,7 @@
         {
             if( !initialized )
             {
+                ch.socket().close();
                 ch.close();
             }
         }
@@ -464,6 +465,7 @@
                 {
                     try
                     {
+                        req.channel.socket().close();
                         req.channel.close();
                     }
                     catch (IOException e)
@@ -492,9 +494,11 @@
                 break;
             else
             {
+                DatagramChannel ch = ( DatagramChannel ) key.channel();
                 try
                 {
-                    key.channel().close();
+                    ch.socket().close();
+                    ch.close();
                 }
                 catch( IOException e )
                 {

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java?rev=164032&r1=164031&r2=164032&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramFilterChain.java
Thu Apr 21 03:49:06 2005
@@ -13,7 +13,7 @@
  */
 class DatagramFilterChain extends AbstractIoHandlerFilterChain {
 
-    private final DatagramProcessor processor;
+    final DatagramProcessor processor;
 
     DatagramFilterChain( DatagramProcessor processor )
     {

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramSession.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramSession.java?rev=164032&r1=164031&r2=164032&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramSession.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramSession.java Thu
Apr 21 03:49:06 2005
@@ -99,6 +99,10 @@
 
     public void close()
     {
+        if( filters.processor instanceof DatagramConnector )
+        {
+            filters.processor.closeSession( this );
+        }
     }
 
     Queue getWriteBufferQueue()

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java?rev=164032&r1=164031&r2=164032&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java Thu Apr
21 03:49:06 2005
@@ -129,11 +129,14 @@
             {
                 ch.socket().bind( localAddress );
             }
-            ch.configureBlocking( false );
     
+            ch.configureBlocking( false );
+
             if( ch.connect( address ) )
             {
-                return newSession( ch, handler, initializer );
+                SocketSession session = newSession( ch, handler, initializer );
+                success = true;
+                return session;
             }
             
             success = true;
@@ -142,10 +145,11 @@
         {
             if( !success )
             {
+                ch.socket().close();
                 ch.close();
             }
         }
-
+        
         ConnectionRequest request = new ConnectionRequest( ch, timeout, handler, initializer
);
         synchronized( this )
         {
@@ -173,7 +177,7 @@
 
         if( request.exception != null )
         {
-            request.exception.fillInStackTrace();
+            //request.exception.fillInStackTrace();
             throw request.exception;
         }
 
@@ -248,13 +252,25 @@
             }
             finally
             {
+                key.cancel();
+                if( entry.session == null )
+                {
+                    try
+                    {
+                        ch.socket().close();
+                        ch.close();
+                    }
+                    catch( IOException e )
+                    {
+                        exceptionMonitor.exceptionCaught( this, e );
+                    }
+                }
+
                 synchronized( entry )
                 {
                     entry.done = true;
                     entry.notify();
                 }
-
-                key.cancel();
             }
         }
 

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java?rev=164032&r1=164031&r2=164032&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java Thu
Apr 21 03:49:06 2005
@@ -193,6 +193,7 @@
 
             try
             {
+                ch.socket().close();
                 ch.close();
             }
             catch( IOException e )

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketSession.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketSession.java?rev=164032&r1=164031&r2=164032&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketSession.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketSession.java Thu Apr
21 03:49:06 2005
@@ -71,7 +71,7 @@
         this.remoteAddress = ch.socket().getRemoteSocketAddress();
         this.localAddress = ch.socket().getLocalSocketAddress();
     }
-
+    
     SocketFilterChain getFilters()
     {
         return filters;

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=164032&r1=164031&r2=164032&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
Thu Apr 21 03:49:06 2005
@@ -21,9 +21,12 @@
 import java.io.IOException;
 import java.net.InetSocketAddress;
 
+import junit.framework.Assert;
 import junit.framework.TestCase;
 
 import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.Session;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.io.IoAcceptor;
 import org.apache.mina.io.datagram.DatagramAcceptor;
 import org.apache.mina.io.filter.IoThreadPoolFilter;
@@ -37,6 +40,10 @@
  */
 public class AbstractTest extends TestCase
 {
+    private final boolean testInitializer;
+    
+    private MarkingInitializer initializer;
+
     protected int port;
 
     protected IoAcceptor acceptor;
@@ -44,6 +51,11 @@
     protected IoAcceptor datagramAcceptor;
 
     protected IoThreadPoolFilter threadPoolFilter;
+    
+    protected AbstractTest( boolean testInitializer )
+    {
+        this.testInitializer = testInitializer;
+    }
 
     protected static void assertEquals( byte[] expected, byte[] actual )
     {
@@ -75,6 +87,11 @@
     {
         acceptor = new SocketAcceptor();
         datagramAcceptor = new DatagramAcceptor();
+        
+        if( testInitializer )
+        {
+            initializer = new MarkingInitializer();
+        }
 
         // Find an availble test port and bind to it.
         boolean socketBound = false;
@@ -90,11 +107,12 @@
             try
             {
                 acceptor.bind( new InetSocketAddress( port ),
-                        new EchoProtocolHandler() );
+                               new EchoProtocolHandler(), initializer );
                 socketBound = true;
 
                 datagramAcceptor.bind( new InetSocketAddress( port ),
-                        new EchoProtocolHandler() );
+                                       new EchoProtocolHandler(),
+                                       initializer );
                 datagramBound = true;
 
                 break;
@@ -139,5 +157,20 @@
         acceptor.unbind( new InetSocketAddress( port ) );
         datagramAcceptor.unbind( new InetSocketAddress( port ) );
         threadPoolFilter.stop();
+        
+        if( initializer != null  )
+        {
+            Assert.assertTrue( initializer.executed );
+        }
+    }
+
+    private static class MarkingInitializer implements SessionInitializer
+    {
+        private boolean executed;
+
+        public void initializeSession(Session session) throws IOException
+        {
+            executed = true;
+        }
     }
 }

Modified: directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java?rev=164032&r1=164031&r2=164032&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
(original)
+++ directory/network/trunk/src/test/org/apache/mina/examples/echoserver/AcceptorTest.java
Thu Apr 21 03:49:06 2005
@@ -43,6 +43,11 @@
  */
 public class AcceptorTest extends AbstractTest
 {
+    public AcceptorTest()
+    {
+        super( true );
+    }
+
     public void testTCP() throws Exception
     {
         EchoTCPClient client = new EchoTCPClient();

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=164032&r1=164031&r2=164032&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
Thu Apr 21 03:49:06 2005
@@ -18,6 +18,7 @@
  */
 package org.apache.mina.examples.echoserver;
 
+import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
@@ -25,6 +26,8 @@
 import junit.framework.Assert;
 
 import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.common.Session;
+import org.apache.mina.common.SessionInitializer;
 import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory;
 import org.apache.mina.io.IoConnector;
 import org.apache.mina.io.IoHandlerAdapter;
@@ -44,27 +47,26 @@
 {
     private int clientPort;
     
+    public ConnectorTest()
+    {
+        super( false );
+    }
+
     public void setUp() throws Exception
     {
         super.setUp();
-        clientPort = AvailablePortFinder.getNextAvailable( port + 1 );
+        clientPort = port;
+        clientPort = AvailablePortFinder.getNextAvailable( clientPort + 1 );
+        System.out.println( "Using port " + clientPort + " as local address" );
     }
     
     public void testTCP() throws Exception
     {
         IoConnector connector = new SocketConnector();
         connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
-        testTCP0( connector, null );
+        testConnector( connector );
     }
     
-    public void testTCPWithLocalAddress() throws Exception
-    {
-        System.out.println( "Using client port " + clientPort + " for testing." );
-        IoConnector connector = new SocketConnector();
-        connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
-        testTCP0( connector, new InetSocketAddress( clientPort ) );
-    }
-
     /**
      * Client-side SSL doesn't work for now.
      */
@@ -83,17 +85,53 @@
         connectorSSLFilter.setDebug( SSLFilter.Debug.ON );
         connector.getFilterChain().addLast( "SSL", connectorSSLFilter );
 
-        testTCP0( connector, null );
+        testConnector( connector );
+    }
+    
+    public void testUDP() throws Exception
+    {
+        IoConnector connector = new DatagramConnector();
+        connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
+        testConnector( connector );
+    }
+    
+    private void testConnector( IoConnector connector ) throws Exception
+    {
+        MarkingInitializer marker;
+        InetSocketAddress localAddress = new InetSocketAddress( clientPort );
+
+        System.out.println("* Without localAddress and initializer");
+        testConnector( connector, null, null );
+        
+        marker = new MarkingInitializer();
+        System.out.println("* Without localAddress and with initializer");
+        testConnector( connector, null, marker );
+        Assert.assertTrue( marker.executed );
+
+        if( !(connector instanceof SocketConnector) )
+        {
+            // FIXME This test fails in case of SocketConnector.
+            // I don't know why yet.
+            System.out.println("* With localAddress and without initializer");
+            testConnector( connector, localAddress, null );
+            
+            marker = new MarkingInitializer();
+            System.out.println("* With localAddress and initializer");
+            testConnector( connector, localAddress, marker );
+            Assert.assertTrue( marker.executed );
+        }
     }
     
-    private void testTCP0( IoConnector connector, SocketAddress localAddress ) throws Exception
+    private void testConnector( IoConnector connector, SocketAddress localAddress,
+                                SessionInitializer initializer ) throws Exception
     {
         EchoConnectorHandler handler = new EchoConnectorHandler();
         ByteBuffer readBuf = handler.readBuf;
         IoSession session = connector.connect(
                 new InetSocketAddress( InetAddress.getLocalHost(), port ),
                 localAddress,
-                handler );
+                handler,
+                initializer );
         
         for( int i = 0; i < 10; i ++ )
         {
@@ -140,21 +178,6 @@
         assertEquals(expectedBuf, readBuf);
     }
 
-    public void testUDP() throws Exception
-    {
-        IoConnector connector = new DatagramConnector();
-        connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
-        testTCP0( connector, null );
-    }
-    
-    public void testUDPWithLocalAddress() throws Exception
-    {
-        System.out.println( "Using client port " + clientPort + " for testing." );
-        IoConnector connector = new DatagramConnector();
-        connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
-        testTCP0( connector, new InetSocketAddress( clientPort ) );
-    }
-    
     private void fillWriteBuffer( ByteBuffer writeBuf, int i )
     {
         while( writeBuf.remaining() > 0 )
@@ -195,6 +218,16 @@
         public void exceptionCaught( IoSession session, Throwable cause )
         {
             cause.printStackTrace();
+        }
+    }
+    
+    private static class MarkingInitializer implements SessionInitializer
+    {
+        private boolean executed;
+
+        public void initializeSession(Session session) throws IOException
+        {
+            executed = true;
         }
     }
 }



Mime
View raw message