directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r366008 - in /directory/trunk/mina/src: main/java/org/apache/mina/common/ main/java/org/apache/mina/common/support/ main/java/org/apache/mina/transport/socket/nio/support/ main/java/org/apache/mina/transport/vmpipe/ main/java/org/apache/min...
Date Wed, 04 Jan 2006 22:27:13 GMT
Author: niklas
Date: Wed Jan  4 14:26:52 2006
New Revision: 366008

URL: http://svn.apache.org/viewcvs?rev=366008&view=rev
Log:
Resolved DIRMINA-42 and DIRMINA-93. The IoAcceptors for socket and vmpipe 
transports will now disconnect all clients by default on unbind. 
Furthermore, the IoAcceptors for these transports also provide methods to 
retrieve the currently connected sessions on a particular local address. 
Neither of these features have been implemented for the datagram transport 
since datagrams are connectionless and this kind of support doesn't make 
much sense in that case. 

Added:
    directory/trunk/mina/src/test/java/org/apache/mina/transport/AbstractBindTest.java
      - copied, changed from r366006, directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/AbstractBindTest.java
    directory/trunk/mina/src/test/java/org/apache/mina/transport/vmpipe/VmPipeBindTest.java
  (with props)
Removed:
    directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/AbstractBindTest.java
Modified:
    directory/trunk/mina/src/main/java/org/apache/mina/common/IoAcceptor.java
    directory/trunk/mina/src/main/java/org/apache/mina/common/support/BaseIoAcceptor.java
    directory/trunk/mina/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketAcceptorDelegate.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketConnectorDelegate.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipe.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeFilterChain.java
    directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeSessionImpl.java
    directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/DatagramBindTest.java
    directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/SocketBindTest.java

Modified: directory/trunk/mina/src/main/java/org/apache/mina/common/IoAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/common/IoAcceptor.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/common/IoAcceptor.java (original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/common/IoAcceptor.java Wed Jan  4 14:26:52
2006
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.util.Collection;
 
 /**
  * Accepts incoming connection, communicates with clients, and fires events to
@@ -68,6 +69,20 @@
     void unbind( SocketAddress address );
     
     /**
+     * Returns all sessions currently connected to the specified local address.
+     * 
+     * @param address the local address to return all sessions for. Must have
+     *        been bound previously.
+     * @return the sessions.
+     * @throws IllegalArgumentException if the specified <tt>address</tt> has

+     *         not been bound.
+     * @throws UnsupportedOperationException if this operation isn't supported
+     *         for the particular transport type implemented by this 
+     *         {@link IoAcceptor}.
+     */
+    Collection getManagedSessions( SocketAddress address );
+    
+    /**
      * (Optional) Returns an {@link IoSession} that is bound to the specified
      * <tt>localAddress</tt> and <tt>remoteAddress</tt> which reuses
      * the <tt>localAddress</tt> that is already bound by {@link IoAcceptor}
@@ -83,16 +98,15 @@
      */
     IoSession newSession( SocketAddress remoteAddress, SocketAddress localAddress );
     
-    // TODO: DIRMINA-93
     /**
      * Returns <tt>true</tt> if and only if all clients are disconnected
      * when this acceptor unbinds the related local address.
      */
-    //boolean isDisconnectClientsOnUnbind();
+    boolean isDisconnectClientsOnUnbind();
     
     /**
-     * Sets whether all clients are disconnected from this acceptor unbinds the
+     * Sets whether all clients are disconnected when this acceptor unbinds the
      * related local address.  The default value is <tt>true</tt>.
      */
-    //void setDisconnectClientsOnUnbind( boolean disconnectClientsOnUnbind );
+    void setDisconnectClientsOnUnbind( boolean disconnectClientsOnUnbind );
 }

Modified: directory/trunk/mina/src/main/java/org/apache/mina/common/support/BaseIoAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/common/support/BaseIoAcceptor.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/common/support/BaseIoAcceptor.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/common/support/BaseIoAcceptor.java
Wed Jan  4 14:26:52 2006
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.util.Collection;
 
 import org.apache.mina.common.IoAcceptor;
 import org.apache.mina.common.IoFilterChainBuilder;
