Search in sources :

Example 1 with ValueWriter

use of org.apache.qpid.server.protocol.v1_0.codec.ValueWriter in project qpid-broker-j by apache.

the class LinkStoreUtils method objectToAmqpBytes.

public static byte[] objectToAmqpBytes(final Object object) {
    ValueWriter valueWriter = DESCRIBED_TYPE_REGISTRY.getValueWriter(object);
    int encodedSize = valueWriter.getEncodedSize();
    try (QpidByteBuffer qpidByteBuffer = QpidByteBuffer.allocate(encodedSize)) {
        valueWriter.writeToBuffer(qpidByteBuffer);
        return qpidByteBuffer.array();
    }
}
Also used : ValueWriter(org.apache.qpid.server.protocol.v1_0.codec.ValueWriter) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 2 with ValueWriter

use of org.apache.qpid.server.protocol.v1_0.codec.ValueWriter in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method sendFrame.

@Override
public int sendFrame(final int channel, final FrameBody body, final QpidByteBuffer payload) {
    if (!_closedForOutput) {
        ValueWriter<FrameBody> writer = _describedTypeRegistry.getValueWriter(body);
        if (payload == null) {
            send(AMQFrame.createAMQFrame(channel, body));
            return 0;
        } else {
            int size = writer.getEncodedSize();
            int maxPayloadSize = _maxFrameSize - (size + 9);
            long payloadLength = (long) payload.remaining();
            if (payloadLength <= maxPayloadSize) {
                send(AMQFrame.createAMQFrame(channel, body, payload));
                return (int) payloadLength;
            } else {
                ((Transfer) body).setMore(Boolean.TRUE);
                writer = _describedTypeRegistry.getValueWriter(body);
                size = writer.getEncodedSize();
                maxPayloadSize = _maxFrameSize - (size + 9);
                try (QpidByteBuffer payloadDup = payload.view(0, maxPayloadSize)) {
                    payload.position(payload.position() + maxPayloadSize);
                    send(AMQFrame.createAMQFrame(channel, body, payloadDup));
                }
                return maxPayloadSize;
            }
        }
    } else {
        return -1;
    }
}
Also used : FrameBody(org.apache.qpid.server.protocol.v1_0.type.FrameBody) ChannelFrameBody(org.apache.qpid.server.protocol.v1_0.type.transport.ChannelFrameBody) SaslFrameBody(org.apache.qpid.server.protocol.v1_0.type.SaslFrameBody) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 3 with ValueWriter

use of org.apache.qpid.server.protocol.v1_0.codec.ValueWriter in project qpid-broker-j by apache.

the class AbstractLinkEndpoint method handleOversizedUnsettledMapIfNecessary.

private Attach handleOversizedUnsettledMapIfNecessary(final Attach attachToSend) {
    final AMQPDescribedTypeRegistry describedTypeRegistry = getSession().getConnection().getDescribedTypeRegistry();
    final ValueWriter<Attach> valueWriter = describedTypeRegistry.getValueWriter(attachToSend);
    if (valueWriter.getEncodedSize() + 8 > getSession().getConnection().getMaxFrameSize()) {
        _localIncompleteUnsettled = true;
        attachToSend.setIncompleteUnsettled(true);
        final int targetSize = getSession().getConnection().getMaxFrameSize();
        int lowIndex = 0;
        Map<Binary, DeliveryState> localUnsettledMap = attachToSend.getUnsettled();
        if (localUnsettledMap == null) {
            localUnsettledMap = Collections.emptyMap();
        }
        int highIndex = localUnsettledMap.size();
        int currentIndex = (highIndex - lowIndex) / 2;
        int oldIndex;
        HashMap<Binary, DeliveryState> unsettledMap = null;
        int totalSize;
        do {
            HashMap<Binary, DeliveryState> partialUnsettledMap = new HashMap<>(currentIndex);
            final Iterator<Map.Entry<Binary, DeliveryState>> iterator = localUnsettledMap.entrySet().iterator();
            for (int i = 0; i < currentIndex; ++i) {
                final Map.Entry<Binary, DeliveryState> entry = iterator.next();
                partialUnsettledMap.put(entry.getKey(), entry.getValue());
            }
            attachToSend.setUnsettled(partialUnsettledMap);
            totalSize = describedTypeRegistry.getValueWriter(attachToSend).getEncodedSize() + FRAME_HEADER_SIZE;
            if (totalSize > targetSize) {
                highIndex = currentIndex;
            } else if (totalSize < targetSize) {
                lowIndex = currentIndex;
                unsettledMap = partialUnsettledMap;
            } else {
                lowIndex = highIndex = currentIndex;
                unsettledMap = partialUnsettledMap;
            }
            oldIndex = currentIndex;
            currentIndex = lowIndex + (highIndex - lowIndex) / 2;
        } while (oldIndex != currentIndex);
        if (unsettledMap == null || unsettledMap.isEmpty()) {
            final End endWithError = new End();
            endWithError.setError(new Error(AmqpError.FRAME_SIZE_TOO_SMALL, "Cannot fit a single unsettled delivery into Attach frame."));
            getSession().end(endWithError);
        }
        attachToSend.setUnsettled(unsettledMap);
    } else {
        _localIncompleteUnsettled = false;
    }
    return attachToSend;
}
Also used : AMQPDescribedTypeRegistry(org.apache.qpid.server.protocol.v1_0.type.codec.AMQPDescribedTypeRegistry) HashMap(java.util.HashMap) Attach(org.apache.qpid.server.protocol.v1_0.type.transport.Attach) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) DeliveryState(org.apache.qpid.server.protocol.v1_0.type.DeliveryState) End(org.apache.qpid.server.protocol.v1_0.type.transport.End) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ValueWriter (org.apache.qpid.server.protocol.v1_0.codec.ValueWriter)1 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)1 DeliveryState (org.apache.qpid.server.protocol.v1_0.type.DeliveryState)1 FrameBody (org.apache.qpid.server.protocol.v1_0.type.FrameBody)1 SaslFrameBody (org.apache.qpid.server.protocol.v1_0.type.SaslFrameBody)1 AMQPDescribedTypeRegistry (org.apache.qpid.server.protocol.v1_0.type.codec.AMQPDescribedTypeRegistry)1 AmqpError (org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError)1 Attach (org.apache.qpid.server.protocol.v1_0.type.transport.Attach)1 ChannelFrameBody (org.apache.qpid.server.protocol.v1_0.type.transport.ChannelFrameBody)1 End (org.apache.qpid.server.protocol.v1_0.type.transport.End)1 Error (org.apache.qpid.server.protocol.v1_0.type.transport.Error)1 Transfer (org.apache.qpid.server.protocol.v1_0.type.transport.Transfer)1