Example #1
0
        public NetworkResponse Fetch(CacheIndex cacheIndex, CacheIndexItem cacheIndexItem, int timeoutMilliseconds)
        {
            NetworkResourceArguments args = new NetworkResourceArguments()
            {
                Headers             = Device.RequestInjectionHeaders,
                TimeoutMilliseconds = timeoutMilliseconds,
            };

            return(Fetch(cacheIndex, cacheIndexItem, args));
        }
Example #2
0
        /// <summary>
        /// Enqueues the specified cache index delegate.
        /// </summary>
        /// <param name="cacheIndexDelegate">The cache index delegate.</param>
        /// <param name="cacheIndexItem">The cache index item.</param>
        public void Enqueue(CacheIndexDelegate cacheIndexDelegate, CacheIndexItem cacheIndexItem)
        {
            CacheIndexDelegateCall cacheIndexDelegateCall = new CacheIndexDelegateCall()
            {
                Delegate = cacheIndexDelegate,
                Item     = cacheIndexItem
            };

            base.Enqueue(cacheIndexDelegateCall);

            TriggerTimer();
        }
Example #3
0
        /// <summary>
        /// Extracts a resoure into the NRL cache files and applies metadata.
        /// </summary>
        /// <param name="uri">The URI of the resource.</param>
        /// <param name="metadataFileName">Name of the metadata file.</param>
        /// <param name="headers">The headers for the request.</param>
        /// <param name="timeout">The timeout for the requst in milliseconds.</param>
        /// <param name="postObject">The object to post.</param>
        /// <param name="serializationFormat">The serialization format.</param>
        /// <returns></returns>
        public NetworkResponse Extract(string uri, string metadataFileName, IDictionary <string, string> headers, int timeout,
                                       object postObject, SerializationFormat serializationFormat)
        {
            // if cache is not supported then don't bother extracting.
            if (Device.NetworkGetMethod == MonoCross.Utilities.Network.NetworkGetMethod.NoCache)
            {
                Device.Log.Debug("Cache is not supported, Zip file extraction request ignored.");
                return(new NetworkResponse()
                {
                    StatusCode = HttpStatusCode.OK,
                    Message = "Cache is not supported, Zip file extraction request ignored.",
                    URI = uri,
                    Verb = "GET"
                });
            }

            // validate parameter
            if (string.IsNullOrEmpty(uri))
            {
                throw new ArgumentNullException("uri");
            }

            if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
            {
                throw new ArgumentNullException("Cache Zip location is not a valid Absolute URI " + uri, "uri");
            }

            DateTime dtMetric = DateTime.UtcNow;

            CacheIndex     cacheIndex        = CacheIndexMap.GetFromUri(uri);
            CacheIndexItem zipCacheIndexItem = cacheIndex.Get(uri);

            zipCacheIndexItem.PreFetch = false;

            NetworkResponse networkResponse = null;

            bool idleQueueEnabled = Device.Thread.IdleQueueEnabled;

            try
            {
                // temporarily deactivate idle queue thread.
                if (idleQueueEnabled)
                {
                    Device.Thread.IdleQueueEnabled = false;
                }

                // download zip file from uri
                string zipFileName = cacheIndex.GetCachePath(zipCacheIndexItem);
                networkResponse = DownloadZipFile(uri, zipFileName, headers, timeout, postObject); //, serializationFormat );

                if (networkResponse.StatusCode != HttpStatusCode.OK)
                {
                    return(networkResponse);
                }

                string cachePath = cacheIndex.CachePath.AppendPath(cacheIndex.BaseUriPath);

                // extract zip file into NRL cache
                networkResponse.Message = "Extract Zip File";
                ExtractZipFile(zipFileName, cachePath);

                // process metadata for extracted files.
                networkResponse.Message = "Import Metadata";
                metadataFileName        = cacheIndex.CachePath.AppendPath(cacheIndex.BaseUriPath).AppendPath(metadataFileName);
                ImportMetadata(metadataFileName, cacheIndex);

                Device.Log.Metric(string.Format("Extract and process zip file: file: {0} Time: {1} milliseconds", zipFileName, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds));

                networkResponse.Message = "Complete";
            }
            //catch ( WebException ex )
            //{
            //    if ( ex.Response != null )
            //    {
            //        networkResponse.StatusCode = ( (HttpWebResponse) ex.Response ).StatusCode;
            //        ex.Data["StatusDescription"] = ( (HttpWebResponse) ex.Response ).StatusDescription;
            //    }
            //    else
            //    {
            //        networkResponse.StatusCode = (HttpStatusCode) ( -2 );
            //    }
            //    networkResponse.WebExceptionStatusCode = ex.Status;

            //    Device.Log.Error( ex );
            //    networkResponse.Exception = ex;
            //}
            catch (Exception exc)
            {
                Device.Log.Error(exc);
                networkResponse.Exception  = exc;
                networkResponse.StatusCode = (HttpStatusCode)(-1);
            }
            finally
            {
                // remove zip file after processing is complete. perhaps in a finally block
                if (cacheIndex != null && zipCacheIndexItem != null)
                {
                    cacheIndex.RemoveCurrentCache(zipCacheIndexItem);
                    // cacheIndex.Remove( zipCacheIndexItem );
                }

                // reactivate queue thread if it was previously active.
                if (idleQueueEnabled)
                {
                    Device.Thread.IdleQueueEnabled = true;
                }

                // Serialize Cache Index due to changes.
                CacheIndex.SerializeCacheIndex(cacheIndex);
            }

            return(networkResponse);
        }
