Пример #1
0
        /// <summary>
        /// Calculates the bandwidth.
        /// </summary>
        /// <param name='transmittedBytes'>
        /// Transmitted bytes.
        /// </param>
        private void CalculateBandwidth(int transmittedBytes)
        {
            this.bytesTransmittedSinceLastSecond += transmittedBytes;
            TimeSpan diff = DateTime.Now - this.start;
            long?    pos;
            long?    length = null;

            try {
                pos = Stream.Position;
                if (pos > this.transmissionEvent.Status.Length)
                {
                    length = this.Stream.Length;
                }
            } catch (NotSupportedException) {
                pos = null;
            }

            if (diff.Seconds >= 1)
            {
                long?result = TransmissionProgressEventArgs.CalcBitsPerSecond(this.start, DateTime.Now, this.bytesTransmittedSinceLastSecond);
                this.transmissionEvent.ReportProgress(new TransmissionProgressEventArgs {
                    ActualPosition = pos, BitsPerSecond = result, Length = length
                });
                this.bytesTransmittedSinceLastSecond = 0;
                this.start = this.start + diff;
                this.blockingDetectionTimer.Stop();
                this.blockingDetectionTimer.Start();
            }
            else
            {
                this.transmissionEvent.ReportProgress(new TransmissionProgressEventArgs {
                    ActualPosition = pos, Length = length
                });
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CmisSync.Lib.Streams.ProgressStream"/> class.
        /// The given transmission event will be used to report the progress
        /// </summary>
        /// <param name='stream'>
        /// Stream which progress should be monitored.
        /// </param>
        /// <param name='e'>
        /// Transmission event where the progress should be reported to.
        /// </param>
        public ProgressStream(Stream stream, FileTransmissionEvent e) : base(stream)
        {
            if (e == null)
            {
                throw new ArgumentNullException("The event, where to publish the prgress cannot be null");
            }

            try {
                e.Status.Length = stream.Length;
            } catch (NotSupportedException) {
                e.Status.Length = null;
            }

            this.transmissionEvent               = e;
            this.blockingDetectionTimer          = new Timer(2000);
            this.blockingDetectionTimer.Elapsed += delegate(object sender, ElapsedEventArgs args)
            {
                var transmissionArgs = new TransmissionProgressEventArgs
                {
                    BitsPerSecond = (long)(this.bytesTransmittedSinceLastSecond / this.blockingDetectionTimer.Interval)
                };

                this.transmissionEvent.ReportProgress(transmissionArgs);
                this.bytesTransmittedSinceLastSecond = 0;
            };
        }
Пример #3
0
        private void TransmissionEvent(object sender, TransmissionProgressEventArgs status)
        {
            lock (disposeLock)
            {
                if (disposed)
                {
                    return;
                }
                TimeSpan diff = DateTime.Now - updateTime;
                if (diff.Seconds < updateInterval)
                {
                    return;
                }
                updateTime = DateTime.Now;

                ParentControl.BeginInvoke((Action) delegate()
                {
                    lock (disposeLock)
                    {
                        if (disposed)
                        {
                            return;
                        }
                        Text = TransmissionStatus(status);
                    }
                });
            }
        }
Пример #4
0
        public void CalculationOfBitsPerSecondFailsOnIllegalDifference()
        {
            DateTime start = DateTime.Now;
            DateTime end   = start.AddSeconds(1);

            Assert.Throws <ArgumentException>(() => TransmissionProgressEventArgs.CalcBitsPerSecond(end, start, 100));
        }
Пример #5
0
        private void TransmissionEvent(object sender, TransmissionProgressEventArgs e)
        {
            lock (disposeLock) {
                if (disposed)
                {
                    return;
                }
                TimeSpan diff = DateTime.Now - updateTime;
                if (diff.Seconds < updateInterval)
                {
                    return;
                }
                if (run)
                {
                    return;
                }

                run        = true;
                updateTime = DateTime.Now;
                string title = TransmissionStatus(e);
                BeginInvokeOnMainThread(delegate
                {
                    lock (disposeLock) {
                        if (!disposed)
                        {
                            Title = title;
                        }
                    }
                });
                run = false;
            }
        }
Пример #6
0
        private void UpdateFileStatus(FileTransmissionEvent transmission, TransmissionProgressEventArgs e)
        {
            if (e == null)
            {
                e = transmission.Status;
            }

            string filePath = transmission.CachePath;

            if (filePath == null || !File.Exists(filePath))
            {
                filePath = transmission.Path;
            }
            if (!File.Exists(filePath))
            {
                Logger.Debug(String.Format("None exist {0} for file status update", filePath));
                return;
            }
            if ((e.Aborted == true || e.Completed == true || e.FailedException != null))
            {
                Notifications.FileSystemProgress.RemoveFileProgress(filePath);
            }
            else
            {
                double percent = transmission.Status.Percent.GetValueOrDefault() / 100;
                if (percent < 1)
                {
                    Notifications.FileSystemProgress.SetFileProgress(filePath, percent);
                }
                else
                {
                    Notifications.FileSystemProgress.RemoveFileProgress(filePath);
                }
            }
        }
Пример #7
0
        private void UpdateFileStatus(FileTransmissionEvent transmission, TransmissionProgressEventArgs e)
        {
            if (e == null)
            {
                e = transmission.Status;
            }

            string filePath = transmission.CachePath;

            if (filePath == null || !File.Exists(filePath))
            {
                filePath = transmission.Path;
            }
            if (!File.Exists(filePath))
            {
                Logger.Error(String.Format("None exist {0} for file status update", filePath));
                return;
            }

            string extendAttrKey = "com.apple.progress.fractionCompleted";

            if ((e.Aborted == true || e.Completed == true || e.FailedException != null))
            {
                Syscall.removexattr(filePath, extendAttrKey);
                try {
                    NSFileAttributes attr = NSFileManager.DefaultManager.GetAttributes(filePath);
                    attr.CreationDate = (new FileInfo(filePath)).CreationTime;
                    NSFileManager.DefaultManager.SetAttributes(attr, filePath);
                } catch (Exception ex) {
                    Logger.Error(String.Format("Exception to set {0} creation time for file status update: {1}", filePath, ex));
                }
            }
            else
            {
                double percent = transmission.Status.Percent.GetValueOrDefault() / 100;
                if (percent < 1)
                {
                    Syscall.setxattr(filePath, extendAttrKey, Encoding.ASCII.GetBytes(percent.ToString()));
                    try {
                        NSFileAttributes attr = NSFileManager.DefaultManager.GetAttributes(filePath);
                        attr.CreationDate = new DateTime(1984, 1, 24, 8, 0, 0, DateTimeKind.Utc);
                        NSFileManager.DefaultManager.SetAttributes(attr, filePath);
                    } catch (Exception ex) {
                        Logger.Error(String.Format("Exception to set {0} creation time for file status update: {1}", filePath, ex));
                    }
                }
                else
                {
                    Syscall.removexattr(filePath, extendAttrKey);
                    try {
                        NSFileAttributes attr = NSFileManager.DefaultManager.GetAttributes(filePath);
                        attr.CreationDate = (new FileInfo(filePath)).CreationTime;
                        NSFileManager.DefaultManager.SetAttributes(attr, filePath);
                    } catch (Exception ex) {
                        Logger.Error(String.Format("Exception to set {0} creation time for file status update: {1}", filePath, ex));
                    }
                }
            }
        }
Пример #8
0
        public void CalculateBitsPerSecondWithOneMilisecondDifference()
        {
            DateTime start         = DateTime.Now;
            DateTime end           = start.AddMilliseconds(1);
            long?    bitsPerSecond = TransmissionProgressEventArgs.CalcBitsPerSecond(start, end, 1);

            Assert.AreEqual(8000, bitsPerSecond);
        }
Пример #9
0
        /// <summary>
        /// Close this instance and calculates the bandwidth of the last second.
        /// </summary>
        public override void Close()
        {
            long?result = TransmissionProgressEventArgs.CalcBitsPerSecond(this.start, DateTime.Now.AddMilliseconds(1), this.bytesTransmittedSinceLastSecond);

            this.transmissionEvent.ReportProgress(new TransmissionProgressEventArgs()
            {
                BitsPerSecond = result
            });
            base.Close();
        }
Пример #10
0
        public void CalcBitsPerSecondWithOneSecondDifference()
        {
            DateTime start         = DateTime.Now;
            DateTime end           = start.AddSeconds(1);
            long?    bitsPerSecond = TransmissionProgressEventArgs.CalcBitsPerSecond(start, end, 1);

            Assert.AreEqual(8, bitsPerSecond);
            bitsPerSecond = TransmissionProgressEventArgs.CalcBitsPerSecond(start, start, 100);
            Assert.Null(bitsPerSecond);
            bitsPerSecond = TransmissionProgressEventArgs.CalcBitsPerSecond(start, end, 100);
            Assert.AreEqual(8 * 100, bitsPerSecond);
        }
Пример #11
0
 private void TransmissionReport(object sender, TransmissionProgressEventArgs e)
 {
     using (var a = new NSAutoreleasePool()) {
         FileTransmissionEvent transmission = sender as FileTransmissionEvent;
         if (transmission == null)
         {
             return;
         }
         lock (transmissionLock) {
             if ((e.Aborted == true || e.Completed == true || e.FailedException != null))
             {
                 transmission.TransmissionStatus -= TransmissionReport;
                 transmissionFiles.Remove(transmission.Path);
             }
             else
             {
                 TimeSpan diff = NSDate.Now - transmissionFiles [transmission.Path];
                 if (diff.Seconds < notificationInterval)
                 {
                     return;
                 }
                 transmissionFiles [transmission.Path] = NSDate.Now;
             }
             // UpdateFileStatus (transmission, e);
         }
         notificationCenter.BeginInvokeOnMainThread(delegate
         {
             lock (transmissionLock) {
                 NSUserNotification[] notifications = notificationCenter.DeliveredNotifications;
                 foreach (NSUserNotification notification in notifications)
                 {
                     if (!IsNotificationTransmission(notification))
                     {
                         continue;
                     }
                     bool pathCorrect      = notification.InformativeText == transmission.Path;
                     bool isCompleted      = transmission.Status.Completed == true;
                     bool isAlreadyStarted = startedTransmissions.Contains(transmission.Path);
                     if (pathCorrect && (!isAlreadyStarted || isCompleted))
                     {
                         notificationCenter.RemoveDeliveredNotification(notification);
                         notification.DeliveryDate = NSDate.Now;
                         notification.Subtitle     = TransmissionStatus(transmission);
                         notificationCenter.DeliverNotification(notification);
                         return;
                     }
                 }
             }
         });
     }
 }
Пример #12
0
 /// <summary>
 /// If a transmission is reported as finished/aborted/failed, the transmission is removed from the collection
 /// </summary>
 /// <param name='sender'>
 /// The transmission event.
 /// </param>
 /// <param name='e'>
 /// The progress parameters of the transmission.
 /// </param>
 private void TransmissionFinished(object sender, TransmissionProgressEventArgs e)
 {
     if (e.Aborted == true || e.Completed == true || e.FailedException != null)
     {
         lock (this.collectionLock)
         {
             FileTransmissionEvent transmission = sender as FileTransmissionEvent;
             if (transmission != null && this.activeTransmissions.Contains(transmission))
             {
                 this.activeTransmissions.Remove(transmission);
                 transmission.TransmissionStatus -= this.TransmissionFinished;
                 Logger.Debug("Transmission removed");
             }
         }
     }
 }
Пример #13
0
        private string TransmissionStatus(TransmissionProgressEventArgs e)
        {
            double?percent       = e.Percent;
            long?  bitsPerSecond = e.BitsPerSecond;

            if (percent != null && bitsPerSecond != null)
            {
                return(String.Format("{0} ({1} {2})",
                                     System.IO.Path.GetFileName(transmissionEvent.Path),
                                     CmisSync.Lib.Utils.FormatPercent((double)percent),
                                     CmisSync.Lib.Utils.FormatBandwidth((long)bitsPerSecond)));
            }
            else
            {
                return(System.IO.Path.GetFileName(transmissionEvent.Path));
            }
        }
Пример #14
0
        private string TransmissionStatus(TransmissionProgressEventArgs status)
        {
            double percent       = (status.Percent != null) ? (double)status.Percent : 0;
            long?  bitsPerSecond = status.BitsPerSecond;

            if (bitsPerSecond != null)
            {
                return(String.Format("{0} ({1} {2})",
                                     System.IO.Path.GetFileName(Path),
                                     CmisSync.Lib.Utils.FormatPercent(percent),
                                     CmisSync.Lib.Utils.FormatBandwidth((long)bitsPerSecond)));
            }
            else
            {
                return(String.Format("{0} ({1})",
                                     System.IO.Path.GetFileName(Path),
                                     CmisSync.Lib.Utils.FormatPercent(percent)));
            }
        }
Пример #15
0
        public void PercentTest()
        {
            string filename = "test.txt";
            FileTransmissionEvent transmission = new FileTransmissionEvent(FileTransmissionType.DOWNLOAD_NEW_FILE, filename);
            double?percent = null;

            transmission.TransmissionStatus += delegate(object sender, TransmissionProgressEventArgs e)
            {
                percent = e.Percent;
            };
            transmission.ReportProgress(new TransmissionProgressEventArgs {
            });
            Assert.Null(percent);

            this.expectedArgs = new TransmissionProgressEventArgs
            {
                Length         = 100,
                ActualPosition = 0
            };
            transmission.ReportProgress(this.expectedArgs);
            Assert.AreEqual(0, percent);
            transmission.ReportProgress(new TransmissionProgressEventArgs()
            {
                ActualPosition = 10
            });
            Assert.AreEqual(10, percent);
            transmission.ReportProgress(new TransmissionProgressEventArgs()
            {
                ActualPosition = 100
            });
            Assert.AreEqual(100, percent);
            transmission.ReportProgress(new TransmissionProgressEventArgs()
            {
                Length = 1000
            });
            Assert.AreEqual(10, percent);
            transmission.ReportProgress(new TransmissionProgressEventArgs()
            {
                ActualPosition = 1000, Length = 2000
            });
            Assert.AreEqual(50, percent);
        }
Пример #16
0
        private void AssertThatProgressFitsMinimumLimits(TransmissionProgressEventArgs args, long minLength, double minPercent, long minPos)
        {
            // Console.WriteLine(e.ToString());
            if (args.Length != null)
            {
                Assert.GreaterOrEqual(args.Length, minLength);
                Assert.LessOrEqual(args.Length, this.localContent.Length);
            }

            if (args.Percent != null)
            {
                Assert.GreaterOrEqual(args.Percent, minPercent);
                Assert.LessOrEqual(args.Percent, 100);
            }

            if (args.ActualPosition != null)
            {
                Assert.GreaterOrEqual(args.ActualPosition, minPos);
                Assert.LessOrEqual(args.ActualPosition, this.localContent.Length);
            }
        }
Пример #17
0
        public void ReportProgressTest()
        {
            string filename = "test.txt";
            FileTransmissionEvent transmission = new FileTransmissionEvent(FileTransmissionType.DOWNLOAD_NEW_FILE, filename);

            transmission.TransmissionStatus += this.TransmissionEventHandler;
            this.expectedArgs = new TransmissionProgressEventArgs()
            {
                Length         = 0,
                ActualPosition = 0,
                BitsPerSecond  = 0
            };
            transmission.ReportProgress(this.expectedArgs);
            this.expectedArgs.BitsPerSecond = 1024;
            transmission.ReportProgress(this.expectedArgs);
            this.expectedArgs.Completed = false;
            transmission.ReportProgress(this.expectedArgs);
            this.expectedArgs.Length = 1024;
            transmission.ReportProgress(this.expectedArgs);
            TransmissionProgressEventArgs otherArgs = new TransmissionProgressEventArgs()
            {
                Length         = this.expectedArgs.Length,
                ActualPosition = this.expectedArgs.ActualPosition,
                BitsPerSecond  = this.expectedArgs.BitsPerSecond,
                Completed      = this.expectedArgs.Completed
            };

            transmission.ReportProgress(otherArgs);
            TransmissionProgressEventArgs nullArgs = new TransmissionProgressEventArgs()
            {
                Length         = null,
                ActualPosition = null,
                BitsPerSecond  = null,
                Completed      = null
            };

            transmission.ReportProgress(nullArgs);
        }
Пример #18
0
 public void TestInit()
 {
     this.expectedArgs = null;
 }
Пример #19
0
 private void TransmissionEventHandler(object sender, TransmissionProgressEventArgs e)
 {
     Assert.AreEqual(this.expectedArgs, e, "The reported transmission events doesn't fit to the expected ones");
     Assert.AreEqual(this.expectedArgs.BitsPerSecond, e.BitsPerSecond);
 }