Search in sources :

Example 11 with Ir

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

the class OtfExample method encodeSchema.

private static void encodeSchema(final ByteBuffer byteBuffer) throws Exception {
    final Path path = Paths.get("src/main/resources/example-schema.xml");
    try (InputStream in = new BufferedInputStream(Files.newInputStream(path))) {
        final MessageSchema schema = XmlSchemaParser.parse(in, ParserOptions.DEFAULT);
        final Ir ir = new IrGenerator().generate(schema);
        try (IrEncoder irEncoder = new IrEncoder(byteBuffer, ir)) {
            irEncoder.encode();
        }
    }
}
Also used : Path(java.nio.file.Path) IrEncoder(uk.co.real_logic.sbe.ir.IrEncoder) IrGenerator(uk.co.real_logic.sbe.xml.IrGenerator) MessageSchema(uk.co.real_logic.sbe.xml.MessageSchema) Ir(uk.co.real_logic.sbe.ir.Ir)

Example 12 with Ir

use of uk.co.real_logic.sbe.ir.Ir 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

Ir (uk.co.real_logic.sbe.ir.Ir)12 IrGenerator (uk.co.real_logic.sbe.xml.IrGenerator)7 Test (org.junit.Test)6 MessageSchema (uk.co.real_logic.sbe.xml.MessageSchema)6 IrEncoder (uk.co.real_logic.sbe.ir.IrEncoder)5 ByteBuffer (java.nio.ByteBuffer)4 Path (java.nio.file.Path)3 ParserOptions (uk.co.real_logic.sbe.xml.ParserOptions)3 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)2 IrDecoder (uk.co.real_logic.sbe.ir.IrDecoder)2 Token (uk.co.real_logic.sbe.ir.Token)2 OtfHeaderDecoder (uk.co.real_logic.sbe.otf.OtfHeaderDecoder)2 File (java.io.File)1 DirectBuffer (org.agrona.DirectBuffer)1 StringWriterOutputManager (org.agrona.generation.StringWriterOutputManager)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1 InOrder (org.mockito.InOrder)1 TokenListener (uk.co.real_logic.sbe.otf.TokenListener)1