Ejemplo n.º 1
0
        public List <MrnMsg> GetMrnByGuid(string guid, int fragmentNumber)
        {
            var dict = from x in GetMsgList()
                       where x.Key >= fragmentNumber && x.Value.ContainsFieldList &&
                       x.Value.GetFieldList().ContainsKey(4271) && x.Value.GetFieldList().ContainsValue(guid)
                       select x;

            var list = new List <MrnMsg>();

            foreach (var data in dict)
            {
                var bDuplicate = false;
                foreach (var mrn in list)
                {
                    if (!mrn.ContainsFieldList)
                    {
                        continue;
                    }
                    if (RdmDataConverter.HexStringToInt(mrn.GetFieldList()[32479]) !=
                        RdmDataConverter.HexStringToInt(data.Value.GetFieldList()[32479]))
                    {
                        continue;
                    }
                    bDuplicate = true;
                    break;
                }

                if (!bDuplicate)
                {
                    list.Add(data.Value);
                }
            }

            return(list);
        }
Ejemplo n.º 2
0
        private static bool FieldValueToString(int fidId, string value, out string outputStr, out string errorMsg, RdmFieldDictionary fieldDictionary, RdmEnumTypeDef enumTypeDef)
        {
            outputStr = string.Empty;
            errorMsg  = string.Empty;
            if (value.Trim() == string.Empty)
            {
                errorMsg = $"Fid {fidId} Data is null or empty";
                return(false);
            }

            if (!fieldDictionary.GetDictEntryById(fidId, out var rdmEntry))
            {
                errorMsg = $"Unable to find fid {fidId} in data dictionary";
                return(false);
            }

            var pBuffer = new string(value.ToCharArray()
                                     .Where(c => !char.IsWhiteSpace(c))
                                     .ToArray());

            try
            {
                switch (rdmEntry.RwfType)
                {
                case RwfTypeEnum.Buffer:
                    outputStr = pBuffer;
                    break;

                case RwfTypeEnum.Date:
                    var dt = RdmDataConverter.HexStringToDateTime(pBuffer);
                    outputStr =
                        $"{(dt.HasValue ? $"{dt.Value.Day}/{dt.Value.Month}/{dt.Value.Year}" : string.Empty)}";
                    break;

                case RwfTypeEnum.Enum:
                    outputStr = enumTypeDef.GetEnumDisplayValue(fidId,
                                                                RdmDataConverter.HexStringToInt(pBuffer) ?? 0, out var display)
                            ? $"{display.EnumDisplay.Replace("\"", "")}({RdmDataConverter.HexStringToInt(pBuffer)})"
                            : $"Unknown Enum({int.Parse(pBuffer)})";
                    break;

                case RwfTypeEnum.Int64:
                case RwfTypeEnum.Uint64:
                    var intVal = RdmDataConverter.HexStringToInt(pBuffer) ?? 0;
                    outputStr = rdmEntry.Acronym.Contains("_MS") ? $"{RdmDataConverter.TimeMsToString(intVal)}" : $"{intVal}";
                    break;

                case RwfTypeEnum.Real64:
                    outputStr = $"{RdmDataConverter.RealStringtoDouble(pBuffer).ToString()}";
                    break;

                case RwfTypeEnum.RmtesString:
                {
                    // Check if RMTES Header contains 0x1B 0x25 0x30 follow by UTF-8 string. Then remove the header
                    if (pBuffer.StartsWith("1B2530"))
                    {
                        pBuffer   = pBuffer.Remove(0, 6);
                        outputStr = Encoding.UTF8.GetString(RdmDataConverter.StringToByteArray(pBuffer.Trim()));
                    }
                    else if (pBuffer.StartsWith("1B5B"))
                    {
                        outputStr = DecodePartialUpdate(
                            fidId,
                            Encoding.UTF8.GetString(RdmDataConverter.StringToByteArray(pBuffer.Trim())));
                    }
                    else
                    {
                        outputStr = Encoding.UTF8.GetString(RdmDataConverter.StringToByteArray(pBuffer.Trim()));
                    }

                    var validXmlString = new StringBuilder();
                    validXmlString.Append(outputStr.Where(XmlConvert.IsXmlChar).ToArray());
                    outputStr = validXmlString.ToString();
                }
                break;

                case RwfTypeEnum.AsciiString:

                    outputStr = Encoding.UTF8.GetString(
                        RdmDataConverter.StringToByteArray(pBuffer.Trim()));
                    break;

                case RwfTypeEnum.Time:
                    outputStr = $"{RdmDataConverter.HexStringToTime(pBuffer.Trim())}";
                    break;

                case RwfTypeEnum.Unhandled:
                    outputStr = $"{value}";
                    break;
                }
            }
            catch (Exception exception)
            {
                errorMsg = $"Fid {fidId} {exception.Message}";
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        private static bool DecodeAndCopy(XmlReader reader, XmlWriter writer, RdmFieldDictionary fieldDictionary, RdmEnumTypeDef enumTypeDef, out string errorMsg, int numFragments, bool verbose)
        {
            errorMsg = string.Empty;
            bool   bMrnDecoding        = false;
            long   totalSize           = 0;
            long   currentFragmentSize = 0;
            long   guidSize            = 0;
            var    mrnStrings          = new StringBuilder();
            var    guidstr             = string.Empty;
            JToken jsonToken           = null;
            var    currentElementName  = string.Empty;

            mrnStrings.Clear();
            int currentFragmentNum = 0;
            int percent            = 5;

            try
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                    {
                        currentFragmentNum++;
                        var percentRead = currentFragmentNum / (double)numFragments;
                        percentRead *= 100.0;
                        if (percentRead >= percent)
                        {
                            Console.WriteLine($"Processing completed {percent}% {currentFragmentNum}/{numFragments} elements");
                            var r = new Random();
                            percent += r.Next(10, 18);
                            if (percent > 100)
                            {
                                percent = 100;
                            }
                        }
                        currentElementName = reader.Name;
                        writer.WriteStartElement(reader.Name);
                        if (reader.HasAttributes)
                        {
                            if (currentElementName != "fieldEntry")
                            {
                                writer.WriteAttributes(reader, true);
                            }
                            else
                            {
                                string fieldValue;
                                reader.MoveToFirstAttribute();

                                var fidId = 0;
                                bMrnDecoding = false;
                                do
                                {
                                    // display attribute information
                                    var attributeName = reader.Name.Trim();
                                    var attributeVal  = reader.Value.Trim();
                                    var fieldName     = "Unknown";
                                    fieldValue = attributeVal;
                                    if (attributeName.ToLower() != "fieldid")
                                    {
                                        if (attributeName.ToLower() == "data")
                                        {
                                            if (FieldValueToString(
                                                    fidId,
                                                    attributeVal,
                                                    out var output,
                                                    out errorMsg,
                                                    fieldDictionary: fieldDictionary,
                                                    enumTypeDef: enumTypeDef))
                                            {
                                                fieldValue = output;
                                            }


                                            writer.WriteAttributeString("decodedData",
                                                                        fieldName == "Unknown" ? fieldValue : string.Empty);
                                            writer.WriteAttributeString(attributeName, attributeVal);

                                            if (fidId == 32480)         // Found total_size
                                            {
                                                bMrnDecoding        = false;
                                                totalSize           = 0;
                                                currentFragmentSize = 0;
                                                guidSize            = 0;
                                                mrnStrings          = new StringBuilder();
                                                guidstr             = string.Empty;
                                                if (fieldValue != "")
                                                {
                                                    totalSize = long.Parse(fieldValue);
                                                }
                                            }
                                            else if (fidId == 32641)     //Found fragment
                                            {
                                                mrnStrings.Append(fieldValue);
                                                currentFragmentSize = RdmDataConverter.StringToByteArray(fieldValue)
                                                                      .Length;
                                                guidSize += currentFragmentSize;
                                                if (totalSize > 0 && guidSize > 0 && (totalSize == guidSize))
                                                {
                                                    bMrnDecoding = true;
                                                }
                                            }
                                            else if (fidId == 4271 && fieldValue != string.Empty)     // Found GUID
                                            {
                                                guidstr = fieldValue;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        fidId = int.Parse(attributeVal);
                                        if (fieldDictionary.GetDictEntryById(fidId, out var rdmEntry))
                                        {
                                            fieldName = rdmEntry.Acronym;
                                        }

                                        writer.WriteAttributeString(attributeName, attributeVal);
                                        writer.WriteAttributeString("fieldName", fieldName);
                                    }
                                } while (reader.MoveToNextAttribute());
                            }
                        }


                        // Move reader pointer back to element
                        reader.MoveToElement();

                        if (reader.IsEmptyElement)
                        {
                            writer.WriteEndElement();
                        }


                        if (bMrnDecoding)
                        {
                            var pByteArray = RdmDataConverter.StringToByteArray(mrnStrings.ToString());

                            var jsonString = string.Empty;



                            using (var compressedStream = new MemoryStream(pByteArray))
                                using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
                                    using (var resultStream = new MemoryStream())
                                    {
                                        zipStream.CopyTo(resultStream);
                                        jsonString = Encoding.UTF8.GetString(resultStream.ToArray());
                                    }

                            jsonToken = JToken.Parse(jsonString);
                            if (verbose)
                            {
                                Console.WriteLine($"{jsonToken.ToString(Newtonsoft.Json.Formatting.Indented)}");
                            }

                            mrnStrings.Clear();
                            guidSize     = 0;
                            totalSize    = 0;
                            bMrnDecoding = false;
                        }
                    }
                    break;

                    case XmlNodeType.Comment:
                        writer.WriteComment(reader.Value);
                        break;

                    case XmlNodeType.EndElement:
                        writer.WriteEndElement();
                        if (reader.Name == "updateMsg" && jsonToken != null)
                        {
                            writer.WriteWhitespace("\r\n");
                            writer.WriteComment($"{jsonToken.ToString(Newtonsoft.Json.Formatting.Indented)}");
                            jsonToken = null;
                        }
                        break;

                    case XmlNodeType.Text:
                        writer.WriteString(reader.Value);
                        break;

                    case XmlNodeType.Whitespace:
                        writer.WriteWhitespace(reader.Value);
                        break;
                    }

                    writer.Flush();
                }
            }
            catch (Exception ex)
            {
                errorMsg = $"Error when decoding XML file\r\n{ex.Message}\r\n{ex.StackTrace}";
                return(false);
            }

            return(true);
        }
            public static async Task <Tuple <bool, MrnMsgList, string> > DecodeMrnDataAsync(XmlFragmentList fragmentList)
            {
                var mrnList  = new MrnMsgList();
                var errorMsg = string.Empty;
                await Task.Run(() =>
                {
                    try
                    {
                        List <XElement> elements;
                        foreach (var fragment in fragmentList.Fragments)
                        {
                            //process Attribues
                            if (string.IsNullOrEmpty(fragment.RawXmlData))
                            {
                                continue;
                            }

                            var list = XmlHelper.GetElement(fragment.RawXmlData, fragment.RdmMessageType);
                            elements = list.ToList();
                            if (!elements.Any())
                            {
                                continue;
                            }


                            var mrndata = new MrnMsg(StringtoType(fragment.RdmMessageType));

                            if (elements.First().HasAttributes)
                            {
                                var elementAttribs = new Dictionary <string, string>();
                                foreach (var attrib in elements.First().Attributes())
                                {
                                    elementAttribs.Add(attrib.Name.LocalName, attrib.Value);
                                }

                                // The following check is for EMA Java as it use domainType News_TEXT_ANALYTICS rather than RSSL_DMT_NEWS_TEXT_ANALYTICS as C++ and .NET
                                mrndata.DomainType = elementAttribs["domainType"] == "NEWS_TEXT_ANALYTICS"
                                    ? "RSSL_DMT_NEWS_TEXT_ANALYTICS"
                                    : elementAttribs["domainType"];
                                mrndata.SetFragmentAttribs(elementAttribs);
                            }

                            //process Key
                            var keys       = XmlHelper.GetElement(fragment.RawXmlData, "key");
                            var enumerable = keys.ToList();
                            if (enumerable.Any())
                            {
                                mrndata.ContainsKey = true;
                                var keysAttrib      = new Dictionary <string, string>();
                                foreach (var key in enumerable.First().Attributes())
                                {
                                    if (key.Name == "name")
                                    {
                                        mrndata.RequestKeyName = key.Value;
                                    }
                                    keysAttrib.Add(key.Name.LocalName, key.Value);
                                }

                                mrndata.SetKeyAtrribs(keysAttrib);
                            }

                            if (mrndata.DomainType == "RSSL_DMT_NEWS_TEXT_ANALYTICS" ||
                                mrndata.DomainType == "RSSL_DMT_MARKET_PRICE")
                            {
                                //process fieldList
                                var fields    = XmlHelper.GetElement(fragment.RawXmlData, "fieldEntry");
                                var xElements = fields.ToList();
                                if (xElements.Any())
                                {
                                    mrndata.ContainsFieldList = true;
                                    var fieldEntrys           = new Dictionary <int, string>();
                                    foreach (var elem in xElements)
                                    {
                                        var fid = from id in elem.Attributes()
                                                  where id.Name == "fieldId"
                                                  select id.Value;
                                        var fidId  = int.Parse(fid.First().Trim());
                                        var fValue = from fieldvalue in elem.Attributes()
                                                     where fieldvalue.Name == "data"
                                                     select fieldvalue.Value;
                                        var pBuffer = new string(fValue.First().ToCharArray()
                                                                 .Where(c => !char.IsWhiteSpace(c))
                                                                 .ToArray());
                                        //4271 is GUID.
                                        fieldEntrys.Add(fidId,
                                                        fidId == 4271
                                                ? Encoding.UTF8.GetString(
                                                            RdmDataConverter.StringToByteArray(pBuffer.Trim()))
                                                : pBuffer);
                                    }

                                    mrndata.SetFieldList(fieldEntrys);
                                }
                            }

                            mrndata.FragmentNumber = fragment.FragmentNumber;
                            mrnList.Add(fragment.FragmentNumber, mrndata);
                        }
                    }
                    catch (Exception ex)
                    {
                        errorMsg = $"DecodeMrnData error {ex.Message}\r\n{ex.StackTrace}";
                        return(new Tuple <bool, MrnMsgList, string>(false, mrnList, errorMsg));
                    }

                    return(new Tuple <bool, MrnMsgList, string>(true, mrnList, errorMsg));
                });

                return(new Tuple <bool, MrnMsgList, string>(true, mrnList, errorMsg));
            }
            public static async Task <Tuple <bool, string, string> > UnpackMrnDataAsync(IEnumerable <MrnMsg> list)
            {
                var jsonString = string.Empty;
                var errorMsg   = string.Empty;
                var bSuccess   = true;
                await Task.Run(async() =>
                {
                    try
                    {
                        var totSize        = 0;
                        var bytesArraySize = 0;
                        jsonString         = string.Empty;
                        errorMsg           = string.Empty;

                        var bytesStr = new StringBuilder();
                        foreach (var data in list)
                        {
                            if (!data.ContainsFieldList)
                            {
                                continue;
                            }
                            if (data.GetFieldList().ContainsKey(32480))
                            {
                                totSize = RdmDataConverter.HexStringToInt(data.GetFieldList()[32480]) ?? 0;
                            }

                            if (data.GetFieldList().ContainsKey(32641))
                            {
                                bytesStr.Append(RdmDataConverter.TraceStringToString(data.GetFieldList()[32641]));
                            }
                        }

                        var pByteArray = RdmDataConverter.StringToByteArray(bytesStr.ToString());
                        bytesArraySize = pByteArray.Length;
                        if (bytesArraySize != totSize)
                        {
                            errorMsg =
                                $"Detect incomplete XML Fragments list\r\nCurrent XML Fragments size is {bytesArraySize} while Total Size is {totSize}\r\n";
                            errorMsg += "Data might be corrupted or missing some fragment";
                            bSuccess  = false;
                            return(new Tuple <bool, string, string>(bSuccess, jsonString, errorMsg));
                        }

                        using (var compressedStream = new MemoryStream(pByteArray))
                            using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
                                using (var resultStream = new MemoryStream())
                                {
                                    await zipStream.CopyToAsync(resultStream).ConfigureAwait(false);
                                    jsonString = Encoding.UTF8.GetString(resultStream.ToArray());
                                }

                        return(new Tuple <bool, string, string>(bSuccess, jsonString, errorMsg));
                    }
                    catch (Exception ex)
                    {
                        errorMsg   = $"Exception Error: {ex.Message}";
                        jsonString = "";
                        bSuccess   = false;
                        return(new Tuple <bool, string, string>(bSuccess, jsonString, errorMsg));
                    }
                });

                return(new Tuple <bool, string, string>(bSuccess, jsonString, errorMsg));
            }