Search in sources :

Example 1 with MessageType

use of com.baidu.hugegraph.computer.core.network.message.MessageType in project hugegraph-computer by hugegraph.

the class AbstractNettyHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
    Channel channel = ctx.channel();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Receive remote message from '{}', message: {}", TransportUtil.remoteAddress(channel), msg);
    }
    MessageType msgType = msg.type();
    if (msgType.category() == MessageType.Category.DATA) {
        this.processDataMessage(ctx, channel, (DataMessage) msg);
        return;
    }
    switch(msgType) {
        case START:
            this.processStartMessage(ctx, channel, (StartMessage) msg);
            break;
        case FAIL:
            this.processFailMessage(ctx, channel, (FailMessage) msg);
            break;
        case ACK:
            this.processAckMessage(ctx, channel, (AckMessage) msg);
            break;
        case FINISH:
            this.processFinishMessage(ctx, channel, (FinishMessage) msg);
            break;
        case PING:
            this.processPingMessage(ctx, channel, (PingMessage) msg);
            break;
        case PONG:
            this.processPongMessage(ctx, channel, (PongMessage) msg);
            break;
        default:
            throw new IllegalArgException("Unknown message type: %s", msgType);
    }
}
Also used : Channel(io.netty.channel.Channel) MessageType(com.baidu.hugegraph.computer.core.network.message.MessageType) IllegalArgException(com.baidu.hugegraph.computer.core.common.exception.IllegalArgException)

Example 2 with MessageType

use of com.baidu.hugegraph.computer.core.network.message.MessageType in project hugegraph-computer by hugegraph.

the class MessageDecoder method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf buf = (ByteBuf) msg;
    Message message;
    try {
        MessageType msgType = MessageType.decode(buf);
        message = this.decode(ctx, msgType, buf);
        if (message == null) {
            return;
        }
    } finally {
        buf.release();
    }
    ctx.fireChannelRead(message);
}
Also used : StartMessage(com.baidu.hugegraph.computer.core.network.message.StartMessage) PongMessage(com.baidu.hugegraph.computer.core.network.message.PongMessage) AckMessage(com.baidu.hugegraph.computer.core.network.message.AckMessage) PingMessage(com.baidu.hugegraph.computer.core.network.message.PingMessage) FinishMessage(com.baidu.hugegraph.computer.core.network.message.FinishMessage) DataMessage(com.baidu.hugegraph.computer.core.network.message.DataMessage) Message(com.baidu.hugegraph.computer.core.network.message.Message) ByteBuf(io.netty.buffer.ByteBuf) MessageType(com.baidu.hugegraph.computer.core.network.message.MessageType)

Example 3 with MessageType

use of com.baidu.hugegraph.computer.core.network.message.MessageType in project hugegraph-computer by hugegraph.

the class NettyTransportClientTest method testDataUniformity.

@Test
public void testDataUniformity() throws IOException {
    NettyTransportClient client = (NettyTransportClient) this.oneClient();
    byte[] sourceBytes1 = StringEncoding.encode("test data message");
    byte[] sourceBytes2 = StringEncoding.encode("test data edge");
    byte[] sourceBytes3 = StringEncoding.encode("test data vertex");
    Mockito.doAnswer(invocationOnMock -> {
        MessageType type = invocationOnMock.getArgument(0);
        NetworkBuffer buffer = invocationOnMock.getArgument(2);
        byte[] sourceBytes = null;
        switch(type) {
            case MSG:
                sourceBytes = sourceBytes1;
                break;
            case EDGE:
                sourceBytes = sourceBytes2;
                break;
            case VERTEX:
                sourceBytes = sourceBytes3;
                break;
            default:
        }
        byte[] bytes;
        if (buffer instanceof FileRegionBuffer) {
            String path = ((FileRegionBuffer) buffer).path();
            File file = new File(path);
            bytes = FileUtils.readFileToByteArray(file);
            FileUtils.deleteQuietly(file);
        } else {
            bytes = buffer.copyToByteArray();
        }
        Assert.assertArrayEquals(sourceBytes, bytes);
        Assert.assertNotSame(sourceBytes, bytes);
        return null;
    }).when(serverHandler).handle(Mockito.any(), Mockito.eq(1), Mockito.any());
    client.startSession();
    client.send(MessageType.MSG, 1, ByteBuffer.wrap(sourceBytes1));
    client.send(MessageType.EDGE, 1, ByteBuffer.wrap(sourceBytes2));
    client.send(MessageType.VERTEX, 1, ByteBuffer.wrap(sourceBytes3));
    client.finishSession();
}
Also used : FileRegionBuffer(com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer) NetworkBuffer(com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer) File(java.io.File) MessageType(com.baidu.hugegraph.computer.core.network.message.MessageType) Test(org.junit.Test)

Aggregations

MessageType (com.baidu.hugegraph.computer.core.network.message.MessageType)3 IllegalArgException (com.baidu.hugegraph.computer.core.common.exception.IllegalArgException)1 FileRegionBuffer (com.baidu.hugegraph.computer.core.network.buffer.FileRegionBuffer)1 NetworkBuffer (com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer)1 AckMessage (com.baidu.hugegraph.computer.core.network.message.AckMessage)1 DataMessage (com.baidu.hugegraph.computer.core.network.message.DataMessage)1 FinishMessage (com.baidu.hugegraph.computer.core.network.message.FinishMessage)1 Message (com.baidu.hugegraph.computer.core.network.message.Message)1 PingMessage (com.baidu.hugegraph.computer.core.network.message.PingMessage)1 PongMessage (com.baidu.hugegraph.computer.core.network.message.PongMessage)1 StartMessage (com.baidu.hugegraph.computer.core.network.message.StartMessage)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 File (java.io.File)1 Test (org.junit.Test)1