directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r158877 - in directory/network/trunk/src: java/org/apache/mina/io/datagram/ java/org/apache/mina/io/socket/ java/org/apache/mina/util/ test/org/apache/mina/io/
Date Thu, 24 Mar 2005 04:13:10 GMT
Author: trustin
Date: Wed Mar 23 20:13:08 2005
New Revision: 158877

URL: http://svn.apache.org/viewcvs?view=rev&rev=158877
Log:
Fixed: unbind request sometimes blocks forever because worker thread is not running at the
moment.

Modified:
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.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/util/Queue.java
    directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java

Modified: directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java?view=diff&r1=158876&r2=158877
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java Wed
Mar 23 20:13:08 2005
@@ -96,15 +96,7 @@
             registerQueue.push( request );
         }
 
-        synchronized( this )
-        {
-            if( worker == null )
-            {
-                worker = new Worker();
-                worker.start();
-            }
-        }
-
+        startupWorker();
         selector.wakeup();
         
         synchronized( request )
@@ -139,6 +131,7 @@
             cancelQueue.push( request );
         }
 
+        startupWorker();
         selector.wakeup();
         
         synchronized( request )
@@ -161,6 +154,15 @@
             throw request.exception;
         }
     }
+    
+    private synchronized void startupWorker()
+    {
+        if( worker == null )
+        {
+            worker = new Worker();
+            worker.start();
+        }
+    }
 
     public void flushSession( DatagramSession session )
     {
@@ -197,6 +199,14 @@
 
                     registerNew();
 
+                    if( nKeys > 0 )
+                    {
+                        processReadySessions( selector.selectedKeys() );
+                    }
+
+                    flushSessions();
+                    cancelKeys();
+
                     if( selector.keys().isEmpty() )
                     {
                         synchronized( DatagramAcceptor.this )
@@ -208,14 +218,6 @@
                             }
                         }
                     }
-
-                    if( nKeys > 0 )
-                    {
-                        processReadySessions( selector.selectedKeys() );
-                    }
-
-                    flushSessions();
-                    cancelKeys();
                 }
                 catch( IOException e )
                 {

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?view=diff&r1=158876&r2=158877
==============================================================================
--- 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 Wed
Mar 23 20:13:08 2005
@@ -99,11 +99,7 @@
                 registerQueue.push( request );
             }
 
-            if( worker == null )
-            {
-                worker = new Worker();
-                worker.start();
-            }
+            startupWorker();
         }
 
         selector.wakeup();
@@ -124,6 +120,15 @@
 
         return request.session;
     }
+    
+    private synchronized void startupWorker()
+    {
+        if( worker == null )
+        {
+            worker = new Worker();
+            worker.start();
+        }
+    }
 
     public IoSession connect( SocketAddress address, int timeout,
                              IoHandler handler ) throws IOException
@@ -176,6 +181,14 @@
 
                     registerNew();
 
+                    if( nKeys > 0 )
+                    {
+                        processReadySessions( selector.selectedKeys() );
+                    }
+
+                    flushSessions();
+                    cancelKeys();
+
                     if( selector.keys().isEmpty() )
                     {
                         synchronized( DatagramConnector.this )
@@ -187,14 +200,6 @@
                             }
                         }
                     }
-
-                    if( nKeys > 0 )
-                    {
-                        processReadySessions( selector.selectedKeys() );
-                    }
-
-                    flushSessions();
-                    cancelKeys();
                 }
                 catch( IOException e )
                 {

Modified: directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java?view=diff&r1=158876&r2=158877
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/socket/SocketAcceptor.java Wed Mar
23 20:13:08 2005
@@ -29,6 +29,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.mina.common.FilterChainType;
 import org.apache.mina.io.DefaultExceptionMonitor;
@@ -120,16 +121,7 @@
             registerQueue.push( request );
         }
 
-        synchronized( this )
-        {
-            if( worker == null )
-            {
-                worker = new Worker();
-
-                worker.start();
-            }
-        }
-
+        startupWorker();
         selector.wakeup();
         
         synchronized( request )
