• Version 1.16.0
Show / Hide Table of Contents

Message Fields Iteration Sample Project

This sample demonstrates how to iterate over all fields of a FIX message.

© Onix Solutions

Source code


using System;
using System.IO;
using NLog.Extensions.Logging;
using OnixS.Fix;
using OnixS.Fix.Fix50;

namespace PrettyPrintSample
{
    static class PrettyPrintSample
    {
        static void Log(string msg)
        {
            Console.Out.WriteLine(msg);
        }

        private static string GetLicenseStoreFolder()
        {
            string path = Path.Join(AppContext.BaseDirectory, "../../../../../license");

            if (Directory.Exists(path))
                return path;

            // expecting it run after dotnet publish using default paths
            return Path.Join(AppContext.BaseDirectory, "../../../../../../license");
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static int Main()
        {
            try
            {
                var settings = new EngineSettings()
                {
                    DisableNetworkLevel = true,
                    LicenseStore = GetLicenseStoreFolder(),
                    LoggerProvider = new NLogLoggerProvider()
                };
                Engine.Init(settings);

                Message message = CreateMessageWithNestedRepeatingGroups();

                Log("\n\nIterating...\n");

                Log("Begin of message:");

                IterateOverFields(message, "");

                Log("End of message");

                Engine.Shutdown();
            }
            catch(Exception ex)
            {
                Log("Exception: " + ex);

                return 1;
            }
            finally
            {
                // From https://github.com/NLog/NLog/wiki/Tutorial:
                // NET Application running on Mono / Linux are required to stop threads / timers before entering application shutdown phase.
                // Failing to do this will cause unhandled exceptions and segmentation faults, and other unpredictable behavior.
                NLog.LogManager.Shutdown(); // Flush and close down internal threads and timers
            }

            return 0;
        }

        private static void IterateOverFields(IFieldSet fieldSet, string messageLevel)
        {
            // Iterate over all fields of the message.
            foreach (Field field in fieldSet)
            {
                Log(messageLevel + field.Tag.ToString() + "=" + field.Value);

                if (fieldSet.TryGetGroup(field.Tag, out Group group))
                {
                    messageLevel += "  ";
                    Log(messageLevel + "Begin of group:");
                    IterateOverGroupInstances(group, messageLevel);
                    Log(messageLevel + "End of group");
                }
            }
        }

        private static void IterateOverGroupInstances(Group group, string messageLevel)
        {
            // Iterate over all group instances of the repeating group.
            foreach (GroupInstance instance in group)
            {
                // Iterate over all fields of the group instance.
                IterateOverFields(instance, messageLevel);
            }
        }

        private static Message CreateMessageWithNestedRepeatingGroups()
        {
            var order = new Message(MsgType.NewOrderSingle, Engine.Instance.MessageInfoDictionaryManager[ProtocolVersion.Fix50]);

            order.Set(Tag.ClOrdID, "ClOrdIdValue")
                 .Set(10000, "CustomTag");

            // Create the repeating group.
            Group partiesGroup = order.SetGroup(Tag.NoPartyIDs, 3);

            int instanceCounter = 0;

            // Set the given field in all group instances of the repeating group.
            foreach (GroupInstance instance in partiesGroup)
            {
                instance.Set(Tag.PartyID, "PartyIdValue" + instanceCounter.ToString());
                ++instanceCounter;
            }

            // Create the nested repeating group.
            Group partiesSubGroup = partiesGroup.SetGroup(Tag.NoPartySubIDs, 0, 6);

            instanceCounter = 0;

            // Set the given field in all group instances of the nested repeating group.
            foreach (GroupInstance instance in partiesSubGroup)
            {
                instance.Set(Tag.PartySubID, "PartySubIdValue" + instanceCounter.ToString());
                ++instanceCounter;
            }

            Console.WriteLine("\n\nMessage with nested repeating group:\n" + order);

            return order;
        }
    }
}

In this article
Back to top Copyright © Onix Solutions.
Generated by DocFX