Search in sources :

Example 56 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project NabAlive by jcheype.

the class HttpApiServerHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    if (e.getCause() instanceof ClosedChannelException) {
        return;
    }
    String errorid = UUID.randomUUID().toString();
    logger.error("ERROR HTTP: {}\n", errorid, e.getCause());
    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
    httpResponse.setHeader("id", errorid);
    if (logger.isDebugEnabled()) {
        ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
        PrintWriter printWriter = new PrintWriter(new ChannelBufferOutputStream(channelBuffer));
        e.getCause().printStackTrace(printWriter);
        printWriter.close();
        httpResponse.setContent(channelBuffer);
    }
    ctx.getChannel().write(httpResponse).addListener(ChannelFutureListener.CLOSE);
    Channel ch = e.getChannel();
    ch.close();
}
Also used : ChannelBufferOutputStream(org.jboss.netty.buffer.ChannelBufferOutputStream) ClosedChannelException(java.nio.channels.ClosedChannelException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) PrintWriter(java.io.PrintWriter)

Example 57 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.

the class Nfs3Utils method writeChannelCommit.

public static void writeChannelCommit(Channel channel, XDR out, int xid) {
    if (RpcProgramNfs3.LOG.isDebugEnabled()) {
        RpcProgramNfs3.LOG.debug("Commit done:" + xid);
    }
    ChannelBuffer outBuf = XDR.writeMessageTcp(out, true);
    channel.write(outBuf);
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 58 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project hadoop by apache.

the class TestFrameDecoder method testSingleFrame.

@Test
public void testSingleFrame() {
    RpcFrameDecoder decoder = new RpcFrameDecoder();
    // Test "Length field is not received yet"
    ByteBuffer buffer = ByteBuffer.allocate(1);
    ChannelBuffer buf = new ByteBufferBackedChannelBuffer(buffer);
    ChannelBuffer channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
    assertTrue(channelBuffer == null);
    // Test all bytes are not received yet
    byte[] fragment = new byte[4 + 9];
    // final fragment
    fragment[0] = (byte) (1 << 7);
    fragment[1] = 0;
    fragment[2] = 0;
    // fragment size = 10 bytes
    fragment[3] = (byte) 10;
    assertTrue(XDR.isLastFragment(fragment));
    assertTrue(XDR.fragmentSize(fragment) == 10);
    buffer = ByteBuffer.allocate(4 + 9);
    buffer.put(fragment);
    buffer.flip();
    buf = new ByteBufferBackedChannelBuffer(buffer);
    channelBuffer = (ChannelBuffer) decoder.decode(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Channel.class), buf);
    assertTrue(channelBuffer == null);
}
Also used : RpcFrameDecoder(org.apache.hadoop.oncrpc.RpcUtil.RpcFrameDecoder) Channel(org.jboss.netty.channel.Channel) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) ByteBuffer(java.nio.ByteBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) ByteBufferBackedChannelBuffer(org.jboss.netty.buffer.ByteBufferBackedChannelBuffer) Test(org.junit.Test)

Example 59 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project MSEC by Tencent.

