Example #1
0
 private static void RegisterSingleCommands(CmdR cmdR, IEnumerable <ICmdRCommand> commands)
 {
     foreach (var cmd in commands)
     {
         cmdR.RegisterRoute(cmd.Command, cmd.Execute, cmd.Description);
     }
 }
Example #2
0
        /// <summary>
        /// Uses Reflection to loads and constructs all the ICmdRModules and ICmdRCommands from all the loaded assemblies and initalises them.
        /// This will also Register all Routes using CmdRouteAttributes
        /// </summary>
        public static void AutoRegisterCommands(this CmdR cmdR)
        {
            RegisterCommandModules(cmdR);

            var commands = FindAllTypesImplementingICmdRCommand();

            RegisterSingleCommands(cmdR, commands);
        }
Example #3
0
        private static void RegisterCommandModules(CmdR cmdR)
        {
            var instances = GetICmdRModuleClasses().Select(c => Activator.CreateInstance(c, cmdR))
                            .ToArray();

            foreach (var module in instances)
            {
                var methods = module.GetType()
                              .GetMethods()
                              .Where(m => m.GetCustomAttributes(typeof(CmdRouteAttribute), false).Length > 0)
                              .ToArray();

                foreach (var method in methods)
                {
                    var attribute = method.GetCustomAttributes(false)
                                    .SingleOrDefault(att => att is CmdRouteAttribute);

                    if (attribute != null)
                    {
                        var cmdRoute = (CmdRouteAttribute)attribute;

                        // todo: should we validate the type of parameter before trying to invoke the methods?
                        if (method.GetParameters().Count() == 1)
                        {
                            var meth = method;  // capture closure variables
                            var mod  = module;

                            cmdR.RegisterRoute(cmdRoute.Route, param => meth.Invoke(mod, new object[] { param }), cmdRoute.Description);
                        }

                        else if (method.GetParameters().Count() == 2)
                        {
                            var meth = method;  // capture closure variables
                            var mod  = module;

                            cmdR.RegisterRoute(cmdRoute.Route, (param, cmd) => meth.Invoke(mod, new object[] { param, cmd }), cmdRoute.Description);
                        }

                        else if (method.GetParameters().Count() == 3)
                        {
                            var meth = method;  // capture closure variables
                            var mod  = module;

                            cmdR.RegisterRoute(cmdRoute.Route, (param, console, state) => meth.Invoke(mod, new object[] { param, console, state }), cmdRoute.Description);
                        }
                        else
                        {
                            throw new InvalidMethodSignatureException("The method {0} with a [CmdRouteAttribute] does not have a valid signiture, expecting (Dictonary<string, object>, ICmdR) or (Dictonary<string, object>, ICmdRConsole, ICmdRState)");
                        }
                    }
                }
            }
        }
Example #4
0
        private static void RegisterCommandModules(CmdR cmdR)
        {
            var instances = GetICmdRModuleClasses().Select(c => Activator.CreateInstance(c, cmdR))
                                                   .ToArray();

            foreach (var module in instances)
            {
                var methods = module.GetType()
                                    .GetMethods()
                                    .Where(m => m.GetCustomAttributes(typeof(CmdRouteAttribute), false).Length > 0)
                                    .ToArray();

                foreach (var method in methods)
                {
                    var attribute = method.GetCustomAttributes(false)
                                          .SingleOrDefault(att => att is CmdRouteAttribute);

                    if (attribute != null)
                    {
                        var cmdRoute = (CmdRouteAttribute)attribute;

                        // todo: should we validate the type of parameter before trying to invoke the methods?
                        if (method.GetParameters().Count() == 1)
                        {
                            var meth = method;  // capture closure variables
                            var mod = module;

                            cmdR.RegisterRoute(cmdRoute.Route, param => meth.Invoke(mod, new object[] { param }), cmdRoute.Description);
                        }

                        else if (method.GetParameters().Count() == 2)
                        {
                            var meth = method;  // capture closure variables
                            var mod = module;

                            cmdR.RegisterRoute(cmdRoute.Route, (param, cmd) => meth.Invoke(mod, new object[] { param, cmd }), cmdRoute.Description);
                        }

                        else if (method.GetParameters().Count() == 3)
                        {
                            var meth = method;  // capture closure variables
                            var mod = module;

                            cmdR.RegisterRoute(cmdRoute.Route, (param, console, state) => meth.Invoke(mod, new object[] { param, console, state }), cmdRoute.Description);
                        }
                        else throw new InvalidMethodSignatureException("The method {0} with a [CmdRouteAttribute] does not have a valid signiture, expecting (Dictonary<string, object>, ICmdR) or (Dictonary<string, object>, ICmdRConsole, ICmdRState)");
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Constructs a list of commands using the COMMAND_SEPARATOR to split commands
        /// </summary>
        /// <param name="args">The parts of the commands</param>
        /// <returns>A list of commands</returns>
        public static List <string> ConstructMultipleCommands(this CmdR cmdR, string[] args)
        {
            var commands = new List <string>();

            if (args.Any() && !args.All(x => string.IsNullOrEmpty(x.Trim())))
            {
                var escapedArgs = EscapeEscapeChar(args);

                var i = 0;
                while (i < args.Count())
                {
                    var commandParts = escapedArgs.Skip(i).TakeWhile(arg => arg != COMMAND_SEPARATOR);
                    commands.Add(string.Join(" ", EscapeKeywords(commandParts).ToArray()));

                    i = commands.Sum(x => x.Split(' ').Length) + commands.Count(); // sum of command parts and separators so far
                }
            }

            return(commands);
        }
Example #6
0
 private static void RegisterSingleCommands(CmdR cmdR, IEnumerable<ICmdRCommand> commands)
 {
     foreach (var cmd in commands)
         cmdR.RegisterRoute(cmd.Command, cmd.Execute, cmd.Description);
 }
Example #7
0
        static void Main(string[] args)
        {
            string endpoint = null;

            if(args.Length > 0)
                if (args[0] == "-e")
                    endpoint = args[1];

            if (endpoint == null)
                endpoint = ConfigurationManager.AppSettings["res"];

            if(endpoint == null)
                throw new ArgumentException("Usage: res.dira.exe -e endpoint, or use an app setting named res with the endpoint as the value");

            Console.WriteLine("Hello...shall we try out the res server?");

            Console.WriteLine("Starting client engine for {0}...", endpoint);
            var engine = new ResPublishEngine(endpoint);

            GlobalHack.SetEngine(engine);

            Console.WriteLine("Client engine started.");

            var cmdr = new CmdR("input>", new[] {"exit"});
            cmdr.AutoRegisterCommands();

            cmdr.Run(args);

            engine.Dispose();
            Console.WriteLine("Bye bye.");
        }
Example #8
0
        public void Execute(IDictionary<string, string> param, CmdR cmdR)
        {
            int n = 1;

            if (param.ContainsKey("n"))
                n = int.Parse(param["n"]);

            var client = GlobalHack.GetClient();
            var appender = new Appender(client);
            appender.AppendEvents(n);

            cmdR.State.CmdPrompt = "input>";
        }
Example #9
0
 public void RegisterRoute(CmdR cmdR)
 {
 }
Example #10
0
 public void RegisterRoute(CmdR cmdR)
 {
     
 }