All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.kxbmap.ipchecker.IPCheckerServerHandler Maven / Gradle / Ivy

The newest version!
package com.github.kxbmap.ipchecker;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.InetSocketAddress;


@Sharable
public class IPCheckerServerHandler extends ChannelInboundMessageHandlerAdapter {
    private final InternalLogger logger = InternalLoggerFactory.getInstance(IPCheckerServerHandler.class);

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        final InetSocketAddress remote = (InetSocketAddress) ctx.channel().remoteAddress();
        ctx.write(remote.getAddress()).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
        logger.info("Ignoring message: " + msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.warn("Unexpected exception", cause);
        ctx.close();
    }
}