/// <summary> /// The AddGroup method adds an instance of a FIX group /// to the collection. Multiple fields with the same tag /// can be added to the collection, and will be stored in /// a list that is keyed by the field's FIX tag. /// </summary> /// <param name="group"> /// The FIX group to add to the collection. /// </param> public void AddGroup(FixGroup group) { // REC: Ensure that the group's tag doesn't collide // with a field that is already in the collection: if (!_mapFields.ContainsKey(group.Tag)) { if (!_mapGroups.ContainsKey(group.Tag)) { _mapGroups.Add(group.Tag, new List <FixGroup>()); } _mapGroups[group.Tag].Add(group); if (!_mapElements.ContainsKey(group.Tag)) { _mapElements.Add(group.Tag, new List <IFixElement>()); } _mapElements[group.Tag].Add(group); // REC: Add the group to the sequential list of elements: _listElements.Add(group); } else { throw new ArgumentException("Group's tag collides with an existing field."); } }
/// <summary> /// The GetGroup method returns the first instance of /// a FIX group that has the specified FIX tag. /// </summary> /// <param name="tag"> /// The FIX tag of the group to retrieve. /// </param> /// <returns> /// The first instance of a FIX group in the collection /// that has the specified tag. /// </returns> public FixGroup GetGroup(int tag) { FixGroup result = null; if (_mapGroups.ContainsKey(tag)) { result = _mapGroups[tag][0]; } return(result); }
/// <summary> /// The ConvertElements method converts all of the elements /// in an instance of FixCollection into their corresponding /// representation as a text string. This is a helper method /// for the FixMessage class implementation of ToString(). /// </summary> /// <param name="elements"> /// The collection of elements that are to be converted into /// their corresponding representation as a string. /// </param> /// <returns> /// The string that results from the conversion. /// </returns> private string ConvertElements(FixCollection elements) { StringBuilder sb = new StringBuilder(); foreach (IFixElement element in elements) { sb.Append(string.Format("{0}={1}\x01", element.Tag, element.Content)); FixGroup group = element as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { sb.Append(ConvertElements(instance)); } } } return(sb.ToString()); }
// REC: The CalculateLength method calculates the combined // length of all of the elements in a collection. This is // a support method for the CalculateBodyLength method. private static int CalculateLength(FixCollection collection) { int result = 0; foreach (IFixElement element in collection) { result += element.Tag.ToString().Length; result += 1; result += element.Content.Length; result += 1; FixGroup group = element as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateLength(instance); } } } return(result); }
private static int CalculateSum(FixCollection elements) { int result = 0; foreach (IFixElement element in elements) { result += CalculateSum(element.Tag.ToString()); result += 0x3D; result += CalculateSum(element.Content); result += 0x01; FixGroup group = element as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateSum(instance); } } } return(result); }
/// <summary> /// The AddGroup method is invoked to add a new instance /// of a repeating group to an instance of a message. /// </summary> /// <param name="group"> /// The repeating group to add to the message. /// </param> public void AddGroup(FixGroup group) { _msgElements.AddGroup(group); }
/// <summary> /// The GetChecksum method calculates the checksum for /// an instance of a FIX message. /// </summary> /// <param name="msg"> /// The FIX message to analyze. /// </param> /// <returns> /// The FIX protocol checksum of the message. /// </returns> public static int GetChecksum(FixMessage msg) { int result = 0; // REC: Process the header elements: foreach (IFixElement hdrElement in msg.Header) { result += CalculateSum(hdrElement.Tag.ToString()); result += 0x3D; result += CalculateSum(hdrElement.Content); result += 0x01; FixGroup group = hdrElement as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateSum(instance); } } } // REC: Process the message body: foreach (IFixElement msgElement in msg.Content) { result += CalculateSum(msgElement.Tag.ToString()); result += 0x3D; result += CalculateSum(msgElement.Content); result += 0x01; FixGroup group = msgElement as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateSum(instance); } } } // REC: Process the message trailer: foreach (IFixElement trlElement in msg.Trailer) { if (trlElement.Tag == 10) { break; } result += CalculateSum(trlElement.Tag.ToString()); result += 0x3D; result += CalculateSum(trlElement.Content); result += 0x01; FixGroup group = trlElement as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateSum(instance); } } } return(result); }
/// <summary> /// The GetBodyLength method calculates the appropriate /// value for the FIX BodyLength field of the specified /// message, in accordance with the FIX Protocol. /// </summary> /// <param name="msg"> /// The message that the BodyLength value is to be /// calculated from. /// </param> /// <returns> /// The body length of the message, calculated according /// to the method specified in the FIX protocol. /// </returns> public static int GetBodyLength(FixMessage msg) { int result = 0; // REC: Calculate the combined length of all // elements in the header of the message: bool lengthFound = false; foreach (IFixElement element in msg.Header) { if (lengthFound == true) { result += element.Tag.ToString().Length; result += 1; result += element.Content.Length; result += 1; FixGroup group = element as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateLength(instance); } } } else { if (element.Tag == 9) { lengthFound = true; } } } // REC: Calculate the combined length of all // the body elements in the message: foreach (IFixElement element in msg.Content) { result += element.Tag.ToString().Length; result += 1; result += element.Content.Length; result += 1; FixGroup group = element as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateLength(instance); } } } // REC: Calculate the combined length of all // elements in the trailer of the message: foreach (IFixElement element in msg.Trailer) { // REC: Terminate when the checksum field // is encountered in the trailer: if (element.Tag == 10) { break; } result += element.Tag.ToString().Length; result += 1; result += element.Content.Length; result += 1; FixGroup group = element as FixGroup; if (group != null) { foreach (FixCollection instance in group.Instances) { result += CalculateLength(instance); } } } return(result); }