directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r160647 - in directory/network/trunk: ./ src/java/org/apache/mina/io/ src/java/org/apache/mina/io/datagram/ src/java/org/apache/mina/io/socket/ src/java/org/apache/mina/protocol/ src/java/org/apache/mina/protocol/io/ src/java/org/apache/mina/protocol/vmpipe/ src/test/org/apache/mina/examples/echoserver/
Date Sat, 09 Apr 2005 01:51:09 GMT
Author: trustin
Date: Fri Apr  8 18:51:03 2005
New Revision: 160647

URL: http://svn.apache.org/viewcvs?view=rev&rev=160647
Log:
* Made Connectors can create a session bound to a specific local socket address.

Modified:
    directory/network/trunk/   (props changed)
    directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
    directory/network/trunk/src/java/org/apache/mina/io/socket/SocketConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java
    directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
    directory/network/trunk/src/test/org/apache/mina/examples/echoserver/ConnectorTest.java

Propchange: directory/network/trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Fri Apr  8 18:51:03 2005
@@ -1,6 +1,8 @@
+
 target
 *.iml
 .classpath
 .project
 *.ipr
 *.log
+bin

Modified: directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java?view=diff&r1=160646&r2=160647
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/io/IoConnector.java Fri Apr  8 18:51:03
2005
@@ -60,6 +60,17 @@
             throws IOException;
 
     /**
+     * Connects to the specified <code>address</code>.  If communication starts
+     * successfully, events are fired to the specified
+     * <code>handler</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
+     */
+    IoSession connect( SocketAddress address, SocketAddress loadlAddress,
+                       IoHandler handler ) throws IOException;
+
+    /**
      * Connects to the specified <code>address</code> with timeout.  If
      * communication starts successfully, events are fired to the specified
      * <code>handler</code>.  This method blocks.
@@ -68,6 +79,17 @@
      */
     IoSession connect( SocketAddress address, int timeout, IoHandler handler )
             throws IOException;
+    
+    /**
+     * Connects to the specified <code>address</code> with timeout.  If
+     * communication starts successfully, events are fired to the specified
+     * <code>handler</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
+     */
+    IoSession connect( SocketAddress address, SocketAddress localAddress,
+                       int timeout, IoHandler handler ) throws IOException;
 
     IoHandlerFilterChain getFilterChain();
 

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=160646&r2=160647
==============================================================================
--- 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 Fri
Apr  8 18:51:03 2005
@@ -77,6 +77,12 @@
     public IoSession connect( SocketAddress address, IoHandler handler )
             throws IOException
     {
+        return connect( address, null, handler );
+    }
+    
+    public IoSession connect( SocketAddress address, SocketAddress localAddress,
+                              IoHandler handler ) throws IOException
+    {
         if( address == null )
             throw new NullPointerException( "address" );
         if( handler == null )
@@ -85,10 +91,32 @@
         if( !( address instanceof InetSocketAddress ) )
             throw new IllegalArgumentException( "Unexpected address type: "
                                                 + address.getClass() );
+        
+        if( localAddress != null && !( localAddress instanceof InetSocketAddress
) )
+        {
+            throw new IllegalArgumentException( "Unexpected local address type: "
+                                                + localAddress.getClass() );
+        }
 
         DatagramChannel ch = DatagramChannel.open();
-        ch.connect( address );
-        ch.configureBlocking( false );
+        boolean initialized = false;
+        try
+        {
+            if( localAddress != null )
+            {
+                ch.socket().bind( localAddress );
+            }
+            ch.connect( address );
+            ch.configureBlocking( false );
+            initialized = true;
+        }
+        finally
+        {
+            if( !initialized )
+            {
+                ch.close();
+            }
+        }
 
         RegistrationRequest request = new RegistrationRequest( ch, handler );
         synchronized( this )
@@ -128,10 +156,16 @@
         }
     }
 
-    public IoSession connect( SocketAddress address, int timeout,
-                             IoHandler handler ) throws IOException
+    public IoSession connect( SocketAddress address,
+                              int timeout, IoHandler handler ) throws IOException
+    {
+        return connect( address, null, timeout, handler );
+    }
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress,
+                              int timeout, IoHandler handler ) throws IOException
     {
-        return connect( address, handler );
+        return connect( address, localAddress, handler );
     }
 
     void closeSession( DatagramSession session )

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=160646&r2=160647
==============================================================================
--- 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 Fri Apr
 8 18:51:03 2005
