public ChatCommandHandler(List <IChatCommand> commandHandlers, List <IAsyncChatCommand> asyncCommandHandlers) { Commands = new Dictionary <string, Func <ChatMetaData, List <OutboundChatMessage> > >(); CommandsAsync = new Dictionary <string, Func <ChatMetaData, Task <List <OutboundChatMessage> > > >(); _invalidCommand = new InvalidCommand(); // Add chat commands from list. commandHandlers.ForEach(command => { command.CommandSignatures.ForEach(alias => { // Map all aliases to our callback Commands[alias] = command.ParseChatline; }); }); asyncCommandHandlers.ForEach(command => { command.CommandSignatures.ForEach(alias => { CommandsAsync[alias] = command.ParseChatline; }); }); }
public async Task DispatchCommand_CommandIsNotForMediatR_DispatchesToCommandHandler() { var command = new InvalidCommand(); await dispatcher.Awaiting(d => d.DispatchCommandAsync(command)).Should() .ThrowAsync <InvalidOperationException>(); }
public void AddCommand_Returns_NoCommandAttributeFound() { var expected = RegisterResult.NoCommandAttributeFound; var c1 = new InvalidCommand(); var actual = underTest.AddCommand(c1); Assert.IsTrue(expected == actual); }
public void Then_an_exception_is_thrown_when_the_command_type_is_not_valid() { // arrange var invalidCommand = new InvalidCommand(); // act Func <Task> result = async() => await _sut.Dispatch(invalidCommand); // assert result.Should().Throw <ArgumentException>().WithMessage($"Invalid command type {invalidCommand.GetType().FullName}"); }
/// <summary> /// Method which process input commands /// </summary> /// <param name="command">inut command</param> public void ExecuteCommand(string command) { CommandInfo commandInfo = (CommandInfo)this.commandParser.Parse(command); Command currentCommand = null; switch (commandInfo.Name) { case "start": currentCommand = new StartCommand(this, this.matrix, this.player, this.director, this.builder, this.printer); break; case "turn": currentCommand = new TurnCommand(this, this.matrix, this.player, this.printer); break; case "menu": MainMenu.PrintMenu(this); break; case "exit": currentCommand = new ExitCommand(this.matrix, this.player, this.printer); break; case "save": currentCommand = new SaveCommand(this.matrix, this.player, this.printer); break; case "load": currentCommand = new LoadCommand(this.matrix, this.player, this.printer); break; case "mode": currentCommand = new ChangeModeCommand(this, this.matrix, this.player, this.printer); break; case "highscore": currentCommand = new HighScoreCommand(this, this.matrix, this.player, this.printer); break; default: currentCommand = new InvalidCommand(this.matrix, this.player, this.printer); currentCommand.Execute(commandInfo); this.Start(); return; } currentCommand.Execute(commandInfo); this.printer.PrintMatrix(this.matrix, this.player); this.Start(); }
public static byte[] BuildInvalidResponse(InvalidCommand command) { BinaryResponseStatus status; if (command != null && command.Opcode == Opcode.unknown_command) { status = BinaryResponseStatus.unknown_commnad; } else { status = BinaryResponseStatus.invalid_arguments; } return(BuildResposne(command.Opcode, status, command.Opaque, 0, null, null, null)); }
public async Task WhenProcessingInvalidCommandThenAbortProcesssing() { // Arrange InvalidCommand command = new InvalidCommand(); var handler = this.SetupHandler <InvalidCommand>(); MessageProcessor processor = this.CreatTestableProcessor(); // Act var result = await processor.ProcessAsync(command); // Assert Assert.NotNull(result); Assert.NotEqual(0, result.ModelState.Count); handler.Verify(h => h.Handle(It.IsAny <InvalidCommand>()), Times.Never()); }
private void CreateSlabsCommand(string arguments) { if (string.IsNullOrEmpty(arguments)) { CreateInvalidCommand(); return; } string[] argumentsArray; argumentsArray = arguments.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (argumentsArray.Length > 3 || argumentsArray.Length < 2) { CreateInvalidCommand(); return; } try { bool noreply = false; switch (argumentsArray[0]) { case "reasign": int sourceClass = int.Parse(argumentsArray[1]); int destClass = int.Parse(argumentsArray[2]); noreply = argumentsArray.Length > 2 && argumentsArray[3] == "noreply"; _command = new SlabsReassignCommand(sourceClass, destClass); _command.NoReply = noreply; break; case "automove": int option = int.Parse(argumentsArray[1]); noreply = argumentsArray.Length > 2 && argumentsArray[2] == "noreply"; _command = new SlabsAuomoveCommand(option); _command.NoReply = noreply; break; default: _command = new InvalidCommand(); break; } this.State = ParserState.ReadyToDispatch; } catch (Exception) { CreateInvalidCommand(); } }
public ICommand Parse(string cmd) { ICommand result = null; var args = cmd.Split(new char[] { ' ', ',' }); if (args.Length >= 1) { var factory = Commands.FirstOrDefault(i => i.Name.Equals(args[0], StringComparison.CurrentCultureIgnoreCase)); result = factory != null?factory.Create(args) : new InvalidCommand(); } else { result = new InvalidCommand(); } return(result); }
private void CreateRetrievalCommand(string arguments, Opcode cmdType) { if (arguments == null) { CreateInvalidCommand(); return; } string[] argumentsArray = arguments.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (argumentsArray.Length == 0) { _command = new InvalidCommand(); } else { GetCommand getCommand = new GetCommand(cmdType); getCommand.Keys = argumentsArray; _command = getCommand; } this.State = ParserState.ReadyToDispatch; }
/// <summary> /// Performs the command. /// </summary> /// <param name="message">The message.</param> public void PerformCommand(IMessage message) { string inputCommand = message.FormatMessage(); var commands = this.Commands.Where(command => command.GetType().Name.ToLower().StartsWith(inputCommand)).ToList(); ICommand validCommand = null; // We do not care about this anymore. If a command does not have a ShorthandNameAttribute // it can not be executed by the user. Only the engine can execute non-attribute decorated commands. /* * if (commands.Count != 0) * { * // See if we can find the command by naming convention * try * { * validCommand = commands.FirstOrDefault(); * } * catch(Exception) * { * throw; * } * } * else */ if (this.shorthandCommands.Keys.Any(key => key.Shorthand.ToLower() == inputCommand || key.Command.ToLower() == inputCommand)) { // If we get this far, then commands is empty. // Find a shorthand command. try { validCommand = this.shorthandCommands.FirstOrDefault(key => key.Key.Command.ToLower() == inputCommand || key.Key.Shorthand.ToLower() == inputCommand).Value as ICommand; } catch (Exception) { throw; } } else if (this.previousCommands.Count > 0) { validCommand = this.previousCommands.Pop(); } else { validCommand = new InvalidCommand(); } this.currentCommand = validCommand; validCommand.Execute(this.Mob, inputCommand); // Check if the command needs to continue executing. if (validCommand.IsIncomplete) { this.previousCommands.Push(validCommand); } this.currentCommand = validCommand; }
public async Task <CommandResult> ValidateAsync(InvalidCommand command) { return(await RejectCommand(command)); }
public Task <CommandResult> ExecuteAsync(InvalidCommand command) { return(Task.FromResult(InvalidResult)); }
/// <summary> /// Handle Falcom commands. Determine if command is valid, /// what is the type of it, and execute (if valid). /// </summary> /// <param name="input">Falcon command (including cmd char)</param> /// <param name="output">Execution result</param> /// <returns>Is command valid, Command message, Command Type, On or Off flag</returns> public static bool Parse(string cmd, ref string message, ref Command.Type type, ref Argument argumentObj) { // extract command name and arguments string[] cmdArr = cmd.Split(CMD_SPLITTER); string cmdName = cmdArr[CMD_NAME_INDX]; bool noArgument = (cmdArr.Length <= 1) ? true : false; SshCommand sshCmd = new SshCommand(); PingCommand pingCmd = new PingCommand(); ClearCommand clearCmd = new ClearCommand(); HelpCommand helpCmd = new HelpCommand(); InvalidCommand invalidCmd = new InvalidCommand(); // return values to caller according to cmd name switch (cmdName) { case "ssh": if (noArgument) { message = sshCmd.GetNoArgumentMsg(); return(false); } if (cmdArr.Length < 4) { message = sshCmd.GetInvalidArgumentMsg(); return(false); } string sshArgs = cmdArr[CMD_ARG_INDX] + " " + cmdArr[CMD_ARG_INDX + 1] + " " + cmdArr[CMD_ARG_INDX + 2]; sshCmd.InitArgument(sshArgs); type = sshCmd.GetCommandType(); if (!sshCmd.IsValidArgument()) { message = sshCmd.GetInvalidArgumentMsg(); return(false); } argumentObj = sshCmd.GetArgumentObject(); return(true); case "ping": if (noArgument) { message = pingCmd.GetNoArgumentMsg(); return(false); } pingCmd.InitArgument(cmdArr[CMD_ARG_INDX]); type = pingCmd.GetCommandType(); argumentObj = pingCmd.GetArgumentObject(); message = pingCmd.GetSuccessMsg(); return(true); case "clear": type = clearCmd.GetCommandType(); message = clearCmd.GetSuccessMsg(); return(true); case "help": if (noArgument) { message = helpCmd.GetNoArgumentMsg(); return(false); } helpCmd.InitArgument(cmdArr[CMD_ARG_INDX]); if (!helpCmd.IsValidArgument()) { message = helpCmd.GetInvalidArgumentMsg(); return(false); } switch (cmdArr[CMD_ARG_INDX]) { case "ssh": message = sshCmd.GetHelpMsg(); break; case "ping": message = pingCmd.GetHelpMsg(); break; case "clear": message = clearCmd.GetHelpMsg(); break; case "help": message = helpCmd.GetHelpMsg(); break; } break; default: { invalidCmd.InitArgument(cmd); type = invalidCmd.GetCommandType(); message = invalidCmd.GetInvalidArgumentMsg(); break; } } return(false); }
private void CreateInvalidCommand() { _command = new InvalidCommand(null); this.State = ParserState.ReadyToDispatch; return; }
private void CreateInvalidCommand(string error) { _command = new InvalidCommand(error); this.State = ParserState.ReadyToDispatch; return; }
/// <summary> /// Parses an <see cref="Alachisoft.NCache.Integrations.Memcached.ProxyServer.Commands.AbstractCommand"/> from string /// </summary> /// <param name="command">string command to pe parsed</param> public void Parse(string command) { string[] commandParts = command.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries); if (commandParts == null || commandParts.Length == 0) { _command = new InvalidCommand(); this.State = ParserState.ReadyToDispatch; return; } string arguments = null; if (commandParts.Length > 1) { arguments = commandParts[1]; } switch (commandParts[0]) { case "get": CreateRetrievalCommand(arguments, Opcode.Get); break; case "gets": CreateRetrievalCommand(arguments, Opcode.Gets); break; case "set": CreateStorageCommand(arguments, Opcode.Set); break; case "add": CreateStorageCommand(arguments, Opcode.Add); break; case "replace": CreateStorageCommand(arguments, Opcode.Replace); break; case "append": CreateStorageCommand(arguments, Opcode.Append); break; case "prepend": CreateStorageCommand(arguments, Opcode.Prepend); break; case "cas": CreateStorageCommand(arguments, Opcode.CAS); break; case "delete": CreateDeleteCommand(arguments); break; case "incr": CreateCounterCommand(arguments, Opcode.Increment); break; case "decr": CreateCounterCommand(arguments, Opcode.Decrement); break; case "touch": CreateTouchCommand(arguments); break; case "flush_all": CreateFlushCommand(arguments); break; case "stats": CreateStatsCommand(arguments); break; case "slabs": break; case "version": CreateVersionCommand(arguments); break; case "verbosity": CreateVerbosityCommand(arguments); break; case "quit": CreateQuitCommand(); break; default: CreateInvalidCommand(); break; } }
public void CreateInvalidCommand() { _command = new InvalidCommand(); }
protected async Task <CommandBase> GetCommandAsync(String line, List <Type> possibleCommands) { if (String.IsNullOrEmpty(line)) { return(null); } CommandBase cmd = null; String payload = null; if ((payload = this.GetPayloadByCommand(line, HeloCommand.Command)) != null) { cmd = new HeloCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, EhloCommand.Command)) != null) { cmd = new EhloCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, StartTlsCommand.Command)) != null) { cmd = new StartTlsCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, MailCommand.Command)) != null) { cmd = new MailCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, RcptCommand.Command)) != null) { cmd = new RcptCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, DataCommand.Command)) != null) { cmd = new DataCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, QuitCommand.Command)) != null) { cmd = new QuitCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, RstCommand.Command)) != null) { cmd = new RstCommand(payload); } else if ((payload = this.GetPayloadByCommand(line, NoopCommand.Command)) != null) { cmd = new NoopCommand(payload); } if (cmd != null) { if (possibleCommands.Contains(cmd.GetType())) { var error = cmd.ParseParameter(); if (error == null) { error = await cmd.ParseParameterAsync(this.Connection); } if (error != null) { await this.Connection.WriteLineAsync(error); cmd = null; } } else { cmd = new InvalidCommand(payload); } } else { await this.Connection.WriteLineAsync("500 Unrecognized command"); } return(cmd); }