Example #1
0
        /// <summary>
        /// parses a single line and queues/executes it
        /// </summary>
        /// <param name="line"></param>
        /// <param name="queue">true if the command is to be enqueue. False mean it should be ran immediately</param>
        /// <returns></returns>
        public static LineSyntax executeLine(string line, bool queue)
        {
            //regex init
            Regex rgxComment   = new Regex(RGX_COMMENT, RegexOptions.IgnoreCase);
            Regex rgxStart     = new Regex(RGX_START, RegexOptions.IgnoreCase);
            Regex rgxInterval  = new Regex(RGX_INTERVAL, RegexOptions.IgnoreCase);
            Regex rgxStatus    = new Regex(RGX_STATUS, RegexOptions.IgnoreCase);
            Regex rgxCrash     = new Regex(RGX_CRASH, RegexOptions.IgnoreCase);
            Regex rgxFreeze    = new Regex(RGX_FREEZE, RegexOptions.IgnoreCase);
            Regex rgxUnfreeze  = new Regex(RGX_UNFREEZE, RegexOptions.IgnoreCase);
            Regex rgxWait      = new Regex(RGX_WAIT, RegexOptions.IgnoreCase);
            Regex rgxLog       = new Regex(RGX_LOG, RegexOptions.IgnoreCase);
            Regex rgxSemantics = new Regex(RGX_SEMANTICS, RegexOptions.IgnoreCase);
            Regex rgxConf      = new Regex(RGX_CONF, RegexOptions.IgnoreCase);

            //comment
            if (rgxComment.IsMatch(line))
            {
                Logger.debugWriteLine("Parser: return match COMMENT");
                return(LineSyntax.COMMENT);
            }
            //hack just in case the config file has more spaces than it should, such as the sample one
            line = line.Replace(", ", ",");
            String[] fields  = line.Split(' ');
            ICommand command = null;

            //start
            if (rgxStart.IsMatch(line))
            {
                String opID = fields[1];
                command = new Start(opID);
            } //interval
            else if (rgxInterval.IsMatch(line))
            {
                String opID     = fields[1];
                int    millisec = Int32.Parse(fields[2]);
                command = new Interval(opID, millisec);
            } //status
            else if (rgxStatus.IsMatch(line))
            {
                command = new Status();
            } //crash
            else if (rgxCrash.IsMatch(line))
            {
                String opID = fields[1];
                int    rep  = Int32.Parse(fields[2]);
                command = new Crash(opID, rep);
            } //unfreeze
            else if (rgxUnfreeze.IsMatch(line))
            {
                String opID = fields[1];
                int    rep  = Int32.Parse(fields[2]);
                command = new Unfreeze(opID, rep);
            } //freeze
            else if (rgxFreeze.IsMatch(line))
            {
                String opID = fields[1];
                int    rep  = Int32.Parse(fields[2]);
                command = new Freeze(opID, rep);
            } //wait
            else if (rgxWait.IsMatch(line))
            {
                int millisec = Int32.Parse(fields[1]);
                command = new Wait(millisec);
            }
            else if (rgxLog.IsMatch(line))
            {
                String loggingLevel = fields[1];
                command = new Log(loggingLevel);
            }
            else if (rgxSemantics.IsMatch(line))
            {
                String semantics = fields[1];
                command = new SetSemantics(semantics);
            }
            //TODO MISSING: LOG & SEMANTICS

            //configuration of operator
            else if (rgxConf.IsMatch(line))
            {
                String   opID      = fields[0];
                String[] inputOps  = fields[2].Split(',');
                int      repFact   = Int32.Parse(fields[4]);
                String   routing   = fields[6];
                String[] addresses = fields[8].Split(',');
                String[] opSpec    = fields.Skip(10).ToArray();
                command = new ConfigureOperator(opID, inputOps, repFact, routing, addresses, opSpec);
            } //invalid command
            else
            {
                Logger.debugWriteLine("Parser: return match INVALID");
                return(LineSyntax.INVALID);
            }
            //store commands
            if (command != null)
            {
                if (queue)
                {
                    Logger.debugWriteLine("Parser: Enqueue");
                    PuppetMaster.Commands.Enqueue(command);
                }
                else
                {
                    Logger.debugWriteLine("Parser: execute");
                    command.execute();
                }
            } // FIXME
            else
            {
                Logger.debugWriteLine("Parser: return match INVALID");
                return(LineSyntax.INVALID);
            }
            Logger.debugWriteLine("Parser: return match VALID");
            return(LineSyntax.VALID);
        }