|  | 
|  | 1 | +package uk.co.real_logic.sbe.examples; | 
|  | 2 | + | 
|  | 3 | +import baseline.Car; | 
|  | 4 | +import baseline.MessageHeader; | 
|  | 5 | +import uk.co.real_logic.sbe.codec.java.DirectBuffer; | 
|  | 6 | +import uk.co.real_logic.sbe.ir.Decoder; | 
|  | 7 | +import uk.co.real_logic.sbe.ir.Encoder; | 
|  | 8 | +import uk.co.real_logic.sbe.ir.IntermediateRepresentation; | 
|  | 9 | +import uk.co.real_logic.sbe.xml.IrGenerator; | 
|  | 10 | +import uk.co.real_logic.sbe.xml.MessageSchema; | 
|  | 11 | +import uk.co.real_logic.sbe.xml.XmlSchemaParser; | 
|  | 12 | + | 
|  | 13 | +import java.io.FileInputStream; | 
|  | 14 | + | 
|  | 15 | +import java.io.IOException; | 
|  | 16 | +import java.io.InputStream; | 
|  | 17 | +import java.nio.ByteBuffer; | 
|  | 18 | + | 
|  | 19 | +public class OtfExample | 
|  | 20 | +{ | 
|  | 21 | + private static final MessageHeader MESSAGE_HEADER = new MessageHeader(); | 
|  | 22 | + private static final Car CAR = new Car(); | 
|  | 23 | + private static final int ACTING_VERSION = 0; | 
|  | 24 | + private static final int MSG_BUFFER_CAPACITY = 16 * 1024; | 
|  | 25 | + private static final int SCHEMA_BUFFER_CAPACITY = 16 * 1024; | 
|  | 26 | + | 
|  | 27 | + public static void main(final String[] args) throws Exception | 
|  | 28 | + { | 
|  | 29 | + // Encode up message and schema as if we just got them off the wire. | 
|  | 30 | + final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocateDirect(SCHEMA_BUFFER_CAPACITY); | 
|  | 31 | + encodeSchema(encodedSchemaBuffer); | 
|  | 32 | + | 
|  | 33 | + final ByteBuffer encodedMsgBuffer = ByteBuffer.allocateDirect(MSG_BUFFER_CAPACITY); | 
|  | 34 | + encodeTestMessage(encodedMsgBuffer); | 
|  | 35 | + | 
|  | 36 | + | 
|  | 37 | + // Now lets decode the schema IR so we have IR objects. | 
|  | 38 | + encodedSchemaBuffer.flip(); | 
|  | 39 | + final IntermediateRepresentation ir = decodeIr(encodedSchemaBuffer); | 
|  | 40 | + | 
|  | 41 | + // Now we have IR we can read the message header | 
|  | 42 | + | 
|  | 43 | + // Given the message header we can select the appropriate message to decode. | 
|  | 44 | + } | 
|  | 45 | + | 
|  | 46 | + private static void encodeSchema(final ByteBuffer buffer) | 
|  | 47 | + throws Exception | 
|  | 48 | + { | 
|  | 49 | + try (final InputStream in = new FileInputStream("examples/resources/TestSchema.xml")) | 
|  | 50 | + { | 
|  | 51 | + final MessageSchema schema = XmlSchemaParser.parse(in); | 
|  | 52 | + final IntermediateRepresentation ir = new IrGenerator().generate(schema); | 
|  | 53 | + new Encoder(buffer, ir).encode(); | 
|  | 54 | + } | 
|  | 55 | + } | 
|  | 56 | + | 
|  | 57 | + private static void encodeTestMessage(final ByteBuffer buffer) | 
|  | 58 | + { | 
|  | 59 | + final DirectBuffer directBuffer = new DirectBuffer(buffer); | 
|  | 60 | + | 
|  | 61 | + int bufferOffset = 0; | 
|  | 62 | + MESSAGE_HEADER.wrap(directBuffer, bufferOffset, ACTING_VERSION) | 
|  | 63 | + .blockLength(CAR.blockLength()) | 
|  | 64 | + .templateId(CAR.templateId()) | 
|  | 65 | + .version(CAR.templateVersion()); | 
|  | 66 | + | 
|  | 67 | + bufferOffset += MESSAGE_HEADER.size(); | 
|  | 68 | + | 
|  | 69 | + GeneratedStubExample.encode(CAR, directBuffer, bufferOffset); | 
|  | 70 | + } | 
|  | 71 | + | 
|  | 72 | + private static IntermediateRepresentation decodeIr(final ByteBuffer buffer) | 
|  | 73 | + throws IOException | 
|  | 74 | + { | 
|  | 75 | + final Decoder decoder = new Decoder(buffer); | 
|  | 76 | + return decoder.decode(); | 
|  | 77 | + } | 
|  | 78 | +} | 
0 commit comments