Search in sources :

Example 1 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class MergedWarpMessageSerializerTest method test_codec.

/**
 * Test codec.
 */
@Test
public void test_codec() {
    MergedWarpMessage mergedWarpMessage = new MergedWarpMessage();
    final ArrayList<AbstractMessage> msgs = new ArrayList<>();
    final GlobalBeginRequest globalBeginRequest1 = buildGlobalBeginRequest("x1");
    final GlobalBeginRequest globalBeginRequest2 = buildGlobalBeginRequest("x2");
    msgs.add(globalBeginRequest1);
    msgs.add(globalBeginRequest2);
    mergedWarpMessage.msgs = msgs;
    byte[] body = seataSerializer.serialize(mergedWarpMessage);
    MergedWarpMessage mergedWarpMessage2 = seataSerializer.deserialize(body);
    assertThat(mergedWarpMessage2.msgs.size()).isEqualTo(mergedWarpMessage.msgs.size());
    GlobalBeginRequest globalBeginRequest21 = (GlobalBeginRequest) mergedWarpMessage2.msgs.get(0);
    assertThat(globalBeginRequest21.getTimeout()).isEqualTo(globalBeginRequest1.getTimeout());
    assertThat(globalBeginRequest21.getTransactionName()).isEqualTo(globalBeginRequest1.getTransactionName());
    GlobalBeginRequest globalBeginRequest22 = (GlobalBeginRequest) mergedWarpMessage2.msgs.get(1);
    assertThat(globalBeginRequest22.getTimeout()).isEqualTo(globalBeginRequest2.getTimeout());
    assertThat(globalBeginRequest22.getTransactionName()).isEqualTo(globalBeginRequest2.getTransactionName());
}
Also used : GlobalBeginRequest(io.seata.core.protocol.transaction.GlobalBeginRequest) AbstractMessage(io.seata.core.protocol.AbstractMessage) ArrayList(java.util.ArrayList) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) Test(org.junit.jupiter.api.Test)

Example 2 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class SeataSerializer method serialize.

@Override
public <T> byte[] serialize(T t) {
    if (t == null || !(t instanceof AbstractMessage)) {
        throw new IllegalArgumentException("AbstractMessage isn't available.");
    }
    AbstractMessage abstractMessage = (AbstractMessage) t;
    // typecode
    short typecode = abstractMessage.getTypeCode();
    // msg codec
    MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode);
    // get empty ByteBuffer
    ByteBuf out = Unpooled.buffer(1024);
    // msg encode
    messageCodec.encode(t, out);
    byte[] body = new byte[out.readableBytes()];
    out.readBytes(body);
    // typecode + body
    ByteBuffer byteBuffer = ByteBuffer.allocate(2 + body.length);
    byteBuffer.putShort(typecode);
    byteBuffer.put(body);
    byteBuffer.flip();
    byte[] content = new byte[byteBuffer.limit()];
    byteBuffer.get(content);
    return content;
}
Also used : AbstractMessage(io.seata.core.protocol.AbstractMessage) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 3 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class SeataSerializer method deserialize.

@Override
public <T> T deserialize(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
        throw new IllegalArgumentException("Nothing to decode.");
    }
    if (bytes.length < 2) {
        throw new IllegalArgumentException("The byte[] isn't available for decode.");
    }
    ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
    // typecode
    short typecode = byteBuffer.getShort();
    // msg body
    byte[] body = new byte[byteBuffer.remaining()];
    byteBuffer.get(body);
    ByteBuffer in = ByteBuffer.wrap(body);
    // new Messgae
    AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typecode);
    // get messageCodec
    MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typecode);
    // decode
    messageCodec.decode(abstractMessage, in);
    return (T) abstractMessage;
}
Also used : AbstractMessage(io.seata.core.protocol.AbstractMessage) ByteBuffer(java.nio.ByteBuffer)

Example 4 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class MergedWarpMessageCodec method doDecode.

private void doDecode(MergedWarpMessage mergedWarpMessage, ByteBuffer byteBuffer) {
    short msgNum = byteBuffer.getShort();
    List<AbstractMessage> msgs = new ArrayList<AbstractMessage>();
    for (int idx = 0; idx < msgNum; idx++) {
        short typeCode = byteBuffer.getShort();
        AbstractMessage abstractMessage = MessageCodecFactory.getMessage(typeCode);
        MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode);
        messageCodec.decode(abstractMessage, byteBuffer);
        msgs.add(abstractMessage);
    }
    mergedWarpMessage.msgs = msgs;
}
Also used : AbstractMessage(io.seata.core.protocol.AbstractMessage) ArrayList(java.util.ArrayList) MessageSeataCodec(io.seata.serializer.seata.MessageSeataCodec)

Example 5 with AbstractMessage

use of io.seata.core.protocol.AbstractMessage in project seata by seata.

the class MergedWarpMessageConvertor method convert2Proto.

@Override
public MergedWarpMessageProto convert2Proto(MergedWarpMessage mergedWarpMessage) {
    final short typeCode = mergedWarpMessage.getTypeCode();
    final AbstractMessageProto abstractMessage = AbstractMessageProto.newBuilder().setMessageType(MessageTypeProto.forNumber(typeCode)).build();
    List<Any> lists = new ArrayList<>();
    for (AbstractMessage msg : mergedWarpMessage.msgs) {
        final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchConvertor(msg.getClass().getName());
        lists.add(Any.pack((Message) pbConvertor.convert2Proto(msg)));
    }
    MergedWarpMessageProto mergedWarpMessageProto = MergedWarpMessageProto.newBuilder().setAbstractMessage(abstractMessage).addAllMsgs(lists).addAllMsgIds(mergedWarpMessage.msgIds).build();
    return mergedWarpMessageProto;
}
Also used : MergedWarpMessageProto(io.seata.serializer.protobuf.generated.MergedWarpMessageProto) AbstractMessage(io.seata.core.protocol.AbstractMessage) AbstractMessage(io.seata.core.protocol.AbstractMessage) Message(com.google.protobuf.Message) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) ArrayList(java.util.ArrayList) Any(com.google.protobuf.Any) AbstractMessageProto(io.seata.serializer.protobuf.generated.AbstractMessageProto)

Aggregations

AbstractMessage (io.seata.core.protocol.AbstractMessage)12 MergedWarpMessage (io.seata.core.protocol.MergedWarpMessage)6 AbstractResultMessage (io.seata.core.protocol.AbstractResultMessage)5 ArrayList (java.util.ArrayList)5 MergeResultMessage (io.seata.core.protocol.MergeResultMessage)4 MessageSeataCodec (io.seata.serializer.seata.MessageSeataCodec)4 Any (com.google.protobuf.Any)2 Message (com.google.protobuf.Message)2 ByteBuf (io.netty.buffer.ByteBuf)2 GlobalBeginRequest (io.seata.core.protocol.transaction.GlobalBeginRequest)2 AbstractMessageProto (io.seata.serializer.protobuf.generated.AbstractMessageProto)2 MergedWarpMessageProto (io.seata.serializer.protobuf.generated.MergedWarpMessageProto)2 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.jupiter.api.Test)2 RpcContext (io.seata.core.rpc.RpcContext)1 MergedResultMessageProto (io.seata.serializer.protobuf.generated.MergedResultMessageProto)1