/*
    Java Libraries https://github.com/foilen/java-libraries
    Copyright (c) 2015 Foilen (http://foilen.com)

    The MIT License
    http://opensource.org/licenses/MIT
    
 */
package com.foilen.smalltools.net.commander.channel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foilen.smalltools.net.commander.command.CommandRequest;
import com.foilen.smalltools.tools.JsonTools;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;

/**
 * <pre>
 * Encodes this protocol:
 * - classNameSize:int
 * - className:String (must be a {@link CommandRequest})
 * - jsonContentSize:int
 * - jsonContent:String
 * </pre>
 * 
 * <pre>
 * Dependencies:
 * compile 'com.fasterxml.jackson.core:jackson-databind:2.4.5'
 * compile 'io.netty:netty-all:5.0.0.Alpha2'
 * </pre>
 */
public class CommanderEncoder extends MessageToByteEncoder<CommandRequest> {

    private static final Logger logger = LoggerFactory.getLogger(CommanderEncoder.class);

    @Override
    protected void encode(ChannelHandlerContext ctx, CommandRequest msg, ByteBuf out) throws Exception {

        String className = msg.commandImplementationClass();

        logger.debug("Encoding message of type {} to call the implementation {}", msg.getClass().getName(), className);

        byte[] classNameBytes = className.getBytes(CharsetUtil.UTF_8);
        String jsonContent = JsonTools.writeToString(msg);
        byte[] jsonContentBytes = jsonContent.getBytes(CharsetUtil.UTF_8);

        out.writeInt(classNameBytes.length);
        out.writeBytes(classNameBytes);

        out.writeInt(jsonContentBytes.length);
        out.writeBytes(jsonContentBytes);

    }

}

@@ -227,19 +227,17 @@
 channel;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -565,19 +565,17 @@
 setUtil;
-%0D%0A%0D
+%0A
 %0A/**%0A * 
@@ -896,17 +896,16 @@
 pre%3E%0A */
-%0D
 %0Apublic 
@@ -973,19 +973,17 @@
 quest%3E %7B
-%0D%0A%0D
+%0A
 %0A    pri
@@ -1064,19 +1064,17 @@
 .class);
-%0D%0A%0D
+%0A
 %0A    @Ov
@@ -1079,17 +1079,16 @@
 Override
-%0D
 %0A    pro
@@ -1246,19 +1246,17 @@
 Class();
-%0D%0A%0D
+%0A
 %0A       
@@ -1367,20 +1367,36 @@
 ssName);
-%0D%0A%0D%0A
+%0A%0A        try %7B%0A    
         
@@ -1457,26 +1457,28 @@
 .UTF_8);
-%0D
 %0A
+   
         
 String j
@@ -1465,24 +1465,25 @@
 %0A           
+ 
 String jsonC
@@ -1520,18 +1520,21 @@
 ng(msg);
-%0D
 %0A
+    
         
@@ -1599,28 +1599,28 @@
 .UTF_8);
-%0D%0A%0D%0A
+%0A%0A  
         
 out.writ
@@ -1603,32 +1603,34 @@
 _8);%0A%0A          
+  
 out.writeInt(cla
@@ -1645,34 +1645,36 @@
 tes.length);
-%0D
 %0A
+   
         
 out.writeByt
@@ -1653,32 +1653,33 @@
 th);%0A           
+ 
 out.writeBytes(c
@@ -1697,20 +1697,20 @@
 es);
-%0D%0A%0D%0A
+%0A%0A  
         
 out.
@@ -1697,32 +1697,34 @@
 es);%0A%0A          
+  
 out.writeInt(jso
@@ -1749,18 +1749,19 @@
 th);
-%0D
 %0A
+  
         
 out.
@@ -1748,32 +1748,34 @@
 gth);%0A          
+  
 out.writeBytes(j
@@ -1795,20 +1795,117 @@
 es);
-%0D%0A%0D
+%0A        %7D catch (Exception e) %7B%0A            logger.warn(%22Problem encoding the message%22, e);%0A        %7D%0A
 %0A    %7D
-%0D%0A%0D
+%0A
 %0A%7D
-%0D
 %0A

Add logging