the class RequestDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext channelHandlerContext, Channel channel, Object msg) throws Exception {
    long receiveTime = System.currentTimeMillis();
    if (!(msg instanceof ChannelBuffer)) {
        return msg;
    }
    ChannelBuffer cb = (ChannelBuffer) NettyCodecUtils.getAttachment(channelHandlerContext, Constants.ATTACHMENT_BYTEBUFFER);
    ChannelBuffer cb_ = (ChannelBuffer) msg;
    if (cb == null) {
        cb = cb_;
    } else {
        cb.writeBytes(cb_);
    }
    List<Object> messages = null;
    int lastReadIndex = cb.readerIndex();
    int deserializeMode = -1;
    while (cb.readable()) {
        byte stx = cb.readByte();
        RpcRequest rpcRequest = null;
        if (stx == (byte) '(') {
            if (cb.readableBytes() < Constants.PKG_LEAST_LENGTH - 1) {
                setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
                break;
            }
            //Test whether protocol is protobuf
            //Format:  ( + dwHeadLength + dwBodyLength + strHead + strBody + )
            int headLength = cb.readInt();
            int bodyLength = cb.readInt();
            if (cb.readableBytes() < headLength + bodyLength + 1) {
                setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
                break;
            }
            byte[] headBytes = new byte[headLength];
            byte[] bodyBytes = new byte[bodyLength];
            cb.readBytes(headBytes);
            cb.readBytes(bodyBytes);
            byte etx = cb.readByte();
            if (etx != ')') {
                log.error("Invalid package etx.");
                throw new IllegalArgumentException("Request decode error: invalid package etx " + etx);
            }
            //parse protobuf package
            rpcRequest = deserializeProtobufPackage(headBytes, bodyBytes);
            rpcRequest.setSerializeMode(RpcRequest.SerializeMode.SERIALIZE_MODE_PROTOBUF);
        } else {
            //Test whether protocol is HTTP
            cb.readerIndex(lastReadIndex);
            int totalLength = cb.readableBytes();
            byte[] totalBytes = new byte[totalLength];
            cb.readBytes(totalBytes);
            String total = new String(totalBytes);
            int pos = total.indexOf("\r\n\r\n");
            if (pos < 0) {
                setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
                break;
            }
            int contentLength = getHTTPContentLength(total.substring(0, pos + 4));
            if (totalLength < pos + 4 + contentLength) {
                setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
                break;
            }
            cb.readerIndex(pos + 4 + contentLength);
            //parse HTTP package
            rpcRequest = deserializeHTTPPackage(total.substring(0, pos + 4 + contentLength));
            rpcRequest.setSerializeMode(RpcRequest.SerializeMode.SERIALIZE_MODE_HTTP);
        }
        if (rpcRequest != null) {
            if (messages == null) {
                messages = new ArrayList<Object>();
            }
            messages.add(rpcRequest);
            lastReadIndex = cb.readerIndex();
        } else {
            setAttachment(channelHandlerContext, channel, cb, lastReadIndex);
            break;
        }
    }
    return messages;
}
Also used : RpcRequest(org.msec.rpc.RpcRequest) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DynamicChannelBuffer(org.jboss.netty.buffer.DynamicChannelBuffer)

Example 60 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project MSEC by Tencent.

the class RequestDecoder method setAttachment.

private void setAttachment(ChannelHandlerContext ctx, Channel channel, ChannelBuffer cb, int lastReadIndex) {
    cb.readerIndex(lastReadIndex);
    if (!(cb instanceof DynamicChannelBuffer) || cb.writerIndex() > 102400) {
        ChannelBuffer db = ChannelBuffers.dynamicBuffer(cb.readableBytes() * 2, channel.getConfig().getBufferFactory());
        db.writeBytes(cb);
        cb = db;
    }
    NettyCodecUtils.setAttachment(ctx, Constants.ATTACHMENT_BYTEBUFFER, cb);
}
Also used : DynamicChannelBuffer(org.jboss.netty.buffer.DynamicChannelBuffer) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DynamicChannelBuffer(org.jboss.netty.buffer.DynamicChannelBuffer)

Aggregations

ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)494 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)70 Test (org.junit.Test)67 DeviceSession (org.traccar.DeviceSession)64 Position (org.traccar.model.Position)62 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)61 Test (org.testng.annotations.Test)49 HttpChunk (org.jboss.netty.handler.codec.http.HttpChunk)48 DefaultHttpChunk (org.jboss.netty.handler.codec.http.DefaultHttpChunk)44 HttpChunkTrailer (org.jboss.netty.handler.codec.http.HttpChunkTrailer)37 DefaultHttpChunkTrailer (org.jboss.netty.handler.codec.http.DefaultHttpChunkTrailer)34 ChannelFuture (org.jboss.netty.channel.ChannelFuture)28 Checkpoint (com.linkedin.databus.core.Checkpoint)27 ByteBuffer (java.nio.ByteBuffer)27 RecoverablePduException (com.cloudhopper.smpp.type.RecoverablePduException)26 UnrecoverablePduException (com.cloudhopper.smpp.type.UnrecoverablePduException)26 DateBuilder (org.traccar.helper.DateBuilder)26 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)25 IOException (java.io.IOException)23 ArrayList (java.util.ArrayList)23