Search in sources :

Example 1 with FrameDecoder

use of com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder in project hugegraph-computer by hugegraph.

the class NettyProtocol method initializeClientPipeline.

/**
 * Initialize the client channel handlers.
 *
 * <pre>
 *                                         +----------------------+
 *                                         | request client       |
 *                                         +-----------+----------+
 *                                                     | (1) send message
 * +-------------------------------------------------------------------+
 * |                        CLIENT CHANNEL PIPELINE    |               |
 * |                                                  \|/              |
 * |    +---------------------+            +----------------------+    |
 * |    | ClientHandler       |            | MessageEncoder       |    |
 * |    +----------+----------+            +-----------+----------+    |
 * |              /|\                                 \|/              |
 * |               |                                   |               |
 * |    +----------+----------+                        |               |
 * |    | MessageDecoder      |                        |               |
 * |    +----------+----------+                        |               |
 * |              /|\                                  |               |
 * |               |                                   |               |
 * |   +-----------+-----------+                       |               |
 * |   | FrameDecoder          |                       |               |
 * |   +-----------+-----------+                       |               |
 * |              /|\                                  |               |
 * +---------------+-----------------------------------+---------------+
 * |               | (3) server response              \|/ (2) client request
 * +---------------+-----------------------------------+---------------+
 * |               |                                   |               |
 * |       [ Socket.read() ]                    [ Socket.write() ]     |
 * |                                                                   |
 * |  Netty Internal I/O Threads (Transport Implementation)            |
 * +-------------------------------------------------------------------+
 * </pre>
 */
protected void initializeClientPipeline(Channel channel) {
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast("encoder", MessageEncoder.INSTANCE);
    pipeline.addLast("frameDecoder", new FrameDecoder());
    pipeline.addLast("decoder", MessageDecoder.INSTANCE_MEMORY_BUFFER);
    pipeline.addLast("clientIdleStateHandler", this.newClientIdleStateHandler());
    // NOTE: The heartbeatHandler can reuse
    pipeline.addLast("heartbeatHandler", HEART_BEAT_HANDLER);
    // NOTE: It will be replaced when the client object is initialized!
    pipeline.addLast(CLIENT_HANDLER_NAME, SLOT_HANDLER);
    // Init heartbeat times
    channel.attr(HeartbeatHandler.TIMEOUT_HEARTBEAT_COUNT).set(0);
    channel.attr(HeartbeatHandler.MAX_TIMEOUT_HEARTBEAT_COUNT).set(this.conf.maxTimeoutHeartbeatCount());
}
Also used : PreciseFrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.PreciseFrameDecoder) FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 2 with FrameDecoder

use of com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder in project hugegraph-computer by hugegraph.

the class NettyProtocol method initializeServerPipeline.

/**
 * Initialize the server channel handlers.
 *
 * <pre>
 *     +----------------------+
 *     | File / Local Buffer  |
 *     +-----------+----------+
 *                /|\ (2) zero-copy
 * +---------------+---------------------------------------------------+
 * |               |        SERVER CHANNEL PIPELINE                    |
 * |               |                                                   |
 * |    +----------+----------+ (3)write ack +----------------------+  |
 * |    | ServerHandler       |------------->| MessageEncoder       |  |
 * |    +----------+----------+              +-----------+----------+  |
 * |              /|\                                 \|/              |
 * |               |                                   |               |
 * |    +----------+----------+                        |               |
 * |    | MessageDecoder      |                        |               |
 * |    +----------+----------+                        |               |
 * |              /|\                                  |               |
 * |               |                                   |               |
 * |   +-----------+-----------+                       |               |
 * |   | PreciseFrameDecoder   |                       |               |
 * |   +-----------+-----------+                       |               |
 * |              /|\                                  |               |
 * +---------------+-----------------------------------+---------------+
 * |               | (1) read request                 \|/              |
 * +---------------+-----------------------------------+---------------+
 * |               |                                   |               |
 * |       [ Socket.read() ]                    [ Socket.write() ]     |
 * |                                                                   |
 * |  Netty Internal I/O Threads (Transport Implementation)            |
 * +-------------------------------------------------------------------+
 * </pre>
 */
protected void initializeServerPipeline(Channel channel, MessageHandler handler) {
    ChannelPipeline pipeline = channel.pipeline();
    pipeline.addLast("encoder", MessageEncoder.INSTANCE);
    if (this.conf.recvBufferFileMode()) {
        pipeline.addLast("frameDecoder", new PreciseFrameDecoder());
        pipeline.addLast("decoder", MessageDecoder.INSTANCE_FILE_REGION);
    } else {
        pipeline.addLast("frameDecoder", new FrameDecoder());
        pipeline.addLast("decoder", MessageDecoder.INSTANCE_MEMORY_BUFFER);
    }
    pipeline.addLast("serverIdleStateHandler", this.newServerIdleStateHandler());
    // NOTE: The heartbeatHandler can reuse of a server
    pipeline.addLast("serverIdleHandler", SERVER_IDLE_HANDLER);
    pipeline.addLast(SERVER_HANDLER_NAME, this.newNettyServerHandler(handler));
}
Also used : PreciseFrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.PreciseFrameDecoder) PreciseFrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.PreciseFrameDecoder) FrameDecoder(com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 3 with FrameDecoder

use of com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder 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 4 with FrameDecoder

use of com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder 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 FrameDecoder

use of com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder 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

FrameDecoder (com.baidu.hugegraph.computer.core.network.netty.codec.FrameDecoder)5 NettyBuffer (com.baidu.hugegraph.computer.core.network.buffer.NettyBuffer)3 NetworkBuffer (com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer)3 ByteBuf (io.netty.buffer.ByteBuf)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 Test (org.junit.Test)3 PreciseFrameDecoder (com.baidu.hugegraph.computer.core.network.netty.codec.PreciseFrameDecoder)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2