<commit_before>package lt.msemys.esjc.tcp;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TcpChannelHandler extends SimpleChannelInboundHandler<TcpPackage> {

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

    private final TcpConnectionSupplier tcpConnectionSupplier;

    public TcpChannelHandler(TcpConnectionSupplier tcpConnectionSupplier) {
        this.tcpConnectionSupplier = tcpConnectionSupplier;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TcpPackage msg) throws Exception {
        switch (msg.command) {
            case HeartbeatRequestCommand:
                doHeartbeatResponse(ctx, msg);
                break;
            default:
                throw new IllegalArgumentException(String.format("Unsupported command received '%s'", msg.command));
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.debug("[{}] Connected to {}", ctx.channel().localAddress(), ctx.channel().remoteAddress());
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.debug("[{}] Disconnected from {}", ctx.channel().localAddress(), ctx.channel().remoteAddress());
        tcpConnectionSupplier.get(ctx.channel().eventLoop());
        super.channelInactive(ctx);
    }

    private static ChannelFuture doHeartbeatResponse(ChannelHandlerContext ctx, TcpPackage msg) {
        return ctx.writeAndFlush(new TcpPackage.Builder()
                .withCommand(TcpCommand.HeartbeatResponseCommand)
                .withCorrelationId(msg.correlationId)
                .build());
    }

}<commit_msg>fix: attempting to reconnect when event-loop is not shutting down<commit_after>package lt.msemys.esjc.tcp;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoop;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TcpChannelHandler extends SimpleChannelInboundHandler<TcpPackage> {

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

    private final TcpConnectionSupplier tcpConnectionSupplier;

    public TcpChannelHandler(TcpConnectionSupplier tcpConnectionSupplier) {
        this.tcpConnectionSupplier = tcpConnectionSupplier;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TcpPackage msg) throws Exception {
        switch (msg.command) {
            case HeartbeatRequestCommand:
                doHeartbeatResponse(ctx, msg);
                break;
            default:
                throw new IllegalArgumentException(String.format("Unsupported command received '%s'", msg.command));
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        logger.debug("[{}] Connected to {}", ctx.channel().localAddress(), ctx.channel().remoteAddress());
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        logger.debug("[{}] Disconnected from {}", ctx.channel().localAddress(), ctx.channel().remoteAddress());

        final EventLoop loop = ctx.channel().eventLoop();

        if (!loop.isShuttingDown()) {
            tcpConnectionSupplier.get(ctx.channel().eventLoop());
        }

        super.channelInactive(ctx);
    }

    private static ChannelFuture doHeartbeatResponse(ChannelHandlerContext ctx, TcpPackage msg) {
        return ctx.writeAndFlush(new TcpPackage.Builder()
                .withCommand(TcpCommand.HeartbeatResponseCommand)
                .withCorrelationId(msg.correlationId)
                .build());
    }

}
{'fraction_non_alphanumeric': 0.0940940940940941, 'fraction_numerical': 0.0015015015015015015, 'mean_word_length': 3.455964325529543, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7055857', 'n_tokens_mistral': 1046, 'n_tokens_neox': 1009, 'n_words': 209}