Esempio n. 1
0
 public GossipTimestampAsk(Guid guid, IList <Timestamps> timestamps, IList <string> queryNames, int level, ValueContact contact)
     : base(guid)
 {
     Timestamps = timestamps;
     QueryNames = queryNames;
     Level      = level;
     Contact    = contact;
 }
Esempio n. 2
0
 public TimestampsInfo(ValueContact contact, int level, IList <Timestamps> myTimestamps,
                       IList <Timestamps> hisTimestamps, IList <string> myQueryNames, IList <string> hisQueryNames,
                       ValueTime hisSendTime, ValueTime myReceiveTime)
     : this(contact, level, myTimestamps, hisTimestamps, myQueryNames, hisQueryNames)
 {
     HisSendTime   = hisSendTime;
     MyReceiveTime = myReceiveTime;
 }
Esempio n. 3
0
 public void Deconstruct(out ValueContact contact, out int level, out IList <Timestamps> myTimestamps,
                         out IList <Timestamps> hisTimestamps, out IList <string> myQueryNames, out IList <string> hisQueryNames)
 {
     contact       = Contact;
     level         = Level;
     myTimestamps  = MyTimestamps;
     hisTimestamps = HisTimestamps;
     myQueryNames  = MyQueryNames;
     hisQueryNames = HisQueryNames;
 }
Esempio n. 4
0
 public TimestampsInfo(ValueContact contact, int level, IList <Timestamps> myTimestamps,
                       IList <Timestamps> hisTimestamps, IList <string> myQueryNames, IList <string> hisQueryNames,
                       int attempts = 1)
 {
     Contact       = contact;
     Level         = level;
     MyTimestamps  = myTimestamps;
     HisTimestamps = hisTimestamps;
     MyQueryNames  = myQueryNames;
     HisQueryNames = hisQueryNames;
     Attempts      = attempts;
 }
Esempio n. 5
0
        public bool TryGetContact(ZMI zmi, out ValueContact contact, out int level)
        {
            contact = null;

            if (!zmi.Attributes.TryGetValue("level", out var attrLevel))
            {
                throw new ArgumentException($"Could not find `level` in given zmi {zmi}");
            }

            var maxLevel = (int)((ValueInt)attrLevel).Value.Ref;

            level = GetZoneIndex(maxLevel);

            var currentZmi = zmi;

            while (currentZmi.Attributes.TryGetValue("level", out var currLevel) &&
                   ((ValueInt)currLevel).Value.Ref != level)
            {
                currentZmi = currentZmi.Father;
            }

            var currFather             = currentZmi.Father;
            var otherSons              = currFather.Sons.Where(z => !Equals(z, currentZmi)).ToList();
            var randomOtherSonsIndexes = Enumerable.Range(0, otherSons.Count).ToList();

            randomOtherSonsIndexes.Shuffle();

            foreach (var sibling in randomOtherSonsIndexes.Select(i => otherSons[i]))
            {
                IList <Value> contacts;
                if (!sibling.Attributes.TryGetValue("contacts", out var contactsAttr) ||
                    contactsAttr.IsNull || (contacts = ((ValueSet)contactsAttr).ToList()).Count == 0)
                {
                    continue;
                }

                int randomIndex;
                lock (Random)
                    randomIndex = Random.Next(contacts.Count);
                contact = (ValueContact)contacts[randomIndex]
                          .ConvertTo(AttributeTypePrimitive.Contact);

                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        public static bool TryCreateValue(string typeString, string valueString, out Value value)
        {
            value = null;

            var isNull = valueString.Equals("NULL") || valueString.Equals("null") || valueString.Equals("");

            switch (typeString)
            {
            case "integer":
                if (isNull)
                {
                    value = new ValueInt(null);
                    return(true);
                }
                if (!long.TryParse(valueString, out var intResult))
                {
                    return(false);
                }
                value = new ValueInt(intResult);
                return(true);

            case "string":
                if (isNull)
                {
                    value = new ValueString(null);
                    return(true);
                }
                value = new ValueString(valueString.Trim('\"'));
                return(true);

            case "time":
                if (isNull)
                {
                    value = new ValueTime(null as RefStruct <long>);
                    return(true);
                }
                value = new ValueTime(valueString);
                return(true);

            case "double":
                if (isNull)
                {
                    value = new ValueDouble(null);
                    return(true);
                }
                if (!double.TryParse(valueString, NumberStyles.Any, CultureInfo.InvariantCulture, out var doubleResult))
                {
                    return(false);
                }
                value = new ValueDouble(doubleResult);
                return(true);

            case "boolean":
                if (isNull)
                {
                    value = new ValueBoolean(null);
                    return(true);
                }
                if (!bool.TryParse(valueString, out var booleanResult))
                {
                    return(false);
                }
                value = new ValueBoolean(booleanResult);
                return(true);

            case "duration":
                if (isNull)
                {
                    value = new ValueDuration(null as RefStruct <long>);
                    return(true);
                }
                value = new ValueDuration(valueString);
                return(true);

            case "contact":
                if (isNull)
                {
                    value = new ValueContact(null, null);
                    return(true);
                }

                var split = valueString.Split(' ');
                if (split.Length != 2 && split.Length != 3)
                {
                    return(false);
                }
                if (!IPAddress.TryParse(split[1], out var address))
                {
                    return(false);
                }
                var port = 555;
                if (split.Length == 3 && !int.TryParse(split[2], out port))
                {
                    return(false);
                }
                if (!split[0].StartsWith("/"))
                {
                    split[0] = '/' + split[0];
                }
                value = new ValueContact(new PathName(split[0]), address, port);
                return(true);
            }

            var splitByOf = typeString.Split(" of ", 2);

            if (splitByOf.Length != 2)
            {
                return(false);
            }
            var splitBySpace = splitByOf[1].Split(' ', 2);

            if (splitBySpace.Length != 2)
            {
                return(false);
            }

            if (!int.TryParse(splitBySpace[0], out var count))
            {
                return(false);
            }

            // based on StackOverflow answer: https://stackoverflow.com/a/3147901
            var collectionStrings = Regex.Split(valueString.Trim('{', '}', '[', ']'), ",(?=(?:[^']*'[^']*')*[^']*$)")
                                    .Select(s => s.Trim(' ')).ToList();

            if (collectionStrings.Count != count)
            {
                if (count != 0 && (collectionStrings.Count != 1 || !string.IsNullOrEmpty(collectionStrings[0])))
                {
                    return(false);
                }
                collectionStrings = new List <string>(count);
            }

            var elements          = new List <Value>(count);
            var elementTypeString = splitBySpace[1];

            foreach (var element in collectionStrings)
            {
                if (!TryCreateValue(elementTypeString, element, out var elementVal))
                {
                    return(false);
                }
                elements.Add(elementVal);
            }

            if (!TryGetPrimitiveAttributeType(elementTypeString, out var elementType))
            {
                return(false);
            }

            switch (splitByOf[0])
            {
            case "set":
                value = new ValueSet(elements.ToHashSet(), elementType);
                return(true);

            case "list":
                value = new ValueList(elements, elementType);
                return(true);
            }

            return(false);
        }