Example #1
0
 /// <summary>
 /// URLChangedOnNewPing executing helper method.
 /// </summary>
 public void OnNewPing(PingerData pingerData)
 {
     if (NewPing != null)
     {
         NewPing(this, pingerData);
     }
 }
Example #2
0
        /// <summary>
        /// Gets latency data.
        /// </summary>
        private void Ping()
        {
            // start pinging
            PingReply pingReply = _p.Send(_url);

            // get ping
            long ping = pingReply.RoundtripTime;

            // calculate time passed
            TimeSpan timeDifference = DateTime.UtcNow - _startTime;

            // create pinger data
            PingerData pingerData = null;

            switch (pingReply.Status)
            {
            case IPStatus.Success:
                pingerData = PingerData.CreateSuccess(ping, timeDifference);
                break;

            case IPStatus.TimedOut:
            default:
                pingerData = PingerData.CreateTimeout(timeDifference);
                break;
            }

            // notify
            OnNewPing(pingerData);
        }
        /// <summary>
        /// Calculates the average from the latency queue and handle the queue size -> number of pings from which the average is calculated.
        /// </summary>
        private double ClaculateAverage(double manipulatedPing, PingerData pingerData)
        {
            // put value in queue, if not in queue its not used
            if (pingerData.PingStatus == PingStatus.Success)
            {
                _pingQueue.Enqueue(manipulatedPing);
            }

            // initialize sum and count counters
            double sum   = 0;
            int    count = 0;

            // sum all values and count them
            _pingQueue.ToList().ForEach(new Action <double>((value) => { sum += value; count++; }));

            // calculate average
            double averagePing = sum / count;

            // dequeue old values if there is more than 50 stored
            if (_pingQueue.Count > 50)
            {
                _pingQueue.Dequeue();
            }


            // return result
            return(averagePing);
        }
 /// <summary>
 /// Creates a new instance of the  <see cref="PingManipulatorResult"/> class.
 /// </summary>
 public PingManipulatorResult(PingerData pingerData, double ping, double average, double time)
 {
     PingerData = pingerData;
     Ping       = ping;
     Average    = average;
     Time       = time;
 }
 /// <summary>
 /// If pinger data is success just converts the value if not returns the last successful.
 /// </summary>
 private double CalculatePing(PingerData pingerData)
 {
     if (pingerData.PingStatus == PingStatus.Success)
     {
         return(Convert.ToDouble(pingerData.Ping));
     }
     else
     {
         return(_pingQueue.Peek());
     }
 }
        /// <summary>
        /// Manipulates the raw data into PingManipulatorResult.
        /// Calculates latency, average latency, time between pings.
        /// </summary>
        public PingManipulatorResult Calculate(PingerData pingerData)
        {
            // get ping
            double manipulatedPing = CalculatePing(pingerData);

            // handle average calculation
            double averagePing = ClaculateAverage(manipulatedPing, pingerData);

            // calculate time
            double time = CalculateTime(pingerData.Time);

            // return result
            return(new PingManipulatorResult(pingerData, manipulatedPing, averagePing, time));
        }