Example #1
0
        /// <summary>
        /// Gets a collection from the cache
        /// </summary>
        /// <param name="collectionIdentifier"></param>
        /// <returns></returns>
        private async Task <IonCollection> getCollectionFromCacheAsync(CollectionCacheIndex cacheIndex, bool serverCallAsBackup)
        {
            string collectionURL = PagesURLs.getCollectionURL(_config);

            // retrieve from memory cache
            IonCollection collection = _memoryCache.collection;

            if (collection != null)
            {
                return(collection);
            }

            // try to load collection from isolated storage
            try
            {
                collection = await StorageUtils.loadCollectionFromIsolatedStorageAsync(_config).ConfigureAwait(false);

                // Add collection to memory cache
                if (collection != null)
                {
                    _memoryCache.collection = collection;
                }
            }
            catch (Exception e)
            {
                IonLogging.log("Error getting collection from isolated storage. Message: " + e.Message, IonLogMessageTypes.ERROR);
            }

            return(collection);
        }
Example #2
0
        /// <summary>
        /// Used to get the collection of the class
        /// </summary>
        /// <returns>The collection of this pages</returns>
        public async Task <IonCollection> getCollectionAsync()
        {
            string collectionURL            = PagesURLs.getCollectionURL(_config);
            CollectionCacheIndex cacheIndex = await CollectionCacheIndex.retrieve(collectionURL, _config).ConfigureAwait(false);

            // Check if there is a not outdated cacheIndex avialible
            bool currentCacheEntry = cacheIndex != null && !cacheIndex.isOutdated(_config);
            bool networkConnected  = NetworkUtils.isOnline();


            if (currentCacheEntry)
            {
                // retrieve current version from cache
                IonLogging.log("Loading collection \"" + _config.collectionIdentifier + "\" from cache.", IonLogMessageTypes.SUCCESS);
                return(await getCollectionFromCacheAsync(cacheIndex, false).ConfigureAwait(false));
            }
            else
            {
                if (networkConnected)
                {
                    // download collection or check for modifications
                    IonLogging.log("Loading collection \"" + _config.collectionIdentifier + "\" from server.", IonLogMessageTypes.SUCCESS);
                    return(await getCollectionFromServerAsync(cacheIndex, false).ConfigureAwait(false));
                }
                else
                {
                    if (cacheIndex != null)
                    {
                        // no network: use potential old version from cache
                        IonLogging.log("Using potentially old version of collection \"" + _config.collectionIdentifier + "\" from cache, because there is no internet connection.", IonLogMessageTypes.WARNING);
                        return(await getCollectionFromCacheAsync(cacheIndex, false).ConfigureAwait(false));
                    }
                    else
                    {
                        // Collection can neither be downloaded nor be found in cache
                        IonLogging.log("Couldn't get collection \"" + _config.collectionIdentifier + "\" either from server or cache.", IonLogMessageTypes.ERROR);
                        throw new CollectionNotAvailableException();
                    }
                }
            }
        }
Example #3
0
        private async Task <bool> IsFileUpToDateAsync(string url, string checksum)
        {
            FileCacheIndex fileCacheIndex = await FileCacheIndex.retrieveAsync(url, _config).ConfigureAwait(false);

            if (fileCacheIndex == null)
            {
                return(false);
            }

            if (checksum != null)
            {
                // check with file's checksum
                return(!fileCacheIndex.IsOutdated(checksum));
            }
            else
            {
                // check with collection's last_modified (previewPage.last_changed would be slightly more precise)
                CollectionCacheIndex collectionCacheIndex = await CollectionCacheIndex.retrieve(_config).ConfigureAwait(false);

                DateTime collectionLastModified = collectionCacheIndex == null ? default(DateTime) : collectionCacheIndex.lastModified;
                return(collectionLastModified != null && fileCacheIndex.lastUpdated != null && !(collectionLastModified.CompareTo(fileCacheIndex.lastUpdated) > 0));
            }
        }
Example #4
0
 /// <summary>
 /// Saves the collection cache index
 /// </summary>
 /// <param name="lastModified"></param>
 private async Task saveCollectionCacheIndexAsync(DateTime lastModified)
 {
     await CollectionCacheIndex.save(_config, lastModified).ConfigureAwait(false);
 }
Example #5
0
        /// <summary>
        /// Gets a collection from the server
        /// </summary>
        /// <param name="collectionIdentifier"></param>
        /// <returns></returns>
        private async Task <IonCollection> getCollectionFromServerAsync(CollectionCacheIndex cacheIndex, bool cacheAsBackup)
        {
            //DateTime lastModified = cacheIndex != null ? cacheIndex.lastModified : DateTime.MinValue;

            try
            {
                // Retrive collecion from server and parse it
                HttpResponseMessage response = await _dataClient.getCollectionAsync(_config.collectionIdentifier, cacheIndex != null?cacheIndex.lastModified : DateTime.MinValue).ConfigureAwait(false);

                // Only parse the answer if it is not newer than the cached version
                if (!(response.StatusCode == System.Net.HttpStatusCode.NotModified))
                {
                    // Parse collection
                    IonCollection collection = await DataParser.parseCollectionAsync(response).ConfigureAwait(false);

                    // Add collection to memory cache
                    _memoryCache.collection = collection;

                    // Save collection to isolated storage
                    await StorageUtils.saveCollectionToIsolatedStorageAsync(collection, _config).ConfigureAwait(false);

                    // save cacheIndex
                    await saveCollectionCacheIndexAsync(collection.last_changed).ConfigureAwait(false);

                    return(collection);
                }
                else
                {
                    // Collection in the server is the same as stored already in isolated storage cache
                    if (_memoryCache.collection == null)
                    {
                        // Only load collection from isolated storage cache, if the memory cache has no collection cached
                        try
                        {
                            // Get collection from isolated storage
                            IonCollection collection = await StorageUtils.loadCollectionFromIsolatedStorageAsync(_config).ConfigureAwait(false);

                            // Add collection to memory cache
                            if (collection != null)
                            {
                                _memoryCache.collection = collection;
                            }

                            // change the last-mofied date in the cacheIndex to now
                            await saveCollectionCacheIndexAsync(collection.last_changed).ConfigureAwait(false);

                            return(collection);
                        }
                        catch (Exception e)
                        {
                            IonLogging.log("Error getting collection from isolated storage. Message: " + e.Message, IonLogMessageTypes.ERROR);
                            return(null);
                        }
                    }
                    else
                    {
                        // change the last-mofied date in the cacheIndex to now
                        await saveCollectionCacheIndexAsync(_memoryCache.collection.last_changed).ConfigureAwait(false);

                        return(_memoryCache.collection);
                    }
                }
            }
            catch (Exception e)
            {
                IonLogging.log("Error retreiving collection data: " + e.Message, IonLogMessageTypes.ERROR);
                return(null);
            }
        }