Пример #1
0
        internal override void SetValue(byte[] value)
        {
            if (VarLength && Length < 0)
            {
                Length = value.Length * 2;
                if (IsOdd && Length > 0)
                {
                    Length = Length - 1;
                }
            }
            if (Length < 0)
            {
                throw new MessageProcessorException("The Length property is not yet set (still negative).");
            }
            if (value.Length > (Length + 1) / 2)
            {
                throw new MessageProcessorException("The length of the assigned value is greater than defined length (Length property).");
            }

            String str = MessageUtility.HexToString(value);

            foreach (char ch in str)
            {
                if (ch < '0' || ch > '9')
                {
                    throw new MessageProcessorException("Invalid assigned value, cannot converted to numeric.");
                }
            }
            if (str.Length < Length)
            {
                str = str.PadLeft(Length, '0');
            }
            else if (str.Length > Length) // str.Length == Length + 1 //odd effect
            {
                if (VarLength)
                {
                    str = str.Substring(0, str.Length - 1);
                }
                else
                {
                    str = str.Substring(1);
                }
            }

            if (value.Length * 2 < Length)
            {
                byte[] val = new byte[(Length + 1) / 2 - value.Length]; //padded zero bytes
                value = val.Concat(value).ToArray();
            }
            _bytes = value;

            _string = str;
            if (FracDigits > 0)
            {
                _string = _string.Insert(_string.Length - FracDigits, ".");
            }

            _decimal = MessageUtility.HexToDecimal(value, FracDigits);
        }
Пример #2
0
 internal override void SetValue(decimal value)
 {
     SetValue(MessageUtility.DecimalToAsciiArray(value, FracDigits));
     _decimal = value;
     if (FracDigits > 0)
     {
         _string.Insert(_string.Length - FracDigits, ".");
     }
 }
Пример #3
0
        internal override void SetValue(String value)
        {
            decimal?val = MessageUtility.StringToDecimal(value);

            if (val == null)
            {
                throw new MessageProcessorException("Invalid string, cannot converted to numeric.");
            }
            SetValue(MessageUtility.DecimalToAsciiArray((decimal)val, FracDigits));
        }
Пример #4
0
        private void CheckMessageLength()
        {
            byte[] lenHdr = new byte[_msgCfg.LengthHeader];
            Array.Copy(_bytes, 0, lenHdr, 0, _msgCfg.LengthHeader);
            int length = (int)MessageUtility.BytesToInt(lenHdr);

            if (_bytes.Length - _msgCfg.LengthHeader != length)
            {
                throw new MessageParserException("Incompatible length header value with the actual message length.");
            }
            _token += _msgCfg.LengthHeader;
        }
Пример #5
0
 internal override void SetValue(decimal value)
 {
     SetValue(MessageUtility.DecimalToHex(value, FracDigits));
     if (_decimal == null)
     {
         _decimal = value;
         if (FracDigits > 0)
         {
             _string = _string.Insert(_string.Length - FracDigits, ".");
         }
     }
 }
Пример #6
0
 public MessageTypeHeader(byte[] bytes)
 {
     if (bytes == null || bytes.Length != 2)
     {
         throw new MessageProcessorException("Invalid value for MessageTypeHeader.");
     }
     _bytes               = bytes;
     StringValue          = MessageUtility.HexToString(_bytes);
     this.MessageVersion  = (MessageVersion)(_bytes[0] >> 4);
     this.MessageClass    = (MessageClass)(_bytes[0] & 0x0f);
     this.MessageFunction = (MessageFunction)(_bytes[1] >> 4);
     this.MessageOrigin   = (MessageOrigin)(_bytes[1] & 0x0f);
 }