@@ -155,6 +147,17 @@
     }
 
 
+    private synchronized void startupWorker()
+    {
+        if( worker == null )
+        {
+            worker = new Worker();
+
+            worker.start();
+        }
+    }
+
+
     public void unbind( SocketAddress address )
     {
         if( address == null )
@@ -169,6 +172,7 @@
             cancelQueue.push( request );
         }
 
+        startupWorker();
         selector.wakeup();
 
         synchronized( request )
@@ -210,6 +214,12 @@
                     int nKeys = selector.select();
 
                     registerNew();
+                    cancelKeys();
+
+                    if( nKeys > 0 )
+                    {
+                        processSessions( selector.selectedKeys() );
+                    }
 
                     if( selector.keys().isEmpty() )
                     {
@@ -223,40 +233,6 @@
                             }
                         }
                     }
-
-                    cancelKeys();
-
-                    if( nKeys == 0 )
-                    {
-                        continue;
-                    }
-
-                    Iterator it = selector.selectedKeys().iterator();
-
-                    while( it.hasNext() )
-                    {
-                        SelectionKey key = ( SelectionKey ) it.next();
-
-                        it.remove();
-
-                        if( !key.isAcceptable() )
-                        {
-                            continue;
-                        }
-
-                        ServerSocketChannel ssc = ( ServerSocketChannel ) key.channel();
-
-                        SocketChannel ch = ssc.accept();
-
-                        if( ch == null )
-                        {
-                            continue;
-                        }
-
-                        SocketSession session = new SocketSession( filters, ch, ( IoHandler
) key.attachment() );
-
-                        SocketIoProcessor.getInstance().addSession( session );
-                    }
                 }
                 catch( IOException e )
                 {
@@ -272,6 +248,35 @@
                 }
             }
         }
+
+        private void processSessions( Set keys ) throws IOException
+        {
+            Iterator it = keys.iterator();
+            while( it.hasNext() )
+            {
+                SelectionKey key = ( SelectionKey ) it.next();
+   
+                it.remove();
+   
+                if( !key.isAcceptable() )
+                {
+                    continue;
+                }
+   
+                ServerSocketChannel ssc = ( ServerSocketChannel ) key.channel();
+   
+                SocketChannel ch = ssc.accept();
+   
+                if( ch == null )
+                {
+                    continue;
+                }
+   
+                SocketSession session = new SocketSession( filters, ch, ( IoHandler ) key.attachment()
);
+   
+                SocketIoProcessor.getInstance().addSession( session );
+            }
+        }
     }
 
 
@@ -354,7 +359,7 @@
             {
                 request = ( CancellationRequest ) cancelQueue.pop();
             }
-            
+
             if( request == null )
             {
                 break;

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?view=diff&r1=158876&r2=158877
==============================================================================
--- 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 Wed Mar
23 20:13:08 2005
@@ -76,7 +76,7 @@
     }
 
     public IoSession connect( SocketAddress address, int timeout,
-                             IoHandler handler ) throws IOException
+                              IoHandler handler ) throws IOException
     {
         if( address == null )
             throw new NullPointerException( "address" );
@@ -106,15 +106,7 @@
                 connectQueue.push( request );
             }
 
-            synchronized( this )
-            {
-                if( worker == null )
-                {
-                    worker = new Worker();
-                    worker.start();
-                }
-            }
-            
+            startupWorker();
             selector.wakeup();
 
             synchronized( request )
@@ -142,6 +134,15 @@
 
         return session;
     }
+    
+    private synchronized void startupWorker()
+    {
+        if( worker == null )
+        {
+            worker = new Worker();
+            worker.start();
+        }
+    }
 
     private void registerNew()
     {
@@ -268,6 +269,13 @@
 
                     registerNew();
                     
+                    if( nKeys > 0 )
+                    {
+                        processSessions( selector.selectedKeys() );
+                    }
+
+                    processTimedOutSessions( selector.keys() );
+
                     if( selector.keys().isEmpty() )
                     {
                         synchronized( SocketConnector.this )
@@ -279,11 +287,6 @@
                             }
                         }
                     }
