/// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(IConsumer <T> consumer, IProducerContext producerContext)
        {
            IProducerListener producerListener = producerContext.Listener;

            producerListener.OnProducerStart(producerContext.Id, PRODUCER_NAME);

            bool delayRequest;

            lock (_gate)
            {
                if (_numCurrentRequests >= _maxSimultaneousRequests)
                {
                    _pendingRequests.Enqueue(
                        new Tuple <IConsumer <T>, IProducerContext>(consumer, producerContext));

                    delayRequest = true;
                }
                else
                {
                    _numCurrentRequests++;
                    delayRequest = false;
                }
            }

            if (!delayRequest)
            {
                ProduceResultsInternal(consumer, producerContext);
            }
        }
        public void Initialize()
        {
            // Initializes the mock data
            _producerListener = new ProducerListenerImpl(
                (_, __) => { },
                (_, __, ___) => { },
                (_, __, ___) => { },
                (_, __, ___, ____) => { },
                (_, __, ___) => { },
                (_) =>
            {
                return(false);
            });
            _consumer = new BaseConsumerImpl <object>(
                (result, isLast) =>
            {
                ++_consumerOnNewResultCount;
                _consumerInternalResult = result;
                _consumerInternalIsLast = isLast;
            },
                (_) => { },
                () => { },
                (_) => { });
            _producerContext = new SettableProducerContext(
                IMAGE_REQUEST,
                REQUEST_ID,
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);

            _nullProducer = new NullProducer <object>();
        }
        void ProduceResultsInternal(IConsumer <T> consumer, IProducerContext producerContext)
        {
            IProducerListener producerListener = producerContext.Listener;

            producerListener.OnProducerFinishWithSuccess(producerContext.Id, PRODUCER_NAME, null);
            _inputProducer.ProduceResults(new ThrottlerConsumer(this, consumer), producerContext);
        }
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(IConsumer <object> consumer, IProducerContext producerContext)
        {
            DelegatingConsumer <T, object> swallowResultConsumer =
                new SwallowResultConsumer(consumer);

            _inputProducer.ProduceResults(swallowResultConsumer, producerContext);
        }
Esempio n. 5
0
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(
            IConsumer <CloseableReference <CloseableImage> > consumer,
            IProducerContext context)
        {
            IProducerListener     listener                  = context.Listener;
            IPostprocessor        postprocessor             = context.ImageRequest.Postprocessor;
            PostprocessorConsumer basePostprocessorConsumer =
                new PostprocessorConsumer(this, consumer, listener, context.Id, postprocessor, context);

            IConsumer <CloseableReference <CloseableImage> > postprocessorConsumer;

            if (postprocessor.GetType() == typeof(IRepeatedPostprocessor))
            {
                postprocessorConsumer = new RepeatedPostprocessorConsumer(
                    basePostprocessorConsumer,
                    (IRepeatedPostprocessor)postprocessor,
                    context);
            }
            else
            {
                postprocessorConsumer = new SingleUsePostprocessorConsumer(basePostprocessorConsumer);
            }

            _inputProducer.ProduceResults(postprocessorConsumer, context);
        }
Esempio n. 6
0
 /// <summary>
 /// Start producing results for given context.
 /// Provided consumer is notified whenever progress is made
 /// (new value is ready or error occurs).
 /// </summary>
 public void ProduceResults(
     IConsumer <CloseableReference <IPooledByteBuffer> > consumer,
     IProducerContext context)
 {
     _inputProducer.ProduceResults(
         new RemoveImageTransformMetaDataConsumer(this, consumer), context);
 }
 /// <summary>
 /// Instantiates the <see cref="FetchState"/>.
 /// </summary>
 public FetchState(
     IConsumer <EncodedImage> consumer,
     IProducerContext context)
 {
     _consumer = consumer;
     _context  = context;
     _lastIntermediateResultTimeMs = 0;
 }
 /// <summary>
 /// Gets the cache key.
 /// </summary>
 protected override Tuple <ICacheKey, int> GetKey(IProducerContext producerContext)
 {
     return(new Tuple <ICacheKey, int>(
                _cacheKeyFactory.GetEncodedCacheKey(
                    producerContext.ImageRequest,
                    producerContext.CallerContext),
                producerContext.LowestPermittedRequestLevel));
 }