Пример #7
0
        private static void PrintModel(Object model, StringBuilder str, String indent, String[] printableClassPrefixes)
        {
            String newLine = Environment.NewLine;

            str.Append(model.GetType().FullName).Append("()").Append(newLine);
            str.Append(indent).Append("{").Append(newLine);
            PropertyInfo[] Properties = model.GetType().GetProperties();
            foreach (PropertyInfo property in Properties)
            {
                Object value = property.CanRead ? property.GetValue(model, null) : "null";
                str.Append(indent).Append(property.Name).Append(" = ");
                if (value != null)
                {
                    if (property.PropertyType.IsEnum)
                    {
                        str.Append(property.PropertyType.Name).Append(".")
                        .Append(Enum.Format(property.PropertyType, value, "G"));
                    }
                    else if (IsPrintableClass(value.GetType(), printableClassPrefixes))
                    {
                        PrintModel(value, str, indent + "\t", printableClassPrefixes);
                    }
                    else if (value is byte[])
                    {
                        byte[] val = (byte[])value;
                        str.Append(MessageUtility.HexToReadableString(val));
                    }
                    else if (value is string[])
                    {
                        string[] val = (string[])value;
                        str.Append("[");
                        if (val.Length > 0)
                        {
                            str.Append(String.Join(", ", val));
                        }
                        str.Append("]");
                    }
                    else
                    {
                        str.Append(value.ToString());
                    }
                }
                else
                {
                    str.Append("null");
                }
                str.Append(newLine);
            }
            str.Append(indent).Append("}");//.Append(newLine);
        }
Пример #8
0
        private void ParseFields()
        {
            foreach (KeyValuePair <int, MessageFieldConfig> kvp in _msgCfg.Fields)
            {
                MessageFieldConfig cfg = kvp.Value;

                /* Because message fields configuration has been ordered based on their sequence number. The statement
                 * below never misses secondary or tertiary BitMap (secondary and/or tertiary BitMap have been included
                 * in _bitMap collection if cfg.Seq must be identified by those BitMaps) */
                if (!_bitMap.IsBitOn(cfg.Seq))
                {
                    continue;
                }

                int length;
                if (cfg.Length >= 0)
                {
                    length = cfg.Length;
                }
                else
                {
                    int hdrLen = cfg.LengthHeader;
                    CheckToken(hdrLen, null, cfg);
                    byte[] hdr = new byte[hdrLen];
                    Array.Copy(_bytes, _token, hdr, 0, hdrLen);
                    _token += hdrLen;

                    length = (int)MessageUtility.HexNDigitsToInt(hdr);
                }

                MessageField msgFld      = (MessageField)Activator.CreateInstance(cfg.FieldType);
                int          bytesLength = msgFld.GetBytesLengthFromActualLength(length);
                CheckToken(bytesLength, null, cfg);
                byte[] bytesVal = new byte[bytesLength];
                Array.Copy(_bytes, _token, bytesVal, 0, bytesLength);
                _token += bytesLength;

                msgFld.Length    = length;
                msgFld.VarLength = (cfg.Length < 0);
                msgFld.SetValue(bytesVal);
                _struct.Fields.Add(cfg.Seq, msgFld);

                if (msgFld is MessageBitMap)
                {
                    ((MessageBitMap)msgFld).FieldSeq = cfg.Seq;
                    _bitMap.Add((MessageBitMap)msgFld);
                }
            }
        }
Пример #9
0
        internal override void SetValue(byte[] value)
        {
            if (VarLength && Length < 0)
            {
                Length = value.Length * 2;
                if (IsOdd && Length > 0)
                {
                    Length = Length - 1;
                }
            }
            if (Length < 0)
            {
                throw new MessageProcessorException("The Length property is not yet set (still negative).");
            }
            if (value.Length > (Length + 1) / 2)
            {
                throw new MessageProcessorException("The length of the assigned value is greater than defined length (Length property).");
            }

            String str = MessageUtility.HexToString(value);

            if (str.Length < Length)
            {
                str = str.PadLeft(Length, '0');
            }
            else if (str.Length > Length) // str.Length == Length + 1 //odd effect
            {
                if (VarLength)
                {
                    str = str.Substring(0, str.Length - 1);            //left aligned
                }
                else
                {
                    str = str.Substring(1);  //right aligned
                }
            }

            if (value.Length * 2 < Length)
            {
                byte[] val = new byte[(Length + 1) / 2 - value.Length]; //padded zero bytes
                value = val.Concat(value).ToArray();
            }

            _bytes   = value;
            _string  = str;
            _decimal = null;
        }