-
-                    if( nKeys > 0 )
-                        processSessions( selector.selectedKeys() );
-
-                    processTimedOutSessions( selector.keys() );
                 }
                 catch( IOException e )
                 {

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?view=diff&r1=158876&r2=158877
==============================================================================
--- 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 Wed
Mar 23 20:13:08 2005
@@ -94,11 +94,7 @@
                 newSessions.push( session );
             }
 
-            if( worker == null )
-            {
-                worker = new Worker();
-                worker.start();
-            }
+            startupWorker();
         }
 
         selector.wakeup();
@@ -107,9 +103,19 @@
     void removeSession( SocketSession session )
     {
         scheduleRemove( session );
+        startupWorker();
         selector.wakeup();
     }
 
+    private synchronized void startupWorker()
+    {
+        if( worker == null )
+        {
+            worker = new Worker();
+            worker.start();
+        }
+    }
+
     void flushSession( SocketSession session )
     {
         scheduleFlush( session );
@@ -480,6 +486,15 @@
                     int nKeys = selector.select( 1000 );
                     addSessions();
 
+                    if( nKeys > 0 )
+                    {
+                        processSessions( selector.selectedKeys() );
+                    }
+
+                    flushSessions();
+                    removeSessions();
+                    notifyIdleSessions();
+
                     if( selector.keys().isEmpty() )
                     {
                         synchronized( SocketIoProcessor.this )
@@ -491,15 +506,6 @@
                             }
                         }
                     }
-
-                    if( nKeys > 0 )
-                    {
-                        processSessions( selector.selectedKeys() );
-                    }
-
-                    flushSessions();
-                    removeSessions();
-                    notifyIdleSessions();
                 }
                 catch( IOException e )
                 {

Modified: directory/network/trunk/src/java/org/apache/mina/util/Queue.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/Queue.java?view=diff&r1=158876&r2=158877
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/Queue.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/util/Queue.java Wed Mar 23 20:13:08 2005
@@ -173,4 +173,9 @@
     {
         return size;
     }
+    
+    public String toString()
+    {
+        return "first=" + first + ", last=" + last + ", size=" + size + ", mask = " + mask;
+    }
 }

Modified: directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java?view=diff&r1=158876&r2=158877
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/io/AbstractBindTest.java Wed Mar 23 20:13:08
2005
@@ -5,6 +5,7 @@
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.util.Date;
 
 import junit.framework.Assert;
 import junit.framework.TestCase;
@@ -50,7 +51,7 @@
             throw new IOException( "Cannot bind any test port." );
         }
 
-        System.out.println( "Using port " + port + " for testing." );
+        //System.out.println( "Using port " + port + " for testing." );
     }
     
     public void tearDown()
@@ -97,11 +98,31 @@
     {
         InetSocketAddress addr = new InetSocketAddress( port );
         EchoProtocolHandler handler = new EchoProtocolHandler();
-        for( int i = 0; i < 8192; i++ )
+        // we test only 512 times to avoid too many open files exception.
+        for( int i = 0; i < 512; i++ ) 
         {
             acceptor.unbind( addr );
             acceptor.bind( addr, handler );
         }
+    }
+    
+    public void _testRegressively() throws IOException
+    {
+        tearDown();
+
+        InetSocketAddress addr = new InetSocketAddress( port );
+        EchoProtocolHandler handler = new EchoProtocolHandler();
+        for( int i = 0; i < 1048576; i++ )
+        {
+            acceptor.bind( addr, handler );
+            testDuplicateBind();
+            testDuplicateUnbind();
+            if( i % 100 == 0 )
+            {
+                System.out.println( i + " (" + new Date() + ")" );
+            }
+        }
+        setUp();
     }
 
 }



Mime
View raw message