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