/// <summary>
        /// Execute ServerCommand based on Command Type
        /// </summary>
        /// <param name="handlerId">Handler for which command is to be executed</param>
        /// <param name="command">Type of the command to be executed</param>
        private void ExecuteCommand(string handlerId, string command)
        {
            ServerCommandContext commandContext = new ServerCommandContext(Services, handlerId, command);
            IServerCommand       serverCommand  = _commandFactory.GetCommand(commandContext);

            serverCommand.Execute();
        }
Example #2
0
        public void Start()
        {
            _writer.Write("Listening for commands");
            _writer.Write($"To see list of commands type : {ServerCommandList.Help}");

            _writer.InjectServerPrefix();
            var userInput = DesktopConsole.ReadLine();

ContinueWhenStopped:

            while (userInput != null && !userInput.Equals(ServerCommandList.StopProgram, StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    if (userInput.Equals(ServerCommandList.Help))
                    {
                        _writer.WriteHelp();
                    }
                    else
                    {
                        var cmdContext = _reader.Read(userInput);
                        var command    = _commandFactory.GetCommand(cmdContext);
                        command.Execute();

                        var commandId     = _commandResults.GetLastCommandId();
                        var commandResult = _commandResults.GetResult(commandId);
                        _writer.Write(commandResult != null
                            ? $"{commandResult.ToString()}"
                            : $"Executed {commandId.Value}");
                    }
                }
                catch (Exception ex)
                {
                    _writer.Write(ex.Message);
                }

                _writer.InjectServerPrefix();
                userInput = DesktopConsole.ReadLine();
            }

            if (userInput != null && userInput.Equals(ServerCommandList.ShutDown, StringComparison.OrdinalIgnoreCase))
            {
                DesktopConsole.WriteLine("Application shutting down...");
                return;
            }
            ;

            _writer.InjectServerPrefix();
            DesktopConsole.WriteLine("Server is stopped...");
            while (!String.IsNullOrEmpty(userInput = DesktopConsole.ReadLine()))
            {
                if (userInput.Equals(ServerCommandList.StartProgram, StringComparison.OrdinalIgnoreCase))
                {
                    goto ContinueWhenStopped;
                }
            }
        }