Search in sources :

Example 6 with AMQFrame

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

the class AMQDecoderTest method testDecodeWithManyBuffers.

public void testDecodeWithManyBuffers() throws AMQProtocolVersionException, AMQFrameDecodingException, IOException {
    Random random = new Random();
    final byte[] payload = new byte[2048];
    random.nextBytes(payload);
    final AMQBody body = new ContentBody(ByteBuffer.wrap(payload));
    AMQFrame frame = new AMQFrame(1, body);
    TestSender sender = new TestSender();
    frame.writePayload(sender);
    ByteBuffer allData = combine(sender.getSentBuffers());
    for (int i = 0; i < allData.remaining(); i++) {
        byte[] minibuf = new byte[1];
        minibuf[0] = allData.get(i);
        _decoder.decodeBuffer(ByteBuffer.wrap(minibuf));
    }
    List<AMQDataBlock> frames = _methodProcessor.getProcessedMethods();
    if (frames.get(0) instanceof AMQFrame) {
        assertEquals(ContentBody.TYPE, ((AMQFrame) frames.get(0)).getBodyFrame().getFrameType());
        ContentBody decodedBody = (ContentBody) ((AMQFrame) frames.get(0)).getBodyFrame();
        byte[] bodyBytes;
        try (QpidByteBuffer payloadBuffer = decodedBody.getPayload()) {
            bodyBytes = new byte[payloadBuffer.remaining()];
            payloadBuffer.get(bodyBytes);
        }
        assertTrue("Body was corrupted", Arrays.equals(payload, bodyBytes));
    } else {
        fail("decode was not a frame");
    }
}
Also used : AMQDataBlock(org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock) Random(java.util.Random) ContentBody(org.apache.qpid.server.protocol.v0_8.transport.ContentBody) AMQBody(org.apache.qpid.server.protocol.v0_8.transport.AMQBody) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) AMQFrame(org.apache.qpid.server.protocol.v0_8.transport.AMQFrame) ByteBuffer(java.nio.ByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 7 with AMQFrame

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

the class AMQDecoderTest method testSingleFrameDecode.

public void testSingleFrameDecode() throws AMQProtocolVersionException, AMQFrameDecodingException, IOException {
    ByteBuffer msg = getHeartbeatBodyBuffer();
    _decoder.decodeBuffer(msg);
    List<AMQDataBlock> frames = _methodProcessor.getProcessedMethods();
    if (frames.get(0) instanceof AMQFrame) {
        assertEquals(HeartbeatBody.FRAME.getBodyFrame().getFrameType(), ((AMQFrame) frames.get(0)).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 8 with AMQFrame

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

the class AMQDecoderTest method testContentHeaderPropertiesFrame.

public void testContentHeaderPropertiesFrame() throws AMQProtocolVersionException, AMQFrameDecodingException, IOException {
    final BasicContentHeaderProperties props = new BasicContentHeaderProperties();
    final FieldTable table = new FieldTable();
    table.setString("hello", "world");
    table.setInteger("1+1=", 2);
    props.setHeaders(table);
    final AMQBody body = new ContentHeaderBody(props);
    AMQFrame frame = new AMQFrame(1, body);
    TestSender sender = new TestSender();
    frame.writePayload(sender);
    ByteBuffer msg = combine(sender.getSentBuffers());
    _decoder.decodeBuffer(msg);
    List<AMQDataBlock> frames = _methodProcessor.getProcessedMethods();
    AMQDataBlock firstFrame = frames.get(0);
    if (firstFrame instanceof AMQFrame) {
        assertEquals(ContentHeaderBody.TYPE, ((AMQFrame) firstFrame).getBodyFrame().getFrameType());
        BasicContentHeaderProperties decodedProps = ((ContentHeaderBody) ((AMQFrame) firstFrame).getBodyFrame()).getProperties();
        final FieldTable headers = decodedProps.getHeaders();
        assertEquals("world", headers.getString("hello"));
    } else {
        fail("decode was not a frame");
    }
}
Also used : AMQDataBlock(org.apache.qpid.server.protocol.v0_8.transport.AMQDataBlock) ContentHeaderBody(org.apache.qpid.server.protocol.v0_8.transport.ContentHeaderBody) AMQBody(org.apache.qpid.server.protocol.v0_8.transport.AMQBody) BasicContentHeaderProperties(org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties) AMQFrame(org.apache.qpid.server.protocol.v0_8.transport.AMQFrame) ByteBuffer(java.nio.ByteBuffer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer)

Example 9 with AMQFrame

use of org.apache.qpid.server.protocol.v0_8.transport.AMQFrame 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 10 with AMQFrame

use of org.apache.qpid.server.protocol.v0_8.transport.AMQFrame 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)

Aggregations

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