You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by jv...@apache.org on 2006/07/31 14:07:21 UTC

svn commit: r427087 - in /directory/sandbox/jvermillard/mina/java/org/apache/mina: common/support/BaseIoSession.java transport/socket/nio/SocketConnector.java transport/socket/nio/SocketSessionImpl.java

Author: jvermillard
Date: Mon Jul 31 05:07:20 2006
New Revision: 427087

URL: http://svn.apache.org/viewvc?rev=427087&view=rev
Log:
DIRMINA-68

BaseIoSession can be "unclosed();"
Socketconnector have reconnection facilities

Modified:
    directory/sandbox/jvermillard/mina/java/org/apache/mina/common/support/BaseIoSession.java
    directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketConnector.java
    directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketSessionImpl.java

Modified: directory/sandbox/jvermillard/mina/java/org/apache/mina/common/support/BaseIoSession.java
URL: http://svn.apache.org/viewvc/directory/sandbox/jvermillard/mina/java/org/apache/mina/common/support/BaseIoSession.java?rev=427087&r1=427086&r2=427087&view=diff
==============================================================================
--- directory/sandbox/jvermillard/mina/java/org/apache/mina/common/support/BaseIoSession.java (original)
+++ directory/sandbox/jvermillard/mina/java/org/apache/mina/common/support/BaseIoSession.java Mon Jul 31 05:07:20 2006
@@ -46,7 +46,7 @@
     /** 
      * A future that will be set 'closed' when the connection is closed.
      */
-    private final CloseFuture closeFuture = new DefaultCloseFuture( this );
+    private CloseFuture closeFuture = new DefaultCloseFuture( this );
     private boolean closing;
 
     // Configuration variables
@@ -111,6 +111,20 @@
 
         close0();
         return closeFuture;
+    }
+    
+    /**
+     * Uncloses this session immediately. Prepare the session for a reconnection
+     */
+    public void unclose() {
+        synchronized( this )
+        {
+            if( isClosing() )
+            {
+            	closeFuture = new DefaultCloseFuture( this );
+            	closing=false;
+            }
+        }
     }
 
     /**

Modified: directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketConnector.java
URL: http://svn.apache.org/viewvc/directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketConnector.java?rev=427087&r1=427086&r2=427087&view=diff
==============================================================================
--- directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketConnector.java (original)
+++ directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketConnector.java Mon Jul 31 05:07:20 2006
@@ -36,6 +36,7 @@
 import org.apache.mina.common.IoConnectorConfig;
 import org.apache.mina.common.IoHandler;
 import org.apache.mina.common.IoServiceConfig;
+import org.apache.mina.common.IoSession;
 import org.apache.mina.common.support.BaseIoConnector;
 import org.apache.mina.common.support.DefaultConnectFuture;
 import org.apache.mina.util.Queue;
@@ -129,7 +130,13 @@
     }
 
     public ConnectFuture connect( SocketAddress address, SocketAddress localAddress,
-                                  IoHandler handler, IoServiceConfig config )
+            IoHandler handler, IoServiceConfig config ) {
+    	return connect( address, localAddress, handler, config ,null);
+    }
+
+    
+    public ConnectFuture connect( SocketAddress address, SocketAddress localAddress,
+                                  IoHandler handler, IoServiceConfig config , IoSession reconnectingSession)
     {
         if( address == null )
             throw new NullPointerException( "address" );
@@ -143,7 +150,9 @@
         if( localAddress != null && !( localAddress instanceof InetSocketAddress ) )
             throw new IllegalArgumentException( "Unexpected local address type: "
                                                 + localAddress.getClass() );
-
+        if(reconnectingSession!=null && ! (reconnectingSession instanceof SocketSessionImpl))
+        	throw new IllegalArgumentException( "Unexpected reconnectingSession type: "
+                    + reconnectingSession.getClass() );
         if( config == null )
         {
             config = getDefaultConfig();
@@ -164,7 +173,13 @@
 
             if( ch.connect( address ) )
             {
-                SocketSessionImpl session = newSession( ch, handler, config );
+            	SocketSessionImpl session;
+            	if(reconnectingSession==null)
+            		session= newSession( ch, handler, config );
+            	else {
+            		session=renewSession((SocketSessionImpl)reconnectingSession,ch);
+            	}
+ 
                 success = true;
                 DefaultConnectFuture future = new DefaultConnectFuture();
                 future.setSession( session );
@@ -192,7 +207,11 @@
             }
         }
 
-        ConnectionRequest request = new ConnectionRequest( ch, handler, config );
+        ConnectionRequest request;
+        if(reconnectingSession==null)
+        	request = new ConnectionRequest( ch, handler, config );
+        else
+        	request = new ConnectionRequest( ch, handler, config, reconnectingSession);
         synchronized( lock )
         {
             try
@@ -222,6 +241,24 @@
 
         return request;
     }
+    
+    /**
+     * Reconnects the specified <code>session</code>.  If communication starts
+     * successfully, events are fired to the specified
+     * <code>handler</code>.
+     * @param session the session to reconnect
+     * @return {@link ConnectFuture} that will tell the result of the reconnection attempt
+     */
+    public ConnectFuture reconnect(IoSession session) {
+    	if( ! ( session instanceof SocketSessionImpl ) )
+            throw new IllegalArgumentException( "Unexpected session type: "
+                                                + session.getClass() );
+    	SocketSessionImpl sess=(SocketSessionImpl)session;
+    	if(sess.isConnected())
+    		throw new IllegalArgumentException("The session is not closed, can't reconnect it");
+    	
+    	return connect(sess.getRemoteAddress(),null,sess.getHandler(),null,session);
+    }
 
     public IoServiceConfig getDefaultConfig()
     {
@@ -284,7 +321,11 @@
             try
             {
                 ch.finishConnect();
-                SocketSessionImpl session = newSession( ch, entry.handler, entry.config );
+                SocketSessionImpl session;
+                if(entry.reconnectingSession==null)
+                	session= newSession( ch, entry.handler, entry.config );
+                else
+                	session= renewSession((SocketSessionImpl) entry.reconnectingSession,ch);
                 entry.setSession( session );
                 success = true;
             }
@@ -344,7 +385,19 @@
             }
         }
     }
