Search in sources :

Example 6 with OutMessage

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;
}
Also used : OutMessage(edu.iu.dsc.tws.comms.dfw.OutMessage) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer)

Example 7 with OutMessage

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;
}
Also used : OutMessage(edu.iu.dsc.tws.comms.dfw.OutMessage) ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Aggregations

ChannelMessage (edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)7 DataBuffer (edu.iu.dsc.tws.api.comms.packing.DataBuffer)7 OutMessage (edu.iu.dsc.tws.comms.dfw.OutMessage)7 MessageHeader (edu.iu.dsc.tws.api.comms.messaging.MessageHeader)6 InMessage (edu.iu.dsc.tws.comms.dfw.InMessage)6 ArrayList (java.util.ArrayList)5 ChannelListener (edu.iu.dsc.tws.api.comms.channel.ChannelListener)1 DataDeserializer (edu.iu.dsc.tws.comms.dfw.io.DataDeserializer)1 DataSerializer (edu.iu.dsc.tws.comms.dfw.io.DataSerializer)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1