Search in sources :

Example 1 with DefaultLastMemcacheContent

use of io.netty.handler.codec.memcache.DefaultLastMemcacheContent in project netty by netty.

the class AbstractBinaryMemcacheDecoder method invalidChunk.

/**
     * Helper method to create a content chunk indicating a invalid decoding result.
     *
     * @param cause the cause of the decoding failure.
     * @return a valid content chunk indicating failure.
     */
private MemcacheContent invalidChunk(Exception cause) {
    state = State.BAD_MESSAGE;
    MemcacheContent chunk = new DefaultLastMemcacheContent(Unpooled.EMPTY_BUFFER);
    chunk.setDecoderResult(DecoderResult.failure(cause));
    return chunk;
}
Also used : LastMemcacheContent(io.netty.handler.codec.memcache.LastMemcacheContent) DefaultLastMemcacheContent(io.netty.handler.codec.memcache.DefaultLastMemcacheContent) MemcacheContent(io.netty.handler.codec.memcache.MemcacheContent) DefaultMemcacheContent(io.netty.handler.codec.memcache.DefaultMemcacheContent) DefaultLastMemcacheContent(io.netty.handler.codec.memcache.DefaultLastMemcacheContent)

Example 2 with DefaultLastMemcacheContent

use of io.netty.handler.codec.memcache.DefaultLastMemcacheContent in project netty by netty.

the class BinaryMemcacheEncoderTest method shouldEncodeContent.

@Test
public void shouldEncodeContent() {
    DefaultMemcacheContent content1 = new DefaultMemcacheContent(Unpooled.copiedBuffer("Netty", CharsetUtil.UTF_8));
    DefaultLastMemcacheContent content2 = new DefaultLastMemcacheContent(Unpooled.copiedBuffer(" Rocks!", CharsetUtil.UTF_8));
    int totalBodyLength = content1.content().readableBytes() + content2.content().readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest();
    request.setTotalBodyLength(totalBodyLength);
    boolean result = channel.writeOutbound(request);
    assertThat(result, is(true));
    result = channel.writeOutbound(content1);
    assertThat(result, is(true));
    result = channel.writeOutbound(content2);
    assertThat(result, is(true));
    ByteBuf written = channel.readOutbound();
    assertThat(written.readableBytes(), is(DEFAULT_HEADER_SIZE));
    written.release();
    written = channel.readOutbound();
    assertThat(written.readableBytes(), is(content1.content().readableBytes()));
    assertThat(written.readSlice(content1.content().readableBytes()).toString(CharsetUtil.UTF_8), is("Netty"));
    written.release();
    written = channel.readOutbound();
    assertThat(written.readableBytes(), is(content2.content().readableBytes()));
    assertThat(written.readSlice(content2.content().readableBytes()).toString(CharsetUtil.UTF_8), is(" Rocks!"));
    written.release();
}
Also used : DefaultLastMemcacheContent(io.netty.handler.codec.memcache.DefaultLastMemcacheContent) ByteBuf(io.netty.buffer.ByteBuf) DefaultMemcacheContent(io.netty.handler.codec.memcache.DefaultMemcacheContent) Test(org.junit.Test)

Example 3 with DefaultLastMemcacheContent

use of io.netty.handler.codec.memcache.DefaultLastMemcacheContent in project netty by netty.

the class BinaryMemcacheObjectAggregatorTest method shouldRetainByteBufWhenAggregating.

@Test
public void shouldRetainByteBufWhenAggregating() {
    channel = new EmbeddedChannel(new BinaryMemcacheRequestEncoder(), new BinaryMemcacheRequestDecoder(), new BinaryMemcacheObjectAggregator(MAX_CONTENT_SIZE));
    ByteBuf key = Unpooled.copiedBuffer("Netty", CharsetUtil.UTF_8);
    ByteBuf extras = Unpooled.copiedBuffer("extras", CharsetUtil.UTF_8);
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);
    DefaultMemcacheContent content1 = new DefaultMemcacheContent(Unpooled.copiedBuffer("Netty", CharsetUtil.UTF_8));
    DefaultLastMemcacheContent content2 = new DefaultLastMemcacheContent(Unpooled.copiedBuffer(" Rocks!", CharsetUtil.UTF_8));
    int totalBodyLength = key.readableBytes() + extras.readableBytes() + content1.content().readableBytes() + content2.content().readableBytes();
    request.setTotalBodyLength(totalBodyLength);
    assertTrue(channel.writeOutbound(request, content1, content2));
    assertThat(channel.outboundMessages().size(), is(3));
    assertTrue(channel.writeInbound(channel.readOutbound(), channel.readOutbound(), channel.readOutbound()));
    FullBinaryMemcacheRequest read = channel.readInbound();
    assertThat(read, notNullValue());
    assertThat(read.key().toString(CharsetUtil.UTF_8), is("Netty"));
    assertThat(read.extras().toString(CharsetUtil.UTF_8), is("extras"));
    assertThat(read.content().toString(CharsetUtil.UTF_8), is("Netty Rocks!"));
    read.release();
    assertFalse(channel.finish());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultLastMemcacheContent(io.netty.handler.codec.memcache.DefaultLastMemcacheContent) ByteBuf(io.netty.buffer.ByteBuf) DefaultMemcacheContent(io.netty.handler.codec.memcache.DefaultMemcacheContent) Test(org.junit.Test)