Пример #10
0
        private byte[] Receive()
        {
            if (MessageStream == null)
            {
                return(null);
            }
            int readLength = config.MaskConfig.MinBytesCountToCheck;

            byte[] checkData = new byte[readLength];
            int    offset = 0, numberReadData = 0;

            do
            {
                numberReadData = MessageStream.Receive(checkData, offset, readLength);
                offset        += numberReadData;
                readLength    -= numberReadData;
            } while (readLength > 0 && numberReadData > 0);

            config.MessageToModelConfig cfg = config.MessageConfigs.GetQulifiedMessageToModel(checkData);
            MessageToModelConfig = cfg;
            if (cfg == null)
            {
                throw new MessageProcessorException("Unrecognized message. Check message-to-model elements in configuration file."
                                                    + Environment.NewLine
                                                    + readLength + " first bytes of message: " + MessageUtility.HexToReadableString(checkData));
            }

            byte[] lengthHeader             = new byte[cfg.ModelCfg.MessageCfg.LengthHeader];
            Array.Copy(checkData, lengthHeader, lengthHeader.Length);
            int messageLength = (int)MessageUtility.BytesToInt(lengthHeader);

            messageLength += lengthHeader.Length;

            byte[] data = new byte[messageLength];
            Array.Copy(checkData, data, checkData.Length);
            offset     = checkData.Length;
            readLength = messageLength - checkData.Length;
            do
            {
                numberReadData = MessageStream.Receive(data, offset, readLength);
                offset        += numberReadData;
                readLength    -= numberReadData;
            } while (readLength > 0 && numberReadData > 0);
            return(data);
        }
Пример #11
0
        private void WriteMessageProcessException(Exception e, String log)
        {
            StringBuilder sbLog = new StringBuilder(log);
            String        nl    = System.Environment.NewLine;

            if (e is MessageParserException)
            {
                sbLog.Append(" Message:").Append(nl);
                String message = MessageUtility.HexToReadableString(ReceivedMessage, 80);
                sbLog.Append(message);
            }
            else if (e is MessageCompilerException)
            {
                sbLog.Append(" Model:").Append(nl);
                sbLog.Append(Util.GetReadableStringFromModel(SentModel));
            }
            Logger.GetInstance().Write(e, sbLog.ToString());
        }
Пример #12
0
 internal override void SetValue(byte[] value)
 {
     for (int i = 0; i < value.Length; i++)
     {
         if (value[i] < 0x30 || value[i] > 0x39)
         {
             throw new MessageProcessorException("Invalid assigned value, cannot converted to numeric");
         }
     }
     if (VarLength && Length < 0)
     {
         Length = value.Length;
     }
     if (Length < 0)
     {
         throw new MessageProcessorException("The Length property is not yet set (still negative).");
     }
     else if (value.Length > Length)
     {
         throw new MessageProcessorException("The length of the assigned value is greater than defined length (Length property).");
     }
     else if (value.Length < Length)
     {
         byte[] val = new byte[Length - value.Length];
         for (int i = 0; i < val.Length; i++)
         {
             val[i] = (byte)0x30;                                  //'0'
         }
         value = val.Concat(value).ToArray();
     }
     _bytes  = value;
     _string = MessageUtility.AsciiArrayToString(value);
     if (FracDigits > 0)
     {
         _string = _string.Insert(_string.Length - FracDigits, ".");
     }
     _decimal = decimal.Parse(_string);
 }
