Search in sources :

Example 6 with FrameBody

use of org.apache.qpid.server.protocol.v1_0.type.FrameBody in project qpid-broker-j by apache.

the class Interaction method sendPerformativeAndChainFuture.

private void sendPerformativeAndChainFuture(final FrameBody frameBody, final UnsignedShort channel) throws Exception {
    final TransportFrame transportFrame;
    try (QpidByteBuffer payload = frameBody instanceof Transfer ? ((Transfer) frameBody).getPayload() : null) {
        final QpidByteBuffer duplicate;
        if (payload == null) {
            duplicate = null;
        } else {
            duplicate = payload.duplicate();
        }
        transportFrame = new TransportFrame(channel.shortValue(), frameBody, duplicate);
        ListenableFuture<Void> listenableFuture = sendPerformativeAndChainFuture(transportFrame);
        if (frameBody instanceof Transfer) {
            listenableFuture.addListener(() -> ((Transfer) frameBody).dispose(), MoreExecutors.directExecutor());
        }
        if (duplicate != null) {
            listenableFuture.addListener(() -> duplicate.dispose(), MoreExecutors.directExecutor());
        }
    }
}
Also used : TransportFrame(org.apache.qpid.server.protocol.v1_0.framing.TransportFrame) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 7 with FrameBody

use of org.apache.qpid.server.protocol.v1_0.type.FrameBody in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method receive.

@Override
public void receive(final List<ChannelFrameBody> channelFrameBodies) {
    if (!channelFrameBodies.isEmpty()) {
        PeekingIterator<ChannelFrameBody> itr = Iterators.peekingIterator(channelFrameBodies.iterator());
        boolean cleanExit = false;
        try {
            while (itr.hasNext()) {
                final ChannelFrameBody channelFrameBody = itr.next();
                final int frameChannel = channelFrameBody.getChannel();
                Session_1_0 session = _receivingSessions == null || frameChannel >= _receivingSessions.length ? null : _receivingSessions[frameChannel];
                if (session != null) {
                    final AccessControlContext context = session.getAccessControllerContext();
                    AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
                        ChannelFrameBody channelFrame = channelFrameBody;
                        boolean nextIsSameChannel;
                        do {
                            received(frameChannel, channelFrame.getFrameBody());
                            nextIsSameChannel = itr.hasNext() && frameChannel == itr.peek().getChannel();
                            if (nextIsSameChannel) {
                                channelFrame = itr.next();
                            }
                        } while (nextIsSameChannel);
                        return null;
                    }, context);
                } else {
                    received(frameChannel, channelFrameBody.getFrameBody());
                }
            }
            cleanExit = true;
        } finally {
            if (!cleanExit) {
                while (itr.hasNext()) {
                    final Object frameBody = itr.next().getFrameBody();
                    if (frameBody instanceof Transfer) {
                        ((Transfer) frameBody).dispose();
                    }
                }
            }
        }
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) ChannelFrameBody(org.apache.qpid.server.protocol.v1_0.type.transport.ChannelFrameBody) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer)

Example 8 with FrameBody

use of org.apache.qpid.server.protocol.v1_0.type.FrameBody in project qpid-broker-j by apache.

the class FrameHandler method parse.

