Exemplo n.º 1
0
        public override TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            switch (data.DataType) {
                case TypedDataType.Image:
                    var rawImageProgress = new MutableProgressTracker();
                    var uploadProgress = new MutableProgressTracker();

                    var aggregateProgress = new AggregateProgressTracker(new ProgressTrackerCollection {
                        { rawImageProgress, ImageWriter.ProcessTimeEstimate.ProgressWeight },
                        { uploadProgress, ProcessTimeEstimate.ProgressWeight },
                    });

                    aggregateProgress.BindTo(progress);

                    progress.Status = "Converting image";

                    using (var rawImageData = ImageWriter.Process(data, rawImageProgress, cancelToken)) {
                        progress.Status = "Uploading image";

                        return Upload(rawImageData, uploadProgress, cancelToken);
                    }

                default:
                    return null;
            }
        }
        public void ProgressChangesWhenChildChanges()
        {
            var npts = new[] {
                GetTrackerItem(1, 0),
                GetTrackerItem(1, 0),
                GetTrackerItem(1, 0),
                GetTrackerItem(1, 0),
            };

            var apt = new AggregateProgressTracker(npts);

            Assert.AreEqual(0 / 4.0, apt.Progress);

            ((IMutableProgressTracker) npts[0].ProgressTracker).Progress = 1;
            Assert.AreEqual(1 / 4.0, apt.Progress);

            ((IMutableProgressTracker) npts[1].ProgressTracker).Progress = 1;
            Assert.AreEqual(2 / 4.0, apt.Progress);

            ((IMutableProgressTracker) npts[2].ProgressTracker).Progress = 1;
            Assert.AreEqual(3 / 4.0, apt.Progress);

            ((IMutableProgressTracker) npts[3].ProgressTracker).Progress = 1;
            Assert.AreEqual(4 / 4.0, apt.Progress);

            ((IMutableProgressTracker) npts[2].ProgressTracker).Progress = 0.5;
            Assert.AreEqual(7 / 8.0, apt.Progress);
        }
        public void ProgressAverage()
        {
            var npts = new[] {
                GetTrackerItem(1, 0),
                GetTrackerItem(1, 0),
                GetTrackerItem(1, 0),
                GetTrackerItem(1, 0),
            };

            var apt = new AggregateProgressTracker(npts);

            Assert.AreEqual(0, apt.Progress);

            npts = new[] {
                GetTrackerItem(1, 1),
                GetTrackerItem(1, 0.5),
                GetTrackerItem(1, 0.5),
                GetTrackerItem(1, 0),
            };

            apt = new AggregateProgressTracker(npts);

            Assert.AreEqual((1 + 0.5 + 0.5 + 0) / 4, apt.Progress);

            npts = new[] {
                GetTrackerItem(1, 1),
                GetTrackerItem(1, 1),
                GetTrackerItem(1, 1),
                GetTrackerItem(1, 1),
            };

            apt = new AggregateProgressTracker(npts);

            Assert.AreEqual(1, apt.Progress);
        }
Exemplo n.º 4
0
        public static HttpWebResponse Execute(Uri uri, Stream requestData, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            // QUICK HACK

            var requestProgress = new MutableProgressTracker();
            var responseProgress = new MutableProgressTracker();

            var aggregateProgress = new AggregateProgressTracker(new ProgressTrackerCollection {
                { requestProgress,  TimeEstimates.LongOperation.ProgressWeight },
                { responseProgress, TimeEstimates.ShortOperation.ProgressWeight },
            });

            aggregateProgress.BindTo(progress);

            try {
                var request = CreateRequest(uri, @"POST");
                request.ContentLength = requestData.Length;

                using (var requestStream = request.GetRequestStream())
                using (var progressStream = new ProgressTrackingStreamWrapper(requestStream, request.ContentLength)) {
                    progressStream.BindTo(requestProgress);

                    requestData.CopyTo(progressStream, cancelToken);
                }

                bool canCancel = true;

                cancelToken.Register(() => {
                    if (canCancel) {
                        request.Abort();
                    }
                });

                var response = (HttpWebResponse) request.GetResponse();

                responseProgress.Progress = 1; // TODO HTTP download Progress

                canCancel = false;

                return response;
            } catch (WebException e) {
                if (e.Status == WebExceptionStatus.RequestCanceled) {
                    throw new OperationCanceledException(e.Message, e, cancelToken);
                }

                throw new OperationCanceledException(e.Message, e, cancelToken);
            }
        }