Пример #13
0
        private MessageToModelConfig GetMessageToModelConfig()
        {
            MessageToModelConfig cfg = (_workerThread != null && _workerThread.MessageToModelConfig != null)
                ? _workerThread.MessageToModelConfig
                : MessageConfigs.GetQulifiedMessageToModel(_bytes);

            if (cfg == null)
            {
                byte[] bytes2 = new byte[config.MaskConfig.MinBytesCountToCheck < _bytes.Length
                    ? config.MaskConfig.MinBytesCountToCheck : _bytes.Length];
                Array.Copy(_bytes, bytes2, bytes2.Length);
                throw new MessageParserException("No matching config for this message. Check message-to-model elements in configuration file."
                                                 + Environment.NewLine
                                                 + bytes2.Length + " first bytes of message: " + MessageUtility.HexToReadableString(bytes2));
            }
            return(cfg);
        }
Пример #14
0
        private Object ParseTlv(Type propType, TlvConfig cfg, String tagName, int lengthBytes, Type tlvType,
                                byte[] bytesValue, String locMsg, bool isAlwaysOneTag, ref int tlvLength)
        {
            int tagLength = (cfg != null && cfg.TagsCount > 0) ? cfg.GetTag(0).Name.Length
                : (tagName.Length > 0 ? tagName.Length : 2);

            if (cfg != null && cfg.LengthBytes > 0)
            {
                lengthBytes = cfg.LengthBytes;
            }
            if (cfg != null && cfg.ClassType != null)
            {
                tlvType = cfg.ClassType;
            }

            IDictionary <String, Object> tagValMap = new Dictionary <String, Object>();
            int i = 0;

            byte[] bytes;
            while (i < bytesValue.Length)
            {
                CheckTlvToken(i, tagLength, bytesValue, locMsg);
                bytes = new byte[tagLength];
                Array.Copy(bytesValue, i, bytes, 0, tagLength);
                i += tagLength;
                String tag = MessageUtility.AsciiArrayToString(bytes);
                if (tag == null)
                {
                    throw new MessageParserException("Invalid tag value for a tlv field. Check configuration " + locMsg);
                }

                CheckTlvToken(i, lengthBytes, bytesValue, locMsg);
                bytes = new byte[lengthBytes];
                Array.Copy(bytesValue, i, bytes, 0, lengthBytes);
                i += lengthBytes;
                int len = MessageUtility.AsciiArrayToInt(bytes);
                if (len < 0)
                {
                    throw new MessageParserException("Invalid length value for a tlv field. Check configuration " + locMsg);
                }

                if (len > 0)
                {
                    CheckTlvToken(i, len, bytesValue, locMsg);
                    bytes = new byte[len];
                    Array.Copy(bytesValue, i, bytes, 0, len);
                    i += len;

                    TlvTagConfig tagCfg = cfg != null?cfg.GetTag(tag) : null;

                    if (tagCfg != null && tagCfg.BitContent != null)
                    {
                        tagValMap[tag] = ParseBitContent(tagCfg.BitContent, bytes);
                    }
                    else
                    {
                        String value = System.Text.Encoding.ASCII.GetString(bytes);
                        if (tagCfg != null && tagCfg.Splitter != null)
                        {
                            tagValMap[tag] = value.Split(new String[] { tagCfg.Splitter }, StringSplitOptions.None);
                        }
                        else
                        {
                            tagValMap[tag] = value;
                        }
                    }
                }

                if (isAlwaysOneTag)
                {
                    break;
                }
            }

            tlvLength = i;
            if (tagValMap.Count <= 0)
            {
                return(null);
            }

            String convertFailedMsg = "Cannot convert a tlv field into the mapped property. Check " + locMsg;

            if (tlvType != null && propType.IsAssignableFrom(tlvType))
            {
                Object propValue = Activator.CreateInstance(tlvType);
                foreach (KeyValuePair <String, Object> kvp in tagValMap)
                {
                    PropertyInfo property = tlvType.GetProperty(kvp.Key);
                    if (property == null)
                    {
                        throw new MessageParserException(convertFailedMsg);
                    }

                    Object propValue2 = kvp.Value;
                    if (propValue2 != null)
                    {
                        propValue2 = Util.GetAssignableValue(property.PropertyType, kvp.Value);
                        if (propValue2 == null)
                        {
                            throw new MessageParserException(convertFailedMsg);
                        }
                    }
                    property.SetValue(propValue, propValue2, null);
                }
                return(propValue);
            }
            else
            {
                if (tagValMap.Count == 1)
                {
                    foreach (Object value in tagValMap.Values)
                    {
                        if (value == null)
                        {
                            return(null);
                        }
                        Object val = Util.GetAssignableValue(propType, value);
                        if (val != null)
                        {
                            return(val);
                        }
                    }
                }
                if (!propType.IsAssignableFrom(tagValMap.GetType()))
                {
                    Object propValue = Activator.CreateInstance(propType);
                    foreach (KeyValuePair <String, Object> kvp in tagValMap)
                    {
                        PropertyInfo property = propType.GetProperty(kvp.Key);
                        if (property == null)
                        {
                            throw new MessageParserException(convertFailedMsg);
                        }

                        Object propValue2 = kvp.Value;
                        if (propValue2 != null)
                        {
                            propValue2 = Util.GetAssignableValue(property.PropertyType, kvp.Value);
                            if (propValue2 == null)
                            {
                                throw new MessageParserException(convertFailedMsg);
                            }
                        }
                        property.SetValue(propValue, propValue2, null);
                    }
                    return(propValue);
                }
            }

            return(tagValMap);
        }