@@ -34,9 +35,7 @@
  */
 public abstract class BaseIoAcceptor extends BaseIoSessionManager implements IoAcceptor
 {
-    /* TODO: DIRMINA-93
     private boolean disconnectClientsOnUnbind = true;
-    */
     
     protected BaseIoAcceptor()
     {
@@ -47,7 +46,11 @@
         this.bind( address, handler, IoFilterChainBuilder.NOOP );
     }
 
-    /* TODO: DIRMINA-93
+    public Collection getManagedSessions( SocketAddress address )
+    {
+        throw new UnsupportedOperationException();
+    }
+
     public boolean isDisconnectClientsOnUnbind()
     {
         return disconnectClientsOnUnbind;
@@ -57,7 +60,7 @@
     {
         this.disconnectClientsOnUnbind = disconnectClientsOnUnbind;
     }
-    */
+    
     public IoSession newSession( SocketAddress remoteAddress, SocketAddress localAddress
)
     {
         throw new UnsupportedOperationException();

Modified: directory/trunk/mina/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/common/support/DelegatedIoAcceptor.java
Wed Jan  4 14:26:52 2006
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.util.Collection;
 
 import org.apache.mina.common.DefaultIoFilterChainBuilder;
 import org.apache.mina.common.IoAcceptor;
@@ -68,6 +69,11 @@
         delegate.unbind( address );
     }
 
+    public Collection getManagedSessions( SocketAddress address )
+    {
+        return delegate.getManagedSessions( address );
+    }
+
     public IoSession newSession( SocketAddress remoteAddress, SocketAddress localAddress
)
     {
         return delegate.newSession( remoteAddress, localAddress );
@@ -88,7 +94,6 @@
         return delegate.getFilterChain();
     }
 
-    /* TODO: DIRMINA-93
     public boolean isDisconnectClientsOnUnbind()
     {
         return delegate.isDisconnectClientsOnUnbind();
@@ -98,5 +103,4 @@
     {
         delegate.setDisconnectClientsOnUnbind( disconnectClientsOnUnbind );
     }
-    */
 }

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketAcceptorDelegate.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketAcceptorDelegate.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketAcceptorDelegate.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketAcceptorDelegate.java
Wed Jan  4 14:26:52 2006
@@ -25,7 +25,11 @@
 import java.nio.channels.Selector;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
@@ -33,11 +37,15 @@
 import org.apache.mina.common.ExceptionMonitor;
 import org.apache.mina.common.IoAcceptor;
 import org.apache.mina.common.IoFilterChainBuilder;
+import org.apache.mina.common.IoFuture;
 import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
 import org.apache.mina.common.support.BaseIoAcceptor;
 import org.apache.mina.transport.socket.nio.SocketSessionManager;
 import org.apache.mina.util.Queue;
 
