Ejemplo n.º 1
0
        private static IEnumerable <QueryResult> VisitProgram(ZMI zmi, QueryParser.ProgramContext context)
        {
            var toReturn = new QueryVisitor(zmi).Visit(context).ToList();

            if (toReturn.Any(maybe => maybe.Match(v => v.Name == null, () => false)))
            {
                throw new ArgumentException("All items in top-level SELECT must be aliased");
            }
            return(toReturn.Sequence().Match(list => list, () => new List <QueryResult>()));
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private static bool TryParseConfig(string pathToConfig, out ZMI zmi)
        {
            zmi = null;

            if (!File.Exists(pathToConfig))
            {
                Console.Error.WriteLine($"Could not find config file in path: {pathToConfig}");
                return(false);
            }

            using var file = File.OpenRead(pathToConfig);
            if (ZMIParser.TryParseZMI(new StreamReader(file), ref zmi))
            {
                return(true);
            }

            Console.Error.WriteLine($"Could not parse ZMI from file: {pathToConfig}");
            return(false);
        }
Ejemplo n.º 4
0
        public Table(ZMI zmi)
        {
            _columns    = zmi.Sons.SelectMany(z => z.Attributes).Select(e => e.Key.Name).Distinct().ToList();
            _headersMap = _columns.Select((s, i) => (s, i)).ToDictionary(tuple => tuple.s, tuple => tuple.i);

            foreach (var z in zmi.Sons)
            {
                var row = new Value[_columns.Count];
                for (var j = 0; j < row.Length; j++)
                {
                    row[j] = ValueNull.Instance;
                }
                foreach (var(key, value) in z.Attributes)
                {
                    row[GetColumnIndex(key.Name)] = value;
                }
                AppendRow(new TableRow(row));
            }
        }
Ejemplo n.º 5
0
        public static void ExecuteQueries(ZMI zmi, string query, long maxLevel = long.MaxValue, bool log = false)
        {
            if (!zmi.Sons.Any())
            {
                return;
            }

            if (maxLevel < 0)
            {
                return;
            }

            foreach (var son in zmi.Sons)
            {
                ExecuteQueries(son, query, maxLevel - 1, log);
            }

            QueryParser.ProgramContext programContext = null;

            try
            {
                programContext = Parse(query);
            }
            catch (Exception e)
            {
                Logger.LogException(e);
            }

            var result = programContext.VisitProgram(zmi);
            var zone   = zmi.PathName;

            foreach (var r in result)
            {
                if (log)
                {
                    Console.WriteLine(zone + ": " + r);
                }
                zmi.Attributes.AddOrChange(r.Name, r.Value);
            }
        }
Ejemplo n.º 6
0
        public static bool TryParseZMI(TextReader textReader, ref ZMI root)
        {
            string line;

            ZMI current = null;

            while ((line = textReader.ReadLine()) != null)
            {
                if (root == null && !line.StartsWith('/'))
                {
                    return(false);
                }

                if (line.StartsWith('/')) // zone-line
                {
                    if (!TryParseZoneLine(line, root, out current))
                    {
                        return(false);
                    }

                    if (root == null)
                    {
                        root = current;
                    }

                    continue;
                }

                if (!TryParseAttributeLine(line, out var attribute, out var created))
                {
                    return(false);
                }

                current?.Attributes.AddOrChange(attribute, created);
            }

            return(true);
        }
Ejemplo n.º 7
0
        public static bool TryParseZoneLine(string line, ZMI root, out ZMI result)
        {
            var lastSlash = line.LastIndexOf('/');
            ZMI father    = null;

            result = null;

            if (lastSlash == -1)
            {
                return(false);
            }

            var prefix = line.Substring(0, lastSlash);

            if (root != null && !root.TrySearch(prefix, out father))
            {
                return(false);
            }

            result = new ZMI(father);
            father?.Sons.Add(result);
            return(true);
        }
Ejemplo n.º 8
0
        public static void Main(string[] args)
        {
            var zmiName      = string.Empty;
            ZMI fatherZmi    = null;
            RSA rsa          = null;
            var receiverHost = string.Empty;
            var receiverPort = 0;
            var rpcHost      = string.Empty;
            var rpcPort      = 0;

            IDictionary <string, string> configuration = new Dictionary <string, string>();

            Parser.Default.ParseArguments <Options>(args)
            .WithParsed(opts =>
            {
                zmiName = opts.ZmiName.Trim(' ');
                if (string.IsNullOrEmpty(opts.ConfigFile) || !TryParseConfig(opts.ConfigFile, out fatherZmi))
                {
                    fatherZmi = ZMI.FromPathName(zmiName);
                }

                receiverHost = opts.ReceiverHost;
                receiverPort = opts.ReceiverPort;
                rpcHost      = opts.RpcHost;
                rpcPort      = opts.RpcPort;

                rsa = RSAFactory.FromPublicKey(opts.PublicKeyPath);

                if (string.IsNullOrEmpty(opts.IniFileName))
                {
                    return;
                }

                using var file   = File.OpenRead(opts.IniFileName);
                using var stream = new StreamReader(file);
                configuration    = INIParser.ParseIni(stream);
            })
            .WithNotParsed(errs =>
            {
                foreach (var err in errs)
                {
                    Console.WriteLine($"OPTIONS PARSE ERROR: {err}");
                }
                Environment.Exit(1);
            });

            var creationTimestamp = new ValueTime(DateTimeOffset.Now);

            fatherZmi.ApplyForEach(zmi => zmi.Attributes.AddOrChange("update", creationTimestamp));
            if (!fatherZmi.TrySearch(zmiName, out var myZmi))
            {
                Console.WriteLine($"Could not find node {zmiName} in ZMIs");
                Environment.Exit(1);
            }
            myZmi.Attributes.AddOrChange("timestamp", creationTimestamp);
            myZmi.Attributes.AddOrChange("contacts",
                                         new ValueSet(
                                             new HashSet <Value>(new[]
                                                                 { new ValueContact(myZmi.PathName, IPAddress.Parse(receiverHost), receiverPort) }),
                                             AttributeTypePrimitive.Contact));
            myZmi.Attributes.AddOrChange("isSingleton", new ValueBoolean(true));

            var manager = ManagerFromIni(receiverHost, receiverPort, rpcHost, rpcPort, configuration, rsa, myZmi);

            Console.WriteLine("Press ENTER to exit...");
            Console.ReadLine();
            Console.WriteLine("End");
            manager.Dispose();
        }
Ejemplo n.º 9
0
        private static ModulesManager ManagerFromIni(string receiverHost, int receiverPort, string rpcHost, int rpcPort,
                                                     IDictionary <string, string> configuration, RSA rsa, ZMI zmi)
        {
            if (!configuration.TryGetInt("queryInterval", out var queryInterval))
            {
                queryInterval = 5;
            }
            if (!configuration.TryGetInt("gossipInterval", out var gossipInterval))
            {
                gossipInterval = 5;
            }
            if (!configuration.TryGetInt("purgeInterval", out var purgeInterval))
            {
                purgeInterval = 60;
            }
            if (!configuration.TryGetInt("receiverTimeout", out var receiverTimeout))
            {
                receiverTimeout = 3000;
            }
            if (!configuration.TryGetInt("retryInterval", out var retryInterval))
            {
                retryInterval = 2;
            }
            if (!configuration.TryGetInt("maxRetriesCount", out var maxRetriesCount))
            {
                maxRetriesCount = 5;
            }
            if (!configuration.TryGetInt("maxPacketSize", out var maxPacketSize))
            {
                maxPacketSize = 2000;
            }
            if (!configuration.TryGetValue("gossipStrategy", out var gossipStrategyStr) ||
                !TryGetGossipStrategy(gossipStrategyStr, out var gossipStrategy))
            {
                gossipStrategy = new RandomGossipStrategy();
            }
            if (!configuration.TryGetValue("loggerLevel", out var loggerLevelStr) ||
                !TryGetLoggerLevel(loggerLevelStr, out var loggerLevel))
            {
                loggerLevel = LoggerLevel.All;
            }
            if (!configuration.TryGetValue("loggerVerbosity", out var loggerVerbosityStr) ||
                !TryGetLoggerVerbosity(loggerVerbosityStr, out var loggerVerbosity))
            {
                loggerVerbosity = LoggerVerbosity.WithFileName;
            }

            receiverHost = receiverHost.Trim(' ');
            rpcHost      = rpcHost.Trim(' ');

            Logger.LoggerLevel     = loggerLevel;
            Logger.LoggerVerbosity = loggerVerbosity;

            return(new ModulesManager(maxPacketSize, receiverHost, receiverPort, receiverTimeout, rpcHost, rpcPort,
                                      queryInterval, purgeInterval, rsa, gossipStrategy, gossipInterval, retryInterval, maxRetriesCount, zmi));
        }
Ejemplo n.º 10
0
 public OrderByVisitor(ZMI zmi, Table table)
 {
     _zmi = zmi;
     _table = table;
 }
Ejemplo n.º 11
0
 public WhereVisitor(ZMI zmi, Table table)
 {
     _zmi = zmi;
     _table = table;
 }
Ejemplo n.º 12
0
 public TermExprVisitor(ZMI zmi, Environment env)
 {
     _zmi = zmi;
     _env = env;
 }
Ejemplo n.º 13
0
 public SelItemVisitor(ZMI zmi, Table table)
 {
     _zmi = zmi;
     _table = table;
 }
Ejemplo n.º 14
0
 public QueryVisitor(ZMI zmi) => _zmi = zmi;
Ejemplo n.º 15
0
        public ModulesManager(int maxPacketSize, string receiverHost, int receiverPort, int receiverTimeout,
                              string rpcHost, int rpcPort, int queriesRecomputeTimer, int purgeTimer, RSA rsa,
                              IGossipStrategy gossipStrategy, int gossipTimer, int retryDelay, int maxRetriesCount, ZMI zmi)
        {
            Logger.Log("Creating modules...");
            _registry = new ExecutorRegistry();
            var executor = new Executor(_registry);

            void AddModule(IModule module)
            {
                if (executor.TryAddModule(module))
                {
                    return;
                }
                Logger.LogError($"Could not add {module.GetType().Name}");
                throw new ApplicationException(
                          $"Could not add {module.GetType().Name}, which violates the application");
            }

            Console.WriteLine($"Agent started on {receiverHost}:{receiverPort}\nRPC started on {rpcHost}:{rpcPort}");

            AddModule(_timer         = new TimerModule(executor));
            AddModule(_communication = new CommunicationModule(executor, maxPacketSize, IPAddress.Parse(receiverHost), receiverPort, receiverTimeout));
            AddModule(_zmi           = new ZMIModule(zmi, rsa, queriesRecomputeTimer, purgeTimer, executor));
            AddModule(_rmi           = new RMIModule(executor, new ServerPort(rpcHost, rpcPort, ServerCredentials.Insecure)));
            AddModule(_gossip        = new GossipModule(executor, gossipTimer, retryDelay, maxRetriesCount, gossipStrategy));
        }
Ejemplo n.º 16
0
 private static IEnumerable <QueryResult> VisitProgram(this QueryParser.ProgramContext context, ZMI zmi) =>
 VisitProgram(zmi, context);