Пример #15
0
        private byte[] CompileBitContent(BitContentConfig cfg, Object obj)
        {
            if (cfg.ClassType != obj.GetType())
            {
                throw new MessageCompilerException("Cannot compile a BitContent field. Not match target type of object. The passed "
                                                   + "object has type of " + obj.GetType().FullName + " whereas the BitContent requires type of "
                                                   + cfg.ClassType.FullName + ". Please check configuration for BitContent (id=" + cfg.Id);
            }

            String[] locMsgs = new String[] { "BitContent element (id=" + cfg.Id + ") and field (name=",
                                              null, ")" };
            List <byte> bytes = new List <byte>();

            foreach (BitContentFieldConfig ccfg in cfg.Fields)
            {
                Object propVal = ccfg.PropertyInfo.GetValue(obj, null);
                if (propVal == null)
                {
                    if (!ccfg.IsOptional)
                    {
                        for (int i = 0; i < ccfg.Length; i++)
                        {
                            bytes.Add(ccfg.NullChar);
                        }
                    }
                }
                else if (propVal.GetType() == typeof(byte[]))
                {
                    bytes.AddRange((byte[])propVal);
                }
                else if (ccfg.Tlv != null || !String.IsNullOrEmpty(ccfg.TlvTagName))
                {
                    locMsgs[1] = ccfg.PropertyInfo.Name;
                    byte[] val = CompileTlv(ccfg.Tlv, ccfg.TlvTagName, ccfg.TlvLengthBytes, propVal,
                                            String.Join("", locMsgs));
                    bytes.AddRange(val);
                }
                else
                {
                    byte[] value = MessageUtility.StringToAsciiArray(propVal.ToString());
                    if (value.Length > ccfg.Length)
                    {
                        locMsgs[1] = ccfg.PropertyInfo.Name;
                        throw new MessageCompilerException(
                                  "Cannot compile a BitContent field. The length of field's value is too big more than "
                                  + ccfg.Length + " chars. Check configuration " + String.Join("", locMsgs));
                    }
                    int i = value.Length;
                    if (ccfg.Align == "right")
                    {
                        for (; i < ccfg.Length; i++)
                        {
                            bytes.Add(ccfg.PadChar);
                        }
                    }
                    bytes.AddRange(value);
                    if (ccfg.Align != "right")
                    {
                        for (; i < ccfg.Length; i++)
                        {
                            bytes.Add(ccfg.PadChar);
                        }
                    }
                }
            }
            return(bytes.ToArray());
        }