-
+    
+    
+    // recycle an old session
+    private SocketSessionImpl renewSession(SocketSessionImpl reconnectingSession,SocketChannel ch) throws IOException  {
+    	// is needed to recall session created ? I think no.
+    	//( ( SocketFilterChain ) reconnectingSession.getFilterChain() ).sessionCreated( reconnectingSession );
+    	reconnectingSession.unclose();
+    	
+    	reconnectingSession.getManagedSessions().add( reconnectingSession );
+    	reconnectingSession.getIoProcessor().addNew( reconnectingSession );
+    	reconnectingSession.setSocketChannel(ch);
+		return (SocketSessionImpl)reconnectingSession;
+    }
     private SocketSessionImpl newSession( SocketChannel ch, IoHandler handler, IoServiceConfig config )
         throws IOException
     {
@@ -457,8 +510,13 @@
         private final long deadline;
         private final IoHandler handler;
         private final IoServiceConfig config;
+        private final IoSession reconnectingSession;
+        
+        private ConnectionRequest( SocketChannel channel, IoHandler handler, IoServiceConfig config ) {
+        	this(channel,handler,config,null);
+        }
 
-        private ConnectionRequest( SocketChannel channel, IoHandler handler, IoServiceConfig config )
+        private ConnectionRequest( SocketChannel channel, IoHandler handler, IoServiceConfig config, IoSession reconnectingSession )
         {
             this.channel = channel;
             long timeout;
@@ -473,6 +531,7 @@
             this.deadline = System.currentTimeMillis() + timeout;
             this.handler = handler;
             this.config = config;
+            this.reconnectingSession=reconnectingSession;
         }
     }
 }

Modified: directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketSessionImpl.java
URL: http://svn.apache.org/viewvc/directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketSessionImpl.java?rev=427087&r1=427086&r2=427087&view=diff
==============================================================================
--- directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketSessionImpl.java (original)
+++ directory/sandbox/jvermillard/mina/java/org/apache/mina/transport/socket/nio/SocketSessionImpl.java Mon Jul 31 05:07:20 2006
@@ -48,7 +48,7 @@
     private final SocketSessionConfig config = new SessionConfigImpl();
     private final SocketIoProcessor ioProcessor;
     private final SocketFilterChain filterChain;
-    private final SocketChannel ch;
+    private SocketChannel ch;
     private final Queue writeRequestQueue;
     private final IoHandler handler;
     private final SocketAddress remoteAddress;
@@ -124,7 +124,11 @@
     {
         return ch;
     }
-
+    void setSocketChannel(SocketChannel ch)
+    {
+    	this.ch=ch;
+    }
+    
     Set getManagedSessions()
     {
         return managedSessions;