+import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
+
 /**
  * {@link IoAcceptor} for socket transport (TCP/IP).
  * 
@@ -56,6 +64,7 @@
     private int receiveBufferSize = -1;
     private Selector selector;
     private final Map channels = new HashMap();
+    private final Hashtable sessions = new Hashtable();
 
     private final Queue registerQueue = new Queue();
     private final Queue cancelQueue = new Queue();
@@ -149,14 +158,31 @@
         }
     }
 
+    public Collection getManagedSessions( SocketAddress address )
+    {
+        if( address == null )
+        {
+            throw new NullPointerException( "address" );
+        }
+        
+        Set managedSessions = ( Set ) sessions.get( address );
+        
+        if( managedSessions == null )
+        {
+            throw new IllegalArgumentException( "Address not bound: " + address );
+        }
+        
+        return Collections.unmodifiableCollection( Arrays.asList( managedSessions.toArray()
) );
+    }
+    
     public void unbind( SocketAddress address )
     {
-        // TODO: DIRMINA-93
         if( address == null )
         {
             throw new NullPointerException( "address" );
         }
 
+        final Set managedSessions = ( Set ) sessions.get( address );
         CancellationRequest request = new CancellationRequest( address );
         synchronized( this )
         {
@@ -201,6 +227,52 @@
 
             throw request.exception;
         }
+        
+        
+        // Disconnect all clients
+        if( isDisconnectClientsOnUnbind() && managedSessions != null )
+        {
+            IoSession[] tempSessions = ( IoSession[] ) 
+                                  managedSessions.toArray( new IoSession[ 0 ] );
+            
+            final Object lock = new Object();
+            
+            for( int i = 0; i < tempSessions.length; i++ )
+            {
+                if( !managedSessions.contains( tempSessions[ i ] ) )
+                {
+                    // The session has already been closed and have been 
+                    // removed from managedSessions by the SocketIoProcessor.
+                    continue;
+                }
+                tempSessions[ i ].close().setCallback( new IoFuture.Callback()
+                {
+                    public void operationComplete( IoFuture future )
+                    {
+                        synchronized( lock )
+                        {
+                            lock.notify();
+                        }
+                    }
+                } );
+            }
+
+            try
+            {
+                synchronized( lock )
+                {
+                    while( !managedSessions.isEmpty() )
+                    {
+                        lock.wait( 1000 );
+                    }
+                }
+            }
+            catch( InterruptedException ie )
+            {
+                // Ignored
+            }
+            
+        }        
     }
     
     private class Worker extends Thread
@@ -291,13 +363,16 @@
                 }
    
                 boolean success = false;
+                SocketSessionImpl session = null;
                 try
                 {
                     RegistrationRequest req = ( RegistrationRequest ) key.attachment();
-                    SocketSessionImpl session = new SocketSessionImpl( SocketAcceptorDelegate.this.wrapper,
ch, req.handler );
+                    session = new SocketSessionImpl( SocketAcceptorDelegate.this.wrapper,

+                            ( Set ) sessions.get( req.address ), ch, req.handler );
                     SocketAcceptorDelegate.this.filterChainBuilder.buildFilterChain( session.getFilterChain()
);
                     req.filterChainBuilder.buildFilterChain( session.getFilterChain() );
                     ( ( SocketFilterChain ) session.getFilterChain() ).sessionCreated( session
);
+                    session.getManagedSessions().add( session );
                     session.getIoProcessor().addNew( session );
                     success = true;
                 }
@@ -309,6 +384,10 @@
                 {
                     if( !success )
                     {
+                        if( session != null )
+                        {
+                            session.getManagedSessions().remove( session );
+                        }
                         ch.close();
                     }
                 }
@@ -357,6 +436,7 @@
                 ssc.register( selector, SelectionKey.OP_ACCEPT, req );
 
                 channels.put( req.address, ssc );
+                sessions.put( req.address, Collections.synchronizedSet( new HashSet() ) );
             }
             catch( IOException e )
             {
@@ -408,6 +488,7 @@
                 break;
             }
 
+            sessions.remove( request.address );
             ServerSocketChannel ssc = ( ServerSocketChannel ) channels.remove( request.address
);
             
             // close the channel

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketConnectorDelegate.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketConnectorDelegate.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketConnectorDelegate.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketConnectorDelegate.java
Wed Jan  4 14:26:52 2006
@@ -25,6 +25,8 @@
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
 
@@ -53,6 +55,7 @@
     private final String threadName = "SocketConnector-" + id;
     private Selector selector;
     private final Queue connectQueue = new Queue();
+    private final Set managedSessions = Collections.synchronizedSet( new HashSet() );
     private Worker worker;
 
     /**
@@ -270,7 +273,7 @@
 
     private SocketSessionImpl newSession( SocketChannel ch, IoHandler handler, IoFilterChainBuilder
filterChainBuilder ) throws IOException
     {
-        SocketSessionImpl session = new SocketSessionImpl( wrapper, ch, handler );
+        SocketSessionImpl session = new SocketSessionImpl( wrapper, managedSessions, ch,
handler );
         try
         {
             this.filterChainBuilder.buildFilterChain( session.getFilterChain() );
@@ -281,6 +284,7 @@
         {
             ExceptionUtil.throwException( e );
         }
+        session.getManagedSessions().add( session );
         session.getIoProcessor().addNew( session );
         return session;
     }

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketIoProcessor.java
Wed Jan  4 14:26:52 2006
@@ -189,6 +189,7 @@
             }
             catch( IOException e )
             {
+                session.getManagedSessions().remove( session );
                 registered = false;
                 ( ( SocketFilterChain ) session.getFilterChain() ).exceptionCaught( session,
e );
             }
@@ -244,6 +245,7 @@
             finally
             {
                 releaseWriteBuffers( session );
+                session.getManagedSessions().remove( session );
 
                 ( ( SocketFilterChain ) session.getFilterChain() ).sessionClosed( session
);
                 session.getCloseFuture().setClosed();

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/socket/nio/support/SocketSessionImpl.java
Wed Jan  4 14:26:52 2006
@@ -22,6 +22,7 @@
 import java.net.SocketException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
+import java.util.Set;
 
 import org.apache.mina.common.CloseFuture;
 import org.apache.mina.common.IoFilterChain;
@@ -52,6 +53,7 @@
     private final IoHandler handler;
     private final SocketAddress remoteAddress;
     private final SocketAddress localAddress;
+    private final Set managedSessions;    
     private SelectionKey key;
     private int readBufferSize = DEFAULT_READ_BUFFER_SIZE;
 
@@ -59,10 +61,11 @@
      * Creates a new instance.
      */
     public SocketSessionImpl(
-            IoSessionManager manager,
+            IoSessionManager manager, Set managedSessions,
             SocketChannel ch, IoHandler defaultHandler )
     {
         this.manager = manager;
+        this.managedSessions = managedSessions;
         this.ioProcessor = SocketIoProcessor.getInstance();
         this.filterChain = new SocketFilterChain( this );
         this.ch = ch;
@@ -90,6 +93,11 @@
     SocketChannel getChannel()
     {
         return ch;
+    }
+
+    Set getManagedSessions()
+    {
+        return managedSessions;
     }
 
     SelectionKey getSelectionKey()

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/VmPipeAcceptor.java
Wed Jan  4 14:26:52 2006
@@ -5,11 +5,17 @@
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.mina.common.IoFilterChainBuilder;
+import org.apache.mina.common.IoFuture;
 import org.apache.mina.common.IoHandler;
+import org.apache.mina.common.IoSession;
 import org.apache.mina.common.support.BaseIoAcceptor;
 import org.apache.mina.transport.vmpipe.support.VmPipe;
 
@@ -53,15 +59,85 @@
         }
     }
 
