Search in sources :

Example 6 with AMQDataBlock

use of org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock in project qpid-broker-j by apache.

the class AMQDecoderTest method testMultipleFrameDecode.

public void testMultipleFrameDecode() throws AMQProtocolVersionException, AMQFrameDecodingException, IOException {
    ByteBuffer msgA = getHeartbeatBodyBuffer();
    ByteBuffer msgB = getHeartbeatBodyBuffer();
    ByteBuffer msg = ByteBuffer.allocate(msgA.remaining() + msgB.remaining());
    msg.put(msgA);
    msg.put(msgB);
    msg.flip();
    _decoder.decodeBuffer(msg);
    List<AMQDataBlock> frames = _methodProcessor.getProcessedMethods();
    assertEquals(2, frames.size());
    for (AMQDataBlock frame : frames) {
        if (frame instanceof AMQFrame) {
            assertEquals(HeartbeatBody.FRAME.getBodyFrame().getFrameType(), ((AMQFrame) frame).getBodyFrame().getFrameType());
        } else {
            fail("decode was not a frame");
        }
    }
}
Also used : AMQDataBlock(org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock) AMQFrame(org.apache.qpid.server.protocol.v0_8.transport.AMQFrame) ByteBuffer(java.nio.ByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 7 with AMQDataBlock

use of org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock in project qpid-broker-j by apache.

the class FrameDecoder method decode.

@Override
public Collection<Response<?>> decode(final ByteBuffer inputBuffer) throws Exception {
    _clientDecoder.decodeBuffer(inputBuffer);
    List<AMQDataBlock> receivedFrames = new ArrayList<>(_methodProcessor.getProcessedMethods());
    List<Response<?>> result = new ArrayList<>();
    for (AMQDataBlock frame : receivedFrames) {
        if (frame instanceof AMQFrame) {
            AMQFrame amqFrame = (AMQFrame) frame;
            if (amqFrame.getBodyFrame() instanceof ConnectionTuneBody) {
                ConnectionTuneBody tuneBody = (ConnectionTuneBody) amqFrame.getBodyFrame();
                _clientDecoder.setMaxFrameSize((int) tuneBody.getFrameMax());
            }
            result.add(new PerformativeResponse(amqFrame.getChannel(), amqFrame.getSize(), amqFrame.getBodyFrame()));
        } else if (frame instanceof ProtocolInitiation) {
            byte[] data = new byte[(int) frame.getSize()];
            frame.writePayload(new ByteBufferSender() {

                @Override
                public boolean isDirectBufferPreferred() {
                    return false;
                }

                @Override
                public void send(final QpidByteBuffer msg) {
                    msg.copyTo(data);
                }

                @Override
                public void flush() {
                }

                @Override
                public void close() {
                }
            });
            result.add(new HeaderResponse(data));
        } else {
            throw new IllegalArgumentException(String.format("Unexpected data block received %s", frame));
        }
    }
    _methodProcessor.getProcessedMethods().removeAll(receivedFrames);
    return result;
}
Also used : ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) ArrayList(java.util.ArrayList) AMQDataBlock(org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock) Response(org.apache.qpid.tests.protocol.Response) HeaderResponse(org.apache.qpid.tests.protocol.HeaderResponse) HeaderResponse(org.apache.qpid.tests.protocol.HeaderResponse) ProtocolInitiation(org.apache.qpid.server.protocol.v0_8.transport.ProtocolInitiation) ConnectionTuneBody(org.apache.qpid.server.protocol.v0_8.transport.ConnectionTuneBody) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AMQFrame(org.apache.qpid.server.protocol.v0_8.transport.AMQFrame)

Example 8 with AMQDataBlock

use of org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock in project qpid-broker-j by apache.

the class FrameEncoder method encode.

@Override
public ByteBuffer encode(final Object msg) {
    if (msg instanceof AMQDataBlock) {
        final List<ByteBuffer> buffers = new ArrayList<>();
        ((AMQDataBlock) msg).writePayload(new ByteBufferSender() {

            @Override
            public boolean isDirectBufferPreferred() {
                return false;
            }

            @Override
            public void send(final QpidByteBuffer msg) {
                byte[] data = new byte[msg.remaining()];
                msg.get(data);
                buffers.add(ByteBuffer.wrap(data));
            }

            @Override
            public void flush() {
            }

            @Override
            public void close() {
            }
        });
        int remaining = 0;
        for (ByteBuffer byteBuffer : buffers) {
            remaining += byteBuffer.remaining();
        }
        ByteBuffer result = ByteBuffer.allocate(remaining);
        for (ByteBuffer byteBuffer : buffers) {
            result.put(byteBuffer);
        }
        result.flip();
        return result;
    }
    return null;
}
Also used : AMQDataBlock(org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock) ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) ArrayList(java.util.ArrayList) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)8 AMQDataBlock (org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock)8 ByteBuffer (java.nio.ByteBuffer)7 AMQFrame (org.apache.qpid.server.protocol.v0_8.transport.AMQFrame)7 ArrayList (java.util.ArrayList)2 AMQBody (org.apache.qpid.server.protocol.v0_8.transport.AMQBody)2 ByteBufferSender (org.apache.qpid.server.transport.ByteBufferSender)2 Random (java.util.Random)1 BasicContentHeaderProperties (org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties)1 ConnectionTuneBody (org.apache.qpid.server.protocol.v0_8.transport.ConnectionTuneBody)1 ContentBody (org.apache.qpid.server.protocol.v0_8.transport.ContentBody)1 ContentHeaderBody (org.apache.qpid.server.protocol.v0_8.transport.ContentHeaderBody)1 ProtocolInitiation (org.apache.qpid.server.protocol.v0_8.transport.ProtocolInitiation)1 HeaderResponse (org.apache.qpid.tests.protocol.HeaderResponse)1 Response (org.apache.qpid.tests.protocol.Response)1