Ejemplo n.º 1
0
        public void LoadSettingsAndGetAddresses()
        {
            var currentsettingsBeforeLoading = _pingerSettings.GetAddresses();

            Assert.IsTrue(currentsettingsBeforeLoading is null);

            _pingerSettings.LoadSettings();

            var currentsettingsAfterLoading = _pingerSettings.GetAddresses();

            Assert.IsTrue(currentsettingsAfterLoading != null);
        }
Ejemplo n.º 2
0
        public void StartAllCheckers()
        {
            cancelTokenSource = new CancellationTokenSource();
            token             = cancelTokenSource.Token;

            _pingerSettings.LoadSettings();

            tasks = new List <Task>();

            foreach (var address in _pingerSettings.GetAddresses())
            {
                tasks.Add(new Task(() =>
                {
                    while (true)
                    {
                        if (token.IsCancellationRequested)
                        {
                            Console.WriteLine("Операция завершена: " + address.GetEndPoint());
                            TasksCanceledCount++;
                            CheckForClose();
                            return;
                        }

                        switch (address.GetProtocol())
                        {
                        case "Http":
                            _pingerHttp.CheckConnection(address);
                            break;

                        case "Icmp":
                            _pingerIcmp.CheckConnection(address);
                            break;

                        case "Tcp":
                            _pingerTcp.CheckConnection(address);
                            break;
                        }
                        Console.WriteLine(((IPingerLogSaveble)address).GetSaveLogData());
                        _pingLogWriter.SaveLog(address as IPingerLogSaveble);

                        if (token.IsCancellationRequested)
                        {
                            Console.WriteLine("Операция завершена: " + address.GetEndPoint());
                            TasksCanceledCount++;
                            CheckForClose();
                            return;
                        }
                        Thread.Sleep(address.GetCheckInterval());
                    }
                }));
            }


            foreach (var task in tasks)
            {
                task.Start();
            }
        }