Пример #16
0
        public byte[] GetAllBytes()
        {
            ICollection <int> bitOnList = _fields.Keys;

            _bitMap.SetBitOn(bitOnList);

            //Don't include null BitMap (no bit on).
            List <MessageBitMap> bitMapList = _bitMap.ToList();

            for (int x = bitMapList.Count - 1; x >= 1; x--) //Excludes primary BitMap when checking
            {
                if (bitMapList[x].IsNull)
                {
                    _bitMap.SetBit(bitMapList[x].FieldSeq, false);
                }
            }

            int length = 0;

            foreach (MessageElement hdr in _headers)
            {
                length += hdr.Length;
            }
            foreach (CompiledMessageField fld in _fields.Values)
            {
                if (fld.Content is MessageBitMap && ((MessageBitMap)fld.Content).IsNull)
                {
                    continue;                                                                      //null BitMap not included
                }
                length += fld.Length;
            }

            byte[] bytes  = new byte[LengthHeader + length];
            byte[] lenHdr = MessageUtility.IntToBytes((ulong)length, LengthHeader);
            Array.Copy(lenHdr, 0, bytes, 0, LengthHeader);

            int i = LengthHeader;

            foreach (MessageElement hdr in _headers)
            {
                if (hdr.BytesValue == null)
                {
                    continue;
                }
                Array.Copy(hdr.BytesValue, 0, bytes, i, hdr.Length);
                i += hdr.Length;
            }

            //int[] bits = bitOnList.ToArray();
            //Array.Sort(bits); //Has been ordered by XmlConfigParser
            ICollection <int> bits = bitOnList;

            foreach (int j in bits)
            {
                CompiledMessageField fld = _fields[j];
                if (fld.Content.BytesValue == null)
                {
                    continue;
                }
                if (fld.Content is MessageBitMap && ((MessageBitMap)fld.Content).IsNull)
                {
                    continue;
                }
                if (fld.Header != null)
                {
                    Array.Copy(fld.Header, 0, bytes, i, fld.HeaderLength);
                }
                Array.Copy(fld.Content.BytesValue, 0, bytes, i + fld.HeaderLength, fld.Length - fld.HeaderLength);
                i += fld.Length;
            }
            return(bytes);
        }