Esempio n. 9
0
 /// <summary>
 /// Instantiates the <see cref="OnFirstImageConsumer"/>.
 /// </summary>
 internal OnFirstImageConsumer(
     IProducer <EncodedImage> inputProducer2,
     IConsumer <EncodedImage> consumer,
     IProducerContext producerContext) :
     base(consumer)
 {
     _inputProducer2  = inputProducer2;
     _producerContext = producerContext;
 }
Esempio n. 10
0
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(
            IConsumer <EncodedImage> consumer,
            IProducerContext context)
        {
            OnFirstImageConsumer onFirstImageConsumer = new OnFirstImageConsumer(
                _inputProducer2, consumer, context);

            _inputProducer1.ProduceResults(onFirstImageConsumer, context);
        }
 public LocalImagesProgressiveDecoder(
     DecodeProducer parent,
     IConsumer <CloseableReference <CloseableImage> > consumer,
     IProducerContext producerContext) : base(
         parent,
         consumer,
         producerContext)
 {
 }
 /// <summary>
 /// Instantiates the <see cref="ThumbnailConsumer"/>.
 /// </summary>
 public ThumbnailConsumer(
     ThumbnailBranchProducer parent,
     IConsumer <EncodedImage> consumer,
     IProducerContext producerContext, int producerIndex) :
     base(consumer)
 {
     _parent          = parent;
     _producerContext = producerContext;
     _producerIndex   = producerIndex;
     _resizeOptions   = _producerContext.ImageRequest.ResizeOptions;
 }
