You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by jv...@apache.org on 2013/01/13 10:28:05 UTC

[2/2] git commit: Revert "new codec API"

Updated Branches:
  refs/heads/craps [created] 85a69bea7
  refs/heads/fixcodec [created] 4d581c9c5


Revert "new codec API"

This reverts commit abe18bdf289b7c9bb7ccf4b0a2f1be0a0ec6a7a4.


Project: http://git-wip-us.apache.org/repos/asf/mina/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/4d581c9c
Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/4d581c9c
Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/4d581c9c

Branch: refs/heads/fixcodec
Commit: 4d581c9c5b6d4545f5abc91c49443e0b1f3de909
Parents: a1484db
Author: Julien Vermillard <jv...@apache.org>
Authored: Sun Jan 13 10:23:58 2013 +0100
Committer: Julien Vermillard <jv...@apache.org>
Committed: Sun Jan 13 10:23:58 2013 +0100

----------------------------------------------------------------------
 .../mina/filter/codec/ProtocolCodecFactory.java    |    6 +-
 .../mina/filter/codec/ProtocolCodecFilter.java     |   93 +++++++++------
 .../apache/mina/filter/codec/ProtocolDecoder.java  |   54 +++++++--
 .../apache/mina/filter/codec/ProtocolEncoder.java  |   36 +++++-
 4 files changed, 130 insertions(+), 59 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina/blob/4d581c9c/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java b/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