Пример #17
0
        private byte[] CompileTlv(TlvConfig cfg, String tagName, int lengthBytes, Object obj, String locMsg)
        {
            int tagLength = (cfg != null && cfg.TagsCount > 0) ? cfg.GetTag(0).Name.Length
                : (tagName.Length > 0 ? tagName.Length : 2);

            if (cfg != null && cfg.LengthBytes > 0)
            {
                lengthBytes = cfg.LengthBytes;
            }

            if (String.IsNullOrEmpty(tagName) && (cfg == null || cfg.TagsCount <= 0))
            {
                throw new MessageCompilerException(
                          "Cannot compile a tlv field. It cannot determine the tag ID. Check configuration "
                          + locMsg);
            }

            IList <TlvTagConfig> tagCfgs = null;

            if (cfg != null)
            {
                tagCfgs = cfg.Tags;
            }
            if (tagCfgs == null)
            {
                tagCfgs = new List <TlvTagConfig>();
            }
            if (tagCfgs.Count <= 0)
            {
                TlvTagConfig tagCfg = new TlvTagConfig();
                tagCfg.Name = tagName;
                tagCfgs.Add(tagCfg);
            }

            IDictionary <String, Object> tagMapVal = new Dictionary <String, Object>();

            if (obj is IDictionary <String, Object> )
            {
                IDictionary <String, Object> map = (IDictionary <String, Object>)obj;
                foreach (TlvTagConfig tagCfg in tagCfgs)
                {
                    if (map.ContainsKey(tagCfg.Name))
                    {
                        tagMapVal[tagCfg.Name] = map[tagCfg.Name];
                    }
                }
            }
            else if (obj != null)
            {
                foreach (TlvTagConfig tagCfg in tagCfgs)
                {
                    PropertyInfo property = obj.GetType().GetProperty(tagCfg.Name);
                    if (property != null)
                    {
                        tagMapVal[tagCfg.Name] = property.GetValue(obj, null);
                    }
                }
            }
            if (tagMapVal.Count <= 0 && tagCfgs.Count == 1)
            {
                tagMapVal[tagCfgs[0].Name] = obj;
            }

            StringBuilder sb = new StringBuilder();

            foreach (TlvTagConfig tagCfg in tagCfgs)
            {
                sb.Append(tagCfg.Name);
                String len    = "0";
                String strVal = "";
                if (tagMapVal.ContainsKey(tagCfg.Name) && tagMapVal[tagCfg.Name] != null)
                {
                    Object value = tagMapVal[tagCfg.Name];
                    if (tagCfg.Splitter != null)
                    {
                        if (value is IEnumerable <Object> )
                        {
                            strVal = Util.Join((IEnumerable <Object>)value, tagCfg.Splitter);
                        }
                        else
                        {
                            throw new MessageCompilerException(
                                      "Cannot compile a tlv field. The passed value is not an array. Check configuration "
                                      + locMsg + " for tag " + tagCfg.Name);
                        }
                    }
                    else if (tagCfg.BitContent != null)
                    {
                        byte[] val = CompileBitContent(tagCfg.BitContent, value);
                        strVal = System.Text.Encoding.ASCII.GetString(val);
                    }
                    else
                    {
                        strVal = value.ToString();
                    }
                }

                len = strVal.Length.ToString();
                if (len.Length > lengthBytes)
                {
                    throw new MessageCompilerException(
                              "Cannot compile a tlv field. The length's value is too big more than " + lengthBytes
                              + " digits. Check configuration " + locMsg + " for tag " + tagCfg.Name);
                }
                else if (len.Length < lengthBytes)
                {
                    len = len.PadLeft(lengthBytes, '0');
                }
                sb.Append(len);
                sb.Append(strVal);
            }

            return(MessageUtility.StringToAsciiArray(sb.ToString()));
        }
Пример #18
0
 internal override void SetValue(String value)
 {
     SetValue(MessageUtility.StringToAsciiArray(value));
 }