Esempio n. 13
0
 public MessageContext(
     Message message,
     IMessageHeaders headers,
     IConsumerContext consumer,
     IProducerContext producer)
 {
     this.Message         = message;
     this.Headers         = headers ?? new MessageHeaders();
     this.ConsumerContext = consumer;
     this.ProducerContext = producer;
 }
        private void MaybeStartInputProducer(
            IConsumer <EncodedImage> consumerOfDiskCacheProducer,
            IConsumer <EncodedImage> consumerOfInputProducer,
            IProducerContext producerContext)
        {
            if (producerContext.LowestPermittedRequestLevel >= RequestLevel.DISK_CACHE)
            {
                consumerOfDiskCacheProducer.OnNewResult(null, true);
                return;
            }

            _inputProducer.ProduceResults(consumerOfInputProducer, producerContext);
        }
 private void SubscribeTaskForRequestCancellation(
     AtomicBoolean isCancelled,
     IProducerContext producerContext)
 {
     producerContext.AddCallbacks(
         new BaseProducerContextCallbacks(
             () =>
     {
         isCancelled.Value = true;
     },
             () => { },
             () => { },
             () => { }));
 }
 public NetworkImagesProgressiveDecoder(
     DecodeProducer parent,
     IConsumer <CloseableReference <CloseableImage> > consumer,
     IProducerContext producerContext,
     ProgressiveJpegParser progressiveJpegParser,
     IProgressiveJpegConfig progressiveJpegConfig) : base(
         parent,
         consumer,
         producerContext)
 {
     _progressiveJpegParser   = Preconditions.CheckNotNull(progressiveJpegParser);
     _progressiveJpegConfig   = Preconditions.CheckNotNull(progressiveJpegConfig);
     _lastScheduledScanNumber = 0;
 }
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(
            IConsumer <EncodedImage> consumer,
            IProducerContext producerContext)
        {
            IProducerListener listener     = producerContext.Listener;
            string            requestId    = producerContext.Id;
            ImageRequest      imageRequest = producerContext.ImageRequest;
            StatefulProducerRunnable <EncodedImage> cancellableProducerRunnable =
                new StatefulProducerRunnableImpl <EncodedImage>(
                    consumer,
                    listener,
                    ProducerName,
                    requestId,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    (result) =>
            {
                EncodedImage.CloseSafely(result);
            },
                    async() =>
            {
                EncodedImage encodedImage = await GetEncodedImage(imageRequest)
                                            .ConfigureAwait(false);

                if (encodedImage == null)
                {
                    return(null);
                }

                await encodedImage.ParseMetaDataAsync().ConfigureAwait(false);
                return(encodedImage);
            });

            producerContext.AddCallbacks(
                new BaseProducerContextCallbacks(
                    () =>
            {
                cancellableProducerRunnable.Cancel();
            },
                    () => { },
                    () => { },
                    () => { }));

            _executor.Execute(cancellableProducerRunnable.Runnable);
        }
            /// <summary>
            /// Instantiates the <see cref="ProgressiveDecoder"/>.
            /// </summary>
            public ProgressiveDecoder(
                DecodeProducer parent,
                IConsumer <CloseableReference <CloseableImage> > consumer,
                IProducerContext producerContext) :
                base(consumer)
            {
                _parent             = parent;
                _producerContext    = producerContext;
                _producerListener   = producerContext.Listener;
                _imageDecodeOptions = producerContext.ImageRequest.ImageDecodeOptions;
                _isFinished         = false;
                Func <EncodedImage, bool, Task> job = (encodedImage, isLast) =>
                {
                    if (encodedImage != null)
                    {
                        if (_parent._downsampleEnabled)
                        {
                            ImageRequest request = producerContext.ImageRequest;
                            if (_parent._downsampleEnabledForNetwork ||
                                !UriUtil.IsNetworkUri(request.SourceUri))
                            {
                                encodedImage.SampleSize = DownsampleUtil.DetermineSampleSize(
                                    request, encodedImage);
                            }
                        }

                        return(DoDecode(encodedImage, isLast));
                    }

                    return(Task.CompletedTask);
                };

                _jobScheduler = new JobScheduler(
                    _parent._executor, job, _imageDecodeOptions.MinDecodeIntervalMs);

                _producerContext.AddCallbacks(
                    new BaseProducerContextCallbacks(
                        () => { },
                        () => { },
                        () =>
                {
                    if (_producerContext.IsIntermediateResultExpected)
                    {
                        _jobScheduler.ScheduleJob();
                    }
                },
                        () => { }));
            }
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>

        public void ProduceResults(IConsumer <T> consumer, IProducerContext context)
        {
            IProducerListener            producerListener = context.Listener;
            string                       requestId        = context.Id;
            StatefulProducerRunnable <T> statefulRunnable = new StatefulProducerRunnableImpl <T>(
                consumer,
                producerListener,
                PRODUCER_NAME,
                requestId,
                (T ignored) =>
            {
                producerListener.OnProducerFinishWithSuccess(requestId, PRODUCER_NAME, null);
                _inputProducer.ProduceResults(consumer, context);
            },
                null,
                null,
                (_) =>
            {
                return(default(IDictionary <string, string>));
            },
                (_) =>
            {
                return(default(IDictionary <string, string>));
            },
                () =>
            {
                return(default(IDictionary <string, string>));
            },
                null,
                () =>
            {
                return(Task.FromResult(default(T)));
            });

            context.AddCallbacks(
                new BaseProducerContextCallbacks(
                    () =>
            {
                statefulRunnable.Cancel();
                _threadHandoffProducerQueue.Remove(statefulRunnable.Runnable);
            },
                    () => { },
                    () => { },
                    () => { }));

            _threadHandoffProducerQueue.AddToQueueOrExecute(statefulRunnable.Runnable);
        }
 /// <summary>
 /// Start producing results for given context.
 /// Provided consumer is notified whenever progress is made
 /// (new value is ready or error occurs).
 /// </summary>
 public void ProduceResults(
     IConsumer <EncodedImage> consumer,
     IProducerContext producerContext)
 {
     if (producerContext.ImageRequest.ResizeOptions == null)
     {
         consumer.OnNewResult(null, true);
     }
     else
     {
         bool requested = ProduceResultsFromThumbnailProducer(0, consumer, producerContext);
         if (!requested)
         {
             consumer.OnNewResult(null, true);
         }
     }
 }
