Search in sources :

Example 1 with OtfHeaderDecoder

use of uk.co.real_logic.sbe.otf.OtfHeaderDecoder in project simple-binary-encoding by real-logic.

the class CompositeElementsGenerationTest method shouldOtfDecodeCorrectly.

@Test
public void shouldOtfDecodeCorrectly() throws Exception {
    final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocate(SCHEMA_BUFFER_CAPACITY);
    encodeSchema(encodedSchemaBuffer);
    final ByteBuffer encodedMsgBuffer = ByteBuffer.allocate(MSG_BUFFER_CAPACITY);
    encodeTestMessage(encodedMsgBuffer);
    encodedSchemaBuffer.flip();
    final Ir ir = decodeIr(encodedSchemaBuffer);
    final DirectBuffer decodeBuffer = new UnsafeBuffer(encodedMsgBuffer);
    final OtfHeaderDecoder otfHeaderDecoder = new OtfHeaderDecoder(ir.headerStructure());
    assertThat(otfHeaderDecoder.getBlockLength(decodeBuffer, 0), is(22));
    assertThat(otfHeaderDecoder.getSchemaId(decodeBuffer, 0), is(3));
    assertThat(otfHeaderDecoder.getTemplateId(decodeBuffer, 0), is(1));
    assertThat(otfHeaderDecoder.getSchemaVersion(decodeBuffer, 0), is(0));
    final TokenListener mockTokenListener = mock(TokenListener.class);
    OtfMessageDecoder.decode(decodeBuffer, otfHeaderDecoder.encodedLength(), MSG_ENCODER.sbeSchemaVersion(), MSG_ENCODER.sbeBlockLength(), ir.getMessage(MSG_ENCODER.sbeTemplateId()), mockTokenListener);
    final InOrder inOrder = inOrder(mockTokenListener);
    inOrder.verify(mockTokenListener).onBeginComposite(any(), any(), eq(2), eq(17));
    inOrder.verify(mockTokenListener).onEnum(any(), eq(decodeBuffer), eq(8), any(), eq(3), eq(6), eq(0));
    inOrder.verify(mockTokenListener).onEncoding(any(), eq(decodeBuffer), eq(9), any(), eq(0));
    inOrder.verify(mockTokenListener).onBitSet(any(), eq(decodeBuffer), eq(10), any(), eq(8), eq(12), eq(0));
    inOrder.verify(mockTokenListener).onBeginComposite(any(), any(), eq(13), eq(16));
    inOrder.verify(mockTokenListener).onEncoding(any(), eq(decodeBuffer), eq(14), any(), eq(0));
    inOrder.verify(mockTokenListener).onEncoding(any(), eq(decodeBuffer), eq(22), any(), eq(0));
    inOrder.verify(mockTokenListener).onEndComposite(any(), any(), eq(13), eq(16));
    inOrder.verify(mockTokenListener).onEndComposite(any(), any(), eq(2), eq(17));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) InOrder(org.mockito.InOrder) OtfHeaderDecoder(uk.co.real_logic.sbe.otf.OtfHeaderDecoder) Ir(uk.co.real_logic.sbe.ir.Ir) TokenListener(uk.co.real_logic.sbe.otf.TokenListener) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with OtfHeaderDecoder

use of uk.co.real_logic.sbe.otf.OtfHeaderDecoder in project simple-binary-encoding by real-logic.

the class OtfExample method main.

public static void main(final String[] args) throws Exception {
    System.out.println("\n*** OTF Example ***\n");
    // Encode up message and schema as if we just got them off the wire.
    final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocateDirect(SCHEMA_BUFFER_CAPACITY);
    encodeSchema(encodedSchemaBuffer);
    final ByteBuffer encodedMsgBuffer = ByteBuffer.allocateDirect(MSG_BUFFER_CAPACITY);
    encodeTestMessage(encodedMsgBuffer);
    // Now lets decode the schema IR so we have IR objects.
    encodedSchemaBuffer.flip();
    final Ir ir = decodeIr(encodedSchemaBuffer);
    // From the IR we can create OTF decoder for message headers.
    final OtfHeaderDecoder headerDecoder = new OtfHeaderDecoder(ir.headerStructure());
    // Now we have IR we can read the message header
    int bufferOffset = 0;
    final UnsafeBuffer buffer = new UnsafeBuffer(encodedMsgBuffer);
    final int templateId = headerDecoder.getTemplateId(buffer, bufferOffset);
    final int schemaId = headerDecoder.getSchemaId(buffer, bufferOffset);
    final int actingVersion = headerDecoder.getSchemaVersion(buffer, bufferOffset);
    final int blockLength = headerDecoder.getBlockLength(buffer, bufferOffset);
    bufferOffset += headerDecoder.encodedLength();
    // Given the header information we can select the appropriate message template to do the decode.
    // The OTF Java classes are thread safe so the same instances can be reused across multiple threads.
    final List<Token> msgTokens = ir.getMessage(templateId);
    bufferOffset = OtfMessageDecoder.decode(buffer, bufferOffset, actingVersion, blockLength, msgTokens, new ExampleTokenListener(new PrintWriter(System.out, true)));
    if (bufferOffset != encodedMsgBuffer.position()) {
        throw new IllegalStateException("Message not fully decoded");
    }
}
Also used : OtfHeaderDecoder(uk.co.real_logic.sbe.otf.OtfHeaderDecoder) Ir(uk.co.real_logic.sbe.ir.Ir) Token(uk.co.real_logic.sbe.ir.Token) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)2 Ir (uk.co.real_logic.sbe.ir.Ir)2 OtfHeaderDecoder (uk.co.real_logic.sbe.otf.OtfHeaderDecoder)2 DirectBuffer (org.agrona.DirectBuffer)1 Test (org.junit.Test)1 InOrder (org.mockito.InOrder)1 Token (uk.co.real_logic.sbe.ir.Token)1 TokenListener (uk.co.real_logic.sbe.otf.TokenListener)1