Ejemplo n.º 1
1
        public NetInterface(NetworkInterface adapterIn)
        {
            // set up the adapter
            adapter = adapterIn;
            stats = adapter.GetIPv4Statistics();

            // set up the logging
            logPath = Path.Combine("logs", Path.Combine(adapter.Description, adapter.GetPhysicalAddress().ToString(), adapter.Id));
            logHandler = new LogHandler(logPath);
            loadDataInstant(DateTime.UtcNow.Ticks);

            // set up the data tracking
            dataTransferStart = currentTicks();
            bytesInSession = stats.BytesReceived;
            bytesOutSession = stats.BytesSent;
            properties = adapter.GetIPProperties();
            //Console.WriteLine(adapter.Name + " " + adapter.Description + " " + adapter.OperationalStatus);

            Tracker = new Tracker(logHandler);
        }
        public DataTransferTracker(long minutes, LogHandler logHandler)
        {
            if (minutes < 1)
            {
                throw new System.ArgumentOutOfRangeException("The number of minutes ago the track starts cannot be less than 1");
            }
            minutesIntoPast = minutes;

            // find the data transfer over the period
            setStartInstantValues(logHandler);
            DataTransferInstant currentInstant = logHandler.getDataInstant(DateTime.UtcNow.Ticks);
            bytesIn = currentInstant.bytesIn - startInstant.bytesIn;
            bytesOut = currentInstant.bytesOut - startInstant.bytesOut;

            // initialise the seconds
            second = 0;
            for (byte s = 0; s < 60; s++)
                seconds[s] = new DataTransferLump(0, 0);
        }
        /// <summary>
        /// Loads the values regarding the start instant for the tracker
        /// </summary>
        /// <param name="logHandler"></param>
        private void setStartInstantValues(LogHandler logHandler)
        {
            startInstant = logHandler.getDataInstant(ticksAtStartPoint());
            // try looking slightly after a minute ahead to account for any delays in saving
            minuteAfterStartInstant = logHandler.getDataInstant(ticksAtStartPoint() + (long)(TimeSpan.TicksPerMinute * 0.98));

            // work out how many bytes change should be made each second so there's no sudden jump when the minute changes
            long inDiff = minuteAfterStartInstant.bytesIn - startInstant.bytesIn;
            long outDiff = minuteAfterStartInstant.bytesOut - startInstant.bytesOut;
            if (inDiff < 0)
            {
                inDiff = -inDiff;
            }
            if (outDiff < 0)
            {
                outDiff = -outDiff;
            }
            bytesStartPerSecond.bytesIn = inDiff / 60;
            bytesStartPerSecond.bytesOut = outDiff / 60;

            bytesStartLeftover.bytesIn = inDiff - bytesStartPerSecond.bytesIn * 60;
            bytesStartLeftover.bytesOut = outDiff - bytesStartPerSecond.bytesOut * 60;
        }
 /// <summary>
 /// Make some additional changes to the tracker every minute
 /// </summary>
 private void updateMinute(LogHandler logHandler)
 {
     // remove any leftover bytes
     bytesIn -= bytesStartLeftover.bytesIn;
     bytesOut -= bytesStartLeftover.bytesOut;
     // update the start point
     setStartInstantValues(logHandler);
 }
        /// <summary>
        /// Update the tracker every second
        /// </summary>
        /// <param name="period"></param>
        /// <param name="logHandler"></param>
        public void updateSecond(DataTransferPeriod period, LogHandler logHandler)
        {
            if (second == 60)
            {
                second = 0;
                updateMinute(logHandler);
            }
            bytesIn -= bytesStartPerSecond.bytesIn;
            bytesOut -= bytesStartPerSecond.bytesOut;
            seconds[second].set(period.getBytesIn(), period.getBytesOut());
            bytesIn += seconds[second].bytesIn;
            bytesOut += seconds[second].bytesOut;

            // restarting seems to fix any odd numbers that are being shown
            if (bytesIn < 0 || bytesOut < 0)
            {
                /*
                String text = "Restarted from DTT at: " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + Environment.NewLine +
                                    " Bytes: In: " + bytesIn + " Out: " + bytesOut + Environment.NewLine +
                                    " Per Second: In: " + bytesStartPerSecond.bytesIn + " Out: " + bytesStartPerSecond.bytesOut + Environment.NewLine +
                                    " Leftover: In: " + bytesStartLeftover.bytesIn + " Out: " + bytesStartLeftover.bytesOut + Environment.NewLine;
                File.AppendAllText(Path.Combine("restart-log.txt"), text + Environment.NewLine);

                Program.restarting = true;
                if (!Program.restarting)
                {
                    Application.Restart();
                }
                */

                bytesIn = 0;
                bytesOut = 0;
            }

            second++;
        }