Esempio n. 21
0
        public static IJobProducerContract Create(string simulationName, Guid sessionID, bool initialize, bool reset)
        {
            int id = -1;
            IProducerContext ctx      = Container.GetAppContext();
            String           userName = ctx.UserName;

            using (TurbineModelContainer container = new TurbineModelContainer())
            {
                Simulation obj     = container.Simulations.OrderByDescending(q => q.Create).First <Simulation>(s => s.Name == simulationName);
                User       user    = container.Users.Single <User>(u => u.Name == userName);
                Session    session = container.Sessions.SingleOrDefault <Session>(i => i.Id == sessionID);
                if (session == null)
                {
                    session = new Session()
                    {
                        Id = sessionID, Create = DateTime.UtcNow, User = user
                    };
                    container.Sessions.AddObject(session);
                    container.SaveChanges();
                }
                System.Diagnostics.Debug.WriteLine(String.Format("simulation {0}, User {1}, Session {2}",
                                                                 simulationName, user.Name, session.Id), "AspenJobContract");
                Job job = new Job()
                {
                    guid       = Guid.NewGuid(),
                    Simulation = obj,
                    State      = "create",
                    Create     = DateTime.UtcNow,
                    Process    = new SinterProcess()
                    {
                        Id = Guid.NewGuid()
                    },
                    User       = user,
                    Session    = session,
                    Initialize = initialize,
                    Reset      = reset,
                };
                container.SaveChanges();
                id = job.Id;
            }
            return(new AspenJobProducerContract()
            {
                Id = id
            });
        }
        private bool ProduceResultsFromThumbnailProducer(
            int startIndex,
            IConsumer <EncodedImage> consumer,
            IProducerContext context)
        {
            int producerIndex = FindFirstProducerForSize(
                startIndex, context.ImageRequest.ResizeOptions);

            if (producerIndex == -1)
            {
                return(false);
            }

            _thumbnailProducers[producerIndex].ProduceResults(
                new ThumbnailConsumer(this, consumer, context, producerIndex), context);

            return(true);
        }
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(IConsumer <EncodedImage> consumer, IProducerContext context)
        {
            context.Listener.OnProducerStart(context.Id, PRODUCER_NAME);
            FetchState fetchState = _networkFetcher.CreateFetchState(consumer, context);

            _networkFetcher.Fetch(fetchState, new NetworkFetcherCallbackImpl(
                                      (response, responseLength) =>
            {
                OnResponse(fetchState, response, responseLength);
            },
                                      (throwable) =>
            {
                OnFailure(fetchState, throwable);
            },
                                      () =>
            {
                OnCancellation(fetchState);
            }));
        }
Esempio n. 24
0
 internal RepeatedPostprocessorConsumer(
     PostprocessorConsumer postprocessorConsumer,
     IRepeatedPostprocessor repeatedPostprocessor,
     IProducerContext context) :
     base(postprocessorConsumer)
 {
     repeatedPostprocessor.SetCallback(this);
     context.AddCallbacks(
         new BaseProducerContextCallbacks(
             () =>
     {
         if (Close())
         {
             Consumer.OnCancellation();
         }
     },
             () => { },
             () => { },
             () => { }));
 }
Esempio n. 25
0
            /// <summary>
            /// Starts next producer if it is not started yet and there is
            /// at least one Consumer waiting for the data. If all consumers
            /// are cancelled, then this multiplexer is removed from _request
            /// map to clean up.
            /// </summary>
            internal void StartInputProducerIfHasAttachedConsumers()
            {
                BaseProducerContext multiplexProducerContext;
                ForwardingConsumer  forwardingConsumer;

                lock (_gate)
                {
                    Preconditions.CheckArgument(_multiplexProducerContext == null);
                    Preconditions.CheckArgument(_forwardingConsumer == null);

                    // Cleanup if all consumers have been cancelled before
                    // this method was called
                    if (_consumerContextPairs.IsEmpty)
                    {
                        _parent.RemoveMultiplexer(_key, this);
                        return;
                    }

                    var iterator = _consumerContextPairs.GetEnumerator();
                    iterator.MoveNext();
                    IProducerContext producerContext = iterator.Current.Key.Item2;
                    _multiplexProducerContext = new BaseProducerContext(
                        producerContext.ImageRequest,
                        producerContext.Id,
                        producerContext.Listener,
                        producerContext.CallerContext,
                        producerContext.LowestPermittedRequestLevel,
                        ComputeIsPrefetch(),
                        ComputeIsIntermediateResultExpected(),
                        ComputePriority());

                    _forwardingConsumer      = new ForwardingConsumer(this);
                    multiplexProducerContext = _multiplexProducerContext;
                    forwardingConsumer       = _forwardingConsumer;
                }

                _inputProducer.ProduceResults(
                    forwardingConsumer,
                    multiplexProducerContext);
            }
Esempio n. 26
0
        public void TestFetchSendsSuccessToCallbackAfterRedirect2()
        {
            ManualResetEvent completion = new ManualResetEvent(false);
            bool             failed     = false;

            _producerContext = new SettableProducerContext(
                ImageRequest.FromUri(SUCCESS_REDIRECT_URL2),
                SUCCESS_REDIRECT_URL2,
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);

            _fetchState = new FetchState(_consumer, _producerContext);
            _fetcher.Fetch(
                _fetchState,
                new NetworkFetcherCallbackImpl(
                    (response, responseLength) =>
            {
                Assert.IsTrue(response != null);
                Assert.IsTrue(responseLength == -1);
                completion.Set();
            },
                    (throwable) =>
            {
                failed = true;
                completion.Set();
            },
                    () =>
            {
                failed = true;
                completion.Set();
            }));

            // Wait for callback
            completion.WaitOne();
            Assert.IsFalse(failed);
        }
