<commit_before>/*
    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);

    }

}<commit_msg>Add logging<commit_after>/*
    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);

        try {
            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);
        } catch (Exception e) {
            logger.warn("Problem encoding the message", e);
        }

    }

}
{'fraction_non_alphanumeric': 0.1161480686695279, 'fraction_numerical': 0.008047210300429184, 'mean_word_length': 3.7442748091603053, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3528211', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1081, 'n_words': 210}