directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r158611 - in directory/network/trunk/src: java/org/apache/mina/io/datagram/DatagramAcceptor.java java/org/apache/mina/io/socket/SocketConnector.java test/org/apache/mina/examples/echoserver/ConnectorTest.java
Date Tue, 22 Mar 2005 14:58:33 GMT
Author: trustin
Date: Tue Mar 22 06:58:32 2005
New Revision: 158611

URL: http://svn.apache.org/viewcvs?view=rev&rev=158611
Log:
* Fixed: echoserver.ConnectorTest fails in Windows
* Fixed: SocketConnector implementation is unefficient.
* Fixed: DatagramAcceptor throws NullPointerException.

Modified:
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.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=158610&r2=158611
==============================================================================
--- 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 Tue
Mar 22 06:58:32 2005
@@ -156,7 +156,7 @@
             }
         }
         
-        if( request.exception == null )
+        if( request.exception != null )
         {
             request.exception.fillInStackTrace();
             throw request.exception;

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=158610&r2=158611
==============================================================================
--- 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 Tue Mar
22 06:58:32 2005
@@ -36,6 +36,7 @@
 import org.apache.mina.io.IoHandlerFilter;
 import org.apache.mina.io.IoSession;
 import org.apache.mina.util.IoHandlerFilterManager;
+import org.apache.mina.util.Queue;
 
 /**
  * {@link IoConnector} for socket transport (TCP/IP).
@@ -55,6 +56,8 @@
 
     private ExceptionMonitor exceptionMonitor = new DefaultExceptionMonitor();
 
+    private final Queue connectQueue = new Queue();
+
     private Worker worker;
 
     /**
@@ -98,26 +101,30 @@
         }
         else
         {
-            ConnectEntry entry = new ConnectEntry( timeout, handler );
+            ConnectionRequest request = new ConnectionRequest( ch, timeout, handler );
+            synchronized( connectQueue )
+            {
+                connectQueue.push( request );
+            }
 
             synchronized( this )
             {
-                ch.register( selector, SelectionKey.OP_CONNECT, entry );
-
                 if( worker == null )
                 {
                     worker = new Worker();
                     worker.start();
                 }
             }
+            
+            selector.wakeup();
 
-            synchronized( entry )
+            synchronized( request )
             {
-                while( !entry.done )
+                while( !request.done )
                 {
                     try
                     {
-                        entry.wait();
+                        request.wait();
                     }
                     catch( InterruptedException e )
                     {
@@ -125,15 +132,51 @@
                 }
             }
 
-            if( entry.exception != null )
-                throw entry.exception;
+            if( request.exception != null )
+            {
+                request.exception.fillInStackTrace();
+                throw request.exception;
+            }
 
-            session = entry.session;
+            session = request.session;
         }
 
         return session;
     }
 
+    private void registerNew()
+    {
+        if( connectQueue.isEmpty() )
+            return;
+
+        for( ;; )
+        {
+            ConnectionRequest req;
+            synchronized( connectQueue )
+            {
+                req = ( ConnectionRequest ) connectQueue.pop();
+            }
+
+            if( req == null )
+                break;
+            
+            SocketChannel ch = req.channel;
+            try
+            {
+                ch.register( selector, SelectionKey.OP_CONNECT, req );
+            }
+            catch( IOException e )
+            {
+                req.exception = e;
+                synchronized( req )
+                {
+                    req.done = true;
+                    req.notify();
+                }
+            }
+        }
+    }
+    
     private void processSessions( Set keys )
     {
         Iterator it = keys.iterator();
@@ -146,32 +189,26 @@
                 continue;
 
             SocketChannel ch = ( SocketChannel ) key.channel();
-            ConnectEntry entry = ( ConnectEntry ) key.attachment();
+            ConnectionRequest entry = ( ConnectionRequest ) key.attachment();
 
             try
             {
                 ch.finishConnect();
                 SocketSession session = newSession( ch, entry.handler );
                 entry.session = session;
-                entry.done = true;
-
-                synchronized( entry )
-                {
-                    entry.notify();
-                }
             }
             catch( IOException e )
             {
                 entry.exception = e;
-                entry.done = true;
-
+            }
+            finally
+            {
                 synchronized( entry )
                 {
+                    entry.done = true;
                     entry.notify();
                 }
-            }
-            finally
-            {
+
                 key.cancel();
             }
         }
@@ -191,7 +228,7 @@
             if( !key.isValid() )
                 continue;
 
-            ConnectEntry entry = ( ConnectEntry ) key.attachment();
+            ConnectionRequest entry = ( ConnectionRequest ) key.attachment();
 
             if( currentTime >= entry.deadline )
             {
@@ -228,8 +265,10 @@
             {
                 try
                 {
-                    int nKeys = selector.select( 1000 );
+                    int nKeys = selector.select();
 
+                    registerNew();
+                    
                     if( selector.keys().isEmpty() )
                     {
                         synchronized( SocketConnector.this )
@@ -263,8 +302,10 @@
         }
     }
 
-    private static class ConnectEntry
+    private static class ConnectionRequest
     {
+        private final SocketChannel channel;
+        
         private final long deadline;
 
         private final IoHandler handler;
@@ -275,8 +316,9 @@
 
         private IOException exception;
 
-        private ConnectEntry( int timeout, IoHandler handler )
+        private ConnectionRequest( SocketChannel channel, int timeout, IoHandler handler
)
         {
+            this.channel = channel;
             this.deadline = System.currentTimeMillis() + timeout * 1000L;
             this.handler = handler;
         }

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?view=diff&r1=158610&r2=158611
==============================================================================
--- 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
Tue Mar 22 06:58:32 2005
@@ -3,6 +3,7 @@
  */
 package org.apache.mina.examples.echoserver;
 
+import java.net.InetAddress;
 import java.net.InetSocketAddress;
 
 import junit.framework.Assert;
@@ -56,8 +57,9 @@
     {
         EchoConnectorHandler handler = new EchoConnectorHandler();
         ByteBuffer readBuf = handler.readBuf;
-        IoSession session = connector.connect( new InetSocketAddress( port ),
-                                               handler );
+        IoSession session = connector.connect(
+                new InetSocketAddress( InetAddress.getLocalHost(), port ),
+                handler );
         
         for( int i = 0; i < 10; i ++ )
         {



Mime
View raw message