Exemplo n.º 1
0
        /// <summary>
        /// Main loop waiting for user input and processing it. Call commands and communicate with the slave.
        /// </summary>
        void RunMaster()
        {
            ConsoleTools.DisplayCommandPrompt();

            while (true)
            {
                waitingForUserCommandInput = true;     // Used for the connectionMonitoringTask
                var commandString = CustomConsole.ReadLine();
                waitingForUserCommandInput = false;

                // If the connection was closed, break from the loop
                if (!networkManager.IsConnected())
                {
                    break;
                }

                if (commandString == "clear" || commandString == "cls")
                {
                    // Clear console
                    Console.Clear();
                }
                else if (commandString == "help")
                {
                    // Global help section
                    commandsManager.ShowGlobalHelp();
                }
                else if (commandString != "")
                {
                    var splittedCommand = commandsManager.GetSplittedCommandWithoutQuotes(commandString);
                    var commandName     = splittedCommand[0];

                    var command = (IMasterCommand)commandsManager.GetCommandByName(commandName);
                    if (command != null)
                    {
                        var arguments = new List <string>();

                        if (splittedCommand.Count > 1)
                        {
                            arguments = splittedCommand.GetRange(1, splittedCommand.Count - 1);
                        }

                        if (arguments.Count == 1 && arguments[0] == "help")
                        {
                            // Display command's help
                            commandsManager.ShowCommandHelp(command);

                            ConsoleTools.DisplayCommandPrompt();
                            continue;
                        }

                        if (!commandsManager.CheckCommandSyntax(command, arguments, ref commandString))
                        {
                            ColorTools.WriteCommandError(
                                $"Syntax error, check out the command's help page ({commandName} help)");

                            ConsoleTools.DisplayCommandPrompt();
                            continue;
                        }

                        try
                        {
                            // If the command implements the IPreProcessCommand interface, execute it's PreProcess() method
                            if ((command as IPreProcessCommand)?.PreProcess(arguments) == false)
                            {
                                ConsoleTools.DisplayCommandPrompt();
                                continue;
                            }

                            if (!command.isLocal)
                            {
                                // Send the command to the slave
                                networkManager.WriteLine(commandString);
                            }

                            command.Process(arguments);
                        }
                        catch (ExitException)
                        {
                            // The user called the 'Exit', 'StopConnection' or 'Terminate' command
                            Cleanup(isCommandProcessing: false);
                            break;
                        }
                        catch (NetworkException)
                        {
                            Cleanup(isCommandProcessing: true);
                            break;
                        }
                        catch (Exception ex)
                        {
                            // Some command didn't catch its exception...
                            ColorTools.WriteCommandError("An exception occured, this command might be broken...\nDetails below :");
                            Console.WriteLine(ex.Message);
                            Console.WriteLine(ex.StackTrace);
                        }
                    }
                    else
                    {
                        ColorTools.WriteError($"'{commandName}' is not a known command");
                    }
                }

                ConsoleTools.DisplayCommandPrompt();
            }
        }