Exemplo n.º 1
0
        public void Process(string firstLine, NetworkStream stream, ILogger logger)
        {
            var parts = firstLine.Split(new[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);

            switch (parts[0])
            {
            case "OK":
                try
                {
                    var bytes  = Convert.ToInt32(parts[1]);
                    var buffer = new byte[bytes];
                    stream.Read(buffer, 0, bytes);
                    stream.ReadByte();     // CR
                    stream.ReadByte();     // LF

                    var yaml   = YamlHelper.ParseDictionary(buffer);
                    var result = new JobStatistics();
                    foreach (var pair in yaml)
                    {
                        switch (pair.Key)
                        {
                        case "tube": result.Tube = pair.Value; break;

                        case "id": result.Id = Convert.ToInt32(pair.Value); break;

                        case "pri": result.Priority = Convert.ToInt32(pair.Value); break;

                        case "file": result.File = Convert.ToInt32(pair.Value); break;

                        case "kicks": result.Kicks = Convert.ToInt32(pair.Value); break;

                        case "buries": result.Buries = Convert.ToInt32(pair.Value); break;

                        case "reserves": result.Reserves = Convert.ToInt32(pair.Value); break;

                        case "releases": result.Releases = Convert.ToInt32(pair.Value); break;

                        case "timeouts": result.Timeouts = Convert.ToInt32(pair.Value); break;

                        case "age": result.Age = TimeSpan.FromSeconds(Convert.ToInt32(pair.Value)); break;

                        case "ttr": result.TimeToRun = TimeSpan.FromSeconds(Convert.ToInt32(pair.Value)); break;

                        case "time-left": result.TimeLeft = TimeSpan.FromSeconds(Convert.ToInt32(pair.Value)); break;

                        case "state": result.State = (JobState)Enum.Parse(typeof(JobState), pair.Value, true); break;
                        }
                    }
                    _tcs.SetResult(result);
                    return;
                }
                catch (Exception ex)
                {
                    _tcs.SetException(ex);
                    return;
                }

            case "NOT_FOUND":
                _tcs.SetResult(null);
                return;

            default:
                Reply.SetGeneralException(_tcs, firstLine, "stats-job", logger);
                return;
            }
        }