directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r122629 - in incubator/directory/network/trunk/mina/src: examples/org/apache/mina/examples/reverser java/org/apache/mina/protocol
Date Fri, 17 Dec 2004 08:14:11 GMT
Author: trustin
Date: Fri Dec 17 00:14:08 2004
New Revision: 122629

URL: http://svn.apache.org/viewcvs?view=rev&rev=122629
Log:
 * Separated ProtocolCodec into ProtocolEncoder and ProtocolDecoder
Added:
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineDecoder.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodecFactory.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolDecoder.java
  (contents, props changed)
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolEncoder.java
  (contents, props changed)
Removed:
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineCodec.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java
Modified:
   incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
   incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java

Modified: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java?view=diff&rev=122629&p1=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java&r1=122628&p2=incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java&r2=122629
==============================================================================
--- incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java
(original)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/ReverseProtocolProvider.java
Fri Dec 17 00:14:08 2004
@@ -18,9 +18,11 @@
  */
 package org.apache.mina.examples.reverser;
 
-import org.apache.mina.protocol.ProtocolCodec;
-import org.apache.mina.protocol.ProtocolProvider;
+import org.apache.mina.protocol.ProtocolCodecFactory;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolEncoder;
 import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolProvider;
 
 /**
  * TODO Document me.
@@ -33,9 +35,20 @@
 
     private static ProtocolHandler HANDLER = new ReverseProtocolHandler();
 
-    public ProtocolCodec newCodec()
+    public ProtocolCodecFactory getCodecFactory()
     {
-        return new TextLineCodec();
+        return new ProtocolCodecFactory()
+        {
+            public ProtocolEncoder newEncoder()
+            {
+                return new TextLineEncoder();
+            }
+            
+            public ProtocolDecoder newDecoder()
+            {
+                return new TextLineDecoder();
+            }
+        };
     }
 
     public ProtocolHandler getHandler()

Deleted: /incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineCodec.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineCodec.java?view=auto&rev=122628
==============================================================================

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineDecoder.java?view=auto&rev=122629
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineDecoder.java
Fri Dec 17 00:14:08 2004
@@ -0,0 +1,65 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.examples.reverser;
+
+import org.apache.mina.io.ReadBuffer;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$,
+ */
+public class TextLineDecoder implements ProtocolDecoder
+{
+
+    private StringBuffer decodeBuf = new StringBuffer();
+
+    public Object decode( ProtocolSession session, ReadBuffer in )
+            throws ProtocolViolationException
+    {
+        do
+        {
+            byte b = in.get();
+            switch( b )
+            {
+            case '\r':
+                break;
+            case '\n':
+                String result = decodeBuf.toString();
+                decodeBuf.delete( 0, decodeBuf.length() );
+                return result;
+            default:
+                decodeBuf.append( ( char ) b );
+            }
+
+            if( decodeBuf.length() > 256 )
+            {
+                decodeBuf.delete( 0, decodeBuf.length() );
+                throw new ProtocolViolationException( "The line is too long." );
+            }
+        }
+        while( in.hasRemaining() );
+
+        return null;
+    }
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java?view=auto&rev=122629
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/examples/org/apache/mina/examples/reverser/TextLineEncoder.java
Fri Dec 17 00:14:08 2004
@@ -0,0 +1,70 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.examples.reverser;
+
+import org.apache.mina.io.WriteBuffer;
+import org.apache.mina.protocol.ProtocolEncoder;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$,
+ */
+public class TextLineEncoder implements ProtocolEncoder
+{
+
+    private String encodeData;
+
+    private int encodePos;
+
+    public boolean encode( ProtocolSession session, Object message,
+                          WriteBuffer out ) throws ProtocolViolationException
+    {
+
+        if( message != encodeData )
+        {
+            String val = message.toString();
+            if( val.length() > 256 )
+            {
+                throw new ProtocolViolationException(
+                                                      "Cannot encode too long string." );
+            }
+            encodeData = val + "\r\n";
+            encodePos = 0;
+        }
+
+        for( ; encodePos < encodeData.length(); encodePos++ )
+        {
+            if( out.hasRemaining() )
+            {
+                out.put( ( byte ) encodeData.charAt( encodePos ) );
+            }
+            else
+            {
+                return false;
+            }
+        }
+
+        encodeData = null;
+        return true;
+    }
+}
\ No newline at end of file

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java?view=diff&rev=122629&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java&r1=122628&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java&r2=122629
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java
Fri Dec 17 00:14:08 2004
@@ -46,13 +46,17 @@
 
     private static class SessionHandlerAdapter implements IoHandler
     {
-        private final ProtocolCodec codec;
+        private final ProtocolEncoder encoder;
+        
+        private final ProtocolDecoder decoder;
 
         private final ProtocolHandler handler;
 
         public SessionHandlerAdapter( ProtocolProvider protocolProvider )
         {
-            this.codec = protocolProvider.newCodec();
+            ProtocolCodecFactory codecFactory = protocolProvider.getCodecFactory();
+            this.encoder = codecFactory.newEncoder();
+            this.decoder = codecFactory.newDecoder();
             this.handler = protocolProvider.getHandler();
         }
 
@@ -98,7 +102,7 @@
                     synchronized( in )
                     {
                         sizeBefore = in.remaining();
-                        result = codec.decode( psession, in );
+                        result = decoder.decode( psession, in );
                         sizeAfter = in.remaining();
                     }
 
@@ -154,7 +158,7 @@
                 {
                     synchronized( out )
                     {
-                        if( codec.encode( psession, writeQueue.first(), out ) )
+                        if( encoder.encode( psession, writeQueue.first(), out ) )
                         {
                             out.putMarker( writeQueue.pop() );
                             out.flush();

Deleted: /incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java?view=auto&rev=122628
==============================================================================

Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodecFactory.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodecFactory.java?view=auto&rev=122629
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodecFactory.java
Fri Dec 17 00:14:08 2004
@@ -0,0 +1,32 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.protocol;
+
+/**
+ * TODO Document me.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface ProtocolCodecFactory
+{
+    ProtocolEncoder newEncoder();
+    
+    ProtocolDecoder newDecoder();
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolDecoder.java?view=auto&rev=122629
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolDecoder.java
Fri Dec 17 00:14:08 2004
@@ -0,0 +1,33 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.protocol;
+
+import org.apache.mina.io.ReadBuffer;
+
+/**
+ * TODO Insert type comment.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface ProtocolDecoder
+{
+    Object decode( ProtocolSession session, ReadBuffer in )
+            throws ProtocolViolationException;
+}
\ No newline at end of file

Added: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolEncoder.java?view=auto&rev=122629
==============================================================================
--- (empty file)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolEncoder.java
Fri Dec 17 00:14:08 2004
@@ -0,0 +1,33 @@
+/*
+ *   @(#) $Id$
+ *
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.mina.protocol;
+
+import org.apache.mina.io.WriteBuffer;
+
+/**
+ * TODO Insert type comment.
+ * 
+ * @author Trustin Lee (trustin@apache.org)
+ * @version $Rev$, $Date$
+ */
+public interface ProtocolEncoder
+{
+    boolean encode( ProtocolSession session, Object message, WriteBuffer out )
+            throws ProtocolViolationException;
+}
\ No newline at end of file

Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java
Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java?view=diff&rev=122629&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java&r1=122628&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java&r2=122629
==============================================================================
--- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java
(original)
+++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java
Fri Dec 17 00:14:08 2004
@@ -26,7 +26,7 @@
  */
 public interface ProtocolProvider
 {
-    ProtocolCodec newCodec();
+    ProtocolCodecFactory getCodecFactory();
 
     ProtocolHandler getHandler();
 }

Mime
View raw message