Search in sources :

Example 41 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project sockjs-netty by cgbystrom.

the class WebSocketTransport method handleWebSocketFrame.

private void handleWebSocketFrame(ChannelHandlerContext ctx, Channel channel, WebSocketFrame frame) throws IOException {
    // Check for closing frame
    if (frame instanceof CloseWebSocketFrame) {
        handshaker.close(ctx.getChannel(), (CloseWebSocketFrame) frame);
        return;
    } else if (frame instanceof PingWebSocketFrame) {
        ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData()));
        return;
    } else if (frame instanceof TextWebSocketFrame) {
        // Send the uppercase string back.
        String request = ((TextWebSocketFrame) frame).getText();
        ChannelBuffer payload = frame.getBinaryData();
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Channel %s received '%s'", ctx.getChannel().getId(), request));
        }
        if (frame.getBinaryData().readableBytes() == 0) {
            return;
        }
        ChannelBufferInputStream cbis = new ChannelBufferInputStream(payload);
        String[] messages;
        if (payload.getByte(0) == '[') {
            // decode array
            messages = mapper.readValue(cbis, String[].class);
        } else if (payload.getByte(0) == '"') {
            // decode string
            messages = new String[1];
            messages[0] = mapper.readValue(cbis, String.class);
        } else {
            throw new IOException("Expected message as string or string[]");
        }
        for (String message : messages) {
            SockJsMessage jsMessage = new SockJsMessage(message);
            ctx.sendUpstream(new UpstreamMessageEvent(channel, jsMessage, channel.getRemoteAddress()));
        }
    } else if (frame instanceof PongWebSocketFrame) {
    // Ignore
    } else {
        logger.error("Unhandled frame type: " + frame.getClass().getSimpleName());
    }
}
Also used : ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) IOException(java.io.IOException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 42 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project sockjs-netty by cgbystrom.

the class EventSourceTransport method writeRequested.

@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (e.getMessage() instanceof Frame) {
        Frame frame = (Frame) e.getMessage();
        if (headerSent.compareAndSet(false, true)) {
            HttpResponse response = createResponse(CONTENT_TYPE_EVENT_STREAM);
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), response, e.getRemoteAddress()));
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(NEW_LINE), e.getRemoteAddress()));
        }
        ChannelBuffer wrappedContent = ChannelBuffers.wrappedBuffer(FRAME_BEGIN, Frame.encode(frame, false), FRAME_END);
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(wrappedContent), e.getRemoteAddress()));
        logResponseSize(e.getChannel(), wrappedContent);
    } else {
        super.writeRequested(ctx, e);
    }
}
Also used : Frame(com.cgbystrom.sockjs.Frame) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 43 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class BasePGTypeTest method assertBytesWritten.

void assertBytesWritten(Object value, byte[] expectedBytes, int expectedLength) {
    ChannelBuffer writeBuffer = ChannelBuffers.dynamicBuffer();
    int bytesWritten = pgType.writeAsBinary(writeBuffer, value);
    assertThat(bytesWritten, is(expectedLength));
    byte[] bytes = new byte[expectedLength];
    writeBuffer.getBytes(0, bytes);
    assertThat(bytes, is(expectedBytes));
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 44 with ChannelBuffer

use of org.jboss.netty.buffer.ChannelBuffer in project crate by crate.

the class PGTypesTest method writeAndReadBinary.

private Object writeAndReadBinary(Entry entry, PGType pgType) {
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
    pgType.writeAsBinary(buffer, entry.value);
    int length = buffer.readInt();
    return pgType.readBinaryValue(buffer, length);
}
Also used : ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 45 with ChannelBuffer

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

the class MessageDecoder method decode.

/*
     * Each ControlMessage is encoded as:
     *  code (<0) ... short(2)
     * Each TaskMessage is encoded as:
     *  task (>=0) ... short(2)
     *  len ... int(4)
     *  payload ... byte[]     *  
     */
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    // Make sure that we have received at least a short 
    long available = buf.readableBytes();
    if (available < 2) {
        //need more data
        return null;
    }
    List<Object> ret = new ArrayList<>();
    // Use while loop, try to decode as more messages as possible in single call
    while (available >= 2) {
        // Mark the current buffer position before reading task/len field
        // because the whole frame might not be in the buffer yet.
        // We will reset the buffer position to the marked position if
        // there's not enough bytes in the buffer.
        buf.markReaderIndex();
        // read the short field
        short code = buf.readShort();
        available -= 2;
        // case 1: Control message
        ControlMessage ctrl_msg = ControlMessage.mkMessage(code);
        if (ctrl_msg != null) {
            if (ctrl_msg == ControlMessage.EOB_MESSAGE) {
                continue;
            } else {
                return ctrl_msg;
            }
        }
        //case 2: SaslTokenMessageRequest
        if (code == SaslMessageToken.IDENTIFIER) {
            // Make sure that we have received at least an integer (length) 
            if (buf.readableBytes() < 4) {
                //need more data
                buf.resetReaderIndex();
                return null;
            }
            // Read the length field.
            int length = buf.readInt();
            if (length <= 0) {
                return new SaslMessageToken(null);
            }
            // Make sure if there's enough bytes in the buffer.
            if (buf.readableBytes() < length) {
                // The whole bytes were not received yet - return null.
                buf.resetReaderIndex();
                return null;
            }
            // There's enough bytes in the buffer. Read it.  
            ChannelBuffer payload = buf.readBytes(length);
            // Return a SaslTokenMessageRequest object
            return new SaslMessageToken(payload.array());
        }
        // Make sure that we have received at least an integer (length)
        if (available < 4) {
            // need more data
            buf.resetReaderIndex();
            break;
        }
        // Read the length field.
        int length = buf.readInt();
        available -= 4;
        if (length <= 0) {
            ret.add(new TaskMessage(code, null));
            break;
        }
        // Make sure if there's enough bytes in the buffer.
        if (available < length) {
            // The whole bytes were not received yet - return null.
            buf.resetReaderIndex();
            break;
        }
        available -= length;
        // There's enough bytes in the buffer. Read it.
        ChannelBuffer payload = buf.readBytes(length);
        // Successfully decoded a frame.
        // Return a TaskMessage object
        ret.add(new TaskMessage(code, payload.array()));
    }
    if (ret.size() == 0) {
        return null;
    } else {
        return ret;
    }
}
Also used : ArrayList(java.util.ArrayList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) TaskMessage(org.apache.storm.messaging.TaskMessage)

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