在ZlibEncoder之前调用LengthFieldPrepender

时间:2012-12-10 10:30:13

标签: netty

我目前正在尝试使用netty实现压缩。我正在使用PortUnification示例来测试它。但是,在ZlibEncoder之前总是调用LengthFieldPrepender。

public class PortUnificationServerHandler extends FrameDecoder
{

    private final boolean detectSsl;
    private final boolean detectGzip;
    private AppConfiguration appConfiguration;
    private final ExecutionHandler executionHandler;

public PortUnificationServerHandler(AppConfiguration pAppConfiguration, ExecutionHandler pExecutionHandler)
{
    appConfiguration = pAppConfiguration;
    this.executionHandler = pExecutionHandler;
    detectGzip = false;
    detectSsl = false;
}

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception
{

    String lRequest = buffer.toString(CharsetUtil.UTF_8);
    if (ConnectionServiceHelper.isValidJSON(lRequest))
    {
        ObjectMapper lObjectMapper = new ObjectMapper();
        StringReader lStringReader = new StringReader(lRequest);
        JsonNode lNode = lObjectMapper.readTree(lStringReader);
        if (lNode.get(Constants.REQUEST_TYPE).asText().trim().equalsIgnoreCase(Constants.LOGIN_REQUEST))
        {
            JsonNode lDataNode1 = lNode.get(Constants.REQUEST_DATA);
            LoginRequest lLogin = lObjectMapper.treeToValue(lDataNode1, LoginRequest.class);

            if (lLogin.getCompress() != null)
            {
                if (lLogin.getCompress().trim().equalsIgnoreCase(Constants.COMPRESS_FLAG_TRUE))
                {
                    enableJSON(ctx);
                    enableGzip(ctx);
                    ctx.getPipeline().remove(this);
                }
                else
                {
                    enableJSON(ctx);
                    ctx.getPipeline().remove(this);
                }
            }
            else
            {
                enableJSON(ctx);
                ctx.getPipeline().remove(this);

            }
            for (String lName : ctx.getPipeline().getNames())
            {
                System.out.println("Handler Name " + lName);
            }
        }
    }

    // Forward the current read buffer as is to the new handlers.
    return buffer.readBytes(buffer.readableBytes());
}



private void enableJSON(ChannelHandlerContext ctx)
{
    ChannelPipeline pipeline = ctx.getPipeline();

    boolean lHandlerExists = pipeline.getContext("bufferedwriter") != null;

    if (!lHandlerExists)
    {
        pipeline.addFirst("bufferedwriter", new MyBufferedWriteHandler()); // 80960
    }

    lHandlerExists = pipeline.getContext("framer") != null;
    if (!lHandlerExists)
    {
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); // 80960
    }

    lHandlerExists = pipeline.getContext("decoder") != null;
    if (!lHandlerExists)
    {
        pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
    }

    lHandlerExists = pipeline.getContext("encoder") != null;
    if (!lHandlerExists)
    {
        pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
    }

    lHandlerExists = pipeline.getContext("executor") != null;
    if (!lHandlerExists)
    {
        pipeline.addLast("executor", executionHandler);
    }

    lHandlerExists = pipeline.getContext("handler") != null;
    if (!lHandlerExists)
    {
        pipeline.addLast("handler", new ConnectionServiceUpStreamHandler(appConfiguration));
    }

    lHandlerExists = pipeline.getContext("unite") != null;
    if (!lHandlerExists)
    {
        pipeline.addLast("unite", new PortUnificationServerHandler(appConfiguration, executionHandler));
    }
}

private void enableGzip(ChannelHandlerContext ctx)
{
    ChannelPipeline pipeline = ctx.getPipeline();

    boolean lHandlerExists = pipeline.getContext("encoder") != null;
    if (lHandlerExists)
    {
        pipeline.remove("encoder");
    }


    lHandlerExists = pipeline.getContext("gzipdeflater") != null;
    if (!lHandlerExists)
    {
        pipeline.addBefore("executor", "gzipdeflater", new ZlibEncoder(ZlibWrapper.GZIP));
    }


    lHandlerExists = pipeline.getContext("lengthprepender") != null;
    if (!lHandlerExists)
    {
        pipeline.addAfter("gzipdeflater", "lengthprepender", new LengthFieldPrepender(4));
    }


  }
}

关于我哪里出错的任何建议?我在Ubuntu 12.4上使用netty 3.5.11和jdk 1.7。

由于

1 个答案:

答案 0 :(得分:0)

没有真正弄清楚为什么会这样。管理以构建变通方法。构建了一个扩展ZlibEncoder的自定义处理程序,并在调用super.encode之后立即输入长度前置代码。

很棒的框架!!

干杯。