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());
}
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));
}
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();
}
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());
}
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());
}
Aggregations