Example #1
0
        /// <summary>
        /// Removes the specified cache index.
        /// </summary>
        /// <param name="cacheIndex">Index of the cache.</param>
        public static void Remove(CacheIndex cacheIndex)
        {
            if (cacheIndex == null)
            {
                throw new ArgumentNullException("cacheIndex", "Requested CacheIndex is null");
            }

            string key = CacheIndexMap.GetKey(cacheIndex);

            CacheIndexMap.Remove(key);
        }
Example #2
0
        /// <summary>
        /// Updates the specified cache index.
        /// </summary>
        /// <param name="cacheIndex">Index of the cache.</param>
        public static void Update(CacheIndex cacheIndex)
        {
            if (cacheIndex == null)
            {
                throw new ArgumentNullException("cacheIndex");
            }

            string key = CacheIndexMap.GetKey(cacheIndex);

            // if the key exists in the map then replace it with current parameter..
            if (Map.ContainsKey(key))
            {
                CacheIndexMap.Remove(key);
            }

            CacheIndexMap.Add(cacheIndex);
        }
Example #3
0
        /// <summary>
        /// Gets the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static CacheIndex Get(string key)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", "Requested CacheIndex key is null or empty");
            }

            CacheIndex cacheIndex;

            if (!Map.TryGetValue(key, out cacheIndex))
            {
                cacheIndex = CacheIndex.Create(key);
                CacheIndexMap.Add(cacheIndex);
            }

            return(cacheIndex);
        }
Example #4
0
        /// <summary>
        /// Adds the specified cache index.
        /// </summary>
        /// <param name="cacheIndex">The cache index to add to the map.</param>
        public static void Add(CacheIndex cacheIndex)
        {
            if (cacheIndex == null)
            {
                throw new ArgumentNullException("cacheIndex");
            }

            string key = CacheIndexMap.GetKey(cacheIndex);

            lock (Map)
            {
                // if the cacheIndex key doesn't already exist in the map then add it.
                if (!Map.ContainsKey(key))
                {
                    Map.Add(key, cacheIndex);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Returns cache index that matches Uri
        /// </summary>
        /// <param name="uriString">Absolute Uri for resource represented by Uri</param>
        /// <returns></returns>
        public static CacheIndex GetFromUri(string uriString)
        {
            if (String.IsNullOrEmpty(uriString))
            {
                throw new ArgumentNullException("uriString");
            }

            string key = GetKeyFromUri(uriString);

            // To-Do: should this really create a cache index item
            CacheIndex cacheIndex;

            if (!Map.TryGetValue(key, out cacheIndex))
            {
                cacheIndex = CacheIndex.Create(key);
                CacheIndexMap.Add(cacheIndex);
            }

            return(cacheIndex);
        }
Example #6
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);
        }