index 7406b41..f408e4c 100644
--- a/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
+++ b/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFactory.java
@@ -31,17 +31,17 @@ import org.apache.mina.api.IoSession;
  *
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
-public interface ProtocolCodecFactory<MESSAGE,ENCODED> {
+public interface ProtocolCodecFactory {
 
     /**
      * Returns a new (or reusable) instance of {@link ProtocolEncoder} which
      * encodes message objects into binary or protocol-specific data.
      */
-    ProtocolEncoder<MESSAGE,ENCODED> getEncoder(IoSession session);
+    ProtocolEncoder getEncoder(IoSession session);
 
     /**
      * Returns a new (or reusable) instance of {@link ProtocolDecoder} which
      * decodes binary or protocol-specific data into message objects.
      */
-    ProtocolDecoder<ENCODED,MESSAGE> getDecoder(IoSession session);
+    ProtocolDecoder getDecoder(IoSession session);
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina/blob/4d581c9c/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java b/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
index 2938cf3..baa4227 100644
--- a/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
+++ b/core/src/main/java/org/apache/mina/filter/codec/ProtocolCodecFilter.java
@@ -19,6 +19,8 @@
  */
 package org.apache.mina.filter.codec;
 
+import java.nio.ByteBuffer;
+
 import org.apache.mina.api.AbstractIoFilter;
 import org.apache.mina.api.IoFilter;
 import org.apache.mina.api.IoSession;
@@ -36,24 +38,22 @@ import org.slf4j.LoggerFactory;
  * 
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  */
-public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
+public class ProtocolCodecFilter extends AbstractIoFilter {
     /** A logger for this class */
     private static final Logger LOGGER = LoggerFactory.getLogger(ProtocolCodecFilter.class);
 
     private static final Class<?>[] EMPTY_PARAMS = new Class[0];
 
     /** key for session attribute holding the encoder */
-    @SuppressWarnings("rawtypes")
-	private final AttributeKey<ProtocolEncoder> ENCODER = new AttributeKey<ProtocolEncoder>(ProtocolEncoder.class,
+    private final AttributeKey<ProtocolEncoder> ENCODER = new AttributeKey<ProtocolEncoder>(ProtocolEncoder.class,
             "internal_encoder");
 
     /** key for session attribute holding the decoder */
-    @SuppressWarnings("rawtypes")
-	private final AttributeKey<ProtocolDecoder> DECODER = new AttributeKey<ProtocolDecoder>(ProtocolDecoder.class,
+    private final AttributeKey<ProtocolDecoder> DECODER = new AttributeKey<ProtocolDecoder>(ProtocolDecoder.class,
             "internal_decoder");
 
     /** The factory responsible for creating the encoder and decoder */
-    private final ProtocolCodecFactory<MESSAGE,ENCODED> factory;
+    private final ProtocolCodecFactory factory;
 
     /**
      * 
@@ -61,7 +61,7 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * 
      * @param factory The associated factory
      */
-    public ProtocolCodecFilter(final ProtocolCodecFactory<MESSAGE,ENCODED> factory) {
+    public ProtocolCodecFilter(final ProtocolCodecFactory factory) {
         if (factory == null) {
             throw new IllegalArgumentException("factory");
         }
@@ -76,7 +76,7 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * @param encoder The class responsible for encoding the message
      * @param decoder The class responsible for decoding the message
      */
-    public ProtocolCodecFilter(final ProtocolEncoder<MESSAGE,ENCODED> encoder, final ProtocolDecoder<ENCODED,MESSAGE> decoder) {
+    public ProtocolCodecFilter(final ProtocolEncoder encoder, final ProtocolDecoder decoder) {
         if (encoder == null) {
             throw new IllegalArgumentException("encoder");
         }
@@ -86,14 +86,14 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
         }
 
         // Create the inner Factory based on the two parameters
-        this.factory = new ProtocolCodecFactory<MESSAGE,ENCODED>() {
+        this.factory = new ProtocolCodecFactory() {
             @Override
-            public ProtocolEncoder<MESSAGE,ENCODED> getEncoder(final IoSession session) {
+            public ProtocolEncoder getEncoder(final IoSession session) {
                 return encoder;
             }
 
             @Override
-            public ProtocolDecoder<ENCODED,MESSAGE> getDecoder(final IoSession session) {
+            public ProtocolDecoder getDecoder(final IoSession session) {
                 return decoder;
             }
         };
@@ -107,8 +107,8 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * @param encoderClass The class responsible for encoding the message
      * @param decoderClass The class responsible for decoding the message
      */
-    public ProtocolCodecFilter(final Class<? extends ProtocolEncoder<MESSAGE,ENCODED>> encoderClass,
-            final Class<? extends ProtocolDecoder<ENCODED,MESSAGE>> decoderClass) {
+    public ProtocolCodecFilter(final Class<? extends ProtocolEncoder> encoderClass,
+            final Class<? extends ProtocolDecoder> decoderClass) {
         Assert.assertNotNull(encoderClass, "Encoder Class");
         Assert.assertNotNull(decoderClass, "Decoder Class");
 
@@ -124,7 +124,7 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
             throw new IllegalArgumentException("decoderClass doesn't have a public default constructor.");
         }
 
-        final ProtocolEncoder<MESSAGE,ENCODED> encoder;
+        final ProtocolEncoder encoder;
 
         try {
             encoder = encoderClass.newInstance();
@@ -132,7 +132,7 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
             throw new IllegalArgumentException("encoderClass cannot be initialized");
         }
 
-        final ProtocolDecoder<ENCODED,MESSAGE> decoder;
+        final ProtocolDecoder decoder;
 
         try {
             decoder = decoderClass.newInstance();
@@ -141,14 +141,14 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
         }
 
         // Create the inner factory based on the two parameters.
-        this.factory = new ProtocolCodecFactory<MESSAGE,ENCODED>() {
+        this.factory = new ProtocolCodecFactory() {
             @Override
-            public ProtocolEncoder<MESSAGE,ENCODED> getEncoder(final IoSession session) {
+            public ProtocolEncoder getEncoder(final IoSession session) {
                 return encoder;
             }
 
             @Override
-            public ProtocolDecoder<ENCODED,MESSAGE> getDecoder(final IoSession session) {
+            public ProtocolDecoder getDecoder(final IoSession session) {
                 return decoder;
             }
         };
@@ -160,7 +160,7 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * @param session The associated session we will get the encoder from
      * @return The encoder instance, if any
      */
-    public ProtocolEncoder<MESSAGE,ENCODED> getEncoder(final IoSession session) {
+    public ProtocolEncoder getEncoder(final IoSession session) {
         return factory.getEncoder(session);
     }
 
@@ -170,7 +170,7 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * @param session The associated session we will get the decoder from
      * @return The decoder instance, if any
      */
-    public ProtocolDecoder<ENCODED,MESSAGE> getDecoder(final IoSession session) {
+    public ProtocolDecoder getDecoder(final IoSession session) {
         return factory.getDecoder(session);
     }
 
@@ -185,16 +185,25 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * </code>
      */
     @Override
-    public void messageReceived(final IoSession session, final Object in,
+    public void messageReceived(final IoSession session, final Object message,
             final ReadFilterChainController controller) {
         LOGGER.debug("Processing a MESSAGE_RECEIVED for session {}", session);
 
-        final ProtocolDecoder<ENCODED,MESSAGE> decoder = getDecoder(session);
+        if (!(message instanceof ByteBuffer)) {
+            controller.callReadNextFilter(message);
+            return;
+        }
+
+        final ByteBuffer in = (ByteBuffer) message;
+        final ProtocolDecoder decoder = getDecoder(session);
 
-        // Loop until the codec cannot decode more
-        MESSAGE msg;
-        while ( (msg = decoder.decode(session, (ENCODED)in)) != null) {
-            controller.callReadNextFilter(msg);
+        // Loop until we don't have anymore byte in the buffer,
+        // or until the decoder throws an unrecoverable exception or
+        // can't decoder a message, because there are not enough
+        // data in the buffer
+        while (in.hasRemaining()) {
+            // Call the decoder with the read bytes
+            decoder.decode(session, in, controller);
         }
     }
 
@@ -205,11 +214,9 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
     public void messageWriting(IoSession session, WriteRequest message, WriteFilterChainController controller) {
         LOGGER.debug("Processing a MESSAGE_WRITTING for session {}", session);
 
-        final ProtocolEncoder<MESSAGE,ENCODED> encoder = session.getAttribute(ENCODER, null);
-        ENCODED encoded = encoder.encode(session,(MESSAGE) message.getMessage());
-        message.setMessage(encoded);
-        
-        controller.callWriteNextFilter(message);
+        final ProtocolEncoder encoder = session.getAttribute(ENCODER, null);
+
+        encoder.encode(session, message, controller);
     }
 
     /**
@@ -219,9 +226,9 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
     public void sessionOpened(final IoSession session) {
         // Initialize the encoder and decoder if we use a factory
         if (factory != null) {
-            final ProtocolEncoder<MESSAGE,ENCODED> encoder = factory.getEncoder(session);
+            final ProtocolEncoder encoder = factory.getEncoder(session);
             session.setAttribute(ENCODER, encoder);
-            final ProtocolDecoder<ENCODED,MESSAGE> decoder = factory.getDecoder(session);
+            final ProtocolDecoder decoder = factory.getDecoder(session);
             session.setAttribute(DECODER, decoder);
         }
     }
@@ -250,7 +257,17 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * method.
      */
     private void disposeEncoder(final IoSession session) {
-        session.removeAttribute(ENCODER);
+        final ProtocolEncoder encoder = session.removeAttribute(ENCODER);
+
+        if (encoder == null) {
+            return;
+        }
+
+        try {
+            encoder.dispose(session);
+        } catch (final Throwable t) {
+            LOGGER.warn("Failed to dispose: " + encoder.getClass().getName() + " (" + encoder + ')');
+        }
     }
 
     /**
@@ -258,9 +275,13 @@ public class ProtocolCodecFilter<MESSAGE,ENCODED> extends AbstractIoFilter {
      * method.
      */
     private void disposeDecoder(final IoSession session) {
-        final ProtocolDecoder<ENCODED,MESSAGE> decoder = session.removeAttribute(DECODER);
+        final ProtocolDecoder decoder = session.removeAttribute(DECODER);
+        if (decoder == null) {
+            return;
+        }
+
         try {
-            decoder.finishDecode(session);
+            decoder.dispose(session);
         } catch (final Throwable t) {
             LOGGER.warn("Failed to dispose: " + decoder.getClass().getName() + " (" + decoder + ')');
         }

http://git-wip-us.apache.org/repos/asf/mina/blob/4d581c9c/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java b/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
index 435f9d1..f77e4db 100644
--- a/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
+++ b/core/src/main/java/org/apache/mina/filter/codec/ProtocolDecoder.java
@@ -19,26 +19,54 @@
  */
 package org.apache.mina.filter.codec;
 
+import java.nio.ByteBuffer;
+
 import org.apache.mina.api.IoSession;
+import org.apache.mina.filterchain.ReadFilterChainController;
+import org.apache.mina.util.IoBuffer;
 
 /**
  * Decodes binary or protocol-specific data into higher-level message objects.
+ * MINA invokes {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
+ * method with read data, and then the decoder implementation puts decoded
+ * messages into {@link ProtocolDecoderOutput} by calling
+ * {@link ProtocolDecoderOutput#write(Object)}.
+ * <p>
+ * Please refer to
+ * <a href="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineDecoder.html"><code>TextLineDecoder</code></a>
+ * example.
  *
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  * 
+ * @see ProtocolDecoderException
  */
-public interface ProtocolDecoder<INPUT,OUTPUT> {
+public interface ProtocolDecoder {
+
+    /**
+     * Decodes binary or protocol-specific content into higher-level message objects.
+     * MINA invokes {@link #decode(IoSession, IoBuffer, ProtocolDecoderOutput)}
+     * method with read data, and then the decoder implementation puts decoded
+     * messages into {@link ProtocolDecoderOutput}.
+     *
+     * @throws Exception if the read data violated protocol specification
+     */
+    Object decode(IoSession session, ByteBuffer in, ReadFilterChainController controller);// 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
+     */
+    Object finishDecode(IoSession session) throws Exception;
 
-	/**
-	 * Decode binary or protocol-specific content of type <code>INPUT</code> into higher-level protocol message objects, of type OUTPUT
-	 * @param session the session for this message 
-	 * @param input the received message to decode 
-	 * @return the decoded message or <code>null</code> if nothing was decoded
-	 */
-	OUTPUT decode(IoSession session, INPUT input);
-	
-	/**
-	 * Finish decoding, for example if the decoder accumulated some unused input, it should discard it, or throw an Exception
-	 */
-    void finishDecode(IoSession session);
+    /**
+     * Releases all resources related with this decoder.
+     *
+     * @throws Exception if failed to dispose all resources
+     */
+    void dispose(IoSession session) throws Exception;
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/mina/blob/4d581c9c/core/src/main/java/org/apache/mina/filter/codec/ProtocolEncoder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mina/filter/codec/ProtocolEncoder.java b/core/src/main/java/org/apache/mina/filter/codec/ProtocolEncoder.java
index 6bbc55e..71a3834 100644
--- a/core/src/main/java/org/apache/mina/filter/codec/ProtocolEncoder.java
+++ b/core/src/main/java/org/apache/mina/filter/codec/ProtocolEncoder.java
@@ -19,21 +19,43 @@
  */
 package org.apache.mina.filter.codec;
 
-import java.nio.ByteBuffer;
-
 import org.apache.mina.api.IoSession;
+import org.apache.mina.filterchain.WriteFilterChainController;
+import org.apache.mina.session.WriteRequest;
+import org.apache.mina.util.IoBuffer;
 
 /**
- * In charge of encoding a message of type MESSAGE into another form (could be a {@link ByteBuffer} or any other protocol level construction.
- * 
+ * Encodes higher-level message objects into binary or protocol-specific data.
+ * MINA invokes {@link #encode(IoSession, Object, ProtocolEncoderOutput)}
+ * method with message which is popped from the session write queue, and then
+ * the encoder implementation puts encoded messages (typically {@link IoBuffer}s)
+ * into {@link ProtocolEncoderOutput} by calling {@link ProtocolEncoderOutput#write(Object)}.
+ * <p>
+ * Please refer to
+ * <a href="../../../../../xref-examples/org/apache/mina/examples/reverser/TextLineEncoder.html"><code>TextLineEncoder</code></a>
+ * example.
+ *
  * @author <a href="http://mina.apache.org">Apache MINA Project</a>
  * 
+ * @see ProtocolEncoderException
  */
-public interface ProtocolEncoder<INPUT /* message type */, OUTPUT> {
+public interface ProtocolEncoder {
 
     /**
-     * Encodes higher-level message objects of type <code>INPUT</code> into binary or protocol-specific data of type <code>OUTPUT</code>.
+     * Encodes higher-level message objects into binary or protocol-specific data.
+     * MINA invokes {@link #encode(IoSession, Object, ProtocolEncoderOutput)}
+     * method with message which is popped from the session write queue, and then
+     * the encoder implementation puts encoded messages (typically {@link ByteBuffer}s)
+     * into {@link ProtocolEncoderOutput}.
+     *
+     * @throws Exception if the message violated protocol specification
      */
-	OUTPUT encode(IoSession session, INPUT message);
+    Object encode(IoSession session, WriteRequest message, WriteFilterChainController controller); // throws Exception;
 
+    /**
+     * Releases all resources related with this encoder.
+     *
+     * @throws Exception if failed to dispose all resources
+     */
+    void dispose(IoSession session) throws Exception;
 }
\ No newline at end of file