org.royaldev.enterprise.proxy.ProxyBackendHandler.java Source code

Java tutorial

Introduction

Here is the source code for org.royaldev.enterprise.proxy.ProxyBackendHandler.java

Source

/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.royaldev.enterprise.proxy;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import org.royaldev.enterprise.packets.PacketType;

import java.io.UnsupportedEncodingException;

public class ProxyBackendHandler extends ChannelHandlerAdapter {

    private final Channel inboundChannel;

    public ProxyBackendHandler(Channel inboundChannel) {
        this.inboundChannel = inboundChannel;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.read();
        ctx.write(Unpooled.EMPTY_BUFFER);
    }

    private byte[] getStringBytes(String s) {
        try {
            return s.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            return null;
        }
    }

    private ByteBuf createMessage(String sender, String message) {
        final ByteBuf motd = Unpooled.directBuffer();
        motd.writeByte(PacketType.Server.CHAT);
        motd.writeBytes(new byte[] { 4, 48, 1, 0, 0, 0, 0, 1 }); // 48 is length
        motd.writeByte((byte) sender.length());
        motd.writeBytes(this.getStringBytes(sender));
        motd.writeByte((byte) message.length());
        motd.writeBytes(this.getStringBytes(message));
        return motd;
    }

    private void showHierarchy(Class<?> c) {
        if (c.getSuperclass() == null) {
            System.out.println(c.getName());
            return;
        }
        showHierarchy(c.getSuperclass());
        System.out.println(c.getName());
    }

    @Override
    public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
        this.showHierarchy(msg.getClass());
        if (msg instanceof ByteBuf) {
            final ByteBuf bf = (ByteBuf) msg;
            /*final GenericPacket gp = new GenericPacket(bf);
            switch (gp.getID()) {
            case PacketType.Server.CHAT:
                final ServerPacket04Chat spc = new ServerPacket04Chat(gp);
                break;
            }*/
            bf.resetReaderIndex();
        }
        inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess())
                    ctx.channel().read();
                else
                    future.channel().close();
            }
        });
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        ProxyFrontendHandler.closeOnFlush(inboundChannel);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ProxyFrontendHandler.closeOnFlush(ctx.channel());
    }
}