+    public Collection getManagedSessions( SocketAddress address )
+    {
+        if( address == null )
+            throw new NullPointerException( "address" );
+        
+        VmPipe pipe = null;
+        synchronized( boundHandlers )
+        {
+            pipe = ( VmPipe ) boundHandlers.get( address );
+            if( pipe == null )
+            {
+                throw new IllegalArgumentException( "Address not bound: " + address );
+            }
+        }
+        
+        Set managedSessions = pipe.getManagedServerSessions();
+        return Collections.unmodifiableCollection( Arrays.asList( managedSessions.toArray()
) );
+    }
+
     public void unbind( SocketAddress address )
     {
-        // TODO: DIRMINA-93
         if( address == null )
             throw new NullPointerException( "address" );
 
+        VmPipe pipe = null;
         synchronized( boundHandlers )
         {
-            boundHandlers.remove( address );
+            if( !boundHandlers.containsKey( address ) )
+            {
+                throw new IllegalArgumentException( "Address not bound: " + address );
+            }
+            
+            pipe = ( VmPipe ) boundHandlers.remove( address );
         }
+        
+        Set managedSessions = pipe.getManagedServerSessions();
+        
+        if( isDisconnectClientsOnUnbind() && managedSessions != null )
+        {
+            IoSession[] tempSessions = ( IoSession[] ) 
+                                  managedSessions.toArray( new IoSession[ 0 ] );
+            
+            final Object lock = new Object();
+            
+            for( int i = 0; i < tempSessions.length; i++ )
+            {
+                if( !managedSessions.contains( tempSessions[ i ] ) )
+                {
+                    // The session has already been closed and have been 
+                    // removed from managedSessions by the VmPipeFilterChain.
+                    continue;
+                }
+                tempSessions[ i ].close().setCallback( new IoFuture.Callback()
+                {
+                    public void operationComplete( IoFuture future )
+                    {
+                        synchronized( lock )
+                        {
+                            lock.notify();
+                        }
+                    }
+                } );
+            }
+
+            try
+            {
+                synchronized( lock )
+                {
+                    while( !managedSessions.isEmpty() )
+                    {
+                        lock.wait( 1000 );
+                    }
+                }
+            }
+            catch( InterruptedException ie )
+            {
+                // Ignored
+            }
+            
+        }                
     }
 }

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipe.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipe.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipe.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipe.java
Wed Jan  4 14:26:52 2006
@@ -3,6 +3,10 @@
  */
 package org.apache.mina.transport.vmpipe.support;
 
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
 import org.apache.mina.common.IoFilterChainBuilder;
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.transport.vmpipe.VmPipeAcceptor;
@@ -14,6 +18,8 @@
     private final VmPipeAddress address;
     private final IoHandler handler;
     private final IoFilterChainBuilder filterChainBuilder;
