Beispiel #1
0
        static void Main()
        {
            Console.WriteLine("Client started.");

            const string fileName = "Settings.xml";

            if (!File.Exists(fileName))
            {
                Console.WriteLine($"{fileName} not found.");
                return;
            }

            using var fileStream = File.Open(fileName, FileMode.Open);
            var serializer = new XmlSerializer(typeof(Options));
            var options    = (Options)serializer.Deserialize(fileStream);

            var values     = new BlockingCollection <int>();
            var calculator = new StatisticCalculator();

            using var producer = new Producer(values, options);
            var tasks = new List <Task>
            {
                producer.ProduceMessagesAsync(),
                Task.Run(() => WaitKey(calculator, producer))
            };

            for (var i = 0; i < 2; i++)
            {
                tasks.Add(new Consumer(values, calculator).ConsumeMessagesAsync());
            }

            Task.WaitAll(tasks.ToArray());
        }
Beispiel #2
0
        private static void Main(string[] args)
        {
            long missingPackages     = 0;
            var  lastCounter         = 0;
            var  firstPackage        = true;
            var  statisticCalculator = new StatisticCalculator();
            var  processor           = new Processor();

            void Handler(int number, int counter)
            {
                if (firstPackage)
                {
                    firstPackage = false;
                    lastCounter  = unchecked (counter - 1);
                }

                var missing = unchecked (counter - lastCounter - 1);

                if (missing < 0)
                {
                    missing = 0;
                }

                missingPackages += missing;
                lastCounter      = counter;
                processor.Enqueue(() => statisticCalculator.AddNumber(number));
            }

            var config = Config.GetConfig();

            var receiver = new Receiver(config.Port, IPAddress.Parse(config.Address), Handler);

            new Thread(processor.Start).Start();
            new Thread(receiver.Start).Start();

            Console.WriteLine($@"Enter d to use standard delay ({config.Delay}s)
Enter positive number to delay for custom amount of seconds
Press enter to display statistic");
            while (true)
            {
                var s = Console.ReadLine();
                if (s == "d")
                {
                    receiver.Delay(config.Delay);
                    continue;
                }

                if (int.TryParse(s, out var delaySeconds) && delaySeconds > 0)
                {
                    receiver.Delay(delaySeconds);
                    continue;
                }

                var missingPackagesMessage = missingPackages == 0 ? string.Empty : $"\r\n {missingPackages} packages missing";
                processor.Enqueue(() => Console.WriteLine($"{statisticCalculator.Statistic}{missingPackagesMessage}"));
            }
        }
Beispiel #3
0
 private static void WaitKey(StatisticCalculator calculator, Producer producer)
 {
     while (true)
     {
         if (Console.ReadKey().Key == ConsoleKey.Enter)
         {
             ShowValues(calculator, producer.LossPackagesCount);
         }
     }
 }
Beispiel #4
0
        private static void ShowValues(StatisticCalculator calculator, int lossPackagesCount)
        {
            Console.WriteLine();
#if DEBUG
            Console.WriteLine($"Count: \t\t\t{calculator.Count}");
            Console.WriteLine($"Loss packages count: \t{lossPackagesCount}");
#endif
            Console.WriteLine($"Average: \t\t{calculator.Average}");
            Console.WriteLine($"Standard deviation: \t{calculator.Deviation}");

            if (calculator.TryCalculateModeAndMedian(out long mode, out long median))
            {
                Console.WriteLine($"Mode: \t\t\t{mode}");
                Console.WriteLine($"Median: \t\t{median}");
            }
        }
Beispiel #5
0
 public Consumer(BlockingCollection <int> cache, StatisticCalculator calculator)
 {
     _cache      = cache;
     _calculator = calculator;
 }