@@ -71,12 +71,24 @@
     public IoSession connect( SocketAddress address, IoHandler handler )
             throws IOException
     {
-        return connect( address, Integer.MAX_VALUE, handler );
+        return connect( address, null, handler );
+    }
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress,
+                              IoHandler handler ) throws IOException
+    {
+        return connect( address, localAddress, Integer.MAX_VALUE, handler );
     }
 
     public IoSession connect( SocketAddress address, int timeout,
                               IoHandler handler ) throws IOException
     {
+        return connect( address, null, timeout, handler );
+    }
+
+    public IoSession connect( SocketAddress address, SocketAddress localAddress,
+                              int timeout, IoHandler handler ) throws IOException
+    {
         if( address == null )
             throw new NullPointerException( "address" );
         if( handler == null )
@@ -89,51 +101,68 @@
             throw new IllegalArgumentException( "Unexpected address type: "
                                                 + address.getClass() );
 
-        SocketChannel ch = SocketChannel.open();
-        ch.configureBlocking( false );
+        if( localAddress != null && !( localAddress instanceof InetSocketAddress
) )
+            throw new IllegalArgumentException( "Unexpected local address type: "
+                                                + localAddress.getClass() );
 
-        IoSession session;
-        if( ch.connect( address ) )
+        SocketChannel ch = SocketChannel.open();
+        boolean initialized = false;
+        try
         {
-            session = newSession( ch, handler );
+            if( localAddress != null )
+            {
+                ch.socket().bind( localAddress );
+            }
+            ch.configureBlocking( false );
+    
+            boolean connected = ch.connect( address );
+            initialized = true;
+
+            if( connected )
+            {
+                return newSession( ch, handler );
+            }
         }
-        else
+        finally
         {
-            ConnectionRequest request = new ConnectionRequest( ch, timeout, handler );
-            synchronized( this )
+            if( !initialized )
             {
-                synchronized( connectQueue )
-                {
-                    connectQueue.push( request );
-                }
-                startupWorker();
+                ch.close();
             }
-            selector.wakeup();
+        }
 
-            synchronized( request )
+        ConnectionRequest request = new ConnectionRequest( ch, timeout, handler );
+        synchronized( this )
+        {
+            synchronized( connectQueue )
             {
-                while( !request.done )
-                {
-                    try
-                    {
-                        request.wait();
-                    }
-                    catch( InterruptedException e )
-                    {
-                    }
-                }
+                connectQueue.push( request );
             }
+            startupWorker();
+        }
+        selector.wakeup();
 
-            if( request.exception != null )
+        synchronized( request )
+        {
+            while( !request.done )
             {
-                request.exception.fillInStackTrace();
-                throw request.exception;
+                try
+                {
+                    request.wait();
+                }
+                catch( InterruptedException e )
+                {
+                }
             }
+        }
 
-            session = request.session;
+        if( request.exception != null )
+        {
+            request.exception.fillInStackTrace();
+            throw request.exception;
         }
 
-        return session;
+        return request.session;
     }
     
     private synchronized void startupWorker()

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java?view=diff&r1=160646&r2=160647
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/ProtocolConnector.java Fri Apr
 8 18:51:03 2005
@@ -53,8 +53,18 @@
      * @throws IOException if failed to connect
      */
     ProtocolSession connect( SocketAddress address,
-                            ProtocolProvider protocolProvider )
-            throws IOException;
+                             ProtocolProvider protocolProvider ) throws IOException;
+
+    /**
+     * Connects to the specified <code>address</code>.  If communication starts
+     * successfully, events are fired to the specified
+     * <code>protocolProvider</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
+     */
+    ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                             ProtocolProvider protocolProvider ) throws IOException;
 
     /**
      * Connects to the specified <code>address</code> with timeout.  If
@@ -64,8 +74,18 @@
      * @throws IOException if failed to connect
      */
     ProtocolSession connect( SocketAddress address, int timeout,
-                            ProtocolProvider protocolProvider )
-            throws IOException;
+                             ProtocolProvider protocolProvider ) throws IOException;
+
+    /**
+     * Connects to the specified <code>address</code> with timeout.  If
+     * communication starts successfully, events are fired to the specified
+     * <code>protocolProvider</code>.  This method blocks.
+     * 
+     * @param localAddress the local address the channel is bound to
+     * @throws IOException if failed to connect
+     */
+    ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                             int timeout, ProtocolProvider protocolProvider ) throws IOException;
 
     ProtocolHandlerFilterChain getFilterChain();
 }

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java?view=diff&r1=160646&r2=160647
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/io/IoProtocolConnector.java
Fri Apr  8 18:51:03 2005
@@ -46,20 +46,34 @@
     }
 
     public ProtocolSession connect( SocketAddress address,
-                                   ProtocolProvider provider )
-            throws IOException
+                                    ProtocolProvider provider ) throws IOException
     {
-        IoSession session = connector.connect( address, adapter
-                .adapt( provider ) );
+        IoSession session = connector.connect(
+                address, adapter.adapt( provider ) );
+        return adapter.toProtocolSession( session );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                                    ProtocolProvider provider ) throws IOException
+    {
+        IoSession session = connector.connect(
+                address, localAddress, adapter.adapt( provider ) );
         return adapter.toProtocolSession( session );
     }
 
     public ProtocolSession connect( SocketAddress address, int timeout,
-                                   ProtocolProvider provider )
-            throws IOException
+                                    ProtocolProvider provider ) throws IOException
+    {
+        IoSession session = connector.connect(
+                address, timeout, adapter.adapt( provider ) );
+        return adapter.toProtocolSession( session );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                                    int timeout, ProtocolProvider provider ) throws IOException
     {
-        IoSession session = connector.connect( address, timeout, adapter
-                .adapt( provider ) );
+        IoSession session = connector.connect(
+                address, localAddress, timeout, adapter.adapt( provider ) );
         return adapter.toProtocolSession( session );
     }
 

