public bool TryProcess(CommandProcessorContext context, string[] args)
        {
            if (args == null || args.Length == 0)
            {
                _log.Error("Empty command");
                throw new Exception("Empty command");
            }

            var commandName = args[0].ToUpper();
            var commandArgs = args.Skip(1).ToArray();

            ICommandProcessor commandProcessor;
            if (!_processors.TryGetValue(commandName, out commandProcessor))
            {
                _log.Info("Unknown command: '{0}'", commandName);
                new UsageProcessor(this).Execute(context, CancellationToken.None, new string[0]);
                return false;
            }

            var result = false;

            var timeout = context.Client.Options.Timeout;

            using (var source = new CancellationTokenSource())
            {
                try
                {
                    var token = source.Token;
                    if (timeout > 0)
                    {
                        result = new WaitFor<bool>(TimeSpan.FromSeconds(timeout)).Run(
                                () => commandProcessor.Execute(context, token, commandArgs));
                    }
                    else
                    {
                        result = commandProcessor.Execute(context, token,commandArgs);
                    }
                }
                catch (TimeoutException ex)
                {
                    _log.Error("Command didn't finish in {0} seconds", timeout);
                }
                catch (Exception exc)
                {
                    _log.ErrorException(exc, "Failure while processing {0}", commandName);
                }
                finally
                {
                    source.Cancel();
                }
            }
            return result;
        }
        public bool TryProcess(CommandProcessorContext context, string[] args)
        {
            if (args == null || args.Length == 0)
            {
                _log.Error("Empty command");
                throw new Exception("Empty command");
            }

            var commandName = args[0].ToUpper();
            var commandArgs = args.Skip(1).ToArray();

            ICommandProcessor commandProcessor;
            if (!_processors.TryGetValue(commandName, out commandProcessor))
            {
                _log.Info("Unknown command: '{0}'", commandName);
                return false;
            }

            bool result = false;
            var executedEvent = new AutoResetEvent(false);
            ThreadPool.QueueUserWorkItem(_ =>
            {
                try
                {
                    result = commandProcessor.Execute(context, commandArgs);
                    executedEvent.Set();
                }
                catch (Exception exc)
                {
                    result = false;
                    //todo add log
                    executedEvent.Set();
                }
            });

            executedEvent.WaitOne(1000);
            context.WaitForCompletion();

            return result;
        }
Example #3
0
        bool ExecuteLine(string line)
        {
            try
            {
                var args = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                Log.Info("Processing command: {0}.", string.Join(" ", args));

                var context = new CommandProcessorContext(this, Log);
                return _commands.TryProcess(context, args);
            }
            catch (Exception exception)
            {
                Log.ErrorException(exception, "Error while executing command");
                return false;
            }
        }
Example #4
0
        private bool Execute(string[] args)
        {
            Log.Info("Processing command: {0}.", string.Join(" ", args));
            var context = new CommandProcessorContext(this, Log, new ManualResetEvent(true));

            return _commands.TryProcess(context, args);
        }