use of edu.iu.dsc.tws.comms.dfw.OutMessage in project twister2 by DSC-SPIDAL.
the class BaseSerializer method build.
@Override
public ChannelMessage build(Object data, Object partialBuildObject) {
OutMessage sendMessage = (OutMessage) partialBuildObject;
ChannelMessage channelMessage = new ChannelMessage(sendMessage.getSource(), sendMessage.getDataType(), MessageDirection.OUT, sendMessage.getReleaseCallback());
buildHeader(sendMessage, channelMessage, 0);
// we loop until everything is serialized
while (sendBuffers.size() > 0 && sendMessage.getSendState() != OutMessage.SendState.SERIALIZED) {
// we can continue only if there is a data buffer
DataBuffer buffer = sendBuffers.poll();
if (buffer == null) {
break;
}
int lastBufferIndicatorIndex = DFWIOUtils.SHORT_HEADER_SIZE - 1;
// this is the first time we are seeing this message
if (sendMessage.getSendState() == OutMessage.SendState.INIT || sendMessage.getSendState() == OutMessage.SendState.SENT_INTERNALLY) {
// we set the state here, because we can set it to serialized below
sendMessage.setSendState(OutMessage.SendState.HEADER_BUILT);
// build the header
if (data instanceof AggregatedObjects) {
// for list message we need to put the size of the list
DFWIOUtils.buildHeader(buffer, sendMessage, ((List) data).size());
buildHeader(sendMessage, channelMessage, ((List) data).size());
} else {
if ((sendMessage.getFlags() & MessageFlags.SYNC_EMPTY) == MessageFlags.SYNC_EMPTY) {
sendMessage.setSendState(OutMessage.SendState.SERIALIZED);
// we set the number of messages to 0, only header will be sent
DFWIOUtils.buildHeader(buffer, sendMessage, 0);
buildHeader(sendMessage, channelMessage, 0);
} else {
// for single message we need to put the size as 1
DFWIOUtils.buildHeader(buffer, sendMessage, -1);
buildHeader(sendMessage, channelMessage, -1);
}
}
lastBufferIndicatorIndex = DFWIOUtils.HEADER_SIZE - 1;
} else {
buffer.getByteBuffer().putInt(sendMessage.getSource());
// indicate this is not the last buffer
buffer.getByteBuffer().put((byte) 0);
}
// okay we have a body to build and it is not done fully yet
if (sendMessage.getSendState() == OutMessage.SendState.HEADER_BUILT || sendMessage.getSendState() == OutMessage.SendState.PARTIALLY_SERIALIZED) {
sendMessage.setSendState(OutMessage.SendState.PARTIALLY_SERIALIZED);
serializeBody(data, sendMessage, buffer);
}
if (sendMessage.getSendState() == OutMessage.SendState.SERIALIZED) {
channelMessage.setComplete(true);
// indicate this as the final buffer
buffer.getByteBuffer().put(lastBufferIndicatorIndex, (byte) 1);
}
// okay we are adding this buffer
channelMessage.addBuffer(buffer);
}
// if we didn't do anything lets return null
if (channelMessage.getBuffers().size() == 0) {
return null;
}
return channelMessage;
}
use of edu.iu.dsc.tws.comms.dfw.OutMessage in project twister2 by DSC-SPIDAL.
the class BaseSerializeTest method keyedSingleValueCase.
public InMessage keyedSingleValueCase(int numBuffers, int size, MessageType type, MessageType keyType, Object data) {
BlockingQueue<DataBuffer> bufferQueue = createDataQueue(numBuffers, size);
OutMessage outMessage = new OutMessage(0, 1, -1, 10, 0, null, null, type, keyType, null, data);
KeyedDataSerializer serializer = new KeyedDataSerializer();
serializer.init(Config.newBuilder().build(), bufferQueue);
List<ChannelMessage> messages = new ArrayList<>();
while (outMessage.getSendState() != OutMessage.SendState.SERIALIZED) {
ChannelMessage ch = serializer.build(data, outMessage);
messages.add(ch);
}
KeyedDataDeSerializer deserializer = new KeyedDataDeSerializer();
deserializer.init(Config.newBuilder().build());
MessageHeader header = deserializer.buildHeader(messages.get(0).getBuffers().get(0), 1);
InMessage inMessage = new InMessage(0, type, null, header);
inMessage.setKeyType(keyType);
for (ChannelMessage channelMessage : messages) {
for (DataBuffer dataBuffer : channelMessage.getBuffers()) {
inMessage.addBufferAndCalculate(dataBuffer);
}
}
deserializer.build(inMessage, 1);
return inMessage;
}
Aggregations