Modified: directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java?view=diff&r1=160646&r2=160647
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
(original)
+++ directory/network/trunk/src/java/org/apache/mina/protocol/vmpipe/VmPipeConnector.java
Fri Apr  8 18:51:03 2005
@@ -36,9 +36,14 @@
         return filters;
     }
 
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                                    ProtocolProvider protocolProvider ) throws IOException
+    {
+        return connect( address, protocolProvider );
+    }
+
     public ProtocolSession connect( SocketAddress address,
-                                   ProtocolProvider protocolProvider )
-            throws IOException
+                                    ProtocolProvider protocolProvider ) throws IOException
     {
         if( address == null )
             throw new NullPointerException( "address" );
@@ -66,8 +71,13 @@
     }
 
     public ProtocolSession connect( SocketAddress address, int timeout,
-                                   ProtocolProvider protocolProvider )
-            throws IOException
+                                    ProtocolProvider protocolProvider ) throws IOException
+    {
+        return connect( address, protocolProvider );
+    }
+
+    public ProtocolSession connect( SocketAddress address, SocketAddress localAddress,
+                                    int timeout, ProtocolProvider protocolProvider ) throws
IOException
     {
         return connect( address, protocolProvider );
     }

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=160646&r2=160647
==============================================================================
--- 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
Fri Apr  8 18:51:03 2005
@@ -5,6 +5,7 @@
 
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.net.SocketAddress;
 
 import junit.framework.Assert;
 
@@ -29,7 +30,14 @@
     {
         IoConnector connector = new SocketConnector();
         connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
-        testTCP0( connector );
+        testTCP0( connector, null );
+    }
+    
+    public void testTCPWithLocalAddress() throws Exception
+    {
+        IoConnector connector = new SocketConnector();
+        connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
+        testTCP0( connector, new InetSocketAddress( port + 1 ) );
     }
 
     /**
@@ -50,15 +58,16 @@
         connectorSSLFilter.setDebug( SSLFilter.Debug.ON );
         connector.getFilterChain().addLast( "SSL", connectorSSLFilter );
 
-        testTCP0( connector );
+        testTCP0( connector, null );
     }
     
-    private void testTCP0( IoConnector connector ) throws Exception
+    private void testTCP0( IoConnector connector, SocketAddress localAddress ) throws Exception
     {
         EchoConnectorHandler handler = new EchoConnectorHandler();
         ByteBuffer readBuf = handler.readBuf;
         IoSession session = connector.connect(
                 new InetSocketAddress( InetAddress.getLocalHost(), port ),
+                localAddress,
                 handler );
         
         for( int i = 0; i < 10; i ++ )
@@ -110,7 +119,14 @@
     {
         IoConnector connector = new DatagramConnector();
         connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
-        testTCP0( connector );
+        testTCP0( connector, null );
+    }
+    
+    public void testUDPWithLocalAddress() throws Exception
+    {
+        IoConnector connector = new DatagramConnector();
+        connector.getFilterChain().addFirst( "threadPool", super.threadPoolFilter );
+        testTCP0( connector, new InetSocketAddress( port + 1 ) );
     }
     
     private void fillWriteBuffer( ByteBuffer writeBuf, int i )



Mime
View raw message