Пример #19
0
        private void CompileFields()
        {
            String[] locMsgs = new String[] { "model element (id=" + (_modelCfg.Id == null?"":_modelCfg.Id) + ", class="
                                              + (_modelCfg.ClassType == null?"":_modelCfg.ClassType.FullName) + ") and property (name=", null, ")" };

            foreach (KeyValuePair <int, MessageFieldConfig> kvpFldCfg in _msgCfg.Fields)
            {
                MessageFieldConfig  fldCfg  = kvpFldCfg.Value;
                ModelPropertyConfig propCfg = _modelCfg.Properties.ContainsKey(fldCfg.Seq) ? _modelCfg.Properties[fldCfg.Seq] : null;
                MessageField        fld     = (MessageField)Activator.CreateInstance(fldCfg.FieldType);
                Object propVal = propCfg == null ? null : propCfg.PropertyInfo.GetValue(_model, null);
                byte[] fldVal  = null;

                if (fldCfg.FieldType != typeof(NullMessageField) && fldCfg.FieldType != typeof(MessageBitMap))
                {
                    if (/*propCfg==null ||*/ propVal == null)
                    {
                        if (_reqMsg != null && fldCfg.FromRequest)
                        {
                            IDictionary <int, MessageField> fields = _reqMsg.ParsedMessage.Fields;
                            fldVal = fields.ContainsKey(fldCfg.Seq) ? fields[fldCfg.Seq].BytesValue : null;
                        }
                    }
                    else if (propCfg.Tlv != null || !String.IsNullOrEmpty(propCfg.TlvTagName))
                    {
                        locMsgs[1] = propCfg.PropertyInfo.Name;
                        fldVal     = CompileTlv(propCfg.Tlv, propCfg.TlvTagName, propCfg.TlvLengthBytes, propVal,
                                                String.Join("", locMsgs));
                    }
                    else if (propCfg.BitContent != null)
                    {
                        fldVal = CompileBitContent(propCfg.BitContent, propVal);
                    }
                    else if (fldCfg.GetFieldBytesFunc != null)
                    {
                        fldVal = (byte[])fldCfg.GetFieldBytesFunc.DynamicInvoke(propVal);
                    }

                    byte[] header = null;
                    if (fldVal != null || (propVal != null && propCfg.SetValueFromProperty != null)) //propCfg != null ==> propVal != null
                    {
                        if (fldCfg.Length >= 0)
                        {
                            if (fldVal != null && fldVal.Length != fld.GetBytesLengthFromActualLength(fldCfg.Length))
                            {
                                throw new MessageCompilerException(
                                          "Incompatible length of bytes between compiled bytes length "
                                          + "and the defined one in configuration. Check the config for model (class="
                                          + _modelCfg.ClassType.FullName
                                          + ") and property (name=" + (propCfg != null ? propCfg.PropertyInfo.Name : "")
                                          + ") and also its mapped field in message (id="
                                          + _msgCfg.Id + ")");
                            }
                            fld.Length = fldCfg.Length;
                        }
                        if (propVal is NibbleList)
                        {
                            fld.IsOdd = ((NibbleList)propVal).IsOdd;
                        }
                        if (propCfg != null)
                        {
                            fld.FracDigits = propCfg.FracDigits;
                        }
                        fld.VarLength = (fldCfg.Length < 0);

                        if (fldVal != null)
                        {
                            fld.SetValue(fldVal);
                        }
                        else //if (propVal != null /* && propCfg != null */ && propCfg.SetValueFromProperty != null)
                        {
                            try
                            {
                                Type paramType = propCfg.SetValueFromProperty.GetParameters()[0].ParameterType;
                                propVal = Util.GetAssignableValue(paramType, propVal);
                                propCfg.SetValueFromProperty.Invoke(fld, new Object[] { propVal });
                            }
                            catch (Exception ex)
                            {
                                throw new MessageCompilerException(
                                          "Cannot convert a property value to the coresponding message field. "
                                          + "Check the config for model (class=" + _modelCfg.ClassType.FullName
                                          + ") and property (name=" + propCfg.PropertyInfo.Name
                                          + ") and also its mapped field in message (id="
                                          + _msgCfg.Id + ")", ex);
                            }
                        }

                        if (fldCfg.LengthHeader >= 0) //It must be fld.VarLength == true (See XmlConfigParser)
                        {
                            header = MessageUtility.IntToHex((ulong)fld.Length, fldCfg.LengthHeader);
                        }

                        CompiledMessageField cmf = new CompiledMessageField();
                        cmf.Header  = header;
                        cmf.Content = fld;
                        _struct.AddField(fldCfg.Seq, cmf);
                    }
                }
                else //field is NULL type or BitMap
                {
                    CompiledMessageField cmf = new CompiledMessageField();
                    cmf.Header  = null;
                    fld.Length  = fldCfg.Length;
                    cmf.Content = fld;
                    _struct.AddField(fldCfg.Seq, cmf);
                }
            }
        }
Пример #20
0
 internal override void SetValue(decimal value)
 {
     SetValue(MessageUtility.DecimalToAsciiArray(value, FracDigits));
 }
Пример #21
0
 internal override void SetValue(String value)
 {
     SetValue(MessageUtility.StringToHex(value));
 }