+    private final Set managedClientSessions = Collections.synchronizedSet( new HashSet()
);
+    private final Set managedServerSessions = Collections.synchronizedSet( new HashSet()
);
     
     public VmPipe( VmPipeAcceptor acceptor,
                    VmPipeAddress address,
@@ -44,5 +50,15 @@
     public IoFilterChainBuilder getFilterChainBuilder()
     {
         return filterChainBuilder;
+    }
+
+    public Set getManagedClientSessions()
+    {
+        return managedClientSessions;
+    }
+    
+    public Set getManagedServerSessions()
+    {
+        return managedServerSessions;
     }
 }

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeFilterChain.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeFilterChain.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeFilterChain.java
Wed Jan  4 14:26:52 2006
@@ -96,6 +96,7 @@
         {
             if( !closeFuture.isClosed() )
             {
+                s.getManagedSessions().remove( s );
                 ( ( VmPipeFilterChain ) s.getFilterChain() ).sessionClosed( session );
                 closeFuture.setClosed();
                 s.remoteSession.close();

Modified: directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeSessionImpl.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeSessionImpl.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeSessionImpl.java
(original)
+++ directory/trunk/mina/src/main/java/org/apache/mina/transport/vmpipe/support/VmPipeSessionImpl.java
Wed Jan  4 14:26:52 2006
@@ -5,6 +5,7 @@
 
 import java.io.IOException;
 import java.net.SocketAddress;
+import java.util.Set;
 
 import org.apache.mina.common.CloseFuture;
 import org.apache.mina.common.ExceptionMonitor;
@@ -35,6 +36,7 @@
     private final SocketAddress remoteAddress;
     private final IoHandler handler;
     private final VmPipeFilterChain filterChain;
+    private final Set managedSessions;
     final VmPipeSessionImpl remoteSession;
     final Object lock;
     final Queue pendingDataQueue;
@@ -54,6 +56,8 @@
         this.filterChain = new VmPipeFilterChain( this );
         this.pendingDataQueue = new Queue();
 
+        this.managedSessions = remoteEntry.getManagedClientSessions();
+        
         remoteSession = new VmPipeSessionImpl( manager, this, remoteEntry );
         
         // initialize remote session
@@ -86,6 +90,9 @@
         VmPipeIdleStatusChecker.getInstance().addSession( remoteSession );
         VmPipeIdleStatusChecker.getInstance().addSession( this );
         
+        remoteSession.managedSessions.add( remoteSession );
+        this.managedSessions.add( this );
+        
         ( ( VmPipeFilterChain ) remoteSession.getFilterChain() ).sessionOpened( remoteSession
);
         filterChain.sessionOpened( this );
     }
@@ -103,8 +110,14 @@
         this.filterChain = new VmPipeFilterChain( this );
         this.remoteSession = remoteSession;
         this.pendingDataQueue = new Queue();
+        this.managedSessions = entry.getManagedServerSessions();
     }
     
+    Set getManagedSessions()
+    {
+        return managedSessions;
+    }
+
     public IoSessionManager getManager()
     {
         return manager;

Copied: directory/trunk/mina/src/test/java/org/apache/mina/transport/AbstractBindTest.java
(from r366006, directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/AbstractBindTest.java)
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/test/java/org/apache/mina/transport/AbstractBindTest.java?p2=directory/trunk/mina/src/test/java/org/apache/mina/transport/AbstractBindTest.java&p1=directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/AbstractBindTest.java&r1=366006&r2=366008&rev=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/AbstractBindTest.java
(original)
+++ directory/trunk/mina/src/test/java/org/apache/mina/transport/AbstractBindTest.java Wed
Jan  4 14:26:52 2006
@@ -16,10 +16,10 @@
  *   limitations under the License.
  *
  */
-package org.apache.mina.transport.socket.nio;
+package org.apache.mina.transport;
 
 import java.io.IOException;
-import java.net.InetSocketAddress;
+import java.net.SocketAddress;
 import java.util.Date;
 
 import junit.framework.Assert;
@@ -30,6 +30,9 @@
 import org.apache.mina.common.IoAcceptor;
 import org.apache.mina.common.IoHandlerAdapter;
 import org.apache.mina.common.IoSession;
+import org.apache.mina.transport.socket.nio.DatagramAcceptor;
+import org.apache.mina.transport.socket.nio.SocketAcceptor;
+import org.apache.mina.transport.socket.nio.SocketSession;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,7 +52,9 @@
         this.acceptor = acceptor;
     }
     
