You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Matthew Phillips <ma...@gmail.com> on 2008/02/15 00:22:13 UTC

IoSession and sessionClosed callback in MINA 2.0

While porting to MINA 2.0 I've had a few tests fail that depend on the  
IoHandler.sessionClosed callback. It appears that if the acceptor  
closes a session in its IoHandler, the connector's IoHandler does not  
get a session close event.

The attached tests demonstrate this: the one ending in MINA 1.1 works  
fine under MINA 1.1.5, the one ending in MINA 2.0 (which is a direct  
port of the 1.1 test to the MINA 2.0 API) does not: the sessionClosed  
method is not called when the acceptor closes its end.

Hope someone can either explain why my understanding of how this is  
supposed to work is wrong or have a look at how MINA 2.0 handles this.

Cheers,

Matthew.


Re: IoSession and sessionClosed callback in MINA 2.0

Posted by Wilson Yeung <wi...@gmail.com>.
Hm.  Nevermind.

I guess we should expect the underlyng recv() call to signal the lost
connection as Mina is always interested in reading as far as I can
tell.

Re: IoSession and sessionClosed callback in MINA 2.0

Posted by Wilson Yeung <wi...@gmail.com>.
You never try to send any data on the IoSession created by the Connector.

If you were to, you'd probably get the sessionClosed() callback.

Re: IoSession and sessionClosed callback in MINA 2.0

Posted by Matthew Phillips <ma...@gmail.com>.
Hmm, that possibility did occur to me just after I sent it, thanks for  
pointing it out.

The tests are pasted inline below.

MINA 1.1 test (passes)
------------------------------------------------------

import static java.lang.System.currentTimeMillis;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.net.InetSocketAddress;

import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketConnector;
import org.junit.Test;

public class JUTestSessionClose
{
   private static final int TIMEOUT = 5000;

   @Test
   public void sessionClose ()
     throws Exception
   {
     InetSocketAddress address = new InetSocketAddress ("127.0.0.1",  
29170);

     SocketAcceptor acceptor = new SocketAcceptor ();

     // close session as soon as it's opened
     IoHandler acceptorHandler = new IoHandlerAdapter ()
     {
       @Override
       public void sessionOpened (IoSession session)
         throws Exception
       {
         session.close ();
       }
     };

     acceptor.bind (address, acceptorHandler);

     SocketConnector connector = new SocketConnector ();

     final Object lock = new Object ();

     // call notify on lock when session is closed
     IoHandler connectorHandler = new IoHandlerAdapter ()
     {
       @Override
       public void sessionClosed (IoSession session)
         throws Exception
       {
         synchronized (lock)
         {
           lock.notify ();
         }
       }
     };

     ConnectFuture connectFuture = connector.connect (address,  
connectorHandler);

     assertTrue (connectFuture.join (TIMEOUT));

     long startedAt = currentTimeMillis ();

     // wait for connector to receive sessionClosed
     synchronized (lock)
     {
       lock.wait (TIMEOUT);
     }

     assertFalse ("Did not receive close",
                  connectFuture.getSession ().isConnected ());

     assertTrue (currentTimeMillis () - startedAt < TIMEOUT);

     connectFuture.getSession().close();
     acceptor.unbindAll();
   }
}

------------------------------------------------------

MINA 2.0 test (fails against 2.0 trunk)
------------------------------------------------------

import java.net.InetSocketAddress;

import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.junit.Test;

import static java.lang.System.currentTimeMillis;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class JUTestSessionClose
{
   private static final int TIMEOUT = 5000;

   @Test
   public void sessionClose ()
     throws Exception
   {
     InetSocketAddress address = new InetSocketAddress ("127.0.0.1",  
29170);

     SocketAcceptor acceptor = new NioSocketAcceptor ();

     // close session as soon as it's opened
     acceptor.setHandler (new IoHandlerAdapter ()
     {
       @Override
       public void sessionOpened (IoSession session)
         throws Exception
       {
         session.close ();
       }
     });

     acceptor.bind (address);

     SocketConnector connector = new NioSocketConnector (1);

     final Object lock = new Object ();

     // call notify on lock when session is closed
     connector.setHandler (new IoHandlerAdapter ()
     {
       @Override
       public void sessionClosed (IoSession session)
         throws Exception
       {
         synchronized (lock)
         {
           lock.notify ();
         }
       }
     });

     ConnectFuture connectFuture = connector.connect (address);

     assertTrue (connectFuture.await (TIMEOUT));

     long startedAt = currentTimeMillis ();

     // wait for connector to receive sessionClosed
     synchronized (lock)
     {
       lock.wait (TIMEOUT);
     }

     assertFalse ("Did not receive close",
                  connectFuture.getSession ().isConnected ());

     assertTrue (currentTimeMillis () - startedAt < TIMEOUT);

     connector.dispose ();
     acceptor.dispose ();
   }
}

------------------------------------------------------

On 15/02/2008, at 12:13 PM, Mark Webb wrote:

> your attachment did not come through.  AFAIK, this mailing list does
> not support attachments...
>
>
>
> On Thu, Feb 14, 2008 at 6:22 PM, Matthew Phillips  
> <ma...@gmail.com> wrote:
>> While porting to MINA 2.0 I've had a few tests fail that depend on  
>> the
>> IoHandler.sessionClosed callback. It appears that if the acceptor
>> closes a session in its IoHandler, the connector's IoHandler does not
>> get a session close event.
>>
>> The attached tests demonstrate this: the one ending in MINA 1.1 works
>> fine under MINA 1.1.5, the one ending in MINA 2.0 (which is a direct
>> port of the 1.1 test to the MINA 2.0 API) does not: the sessionClosed
>> method is not called when the acceptor closes its end.
>>
>> Hope someone can either explain why my understanding of how this is
>> supposed to work is wrong or have a look at how MINA 2.0 handles  
>> this.
>>
>> Cheers,
>>
>> Matthew.
>>
>>
>
>
>
> -- 
> --------------------------------
> Talent hits a target no one else can hit; Genius hits a target no one
> else can see.


Re: IoSession and sessionClosed callback in MINA 2.0

Posted by Mark Webb <el...@gmail.com>.
your attachment did not come through.  AFAIK, this mailing list does
not support attachments...



On Thu, Feb 14, 2008 at 6:22 PM, Matthew Phillips <ma...@gmail.com> wrote:
> While porting to MINA 2.0 I've had a few tests fail that depend on the
>  IoHandler.sessionClosed callback. It appears that if the acceptor
>  closes a session in its IoHandler, the connector's IoHandler does not
>  get a session close event.
>
>  The attached tests demonstrate this: the one ending in MINA 1.1 works
>  fine under MINA 1.1.5, the one ending in MINA 2.0 (which is a direct
>  port of the 1.1 test to the MINA 2.0 API) does not: the sessionClosed
>  method is not called when the acceptor closes its end.
>
>  Hope someone can either explain why my understanding of how this is
>  supposed to work is wrong or have a look at how MINA 2.0 handles this.
>
>  Cheers,
>
>  Matthew.
>
>



-- 
--------------------------------
Talent hits a target no one else can hit; Genius hits a target no one
else can see.