Exemplo n.º 5
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            progress.Status = "Uploading data";

            var galleryProgress = new MutableProgressTracker();
            var uploadProgress = new MutableProgressTracker();

            var aggregateProgress = new AggregateProgressTracker(new ProgressTrackerCollection {
                { galleryProgress, TimeEstimates.ShortOperation.ProgressWeight },
                { uploadProgress,  TimeEstimates.LongOperation.ProgressWeight },
            });

            aggregateProgress.BindTo(progress);

            var galleryAccessData = CreateGallery(galleryProgress, cancelToken);

            return UploadData(galleryAccessData, data, uploadProgress, cancelToken);
        }
Exemplo n.º 6
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            // ToList is needed to prevent new MPT's from being
            // created for each loop of commandProgressTrackers
            var commandProgressTrackers = this.commands.Select((command) => new {
                ProgressTracker = new MutableProgressTracker(),
                Command = command
            }).ToList();

            var aggregateProgress = new AggregateProgressTracker(commandProgressTrackers.Select(
                (cpt) => new ProgressTrackerCollectionItem(
                    cpt.ProgressTracker,
                    cpt.Command.ProcessTimeEstimate.ProgressWeight
                )
            ));

            aggregateProgress.BindTo(progress);

            bool shouldDisposeData = false;

            foreach (var cpt in commandProgressTrackers) {
                cancelToken.ThrowIfCancellationRequested();

                TypedData newData;

                try {
                    cancelToken.ThrowIfCancellationRequested();

                    if (!cpt.Command.IsValid()) {
                        throw new CommandInvalidException(cpt.Command);
                    }

                    newData = cpt.Command.Process(data, cpt.ProgressTracker, cancelToken);

                    cancelToken.ThrowIfCancellationRequested();
                } finally {
                    if (shouldDisposeData && data != null) {
                        data.Dispose();
                    }
                }

                data = newData;
                shouldDisposeData = true;
            }

            return data;
        }
Exemplo n.º 7
0
        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            if (string.IsNullOrEmpty(UserName)) {
                throw new InvalidOperationException("User name must be set");
            }

            if (Password == null) {
                throw new InvalidOperationException("Password must be set");
            }

            var progressTrackerCollection = new ProgressTrackerCollection {
                { new MutableProgressTracker(), 1 },
                { new MutableProgressTracker(), 1 },
                { new MutableProgressTracker(), 2 },
                { new MutableProgressTracker(), 100 },
            };

            var progressTrackers = progressTrackerCollection.Select((item) => item.ProgressTracker).OfType<IMutableProgressTracker>().ToArray();

            var aggregateProgressTracker = new AggregateProgressTracker(progressTrackerCollection);
            aggregateProgressTracker.BindTo(progress);

            string token = FetchToken(progressTrackers[0], cancelToken);
            string sessionKey = FetchSessionKey(token, progressTrackers[1], cancelToken);

            string uploadId, uploadInfo;
            ulong maxFileSize;
            var uploadUri = FetchUploadUri(sessionKey, out uploadId, out uploadInfo, out maxFileSize, progressTrackers[2], cancelToken);

            var uri = UploadData(data, uploadUri, uploadId, uploadInfo, maxFileSize, progressTrackers[3], cancelToken);

            return TypedData.FromUri(uri, data.Name);
        }
        public void ProgressUsesWeights()
        {
            var npts = new[] {
                GetTrackerItem(1, 0.5),
                GetTrackerItem(2, 0.25),
                GetTrackerItem(4, 1),
            };

            var apt = new AggregateProgressTracker(npts);

            Assert.AreEqual((1 * 0.5 + 2 * 0.25 + 4 * 1) / (1 + 2 + 4), apt.Progress);
        }
Exemplo n.º 9
0
        public static HttpWebResponse Execute(Uri uri, MultipartData requestData, HttpRequestMethod requestMethod, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            var requestProgress = new MutableProgressTracker();
            var responseProgress = new MutableProgressTracker();

            var aggregateProgress = new AggregateProgressTracker(new ProgressTrackerCollection {
                { requestProgress,  TimeEstimates.LongOperation.ProgressWeight },
                { responseProgress, TimeEstimates.ShortOperation.ProgressWeight },
            });

            aggregateProgress.BindTo(progress);

            try {
                var request = BuildRequest(uri, requestMethod, requestData, requestProgress, cancelToken);

                bool canCancel = true;

                cancelToken.Register(() => {
                    if (canCancel) {
                        request.Abort();
                    }
                });

                var response = (HttpWebResponse) request.GetResponse();

                responseProgress.Progress = 1; // TODO HTTP download Progress

                canCancel = false;

                return response;
            } catch (WebException e) {
                if (e.Status == WebExceptionStatus.RequestCanceled) {
                    throw new OperationCanceledException(e.Message, e, cancelToken);
                }

                throw new OperationCanceledException(e.Message, e, cancelToken);
            }
        }