directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r438841 - in /directory/trunks/mina: core/src/main/java/org/apache/mina/filter/codec/ core/src/main/java/org/apache/mina/filter/codec/textline/ filter-codec-asn1/src/main/java/org/apache/mina/filter/codec/asn1/ filter-codec-netty/src/main/j...
Date Thu, 31 Aug 2006 08:41:54 GMT
Author: trustin
Date: Thu Aug 31 01:41:51 2006
New Revision: 438841

URL: http://svn.apache.org/viewvc?rev=438841&view=rev
Log:
Fixed issue: DIRMINA-248 (ProtocolDecoder should get notified when a connection is closed.)
* Added ProtocolDecoder.finishDecode(IoSession,ProtocolDecoderOutput)
** Added the default implementation to ProtocolDecoderAdapter
* Changed ProtocolCodecFilter to invoke finishDecode() when sessionClosed event is invoked.


Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoderAdapter.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/SynchronizedProtocolDecoder.java
    directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java
    directory/trunks/mina/filter-codec-asn1/src/main/java/org/apache/mina/filter/codec/asn1/Asn1CodecDecoder.java
    directory/trunks/mina/filter-codec-netty/src/main/java/org/apache/mina/filter/codec/netty/NettyDecoder.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java?rev=438841&r1=438840&r2=438841&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
Thu Aug 31 01:41:51 2006
@@ -249,8 +249,35 @@
     
     public void sessionClosed( NextFilter nextFilter, IoSession session ) throws Exception
     {
-        disposeEncoder( session );
-        disposeDecoder( session );
+	// Call finishDecode() first when a connection is closed.
+	ProtocolDecoder decoder = getDecoder( session );
+        ProtocolDecoderOutput decoderOut = getDecoderOut( session, nextFilter );
+        try
+        {
+            decoder.finishDecode( session, decoderOut );
+        }
+        catch( Throwable t )
+        {
+            ProtocolDecoderException pde;
+            if( t instanceof ProtocolDecoderException )
+            {
+                pde = ( ProtocolDecoderException ) t;
+            }
+            else
+            {
+                pde = new ProtocolDecoderException( t );
+            }
+            throw pde;
+        }
+        finally
+        {
+            // Dispose all.
+            disposeEncoder( session );
+            disposeDecoder( session );
+
+            decoderOut.flush();
+        }
+        
         nextFilter.sessionClosed( session );
     }
 

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java?rev=438841&r1=438840&r2=438841&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
Thu Aug 31 01:41:51 2006
@@ -50,6 +50,17 @@
                  ProtocolDecoderOutput out ) throws Exception;
     
     /**
+     * Invoked when the specified <tt>session</tt> is closed.  This method is
useful
+     * when you deal with the protocol which doesn't specify the length of a message
+     * such as HTTP response without <tt>content-length</tt> header. Implement
this
+     * method to process the remaining data that {@link #decode(IoSession, ByteBuffer, ProtocolDecoderOutput)}
+     * method didn't process completely.
+     * 
+     * @throws Exception if the read data violated protocol specification
+     */
+    void finishDecode( IoSession session, ProtocolDecoderOutput out ) throws Exception;
+    
+    /**
      * Releases all resources related with this decoder.
      * 
      * @throws Exception if failed to dispose all resources

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoderAdapter.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoderAdapter.java?rev=438841&r1=438840&r2=438841&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoderAdapter.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoderAdapter.java
Thu Aug 31 01:41:51 2006
@@ -30,8 +30,17 @@
  */
 public abstract class ProtocolDecoderAdapter implements ProtocolDecoder
 {
+    
     /**
-     * Override this method dispose all resources related with this decoder.
+     * Override this method to deal with the closed connection.
+     * The default implementation does nothing.
+     */
+    public void finishDecode( IoSession session, ProtocolDecoderOutput out ) throws Exception
+    {
+    }
+
+    /**
+     * Override this method to dispose all resources related with this decoder.
      * The default implementation does nothing.
      */
     public void dispose( IoSession session ) throws Exception

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/SynchronizedProtocolDecoder.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/SynchronizedProtocolDecoder.java?rev=438841&r1=438840&r2=438841&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/SynchronizedProtocolDecoder.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/SynchronizedProtocolDecoder.java
Thu Aug 31 01:41:51 2006
@@ -63,6 +63,14 @@
         }
     }
 
