Example #1
0
        public void Configure(FixValidatorSettings settings)
        {
            _settings = settings;

            _cachedHeader.Clear();

            _cachedTrailer.Clear();

            if (_settings.Dictionaries.Count > 0)
            {
                FixDictionary primary = _settings.Dictionaries[0];

                // REC: Preprocess the list of elements in the primary
                // dictionary's header so that it doesn't have to be
                // done every time a message is validated:
                FixDxCollection hdrElements = primary.Resolve(primary.Header);
                foreach (IFixDxElement hdrElement in hdrElements)
                {
                    _cachedHeader.Add(CreateResult(hdrElement));
                }

                // REC: Preprocess the list of elements in the primary
                // dictionary's trailer so that it doesn't have to be
                // done every time a message is validated:
                FixDxCollection trlElements = primary.Resolve(primary.Trailer);
                foreach (IFixDxElement trlElement in trlElements)
                {
                    _cachedTrailer.Add(CreateResult(trlElement));
                }
            }
        }
Example #2
0
        /// <summary>
        /// The GetDictionaryFromField method is invoked to attempt
        /// to determine which dictionary associated with a specific
        /// version definition contains a specific FIX field.
        /// </summary>
        /// <param name="version">
        /// The FIX version definition to search.
        /// </param>
        /// <param name="tag">
        /// The FIX tag of the element to search for.
        /// </param>
        /// <returns>
        /// The first dictionary in the version definition that has
        /// a field definition that matches the specified tag.
        /// </returns>
        public FixDictionary GetDictionaryFromField(string version, int tag)
        {
            FixDictionary result = null;

            if (_vxRegistry != null)
            {
                VfxFixVxRecord vxDetails = _vxRegistry.Get(version);
                if (vxDetails != null)
                {
                    if (_dxRegistry != null)
                    {
                        foreach (VfxFixVersion_Dictionary_Reference dxEntry in vxDetails.Dictionaries)
                        {
                            FixDictionary dxInstance = _dxRegistry.GetEntry(dxEntry.Name);
                            if (dxInstance != null)
                            {
                                if (dxInstance.Fields.GetElement(tag) != null)
                                {
                                    result = dxInstance;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// The Execute method is invoked in order to generate the
        /// source code for all of the entities that are defined in
        /// the specified FIX dictionary.
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="dstPath"></param>
        private void Execute(string srcPath, string dstPath, string dstNamespace)
        {
            FixDictionary dxInstance = FixDxImporter.Import(srcPath);

            if (dxInstance != null)
            {
                string fixNamespace = "VfxEngine.Fix00";
                if (!string.IsNullOrEmpty(dstNamespace))
                {
                    fixNamespace = dstNamespace;
                }

                // REC: If the destination folder doesn't exist, create it:
                if (!Directory.Exists(dstPath))
                {
                    Directory.CreateDirectory(dstPath);
                }

                // REC: Generate the path to the tag definitions file:
                string dstPath_Tags = string.Format("{0}\\{1}.Tags.cs", dstPath, fixNamespace);

                // REC: Generate the tag definitions, write them to the file:
                GenerateTags(dxInstance, dstPath_Tags, fixNamespace);
            }
        }
Example #4
0
        private void PopulateMsgElements(string version, string msgType, FixMessage msg)
        {
            VfxFixVxRecord vxDetails = _vxRegistry.Get(version);

            if (vxDetails != null)
            {
                FixDxCollection msgElements = null;
                foreach (VfxFixVersion_Dictionary_Reference dxEntry in vxDetails.Dictionaries)
                {
                    // REC: Retrieve the dictionary associated with
                    // the version definition:
                    FixDictionary dxInstance = _dxRegistry.GetEntry(dxEntry.Name);
                    // REC: Attempt to retrieve the specified message
                    // type from the dictionary:
                    FixDxMessage dxMessage = dxInstance.GetMessageByType(msgType);
                    if (dxMessage != null)
                    {
                        msgElements = dxInstance.Resolve(dxMessage.Elements);
                        break;
                    }
                }

                if (msgElements != null)
                {
                    // REC: Create the sort ordering for the elements
                    // and populate any fields and groups that have been
                    // registered with the assembler:
                    Collection <int> ordering = new Collection <int>();
                    foreach (IFixDxElement dxElement in msgElements)
                    {
                        ordering.Add(dxElement.Tag);

                        if (dxElement is FixDxResolvedField)
                        {
                            if (_mapFields.ContainsKey(dxElement.Tag))
                            {
                                msg.Content.SetField(_mapFields[dxElement.Tag]);
                            }
                        }
                        else
                        {
                            if (_mapGroups.ContainsKey(dxElement.Tag))
                            {
                                msg.Content.AddGroup(_mapGroups[dxElement.Tag]);
                            }
                        }
                    }

                    // REC: Assign the sort ordering for the header
                    // elements to the message's header:
                    msg.Content.SetOrdering(ordering);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Locates the FIX dictionary that is registered under
        /// the specified name and returns the reference to it.
        /// </summary>
        /// <param name="name">
        /// The name of the FIX dictionary to lookup.
        /// </param>
        /// <returns>
        /// A reference to the FIX dictionary that is registered
        /// under the specified name, or null if there is no entry
        /// that matches the specified name.
        /// </returns>
        public FixDictionary GetEntry(string name)
        {
            FixDictionary result = null;

            _rwLock.AcquireReaderLock(Timeout.Infinite);
            if (_mapEntries.ContainsKey(name))
            {
                result = _mapEntries[name];
            }
            _rwLock.ReleaseReaderLock();
            return(result);
        }
Example #6
0
 /// <summary>
 /// Creates a new entry in the registry for an instance
 /// of a FIX dictionary.
 /// </summary>
 /// <param name="name">
 /// The name that the dictionary is registered under.
 /// </param>
 /// <param name="dictionary">
 /// The reference to the dictionary being registered.
 /// </param>
 public void CreateEntry(string name, FixDictionary dictionary)
 {
     _rwLock.AcquireWriterLock(Timeout.Infinite);
     if (!_mapEntries.ContainsKey(name))
     {
         _mapEntries.Add(name, dictionary);
     }
     else
     {
         _mapEntries[name] = dictionary;
     }
     _rwLock.ReleaseWriterLock();
 }
Example #7
0
        /// <summary>
        /// The GetUniqueMessages method retrieves a collection which
        /// represents the set of all FIX message definitions found in
        /// the dictionaries associated with a specific version.
        /// </summary>
        /// <param name="version">
        /// The name of the version definition that is to be used
        /// for looking up the FIX message definitions.
        /// </param>
        /// <returns>
        /// The collection of message definitions that represents
        /// the complete set of all of the field definitions in the
        /// dictionaries associated with the specified version.
        /// </returns>
        public FixDxCollection GetUniqueMessages(string version)
        {
            FixDxCollection result = new FixDxCollection();

            if (_vxRegistry != null)
            {
                VfxFixVxRecord vxDetails = _vxRegistry.Get(version);
                if (vxDetails != null)
                {
                    if (_dxRegistry != null)
                    {
                        // REC: Temporary map that is used to ensure there
                        // is only one instance of each MsgType:
                        Dictionary <string, FixDxMessage> mapMessages = new Dictionary <string, FixDxMessage>();

                        // REC: Iterate over all of the FIX dictionaries that
                        // are associated with the version and build the list
                        // of FIX field definitions from them:
                        foreach (VfxFixVersion_Dictionary_Reference dxEntry in vxDetails.Dictionaries)
                        {
                            FixDictionary dxInstance = _dxRegistry.GetEntry(dxEntry.Name);
                            if (dxInstance != null)
                            {
                                foreach (IFixDxElement dxElement in dxInstance.Messages)
                                {
                                    FixDxMessage dxMessage = dxElement as FixDxMessage;
                                    if (dxMessage != null)
                                    {
                                        if (mapMessages.ContainsKey(dxMessage.MsgType) == false)
                                        {
                                            mapMessages.Add(dxMessage.MsgType, dxMessage);
                                        }
                                        else
                                        {
                                            mapMessages[dxMessage.MsgType] = dxMessage;
                                        }
                                    }
                                }
                            }
                        }

                        foreach (string key in mapMessages.Keys)
                        {
                            result.Add(mapMessages[key]);
                        }
                    }
                }
            }

            return(result);
        }
Example #8
0
        /// <summary>
        /// The GetUniqueFields method retrieves a collection which
        /// represents the set of all FIX field definitions found in
        /// the dictionaries associated with a specific version.
        /// </summary>
        /// <param name="version">
        /// The name of the version definition that is to be used
        /// for looking up the FIX field definitions.
        /// </param>
        /// <returns>
        /// The collection of FIX field definitions that represents
        /// the complete set of all of the field definitions in the
        /// dictionaries associated with the specified version.
        /// </returns>
        public FixDxCollection GetUniqueFields(string version)
        {
            FixDxCollection result = new FixDxCollection();

            if (_vxRegistry != null)
            {
                VfxFixVxRecord vxDetails = _vxRegistry.Get(version);
                if (vxDetails != null)
                {
                    if (_dxRegistry != null)
                    {
                        // REC: Temporary map that is used to ensure there
                        // is only one instance of a field for each tag.
                        Dictionary <int, FixDxField> mapFields = new Dictionary <int, FixDxField>();

                        // REC: Iterate over all of the FIX dictionaries that
                        // are associated with the version and build the list
                        // of FIX field definitions from them:
                        foreach (VfxFixVersion_Dictionary_Reference dxEntry in vxDetails.Dictionaries)
                        {
                            FixDictionary dxInstance = _dxRegistry.GetEntry(dxEntry.Name);
                            if (dxInstance != null)
                            {
                                foreach (IFixDxElement dxElement in dxInstance.Fields)
                                {
                                    FixDxField dxField = dxElement as FixDxField;
                                    if (dxField != null)
                                    {
                                        if (mapFields.ContainsKey(dxField.Tag) == false)
                                        {
                                            mapFields.Add(dxField.Tag, dxField);
                                        }
                                        else
                                        {
                                            mapFields[dxField.Tag] = dxField;
                                        }
                                    }
                                }
                            }
                        }

                        foreach (int key in mapFields.Keys)
                        {
                            result.Add(mapFields[key]);
                        }
                    }
                }
            }

            return(result);
        }
Example #9
0
        /// <summary>
        /// The GenerateTags method is invoked to request that the
        /// system generate a tag definition file, based on the tags
        /// that are discovered in the supplied FIX dictionary.
        /// </summary>
        /// <param name="dxInstance"></param>
        /// <param name="dstPath"></param>
        private void GenerateTags(FixDictionary dxInstance, string dstPath, string dstNamespace)
        {
            StreamWriter mxWriter = new StreamWriter(new FileStream(dstPath, FileMode.Create, FileAccess.Write, FileShare.None));

            mxWriter.WriteLine("//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
            mxWriter.WriteLine("// VERSAFIX FIX ENGINE - FIX TAG DEFINITIONS FILE");
            mxWriter.WriteLine("//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
            mxWriter.WriteLine("using System;");

            mxWriter.WriteLine(string.Format("namespace {0} {{\r\n\r\n", dstNamespace));

            mxWriter.WriteLine(string.Format("\tpublic class Tags {{"));

            foreach (FixDxField dxField in dxInstance.Fields)
            {
                // REC: Ensure that each field name is valid, in terms of being
                // able to represent it as a .NET variable:
                bool isAcceptable = true;
                foreach (char ch in dxField.Name)
                {
                    if (!char.IsLetterOrDigit(ch) && (ch != '_'))
                    {
                        isAcceptable = false;
                        break;
                    }
                }

                if (isAcceptable)
                {
                    mxWriter.WriteLine(string.Format("\t\tpublic static readonly int {0} = {1};\r\n", dxField.Name, dxField.Tag));
                }
                else
                {
                    mxWriter.WriteLine(string.Format("\t\t// PARSER ERROR: Field name does not look acceptable"));
                    mxWriter.WriteLine(string.Format("\t\t// {0} = {1}\r\n", dxField.Name, dxField.Tag));
                }
            }

            mxWriter.WriteLine(string.Format("\t}}"));

            mxWriter.WriteLine(string.Format("}}"));
            mxWriter.Close();
        }
Example #10
0
        public void Run(string[] args)
        {
            // REC: Construct the service container that will be used
            // to provide the engine with references to the services it
            // will need to access when it starts:
            IVfxServices services = new VfxServices();

            // REC: Retrieve the current path to the executable:
            string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            // REC: Construct the complete path to the configuration
            // settings file for the application:
            string pathSettings = Path.Combine(execPath, "FixSessions.xml");

            // REC: Attempt to load the application configuration into
            // an instance of a new XML document:
            XmlDocument configuration = new XmlDocument();

            configuration.Load(pathSettings);

            // REC: After the configuration file has been loaded, it must
            // be added to the application's service container so that the
            // engine can access it when it is started.
            services.AddService(typeof(IVfxSettings), new VfxSettings(configuration));

            // REC: The engine requires an IVfxFixApp implementation that
            // it can direct callbacks to as FIX events occur:
            services.AddService(typeof(IVfxFixApp), _fixApplication);

            // REC: Construct the complete path to the versions file that
            // contains the FIX version definitions for the application:
            string pathVersions = Path.Combine(execPath, "FixVersions.xml");

            // REC: Attempt to load the version definitions from the
            // version definitions file associated with the app:
            VfxFixVxRegistry vxRegistry = new VfxFixVxRegistry();

            vxRegistry.Import(pathVersions);


            //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
            // NOTE: You don't need to do this in your own apps, this is
            // done here because our example code stores all of the files
            // for the FIX dictionaries in a common directory, so the app
            // walks backwards up the directory tree in order to find the
            // folder that contains those files...
            //-----------------------------------------------------------
            string pathDictionaries = GetPathToExampleDictionaries();

            // REC: Scan the dictionaries directory and import all of
            // the dictionaries into a dictionary registry instance:
            IVfxFixDxRegistry dxRegistry = new VfxFixDxRegistry();

            foreach (string dxFile in Directory.GetFiles(pathDictionaries, "*.xml"))
            {
                // REC: Throughout the VersaFix system, dictionaries are
                // referred to by their file name, sans extension:
                string dxName = Path.GetFileNameWithoutExtension(dxFile);
                // REC: Import the actual dictionary data:
                FixDictionary dxEntry = FixDxImporter.Import(dxFile);
                // REC: Create an entry for the imported dictionary
                // in the dictionary registry:
                if (dxEntry != null)
                {
                    dxRegistry.CreateEntry(dxName, dxEntry);
                }
            }

            // REC: The engine requires IVfxFixVxRegistry so that it can
            // lookup information about configured FIX versions:
            services.AddService(typeof(IVfxFixVxRegistry), vxRegistry);

            // REC: The engine requires IVfxFixDxRegistry so that it can
            // lookup information about configured FIX dictionaries:
            services.AddService(typeof(IVfxFixDxRegistry), dxRegistry);

            // REC: If the initialization completed successfully, the
            // engine can be activated. The application will now start
            // establishing new FIX connections in acccordance with the
            // application's configuration settings:
            this._fixEngine.Activate(services);

            // REC: Sophisticated shutdown logic ;-)
            System.Console.ReadLine();
        }
Example #11
0
        void IVfxFixSession.Init(IVfxServices services, IVfxFixSessionHandler handler)
        {
            // REC: Maintain a reference to the set of configuration settings
            // that are supplied to the session instance:
            _localServices = services;

            // REC: Retrieve the version registry that the session
            // has been configured to use:
            IVfxFixVxRegistry vxRegistry = services.GetService(typeof(IVfxFixVxRegistry)) as IVfxFixVxRegistry;

            // REC: Retrieve the dictionary registry that the session
            // has been configured to use:
            IVfxFixDxRegistry dxRegistry = services.GetService(typeof(IVfxFixDxRegistry)) as IVfxFixDxRegistry;

            // REC: Retrieve the session database service from the
            // set of services provided by the session's owner:
            _fixDatabase = services.GetService(typeof(IVfxFixDatabase)) as IVfxFixDatabase;

            // REC: Retrieve the configuration settings and extract
            // the FIX session and application layer versions:
            IVfxSettings settings = this._localServices.GetService(typeof(IVfxSettings)) as IVfxSettings;

            XPathNavigator    xpn = settings.Document.CreateNavigator();
            XPathNodeIterator xpi;

            xpi = xpn.Select("/Session/Protocol/Settings/Setting[@name='Fix.Session.Sx.Version']");
            if ((xpi.Count > 0) && (xpi.MoveNext()))
            {
                this._sxVersion = xpi.Current.GetAttribute("content", "").Trim();
            }

            xpi = xpn.Select("/Session/Protocol/Settings/Setting[@name='Fix.Session.Ax.Version']");
            if ((xpi.Count > 0) && (xpi.MoveNext()))
            {
                this._axVersion = xpi.Current.GetAttribute("content", "").Trim();
            }

            // REC: Retrieve application layer details:
            VfxFixVxRecord axDetails = vxRegistry.Get(_axVersion);

            // REC: Adjust the session layer, if it was not specified
            // and the application layer is FIX 4.0-4.4:
            if (string.IsNullOrEmpty(this._sxVersion))
            {
                if (axDetails.Layer.ToLower().CompareTo("combined") == 0)
                {
                    this._sxVersion = this._axVersion;
                }
            }

            // REC: Retrieve the session layer details:
            VfxFixVxRecord sxDetails = vxRegistry.Get(_sxVersion);

            // REC: Construct a local instance of a version definition
            // registry so that the session relies only on the specific
            // versions that have been specified for the service:
            VfxFixVxRegistry localVxRegistry = new VfxFixVxRegistry();

            // REC: Add the session layer to the local registry:
            localVxRegistry.Add(this._sxVersion, sxDetails);

            // REC: Add the application layer to the local registry:
            localVxRegistry.Add(this._axVersion, axDetails);

            // REC: Construct a new service container and initialize
            // it with the appropriate services:
            IVfxServices localServices = new VfxServices();

            localServices.AddService(typeof(IVfxFixDxRegistry), dxRegistry);
            localServices.AddService(typeof(IVfxFixVxRegistry), localVxRegistry);

            // REC: Initialize the parser with the local services:
            _fixParser.Init(localServices);

            // REC: Configure the FIX session timer to fire every
            // second for conducting timeout checks, etc.
            _fixTimer.Interval = 1000;
            // REC: Subscribe to the timer in order to determine if
            // the session has timed out:
            _fixTimer.Elapsed += HandleElapsed_Timer;

            // REC: Construct the FIX BeginString using the major
            // and minor version numbers from the primary dictionary
            // that is assigned to the version:
            FixDictionary dxPrimary = dxRegistry.GetEntry(sxDetails.Dictionaries[0].Name);

            // REC: Construct the appropriate begin string, based on the
            // major and minor version numbers of the dictionary:
            this._fixBeginString = sxDetails.FixBs;
            _fixAssembler.SetField(new FixField(8, this._fixBeginString));

            // REC: Initialize the FIX message assembler:
            _fixAssembler.Init(localServices);

            // REC: Register required fields for messages:
            _fixAssembler.SetField(new FixField(8, _fixBeginString));
            _fixAssembler.SetField(new FixField(9, ""));
            _fixAssembler.SetField(new FixField(98, "0"));
            _fixAssembler.SetField(new FixField(10, ""));

            xpi = xpn.Select("/Session/Protocol/Settings/Setting[@name='Fix.Session.SenderCompID']");
            if ((xpi.Count > 0) && (xpi.MoveNext()))
            {
                // REC: The client sessions need to maintain an independent
                // reference to the SenderCompID for identification:
                this._fixSenderCompID = xpi.Current.GetAttribute("content", "");

                // REC: Add the field to the assembler so that it gets put
                // into each outgoing message:
                _fixAssembler.SetField(new FixField(49, this._fixSenderCompID));
            }

            xpi = xpn.Select("/Session/Protocol/Settings/Setting[@name='Fix.Session.Reset']");
            if ((xpi.Count > 0) && (xpi.MoveNext()))
            {
                string resetSequence = xpi.Current.GetAttribute("content", "");
                if (resetSequence.CompareTo("true") == 0)
                {
                    this._resetSequence = true;
                }
            }

            // REC: Maintain a reference to the supplied callback
            // handler for issuing event notifications:
            _handler = handler;
        }
Example #12
0
        private void PopulateTrlElements(string version, FixMessage msg)
        {
            VfxFixVxRecord vxDetails = _vxRegistry.Get(version);

            if (vxDetails != null)
            {
                // REC: Ensure that there is at least one FIX dictionary
                // assigned to the specified version definition:
                if (vxDetails.Dictionaries.Count > 0)
                {
                    FixDictionary dxEntry = _dxRegistry.GetEntry(vxDetails.Dictionaries[0].Name);
                    if (dxEntry != null)
                    {
                        FixDxCollection trlElements = dxEntry.Resolve(dxEntry.Trailer);
                        if (trlElements != null)
                        {
                            // REC: Create the sort ordering for the elements
                            // and populate any fields and groups that have been
                            // registered with the assembler:
                            Collection <int> ordering = new Collection <int>();
                            foreach (IFixDxElement dxElement in trlElements)
                            {
                                ordering.Add(dxElement.Tag);

                                if (dxElement is FixDxResolvedField)
                                {
                                    if (_mapFields.ContainsKey(dxElement.Tag))
                                    {
                                        msg.Trailer.SetField(_mapFields[dxElement.Tag]);
                                    }
                                }
                                else
                                {
                                    if (_mapGroups.ContainsKey(dxElement.Tag))
                                    {
                                        msg.Trailer.AddGroup(_mapGroups[dxElement.Tag]);
                                    }
                                }
                            }

                            // REC: Assign the sort ordering for the header
                            // elements to the message's header:
                            msg.Trailer.SetOrdering(ordering);
                        }
                    }
                    else
                    {
                        string error = string.Format("Dictionary \"{0}\" not registered.", vxDetails.Dictionaries[0]);
                        throw new ArgumentException(error);
                    }
                }
                else
                {
                    string error = string.Format("The version \"{0}\" has no associated dictionaries.", version);
                    throw new ArgumentException(error);
                }
            }
            else
            {
                string error = string.Format("The version \"{0}\" is not registered.", version);
                throw new ArgumentException(error);
            }
        }