Esempio n. 27
0
            /// <summary>
            /// Instantiates the <see cref="TransformingConsumer"/>.
            /// </summary>
            public TransformingConsumer(
                ResizeAndRotateProducer parent,
                IConsumer <EncodedImage> consumer,
                IProducerContext producerContext) :
                base(consumer)
            {
                _parent          = parent;
                _isCancelled     = false;
                _producerContext = producerContext;

                Func <EncodedImage, bool, Task> job = (encodedImage, isLast) =>
                {
                    return(DoTransform(encodedImage, isLast));
                };

                _jobScheduler = new JobScheduler(
                    _parent._executor, job, MIN_TRANSFORM_INTERVAL_MS);

                _producerContext.AddCallbacks(
                    new BaseProducerContextCallbacks(
                        () =>
                {
                    _jobScheduler.ClearJob();
                    _isCancelled = true;

                    // This only works if it is safe to discard the output of
                    // previous producer
                    consumer.OnCancellation();
                },
                        () => { },
                        () =>
                {
                    if (_producerContext.IsIntermediateResultExpected)
                    {
                        _jobScheduler.ScheduleJob();
                    }
                },
                        () => {
                }));
            }
Esempio n. 28
0
        public void TestFetchSendsErrorToCallbackAfterHttpError()
        {
            ManualResetEvent completion = new ManualResetEvent(false);
            bool             failed     = false;

            _producerContext = new SettableProducerContext(
                ImageRequest.FromUri(FAILURE_URL),
                FAILURE_URL,
                _producerListener,
                new object(),
                RequestLevel.FULL_FETCH,
                false,
                true,
                Priority.MEDIUM);
            _fetchState = new FetchState(_consumer, _producerContext);
            _fetcher.Fetch(
                _fetchState,
                new NetworkFetcherCallbackImpl(
                    (response, responseLength) =>
            {
                failed = true;
                completion.Set();
            },
                    (throwable) =>
            {
                Assert.IsTrue(throwable.GetType() == typeof(IOException));
                completion.Set();
            },
                    () =>
            {
                failed = true;
                completion.Set();
            }));

            // Wait for callback
            completion.WaitOne();
            Assert.IsFalse(failed);
        }
Esempio n. 29
0
 internal PostprocessorConsumer(
     PostprocessorProducer parent,
     IConsumer <CloseableReference <CloseableImage> > consumer,
     IProducerListener listener,
     string requestId,
     IPostprocessor postprocessor,
     IProducerContext producerContext) :
     base(consumer)
 {
     _parent        = parent;
     _listener      = listener;
     _requestId     = requestId;
     _postprocessor = postprocessor;
     producerContext.AddCallbacks(
         new BaseProducerContextCallbacks(
             () =>
     {
         MaybeNotifyOnCancellation();
     },
             () => { },
             () => { },
             () => { }));
 }
Esempio n. 30
0
        /// <summary>
        /// Start producing results for given context.
        /// Provided consumer is notified whenever progress is made
        /// (new value is ready or error occurs).
        /// </summary>
        public void ProduceResults(IConsumer <T> consumer, IProducerContext context)
        {
            K           key = GetKey(context);
            Multiplexer multiplexer;
            bool        createdNewMultiplexer;

            // We do want to limit scope of this lock to guard only accesses
            // to _multiplexers map. However what we would like to do here
            // is to atomically lookup _multiplexers, add new consumer to
            // consumers set associated with the map's entry and call consumer's
            // callback with last intermediate result. We should not do all of
            // those things under _gate lock.
            do
            {
                createdNewMultiplexer = false;
                lock (_gate)
                {
                    multiplexer = GetExistingMultiplexer(key);
                    if (multiplexer == null)
                    {
                        multiplexer           = CreateAndPutNewMultiplexer(key);
                        createdNewMultiplexer = true;
                    }
                }

                // AddNewConsumer may call consumer's OnNewResult method immediately.
                // For this reason we release _gate lock. If multiplexer is removed
                // from _multiplexers in the meantime, which is not very probable,
                // then AddNewConsumer will fail and we will be able to retry.
            }while (!multiplexer.AddNewConsumer(consumer, context));

            if (createdNewMultiplexer)
            {
                multiplexer.StartInputProducerIfHasAttachedConsumers();
            }
        }