// Copyright © 2008-2010 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://dimdwarf.sourceforge.net/LICENSE

package net.orfjackal.dimdwarf.net;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.*;

/**
 * Decodes {@link com.sun.sgs.protocol.simple.SimpleSgsProtocol} messages.
 */
public class SimpleSgsProtocolDecoder extends CumulativeProtocolDecoder {

    // TODO: ensure compatibility with com.sun.sgs.impl.sharedutil.MessageBuffer

    protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {

        // "doDecode() can't return true when buffer is not consumed."
        for (int i = 0; i < in.limit(); i++) {
            in.get();
        }

        out.write(new LoginRequest());
        return true;
    }
}

@@ -215,16 +215,70 @@
 f.net;%0A%0A
+import com.sun.sgs.protocol.simple.SimpleSgsProtocol;%0A
 import o
@@ -750,25 +750,24 @@
 ption %7B%0A
-%0A
         
 // %22doDe
@@ -762,70 +762,219 @@
     
-// %22doDecode() can't return true when buffer is not consumed.%22
+if (in.prefixedDataAvailable(2, SimpleSgsProtocol.MAX_PAYLOAD_LENGTH)) %7B%0A            int length = in.getUnsignedShort();%0A            byte op = in.get();%0A            assert op != SimpleSgsProtocol.LOGIN_REQUEST;%0A
 %0A   
@@ -978,16 +978,20 @@
         
+    
 for (int
@@ -999,25 +999,21 @@
 i = 
-0
+1
 ; i %3C 
-in.limit()
+length
 ; i+
@@ -1013,24 +1013,28 @@
 gth; i++) %7B%0A
+    
             
@@ -1055,47 +1055,99 @@
     
-%7D%0A%0A        out.write(new LoginRequest()
+    %7D%0A            LoginRequest message = new LoginRequest();%0A%0A            out.write(message
 );%0A 
@@ -1153,16 +1153,20 @@
         
+    
 return t
@@ -1170,16 +1170,69 @@
 n true;%0A
+        %7D else %7B%0A            return false;%0A        %7D%0A
     %7D%0A%7D%0A

read the message's payload length