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");
}
}
}
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;
}
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;
}
Aggregations