@Override
public ProtocolHandler parse(QpidByteBuffer in) {
    try {
        LOGGER.debug("RECV {} bytes", in.remaining());
        Error frameParsingError = null;
        int size;
        int remaining;
        List<ChannelFrameBody> channelFrameBodies = new ArrayList<>();
        while ((remaining = in.remaining()) >= 8 && frameParsingError == null) {
            size = in.getInt();
            if (size < 8) {
                frameParsingError = createFramingError("specified frame size %d smaller than minimum frame header size %d", size, 8);
                break;
            }
            if (size > _connectionHandler.getMaxFrameSize()) {
                frameParsingError = createFramingError("specified frame size %d larger than maximum frame header size %d", size, _connectionHandler.getMaxFrameSize());
                break;
            }
            if (remaining < size) {
                in.position(in.position() - 4);
                break;
            }
            int dataOffset = (in.get() << 2) & 0x3FF;
            if (dataOffset < 8) {
                frameParsingError = createFramingError("specified frame data offset %d smaller than minimum frame header size %d", dataOffset, 8);
                break;
            }
            if (dataOffset > size) {
                frameParsingError = createFramingError("specified frame data offset %d larger than the frame size %d", dataOffset, size);
                break;
            }
            byte type = in.get();
            switch(type) {
                case 0:
                    if (_isSasl) {
                        frameParsingError = createFramingError("received an AMQP frame type when expecting an SASL frame");
                    }
                    break;
                case 1:
                    if (!_isSasl) {
                        frameParsingError = createFramingError("received a SASL frame type when expecting an AMQP frame");
                    }
                    break;
                default:
                    frameParsingError = createFramingError("unknown frame type: %d", type);
            }
            if (frameParsingError != null) {
                break;
            }
            int channel = in.getUnsignedShort();
            if (dataOffset != 8) {
                in.position(in.position() + dataOffset - 8);
            }
            try (QpidByteBuffer dup = in.slice()) {
                dup.limit(size - dataOffset);
                in.position(in.position() + size - dataOffset);
                final boolean hasFrameBody = dup.hasRemaining();
                Object frameBody;
                if (hasFrameBody) {
                    frameBody = _valueHandler.parse(dup);
                    if (dup.hasRemaining()) {
                        if (frameBody instanceof Transfer) {
                            try (QpidByteBuffer payload = dup.slice()) {
                                ((Transfer) frameBody).setPayload(payload);
                            }
                        } else {
                            frameParsingError = createFramingError("Frame length %d larger than contained frame body %s.", size, frameBody);
                            break;
                        }
                    }
                } else {
                    frameBody = null;
                    if (_isSasl) {
                        frameParsingError = createFramingError("Empty (heartbeat) frames are not permitted during SASL negotiation");
                        break;
                    }
                }
                channelFrameBodies.add(new ChannelFrameBody() {

                    @Override
                    public int getChannel() {
                        return channel;
                    }

                    @Override
                    public Object getFrameBody() {
                        return frameBody;
                    }
                });
                if (_isSasl) {
                    break;
                }
            } catch (AmqpErrorException ex) {
                frameParsingError = ex.getError();
            }
        }
        if (frameParsingError != null) {
            _connectionHandler.handleError(frameParsingError);
            _errored = true;
        } else {
            _connectionHandler.receive(channelFrameBodies);
        }
    } catch (RuntimeException e) {
        if (e instanceof ServerScopedRuntimeException) {
            throw e;
        }
        LOGGER.warn("Unexpected exception handling frame", e);
        // This exception is unexpected. The up layer should handle error condition gracefully
        _connectionHandler.handleError(this.createError(AmqpError.INTERNAL_ERROR, e.toString()));
    }
    return this;
}
Also used : ArrayList(java.util.ArrayList) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) ConnectionError(org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ChannelFrameBody(org.apache.qpid.server.protocol.v1_0.type.transport.ChannelFrameBody) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 9 with FrameBody

use of org.apache.qpid.server.protocol.v1_0.type.FrameBody in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method received.

private void received(int channel, Object val) {
    if (channel > getChannelMax()) {
        Error error = new Error(ConnectionError.FRAMING_ERROR, String.format("specified channel %d larger than maximum channel %d", channel, getChannelMax()));
        handleError(error);
        return;
    }
    FRAME_LOGGER.debug("RECV[{}|{}] : {}", _remoteAddress, channel, val);
    if (val instanceof FrameBody) {
        ((FrameBody) val).invoke(channel, this);
    } else if (val instanceof SaslFrameBody) {
        ((SaslFrameBody) val).invoke(channel, this);
    }
}
Also used : SaslFrameBody(org.apache.qpid.server.protocol.v1_0.type.SaslFrameBody) 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) ConnectionError(org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error)

Example 10 with FrameBody

use of org.apache.qpid.server.protocol.v1_0.type.FrameBody 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)

Aggregations

FrameBody (org.apache.qpid.server.protocol.v1_0.type.FrameBody)8 Attach (org.apache.qpid.server.protocol.v1_0.type.transport.Attach)5 ChannelFrameBody (org.apache.qpid.server.protocol.v1_0.type.transport.ChannelFrameBody)4 Detach (org.apache.qpid.server.protocol.v1_0.type.transport.Detach)4 Transfer (org.apache.qpid.server.protocol.v1_0.type.transport.Transfer)4 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)3 Queue (org.apache.qpid.server.model.Queue)2 SaslFrameBody (org.apache.qpid.server.protocol.v1_0.type.SaslFrameBody)2 Source (org.apache.qpid.server.protocol.v1_0.type.messaging.Source)2 AmqpError (org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError)2 ConnectionError (org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError)2 Error (org.apache.qpid.server.protocol.v1_0.type.transport.Error)2 AccessControlContext (java.security.AccessControlContext)1 ArrayList (java.util.ArrayList)1 AMQFrame (org.apache.qpid.server.protocol.v1_0.framing.AMQFrame)1 SASLFrame (org.apache.qpid.server.protocol.v1_0.framing.SASLFrame)1 TransportFrame (org.apache.qpid.server.protocol.v1_0.framing.TransportFrame)1 AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)1 Target (org.apache.qpid.server.protocol.v1_0.type.messaging.Target)1 QueueConsumer (org.apache.qpid.server.queue.QueueConsumer)1