Example #4
0
        /// <summary>
        /// Fetches the specified cache index item from the cache index.
        /// </summary>
        /// <param name="cacheIndex">Index of the cache.</param>
        /// <param name="cacheIndexItem">The cache index item.</param>
        /// <param name="args">NetworkResourceArguments for the request</param>
        /// <returns></returns>
        public NetworkResponse Fetch(CacheIndex cacheIndex, CacheIndexItem cacheIndexItem, NetworkResourceArguments args) //IDictionary<string, string> headers, int timeoutMilliseconds)
        {
            PostNetworkResponse = new NetworkResponse();
            var fetchParameters = new FetchParameters
            {
                CacheIndex        = cacheIndex,
                CacheIndexItem    = cacheIndexItem,
                Headers           = args.Headers,
                DefaultExpiration = args.Expiration,
            };
            int timeoutMilliseconds = args.TimeoutMilliseconds;

            DateTime dtMetric = DateTime.UtcNow;

            // set callback and error handler
            OnDownloadComplete += CacheFetcher_OnDownloadComplete;
            OnError            += CacheFetcher_OnError;

            Exception threadExc = null;

            Device.Thread.QueueWorker(parameters =>
            {
                try
                {
                    FetchAsynch(parameters, timeoutMilliseconds);
                }
                catch (Exception e)
                {
                    // You could react or save the exception to an 'outside' variable
                    threadExc = e;
                }
                finally
                {
                    _autoEvent.Set(); // if you're firing and not forgetting ;)
                }
            }, fetchParameters);

            // WaitOne returns true if autoEvent were signaled (i.e. process completed before timeout expired)
            // WaitOne returns false it the timeout expired before the process completed.
            if (!_autoEvent.WaitOne(timeoutMilliseconds))
            {
                string message = "CacheFetcher call to FetchAsynch timed out. uri " + fetchParameters.CacheIndexItem.RelativeUri;
                Device.Log.Metric(string.Format("CacheFetcher timed out: Uri: {0} Time: {1:F0} milliseconds ", fetchParameters.CacheIndexItem.RelativeUri, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds));

                var networkResponse = new NetworkResponse
                {
                    Message                = message,
                    URI                    = fetchParameters.CacheIndex.GetAbsouteUri(fetchParameters.CacheIndexItem),
                    StatusCode             = HttpStatusCode.RequestTimeout,
                    WebExceptionStatusCode = WebExceptionStatus.RequestCanceled, // not using ConnectFailure because connection may have succeeded
                    ResponseString         = string.Empty,
                    Expiration             = DateTime.MinValue.ToUniversalTime(),
                    Downloaded             = DateTime.MinValue.ToUniversalTime(),
                    AttemptToRefresh       = DateTime.MinValue.ToUniversalTime(),
                    Exception              = threadExc,
                };

                return(networkResponse);
            }
            else if (threadExc != null)
            {
                PostNetworkResponse.Exception  = threadExc;
                PostNetworkResponse.Message    = "CacheFetcher.FetchAsync threw an exception";
                PostNetworkResponse.StatusCode = (HttpStatusCode)(-1);
            }

            Device.Log.Metric(string.Format("CacheFetcher Completed: Uri: {0} Time: {1:F0} milliseconds ", PostNetworkResponse.URI, DateTime.UtcNow.Subtract(dtMetric).TotalMilliseconds));

            return(PostNetworkResponse);
        }