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(); }
/// <summary> /// The Init method is invoked to initialize an instance of /// the session class with the services, setttings, and handler /// that it requires in order to be activated. All instances of /// a session must be initialized before they can start managing /// a FIX session for their owner. /// </summary> /// <param name="services"> /// The service container that contains the references to all of /// the system services that an instance of the session needs. /// </param> /// <param name="handler"> /// The callback interface that the session directs all of its /// events to as they occur. /// </param> public void Init(IVfxServices services, IVfxFixSessionHandler handler) { // 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 session configuration from the // service container for relevant settings: IVfxSettings settings = services.GetService(typeof(IVfxSettings)) as IVfxSettings; // REC: Temporary container to simplify accessing the // session settings from the XML configuration document: Dictionary <string, string> mapSettings = new Dictionary <string, string>(); 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 one has not been // specified for the connection. 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 instance of the service container // and populate it with the services for the session: IVfxServices localServices = new VfxServices(); // REC: The session can use the global dictionaries: localServices.AddService(typeof(IVfxFixDxRegistry), dxRegistry); // REC: The session can use its own version registry: 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: this._fixTimer.Elapsed += HandleElapsed_Timer; // REC: Initialize the FIX message assembler: _fixAssembler.Init(localServices); // REC: Register required fields for messages: this._fixBeginString = sxDetails.FixBs; _fixAssembler.SetField(new FixField(8, this._fixBeginString)); _fixAssembler.SetField(new FixField(9, "")); _fixAssembler.SetField(new FixField(98, "0")); _fixAssembler.SetField(new FixField(108, "60")); xpi = xpn.Select("/Session/Protocol/Settings/Setting[@name='Fix.Session.Heartbeat']"); if ((xpi.Count > 0) && (xpi.MoveNext())) { string fixTimeout = xpi.Current.GetAttribute("content", "").Trim(); // REC: The client sessions need to maintain their own // reference to the heartbeat value: this._fixTimeout = int.Parse(fixTimeout); // REC: Assign the timeout value to the assembler so it // becomes the default for any message that needs it: _fixAssembler.SetField(new FixField(108, fixTimeout)); } 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.TargetCompID']"); if ((xpi.Count > 0) && (xpi.MoveNext())) { // REC: The client sessions need to maintain an independent // reference to the TargetCompID for identification: this._fixTargetCompID = xpi.Current.GetAttribute("content", ""); // REC: Add the field to the assembler so that it gets put // into each outgoing message: _fixAssembler.SetField(new FixField(56, this._fixTargetCompID)); } _fixAssembler.SetField(new FixField(10, "")); // REC: The client session knows the FIX TargetCompID of the // peer system in advance, unlike the server sessions, so we // can construct the session id in the initialization code: _sessionId = string.Format("{0}-{1}", _fixSenderCompID, _fixTargetCompID); // REC: Maintain a reference to the supplied callback // handler for issuing event notifications: _handler = handler; }
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; }
/// <summary> /// The Init method is invoked to initialize an instance of /// the session class with the services, setttings, and handler /// that it requires in order to be activated. All instances of /// a session must be initialized before they can start managing /// a FIX session for their owner. /// </summary> /// <param name="services"> /// The service container that contains the references to all of /// the system services that an instance of the session needs. /// </param> /// <param name="handler"> /// The callback interface that the session directs all of its /// events to as they occur. /// </param> public void Init(IVfxServices services, IVfxFixSessionHandler handler) { // 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 session configuration from the // service container for relevant settings: IVfxSettings settings = services.GetService(typeof(IVfxSettings)) as IVfxSettings; // REC: Temporary container to simplify accessing the // session settings from the XML configuration document: Dictionary<string, string> mapSettings = new Dictionary<string, string>(); XPathNavigator xpn = settings.Document.CreateNavigator(); XPathNodeIterator xpi = null; 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(); } xpi = xpn.Select("/Session/Protocol/Settings/Setting[@name='Fix.Session.Ax.Default']"); if ((xpi.Count > 0) && (xpi.MoveNext())) { this._axDefault = xpi.Current.GetAttribute("content", "").Trim(); } // REC: Retrieve application layer details: VfxFixVxRecord axDetails = vxRegistry.Get(_axVersion); // REC: Adjust the session layer, if one has not been // specified for the connection. 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 instance of the service container // and populate it with the services for the session: IVfxServices localServices = new VfxServices(); // REC: The session can use the global dictionaries: localServices.AddService(typeof(IVfxFixDxRegistry), dxRegistry); // REC: The session can use its own version registry: 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: this._fixTimer.Elapsed += HandleElapsed_Timer; // REC: Initialize the FIX message assembler: _fixAssembler.Init(localServices); // REC: Register required fields for messages: this._fixBeginString = sxDetails.FixBs; _fixAssembler.SetField(new FixField(8, this._fixBeginString)); _fixAssembler.SetField(new FixField(9, "")); _fixAssembler.SetField(new FixField(98, "0")); _fixAssembler.SetField(new FixField(108, "60")); xpi = xpn.Select("/Session/Protocol/Settings/Setting[@name='Fix.Session.Heartbeat']"); if ((xpi.Count > 0) && (xpi.MoveNext())) { string fixTimeout = xpi.Current.GetAttribute("content", "").Trim(); // REC: The client sessions need to maintain their own // reference to the heartbeat value: this._fixTimeout = int.Parse(fixTimeout); // REC: Assign the timeout value to the assembler so it // becomes the default for any message that needs it: _fixAssembler.SetField(new FixField(108, fixTimeout)); } 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.TargetCompID']"); if ((xpi.Count > 0) && (xpi.MoveNext())) { // REC: The client sessions need to maintain an independent // reference to the TargetCompID for identification: this._fixTargetCompID = xpi.Current.GetAttribute("content", ""); // REC: Add the field to the assembler so that it gets put // into each outgoing message: _fixAssembler.SetField(new FixField(56, this._fixTargetCompID)); } _fixAssembler.SetField(new FixField(10, "")); // REC: The client session knows the FIX TargetCompID of the // peer system in advance, unlike the server sessions, so we // can construct the session id in the initialization code: _sessionId = string.Format("{0}-{1}", _fixSenderCompID, _fixTargetCompID); // REC: Maintain a reference to the supplied callback // handler for issuing event notifications: _handler = handler; }