-    private void bind( boolean reuseAddress ) throws IOException
+    protected abstract SocketAddress createSocketAddress( int port );
+    
+    protected void bind( boolean reuseAddress ) throws IOException
     {
         setReuseAddress( reuseAddress );
 
@@ -64,7 +69,7 @@
             socketBound = false;
             try
             {
-                acceptor.bind( new InetSocketAddress( port ),
+                acceptor.bind( createSocketAddress( port ),
                         new EchoProtocolHandler() );
                 socketBound = true;
                 break;
@@ -99,7 +104,7 @@
     {
         try
         {
-            acceptor.unbind( new InetSocketAddress( port ) );
+            acceptor.unbind( createSocketAddress( port ) );
         }
         catch( Exception e )
         {
@@ -113,7 +118,7 @@
         
         try
         {
-            acceptor.bind( new InetSocketAddress( port ), new EchoProtocolHandler() );
+            acceptor.bind( createSocketAddress( port ), new EchoProtocolHandler() );
             Assert.fail( "IOException is not thrown" );
         }
         catch( IOException e )
@@ -126,12 +131,12 @@
         bind( false );
         
         // this should succeed
-        acceptor.unbind( new InetSocketAddress( port ) );
+        acceptor.unbind( createSocketAddress( port ) );
         
         try
         {
             // this should fail
-            acceptor.unbind( new InetSocketAddress( port ) );
+            acceptor.unbind( createSocketAddress( port ) );
             Assert.fail( "Exception is not thrown" );
         }
         catch( Exception e )
@@ -143,7 +148,7 @@
     {
         bind( true );
         
-        InetSocketAddress addr = new InetSocketAddress( port );
+        SocketAddress addr = createSocketAddress( port );
         EchoProtocolHandler handler = new EchoProtocolHandler();
         for( int i = 0; i < 1024; i++ ) 
         {
@@ -156,7 +161,7 @@
     {
         setReuseAddress( true );
 
-        InetSocketAddress addr = new InetSocketAddress( port );
+        SocketAddress addr = createSocketAddress( port );
         EchoProtocolHandler handler = new EchoProtocolHandler();
         for( int i = 0; i < 1048576; i++ )
         {

Modified: directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/DatagramBindTest.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/DatagramBindTest.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/DatagramBindTest.java
(original)
+++ directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/DatagramBindTest.java
Wed Jan  4 14:26:52 2006
@@ -18,6 +18,10 @@
  */
 package org.apache.mina.transport.socket.nio;
 
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+
+import org.apache.mina.transport.AbstractBindTest;
 
 /**
  * Tests {@link DatagramAcceptor} resource leakage.
@@ -33,4 +37,8 @@
         super( new DatagramAcceptor() );
     }
 
+    protected SocketAddress createSocketAddress( int port )
+    {
+        return new InetSocketAddress( port );
+    }    
 }

Modified: directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/SocketBindTest.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/SocketBindTest.java?rev=366008&r1=366007&r2=366008&view=diff
==============================================================================
--- directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/SocketBindTest.java
(original)
+++ directory/trunk/mina/src/test/java/org/apache/mina/transport/socket/nio/SocketBindTest.java
Wed Jan  4 14:26:52 2006
@@ -18,6 +18,17 @@
  */
 package org.apache.mina.transport.socket.nio;
 
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.util.Collection;
+
+import junit.framework.Assert;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.transport.AbstractBindTest;
 
 /**
  * Tests {@link SocketAcceptor} resource leakage.
@@ -32,5 +43,43 @@
     {
         super( new SocketAcceptor() );
     }
-
+    
+    protected SocketAddress createSocketAddress( int port )
+    {
+        return new InetSocketAddress( port );
+    }
+    
+    public void testUnbindDisconnectsClients() throws Exception
+    {
+        // TODO: This test is almost identical to the test with the same name in VmPipeBindTest
+        bind( false );
+        
+        SocketAddress addr = createSocketAddress( port );
+     
+        IoConnector connector = new SocketConnector();
+        IoSession[] sessions = new IoSession[ 5 ];
+        for( int i = 0; i < sessions.length; i++ )
+        {
+            ConnectFuture future = connector.connect( addr, new IoHandlerAdapter() );
+            future.join();
+            sessions[ i ] = future.getSession();
+            Assert.assertTrue( sessions[ i ].isConnected() );
+        }
+        
+        // Wait for the server side sessions to be created.
+        Thread.sleep( 500 );
+        
+        Collection managedSessions = acceptor.getManagedSessions( addr );
+        Assert.assertEquals( 5, managedSessions.size() );
+        
+        acceptor.unbind( addr );
+        
+        // Wait for the client side sessions to close.
+        Thread.sleep( 500 );
+             
+        for( int i = 0; i < sessions.length; i++ )
+        {
+            Assert.assertFalse( sessions[ i ].isConnected() );
+        }
+    }
 }

Added: directory/trunk/mina/src/test/java/org/apache/mina/transport/vmpipe/VmPipeBindTest.java
URL: http://svn.apache.org/viewcvs/directory/trunk/mina/src/test/java/org/apache/mina/transport/vmpipe/VmPipeBindTest.java?rev=366008&view=auto
==============================================================================
--- directory/trunk/mina/src/test/java/org/apache/mina/transport/vmpipe/VmPipeBindTest.java
(added)
+++ directory/trunk/mina/src/test/java/org/apache/mina/transport/vmpipe/VmPipeBindTest.java
Wed Jan  4 14:26:52 2006
@@ -0,0 +1,89 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.transport.vmpipe;
+
+import java.net.SocketAddress;
+import java.util.Collection;
+
+import junit.framework.Assert;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoConnector;
+import org.apache.mina.common.IoHandlerAdapter;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.transport.AbstractBindTest;
+
+/**
+ * Tests {@link VmPipeAcceptor} bind and unbind.
+ * 
+ * @author The Apache Directory Project (dev@directory.apache.org)
+ * @version $Rev$, $Date$ 
+ */
+public class VmPipeBindTest extends AbstractBindTest
+{
+
+    public VmPipeBindTest()
+    {
+        super( new VmPipeAcceptor() );
+    }
+
+    protected SocketAddress createSocketAddress( int port )
+    {
+        return new VmPipeAddress( port );
+    }
+    
+    public void testUnbindDisconnectsClients() throws Exception
+    {
+        // TODO: This test is almost identical to the test with the same name in SocketBindTest
+        bind( false );
+        
+        SocketAddress addr = createSocketAddress( port );
+     
+        IoConnector connector = new VmPipeConnector();
+        IoSession[] sessions = new IoSession[ 5 ];
+        for( int i = 0; i < sessions.length; i++ )
+        {
+            ConnectFuture future = connector.connect( addr, new IoHandlerAdapter() );
+            future.join();
+            sessions[ i ] = future.getSession();
+            Assert.assertTrue( sessions[ i ].isConnected() );
+        }
+        
+        // Wait for the server side sessions to be created.
+        Thread.sleep( 500 );
+        
+        Collection managedSessions = acceptor.getManagedSessions( addr );
+        Assert.assertEquals( 5, managedSessions.size() );
+        // Make sure it's the server side sessions we get when calling getManagedSessions()
+        for( int i = 0; i < sessions.length; i++ )
+        {
+            Assert.assertFalse( managedSessions.contains( sessions[ i ] ) );
+        }
+        
+        acceptor.unbind( addr );
+        
+        // Wait for the client side sessions to close.
+        Thread.sleep( 500 );
+             
+        for( int i = 0; i < sessions.length; i++ )
+        {
+            Assert.assertFalse( sessions[ i ].isConnected() );
+        }
+    }
+}

Propchange: directory/trunk/mina/src/test/java/org/apache/mina/transport/vmpipe/VmPipeBindTest.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message