+    public void finishDecode( IoSession session, ProtocolDecoderOutput out ) throws Exception
+    {
+	synchronized( decoder )
+	{
+	    decoder.finishDecode( session, out );
+	}
+    }
+
     public void dispose( IoSession session ) throws Exception
     {
         synchronized( decoder )

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java?rev=438841&r1=438840&r2=438841&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java
(original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java
Thu Aug 31 01:41:51 2006
@@ -113,12 +113,7 @@
                         ProtocolDecoderOutput out )
             throws Exception
     {
-        Context ctx = ( Context ) session.getAttribute( CONTEXT );
-        if( ctx == null )
-        {
-            ctx = new Context();
-            session.setAttribute( CONTEXT, ctx );
-        }
+        Context ctx = getContext(session);
         
         if( LineDelimiter.AUTO.equals( delimiter ) )
         {
@@ -140,6 +135,21 @@
                             ctx.getDecoder(),
                             out ) );
         }
+    }
+
+    private Context getContext(IoSession session) {
+	Context ctx;
+	ctx = ( Context ) session.getAttribute( CONTEXT );
+        if( ctx == null )
+        {
+            ctx = new Context();
+            session.setAttribute( CONTEXT, ctx );
+        }
+	return ctx;
+    }
+
+    public void finishDecode( IoSession session, ProtocolDecoderOutput out ) throws Exception
+    {
     }
 
     public void dispose( IoSession session ) throws Exception

Modified: directory/trunks/mina/filter-codec-asn1/src/main/java/org/apache/mina/filter/codec/asn1/Asn1CodecDecoder.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/filter-codec-asn1/src/main/java/org/apache/mina/filter/codec/asn1/Asn1CodecDecoder.java?rev=438841&r1=438840&r2=438841&view=diff
==============================================================================
--- directory/trunks/mina/filter-codec-asn1/src/main/java/org/apache/mina/filter/codec/asn1/Asn1CodecDecoder.java
(original)
+++ directory/trunks/mina/filter-codec-asn1/src/main/java/org/apache/mina/filter/codec/asn1/Asn1CodecDecoder.java
Thu Aug 31 01:41:51 2006
@@ -24,7 +24,7 @@
 import org.apache.directory.shared.asn1.codec.stateful.StatefulDecoder;
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.IoSession;
-import org.apache.mina.filter.codec.ProtocolDecoder;
+import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
 
 /**
@@ -33,7 +33,7 @@
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$, 
  */
