public void AddModule(IServerModule module)
        {
            if (_modules.ContainsKey(module.GetType()))
            {
                throw new Exception("A module already exists in the server: " + module.GetType());
            }

            _modules[module.GetType()] = module;
        }
示例#2
0
        private static IReadOnlyList <ImportingProperty> GetImportingProperties(IServerModule plugin)
        {
            var dependencyAttributes = from prop in plugin.GetType().GetProperties()
                                       let att = prop.GetCustomAttribute <RequiredModuleApiAttribute>()
                                                 where att != null
                                                 select new ImportingProperty {
                Property = prop, Attribute = att
            };

            return(dependencyAttributes.ToList());
        }
示例#3
0
        /// <summary>
        /// Sends the commands to the module console
        /// </summary>
        private void ModuleCommandExecutor(string[] fullCommand, IServerModule module)
        {
            var console = module.Console;

            if (fullCommand[0] == "man")
            {
                Console.WriteLine(module.GetType().GetDescription());
            }
            else
            {
                console.ExecuteCommand(fullCommand.Skip(2).ToArray(), Console.WriteLine);
            }
        }
示例#4
0
        /// <summary>
        /// Opens the module specific console
        /// </summary>
        /// <param name="module">The module.</param>
        private void ModuleSpecificConsole(IServerModule module)
        {
            var console = module.Console;

            Console.Clear();
            Console.WriteLine("Welcome to the '{0}' module. Type 'help' to print the possible commands.", module.Name);
            Console.WriteLine();

            var command = string.Empty;

            while (command != "bye")
            {
                WriteModulePostString(module);
                command = CommandHelper.ReadCommand(ModuleManager);
                switch (command)
                {
                case "help":
                    Console.WriteLine(module.GetType().GetDescription());
                    break;

                case "quit":
                case "bye":
                case "exit":
                    break;

                default:
                    var parts = command.Split(' ');
                    Console.WriteLine();
                    console.ExecuteCommand(parts, Console.WriteLine);
                    Console.WriteLine();
                    break;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Exiting module console...bye!");
            Console.WriteLine();
        }
示例#5
0
        /// <summary>
        /// Finds the module that can execute the specified command and executes it.
        /// Also sends the result of the invocation to the client.
        /// </summary>
        /// <param name="ClientStream">A <see cref="StreamWriter"/> instance where the response is written.</param>
        /// <param name="Module">The name of the module.</param>
        /// <param name="Command">The command to execute.</param>
        /// <param name="Arguments">An array of the command's arguments.</param>
        /// <returns>True if the command is executed successfully, false if not.</returns>
        private bool ExecuteHandlerMethod(StreamWriter ClientStream, string Module, string Command, string[] Arguments)
        {
            #region Error checking
            if (ClientStream == null)
            {
                return(false);
            }

            Arguments = Arguments ?? new string[0];
            #endregion

            IServerModule HandlerModule = this.Modules.FirstOrDefault(x => x.ModuleName == Module);
            MethodInfo    Method        = HandlerModule?.GetType().GetMethod(Command);

            #region Error checking
            if (HandlerModule == null || Method == null)
            {
                return(false);
            }

            if (!Method.GetCustomAttributes(typeof(ServerCommandAttribute)).Any())
            {
                return(false);
            }
            #endregion

            object Result = this.InvokeHandlerMethod(HandlerModule, Method, Arguments);

            ServerResponse <object> Response = ServerResponse <object> .GetSuccess(Result);

            ClientStream.WriteLine(
                JsonConvert.SerializeObject(Response, JsonConfig)
                );

            return(true);
        }
 public bool ContainsModule(IServerModule module)
 {
     return(_modules.ContainsKey(module.GetType()));
 }
示例#7
0
        private void PrintServerModule(IServerModule module, IEnumerable <string> printOptions)
        {
            var versionAttribute = module.GetType().Assembly.GetCustomAttribute <AssemblyFileVersionAttribute>();

            Console.Write("    " + module.Name.PadRight(30));

            var warningCount = module.Notifications.Count(n => n.Severity == Severity.Warning);
            var errorCount   = module.Notifications.Count(n => n.Severity >= Severity.Error);

            Console.Write(" (");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(warningCount);
            Console.ResetColor();
            Console.Write(" | ");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write(errorCount);
            Console.ResetColor();
            Console.Write(")");

            Console.CursorLeft = 50;
            CommandHelper.PrintState(module.State, false, 17);
            Console.WriteLine("Version: " + (versionAttribute == null ? "N/A" : versionAttribute.Version));

            if (printOptions == null)
            {
                return;
            }

            Console.WriteLine();
            foreach (var printOption in printOptions)
            {
                switch (printOption)
                {
                case "-e":
                    var relevantNotifications = module.Notifications
                                                .Where(n => n.Severity >= Severity.Warning)
                                                .OrderBy(n => n.Timestamp).ToArray();
                    if (!relevantNotifications.Any())
                    {
                        break;
                    }

                    Console.WriteLine("Notifications for " + module.Name + ":");
                    foreach (var notification in relevantNotifications)
                    {
                        PrintNotification(notification);
                    }
                    break;

                case "-d":
                    Console.WriteLine("Dependencies of " + module.Name + ":");
                    foreach (var startDependency in ModuleManager.StartDependencies(module))
                    {
                        Console.WriteLine(" Module: " + startDependency.Name.PadRight(29) + "State: " +
                                          startDependency.State);
                    }
                    break;

                default:
                    break;
                }
            }
        }