Samples :: SBE Log Decoder

SBE Log Decoder

Description

SBE Log Decoder decodes provided summary file according to the template and writes decoded messages into a separate file.

Usage

  • Run the sample:
    • win: SbeLogDecoder.bat
    • linux: SbeLogDecoder.sh
  • Clean everything:
    • win: clean.bat
    • linux: clean.sh

Source Code

import biz.onixs.cme.ilink3.handler.Utils;
import biz.onixs.sbe.ByteDecoder;
import biz.onixs.sbe.ByteDecoderFactory;
import biz.onixs.sbe.IMessage;
import biz.onixs.sbe.def.MessageSchema;
import biz.onixs.util.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Base64;

/**
 * Sbe Log Decoder sample.
 */
public class SbeLogDecoder implements Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(SbeLogDecoder.class);
    private static final String templateFile = "sample/ilinkbinary.xml";
    private static final char FileExtensionDelimiter = '.';
    private static final String DecodedSummaryFileExtension = ".txt";
    private final String summaryFileName;
    private final ByteDecoder decoder;

    public SbeLogDecoder(final String summaryFileName) {
        this.summaryFileName = summaryFileName;
        final MessageSchema messageSchema = Utils.loadTemplate(templateFile);
        decoder = new ByteDecoderFactory().create(messageSchema);
    }

    public void run() {
        final String decodedSummaryFileName = summaryFileName.
                substring(0, summaryFileName.indexOf(FileExtensionDelimiter)) + DecodedSummaryFileExtension;
        int messageCounter = 0;
        try {
            final File file = new File(summaryFileName, "rw");
            final File outputFile = new File(decodedSummaryFileName, "rw");
            String line;
            while (!("").equals(line = file.getLine('\n'))) {
                final String rawMsg = line.substring(line.lastIndexOf(' ') + 1)
                        .replace("\n", "").replace("\r", "");
                final byte[] rawMsgBytes = Base64.getDecoder().decode(rawMsg);
                try {
                    final IMessage msg = decoder.decodePreCreatedMessage(rawMsgBytes, 0, rawMsgBytes.length);
                    outputFile.write(line.substring(0, 26));
                    outputFile.write(msg.toString());
                    outputFile.write(System.lineSeparator());
                    ++messageCounter;
                } catch (final Exception e) {
                    LOG.warn("Failed to parse message: " + rawMsg + " : " + e.getMessage());
                }
            }
        } catch (final IOException e) {
            throw new RuntimeException("Failed to open file: " + e.getMessage());
        } finally {
            LOG.info("{} messages are decoded successfully and written into {} file.",
                    messageCounter, decodedSummaryFileName);
        }
    }

    public static void main(final String[] args) {
        LOG.info("CME iLink 3 SbeLogDecoder Sample.");
        String summaryFileName = "";
        if (args.length >= 1) {
            summaryFileName = args[0];
        }
        if (summaryFileName.isEmpty()) {
            LOG.error("Usage: SbeLogDecoder [SummaryFileName]");
            return;
        }
        try {
            LOG.info("Decoding {} log file...", summaryFileName);
            final SbeLogDecoder sbeLogDecoder = new SbeLogDecoder(summaryFileName);
            sbeLogDecoder.run();
        } catch (final Exception e) {
            LOG.error("EXCEPTION: {}", e.getMessage());
            e.printStackTrace();
        }
    }
}