You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/01/02 04:13:51 UTC
svn commit: r123875 - incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper
Author: trustin
Date: Sat Jan 1 19:13:50 2005
New Revision: 123875
URL: http://svn.apache.org/viewcvs?view=rev&rev=123875
Log:
* Added wrapper for ASN.1 Codec and Netty2.
Added:
incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/
incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecDecoder.java (contents, props changed)
incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecEncoder.java (contents, props changed)
incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyDecoder.java (contents, props changed)
incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyEncoder.java (contents, props changed)
Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecDecoder.java?view=auto&rev=123875
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecDecoder.java Sat Jan 1 19:13:50 2005
@@ -0,0 +1,49 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.protocol.codec.wapper;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolDecoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+import org.apache.snickers.codec.DecoderException;
+import org.apache.snickers.codec.stateful.DecoderCallback;
+import org.apache.snickers.codec.stateful.StatefulDecoder;
+
+/**
+ * A wrapper for {@link StatefulDecoder} from
+ * <a href="http://incubator.apache.org/directory/subprojects/asn1/codec-stateful/">Apache ASN.1 Codec</a>.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$,
+ */
+public class Asn1CodecDecoder implements ProtocolDecoder {
+
+ private final StatefulDecoder decoder;
+ private final DecoderCallbackImpl callback = new DecoderCallbackImpl();
+
+ public Asn1CodecDecoder(StatefulDecoder decoder) {
+ decoder.setCallback(callback);
+ this.decoder = decoder;
+ }
+
+ public void decode(ProtocolSession session, ByteBuffer in,
+ ProtocolDecoderOutput out) throws ProtocolViolationException {
+ callback.decOut = out;
+ try {
+ decoder.decode(in.buf());
+ } catch (DecoderException e) {
+ throw new ProtocolViolationException("Failed to decode.", e);
+ }
+ }
+
+ private class DecoderCallbackImpl implements DecoderCallback {
+ private ProtocolDecoderOutput decOut;
+
+ public void decodeOccurred(StatefulDecoder decoder, Object decoded) {
+ decOut.write(decoded);
+ }
+ }
+}
Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecEncoder.java?view=auto&rev=123875
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/Asn1CodecEncoder.java Sat Jan 1 19:13:50 2005
@@ -0,0 +1,55 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.protocol.codec.wapper;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolEncoder;
+import org.apache.mina.protocol.ProtocolEncoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+import org.apache.snickers.codec.EncoderException;
+import org.apache.snickers.codec.stateful.EncoderCallback;
+import org.apache.snickers.codec.stateful.StatefulEncoder;
+
+/**
+ * A wrapper for {@link StatefulEncoder} from
+ * <a href="http://incubator.apache.org/directory/subprojects/asn1/codec-stateful/">Apache ASN.1 Codec</a>.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$,
+ */
+public class Asn1CodecEncoder implements ProtocolEncoder {
+ private final StatefulEncoder encoder;
+ private final EncoderCallbackImpl callback = new EncoderCallbackImpl();
+
+ public Asn1CodecEncoder(StatefulEncoder encoder) {
+ encoder.setCallback(callback);
+ this.encoder = encoder;
+ }
+
+ public void encode(ProtocolSession session, Object message, ProtocolEncoderOutput out) throws ProtocolViolationException {
+ callback.encOut = out;
+ try {
+ encoder.encode(message);
+ } catch (EncoderException e) {
+ throw new ProtocolViolationException("Encoding failed.", e);
+ }
+ }
+
+ private class EncoderCallbackImpl implements EncoderCallback {
+ private ProtocolEncoderOutput encOut;
+
+ public void encodeOccurred(StatefulEncoder codec, Object encoded) {
+ if (encoded instanceof java.nio.ByteBuffer) {
+ java.nio.ByteBuffer buf = (java.nio.ByteBuffer) encoded;
+ ByteBuffer outBuf = ByteBuffer.allocate(buf.remaining());
+ outBuf.put(buf);
+ outBuf.flip();
+ encOut.write(outBuf);
+ } else {
+ throw new IllegalArgumentException("Encoded result is not a ByteBuffer: " + encoded.getClass());
+ }
+ }
+ }
+}
Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyDecoder.java?view=auto&rev=123875
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyDecoder.java Sat Jan 1 19:13:50 2005
@@ -0,0 +1,109 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.protocol.codec.wapper;
+
+import net.gleamynode.netty2.Message;
+import net.gleamynode.netty2.MessageParseException;
+import net.gleamynode.netty2.MessageRecognizer;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolDecoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+
+/**
+ * Encodes byte buffers into Trustin Lee's
+ * <a href="http://gleamynode.net/dev/projects/netty2/">Netty</code>
+ * {@link Message}s using {@link MessageRecognizer}s.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$,
+ */
+public class NettyDecoder implements ProtocolDecoder {
+ private final MessageRecognizer recognizer;
+
+ private java.nio.ByteBuffer readBuf = java.nio.ByteBuffer.allocate(1024);
+
+ private Message readingMessage;
+
+ public NettyDecoder(MessageRecognizer recognizer) {
+ if (recognizer == null)
+ throw new NullPointerException();
+
+ this.recognizer = recognizer;
+ }
+
+ public void decode(ProtocolSession session, ByteBuffer in,
+ ProtocolDecoderOutput out) throws ProtocolViolationException {
+
+ 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;
+ }
+ } finally {
+ if (readBuf.hasRemaining()) {
+ readBuf.compact();
+ } else {
+ readBuf.clear();
+ }
+ }
+ }
+ }
+ } catch (MessageParseException e) {
+ m = null; // discard reading message
+ throw new ProtocolViolationException("Failed to decode.", e);
+ }
+ finally {
+ readingMessage = m;
+ }
+ }
+
+ private void put(ByteBuffer in) {
+ // copy to read buffer
+ if (in.remaining() > readBuf.remaining())
+ expand((readBuf.position() + in.remaining()) * 3 / 2);
+ readBuf.put(in.buf());
+ }
+
+ private void expand(int newCapacity) {
+ java.nio.ByteBuffer newBuf = java.nio.ByteBuffer.allocate(newCapacity);
+ readBuf.flip();
+ newBuf.put(readBuf);
+ readBuf = newBuf;
+ }
+}
\ No newline at end of file
Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyEncoder.java?view=auto&rev=123875
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/codec/wapper/NettyEncoder.java Sat Jan 1 19:13:50 2005
@@ -0,0 +1,51 @@
+/*
+ * @(#) $Id$
+ */
+package org.apache.mina.protocol.codec.wapper;
+
+import net.gleamynode.netty2.Message;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolEncoder;
+import org.apache.mina.protocol.ProtocolEncoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+
+/**
+ * Encodes Trustin Lee's
+ * <a href="http://gleamynode.net/dev/projects/netty2/">Netty</code>
+ * {@link Message}s.
+ *
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$,
+ */
+public class NettyEncoder implements ProtocolEncoder {
+
+ public NettyEncoder() {
+ }
+
+ public void encode(ProtocolSession session, Object message,
+ ProtocolEncoderOutput out) throws ProtocolViolationException {
+ if (!(message instanceof Message)) {
+ throw new ProtocolViolationException(
+ "This encoder can decode only Netty Messages.");
+ }
+
+ for (;;) {
+ ByteBuffer buf = ByteBuffer.allocate(8192);
+ Message m = (Message) message;
+ try {
+ if (m.write(buf.buf())) {
+ break;
+ }
+ } finally {
+ buf.flip();
+ if (buf.hasRemaining()) {
+ out.write(buf);
+ } else {
+ ByteBuffer.release(buf);
+ }
+ }
+ }
+ }
+}
\ No newline at end of file