Search in sources :

Example 1 with NettyBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.

the class ReceiverUtil method consumeBuffer.

public static void consumeBuffer(byte[] bytes, Consumer<NetworkBuffer> consumer) {
    ByteBuf buf = Unpooled.directBuffer(bytes.length);
    try {
        buf = buf.writeBytes(bytes);
        NettyBuffer buff = new NettyBuffer(buf);
        consumer.accept(buff);
    } finally {
        buf.release();
    }
}
Also used : NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with NettyBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.

the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecode.

@Test
public void testSendMsgWithFrameDecode() {
    FrameDecoder frameDecoder = new FrameDecoder();
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
    NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
    ByteBuf buf = buffer.nettyByteBuf();
    StartMessage.INSTANCE.encode(buf);
    boolean writeInbound = embeddedChannel.writeInbound(buf);
    Assert.assertTrue(writeInbound);
    Assert.assertTrue(embeddedChannel.finish());
    buffer.release();
}
Also used : FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 3 with NettyBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.

the class NettyEncodeDecodeHandlerTest method testMessageRelease.

@Test
public void testMessageRelease() {
    int requestId = 99;
    int partition = 1;
    byte[] bytes = StringEncoding.encode("mock msg");
    ByteBuf buf = Unpooled.directBuffer().writeBytes(bytes);
    try {
        NettyBuffer managedBuffer = new NettyBuffer(buf);
        DataMessage dataMessage = new DataMessage(MessageType.MSG, requestId, partition, managedBuffer);
        Assert.assertEquals("DataMessage[messageType=MSG," + "sequenceNumber=99,partition=1,hasBody=true," + "bodyLength=8]", dataMessage.toString());
        Assert.assertEquals(1, managedBuffer.referenceCount());
        dataMessage.release();
        Assert.assertEquals(0, managedBuffer.referenceCount());
    } finally {
        if (buf.refCnt() > 0) {
            buf.release();
        }
    }
}
Also used : DataMessage(com.baidu.hugegraph.computer.core.network.message.DataMessage) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 4 with NettyBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.

the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecodeMagicError.

@Test
public void testSendMsgWithFrameDecodeMagicError() {
    FrameDecoder frameDecoder = new FrameDecoder();
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
    NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
    short magicError = 10;
    ByteBuf buf = buffer.nettyByteBuf();
    StartMessage.INSTANCE.encode(buf);
    buf.setShort(0, magicError);
    embeddedChannel.writeInbound(buf);
    Assert.assertFalse(embeddedChannel.finish());
    Assert.assertNull(embeddedChannel.readInbound());
}
Also used : FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 5 with NettyBuffer

use of com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer in project hugegraph-computer by hugegraph.

the class NettyEncodeDecodeHandlerTest method testSendMsgWithFrameDecodeVersionError.

@Test
public void testSendMsgWithFrameDecodeVersionError() {
    FrameDecoder frameDecoder = new FrameDecoder();
    EmbeddedChannel embeddedChannel = new EmbeddedChannel(frameDecoder);
    NetworkBuffer buffer = new NettyBuffer(Unpooled.buffer());
    byte versionError = 10;
    ByteBuf buf = buffer.nettyByteBuf();
    StartMessage.INSTANCE.encode(buf);
    buf.setByte(2, versionError);
    embeddedChannel.writeInbound(buf);
    Assert.assertFalse(embeddedChannel.finish());
    Assert.assertNull(embeddedChannel.readInbound());
}
Also used : FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) NettyBuffer(com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

NettyBuffer (com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer)6 ByteBuf (io.netty.buffer.ByteBuf)6 NetworkBuffer (com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer)4 Test (org.junit.Test)4 FrameDecoder (com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 DataMessage (com.baidu.hugegraph.computer.core.network.message.DataMessage)1