-public class Asn1CodecDecoder implements ProtocolDecoder
+public class Asn1CodecDecoder extends ProtocolDecoderAdapter
 {
 
     private final StatefulDecoder decoder;
@@ -51,10 +51,6 @@
     {
         callback.decOut = out;
         decoder.decode( in.buf() );
-    }
-
-    public void dispose( IoSession session ) throws Exception
-    {
     }
 
     private class DecoderCallbackImpl implements DecoderCallback

Modified: directory/trunks/mina/filter-codec-netty/src/main/java/org/apache/mina/filter/codec/netty/NettyDecoder.java
URL: http://svn.apache.org/viewvc/directory/trunks/mina/filter-codec-netty/src/main/java/org/apache/mina/filter/codec/netty/NettyDecoder.java?rev=438841&r1=438840&r2=438841&view=diff
==============================================================================
--- directory/trunks/mina/filter-codec-netty/src/main/java/org/apache/mina/filter/codec/netty/NettyDecoder.java
(original)
+++ directory/trunks/mina/filter-codec-netty/src/main/java/org/apache/mina/filter/codec/netty/NettyDecoder.java
Thu Aug 31 01:41:51 2006
@@ -25,16 +25,19 @@
 
 import org.apache.mina.common.ByteBuffer;
 import org.apache.mina.common.IoSession;
+import org.apache.mina.filter.codec.ProtocolDecoder;
+import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
 import org.apache.mina.filter.codec.ProtocolDecoderException;
+import org.apache.mina.filter.codec.ProtocolDecoderOutput;
 
 /**
- * A MINA <tt>ProtocolDecoder</tt> that decodes byte buffers into
+ * A MINA {@link ProtocolDecoder} that decodes byte buffers into
  * Netty2 {@link Message}s using specified {@link MessageRecognizer}s. 
  * 
  * @author The Apache Directory Project (mina-dev@directory.apache.org)
  * @version $Rev$, $Date$,
  */
-public class NettyDecoder implements org.apache.mina.filter.codec.ProtocolDecoder
+public class NettyDecoder extends ProtocolDecoderAdapter
 {
     private final MessageRecognizer recognizer;
 
@@ -71,89 +74,86 @@
         readBuf = newBuf;
     }
 
-	public void decode(IoSession session, ByteBuffer in, org.apache.mina.filter.codec.ProtocolDecoderOutput
out) throws Exception {
-	       put( in );
-
-	        Message m = readingMessage;
-	        try
-	        {
-	            for( ;; )
-	            {
-	                readBuf.flip();
-	                if( m == null )
-	                {
-	                    int limit = readBuf.limit();
-	                    boolean failed = true;
-	                    try
-	                    {
-	                        m = recognizer.recognize( readBuf );
-	                        failed = false;
-	                    }
-	                    finally
-	                    {
-	                        if( failed )
-	                        {
-	                            // clear the read buffer if failed to recognize
-	                            readBuf.clear();
-	                            break;
-	                        }
-	                        else
-	                        {
-	                            if( m == null )
-	                            {
-	                                readBuf.limit( readBuf.capacity() );
-	                                readBuf.position( limit );
-	                                break; // finish decoding
-	                            }
-	                            else
-	                            {
-	                                // reset buffer for read
-	                                readBuf.limit( limit );
-	                                readBuf.position( 0 );
-	                            }
-	                        }
-	                    }
-	                }
-
-	                if( m != null )
-	                {
-	                    try
-	                    {
-	                        if( m.read( readBuf ) )
-	                        {
-	                            out.write( m );
-	                            m = null;
-	                        } else {
-	                        	break;
-	                        }
-	                    }
-	                    finally
-	                    {
-	                        if( readBuf.hasRemaining() )
-	                        {
-	                            readBuf.compact();
-	                        }
-	                        else
-	                        {
-	                            readBuf.clear();
-				    break;
-	                        }
-	                    }
-	                }
-	            }
-	        }
-	        catch( MessageParseException e )
-	        {
-	            m = null; // discard reading message
-	            throw new ProtocolDecoderException( "Failed to decode.", e );
-	        }
-	        finally
-	        {
-	            readingMessage = m;
-	        }		
-	}
-
-	public void dispose(IoSession session) throws Exception {
+    public void decode( IoSession session, ByteBuffer in, ProtocolDecoderOutput out ) throws
Exception
+    {
+        put( in );
 
-	}
+        Message m = readingMessage;
+        try
+        {
+            for( ;; )
+            {
+                readBuf.flip();
+                if( m == null )
+                {
+                    int limit = readBuf.limit();
+                    boolean failed = true;
+                    try
+                    {
+                        m = recognizer.recognize( readBuf );
+                        failed = false;
+                    }
+                    finally
+                    {
+                        if( failed )
+                        {
+                            // clear the read buffer if failed to recognize
+                            readBuf.clear();
+                            break;
+                        }
+                        else
+                        {
+                            if( m == null )
+                            {
+                        	readBuf.limit( readBuf.capacity() );
+                        	readBuf.position( limit );
+                        	break; // finish decoding
+                            }
+                            else
+                            {
+                        	// reset buffer for read
+                        	readBuf.limit( limit );
+                        	readBuf.position( 0 );
+                            }
+                        }
+                    }
+                }
+
+                if( m != null )
+                {
+                    try
+                    {
+                	if( m.read( readBuf ) )
+                	{
+                	    out.write( m );
+                	    m = null;
+                	} else {
+                	    break;
+                	}
+                    }
+                    finally
+                    {
+                	if( readBuf.hasRemaining() )
+                	{
+                	    readBuf.compact();
+                	}
+                	else
+                	{
+                	    readBuf.clear();
+                	    break;
+                	}
+                    }
+                }
+            }
+        }
+        catch( MessageParseException e )
+        {
+            m = null; // discard reading message
+            throw new ProtocolDecoderException( "Failed to decode.", e );
+        }
+        finally
+        {
+            readingMessage = m;
+        }		
+    }
 }



Mime
View raw message