Example 4 with DefaultLastMemcacheContent

use of io.netty.handler.codec.memcache.DefaultLastMemcacheContent in project netty by netty.

the class AbstractBinaryMemcacheDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    switch(state) {
        case READ_HEADER:
            try {
                if (in.readableBytes() < 24) {
                    return;
                }
                resetDecoder();
                currentMessage = decodeHeader(in);
                state = State.READ_EXTRAS;
            } catch (Exception e) {
                resetDecoder();
                out.add(invalidMessage(e));
                return;
            }
        case READ_EXTRAS:
            try {
                byte extrasLength = currentMessage.extrasLength();
                if (extrasLength > 0) {
                    if (in.readableBytes() < extrasLength) {
                        return;
                    }
                    currentMessage.setExtras(in.readRetainedSlice(extrasLength));
                }
                state = State.READ_KEY;
            } catch (Exception e) {
                resetDecoder();
                out.add(invalidMessage(e));
                return;
            }
        case READ_KEY:
            try {
                short keyLength = currentMessage.keyLength();
                if (keyLength > 0) {
                    if (in.readableBytes() < keyLength) {
                        return;
                    }
                    currentMessage.setKey(in.readRetainedSlice(keyLength));
                }
                out.add(currentMessage.retain());
                state = State.READ_CONTENT;
            } catch (Exception e) {
                resetDecoder();
                out.add(invalidMessage(e));
                return;
            }
        case READ_CONTENT:
            try {
                int valueLength = currentMessage.totalBodyLength() - currentMessage.keyLength() - currentMessage.extrasLength();
                int toRead = in.readableBytes();
                if (valueLength > 0) {
                    if (toRead == 0) {
                        return;
                    }
                    if (toRead > chunkSize) {
                        toRead = chunkSize;
                    }
                    int remainingLength = valueLength - alreadyReadChunkSize;
                    if (toRead > remainingLength) {
                        toRead = remainingLength;
                    }
                    ByteBuf chunkBuffer = in.readRetainedSlice(toRead);
                    MemcacheContent chunk;
                    if ((alreadyReadChunkSize += toRead) >= valueLength) {
                        chunk = new DefaultLastMemcacheContent(chunkBuffer);
                    } else {
                        chunk = new DefaultMemcacheContent(chunkBuffer);
                    }
                    out.add(chunk);
                    if (alreadyReadChunkSize < valueLength) {
                        return;
                    }
                } else {
                    out.add(LastMemcacheContent.EMPTY_LAST_CONTENT);
                }
                resetDecoder();
                state = State.READ_HEADER;
                return;
            } catch (Exception e) {
                resetDecoder();
                out.add(invalidChunk(e));
                return;
            }
        case BAD_MESSAGE:
            in.skipBytes(actualReadableBytes());
            return;
        default:
            throw new Error("Unknown state reached: " + state);
    }
}
Also used : LastMemcacheContent(io.netty.handler.codec.memcache.LastMemcacheContent) DefaultLastMemcacheContent(io.netty.handler.codec.memcache.DefaultLastMemcacheContent) MemcacheContent(io.netty.handler.codec.memcache.MemcacheContent) DefaultMemcacheContent(io.netty.handler.codec.memcache.DefaultMemcacheContent) DefaultLastMemcacheContent(io.netty.handler.codec.memcache.DefaultLastMemcacheContent) ByteBuf(io.netty.buffer.ByteBuf) DefaultMemcacheContent(io.netty.handler.codec.memcache.DefaultMemcacheContent)

Aggregations

DefaultLastMemcacheContent (io.netty.handler.codec.memcache.DefaultLastMemcacheContent)4 DefaultMemcacheContent (io.netty.handler.codec.memcache.DefaultMemcacheContent)4 ByteBuf (io.netty.buffer.ByteBuf)3 LastMemcacheContent (io.netty.handler.codec.memcache.LastMemcacheContent)2 MemcacheContent (io.netty.handler.codec.memcache.MemcacheContent)2 Test (org.junit.Test)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1