Base class that will handle the processing of a CacheRequest
Example #1
0
        /// <summary>
        /// Method that refreshes the Cache by uploading all modified changes and then downloading the
        /// server changes.
        /// </summary>
        /// <returns>A CacheRefreshStatistics object denoting the statistics from the Refresh call</returns>
        public CacheRefreshStatistics Refresh()
        {
            this._controllerBehavior.Locked = true;
            try
            {
                // First create the CacheRequestHandler
                this._cacheRequestHandler = CacheRequestHandler.CreateRequestHandler(this._serviceUri, this._controllerBehavior);

                CacheRefreshStatistics refreshStats = new CacheRefreshStatistics();
                refreshStats.StartTime = DateTime.Now;

                bool uploadComplete = false;
                bool downloadComplete = false;

                // Start sync by executin an Upload request
                while (!uploadComplete || !downloadComplete)
                {
                    if (!uploadComplete)
                    {
                        Guid changeSetId = Guid.NewGuid();

                        ChangeSet changeSet = this._localProvider.GetChangeSet(changeSetId);

                        if (changeSet.Data == null || changeSet.Data.Count == 0)
                        {
                            // No data to upload. Skip upload phase.
                            uploadComplete = true;
                        }
                        else
                        {
                            // Create a SyncRequest out of this.
                            CacheRequest request = new CacheRequest()
                            {
                                RequestId = changeSetId,
                                RequestType = CacheRequestType.UploadChanges,
                                Changes = changeSet.Data,
                                KnowledgeBlob = changeSet.ServerBlob,
                                IsLastBatch = changeSet.IsLastBatch
                            };

                            // Increment the stats
                            refreshStats.TotalChangeSetsUploaded++;

                            ChangeSetResponse response = (ChangeSetResponse)this._cacheRequestHandler.ProcessCacheRequest(request);

                            // Increment the stats
                            refreshStats.TotalUploads += (uint)request.Changes.Count;
                            response.ConflictsInternal.ForEach((e1) =>
                            {
                                if (e1 is SyncConflict)
                                {
                                    refreshStats.TotalSyncConflicts++;
                                }
                                else
                                {
                                    refreshStats.TotalSyncErrors++;
                                }
                            });

                            // Send the response to the local provider
                            this._localProvider.OnChangeSetUploaded(changeSetId, response);

                            uploadComplete = request.IsLastBatch;
                        }
                    }
                    else if (!downloadComplete)
                    {
                        // Create a SyncRequest for download.
                        CacheRequest request = new CacheRequest()
                        {
                            RequestType = CacheRequestType.DownloadChanges,
                            KnowledgeBlob = this.LocalProvider.GetServerBlob()
                        };

                        ChangeSet changeSet = (ChangeSet)this._cacheRequestHandler.ProcessCacheRequest(request);

                        // Increment the refresh stats
                        refreshStats.TotalChangeSetsDownloaded++;
                        refreshStats.TotalDownloads += (uint)changeSet.Data.Count;

                        // Call the SaveChangeSet method on local provider.
                        this.LocalProvider.SaveChangeSet(changeSet);

                        downloadComplete = changeSet.IsLastBatch;
                    }
                }

                refreshStats.EndTime = DateTime.Now;
                // Finally return the statistics object
                return refreshStats;

            }
            finally
            {
                // Unlock the ControllerBehavior object
                this._controllerBehavior.Locked = false;
            }
        }
Example #2
0
        /// <summary>
        /// Method that refreshes the Cache by uploading all modified changes and then downloading the
        /// server changes.
        /// </summary>
        /// <returns>A CacheRefreshStatistics object denoting the statistics from the Refresh call</returns>
        public CacheRefreshStatistics Refresh()
        {
            this._controllerBehavior.Locked = true;
            try
            {
                // First create the CacheRequestHandler
                this._cacheRequestHandler = CacheRequestHandler.CreateRequestHandler(this._serviceUri, this._controllerBehavior);

                CacheRefreshStatistics refreshStats = new CacheRefreshStatistics();
                refreshStats.StartTime = DateTime.Now;

                bool uploadComplete   = false;
                bool downloadComplete = false;

                // Start sync by executin an Upload request
                while (!uploadComplete || !downloadComplete)
                {
                    if (!uploadComplete)
                    {
                        Guid changeSetId = Guid.NewGuid();

                        ChangeSet changeSet = this._localProvider.GetChangeSet(changeSetId);

                        if (changeSet.Data == null || changeSet.Data.Count == 0)
                        {
                            // No data to upload. Skip upload phase.
                            uploadComplete = true;
                        }
                        else
                        {
                            // Create a SyncRequest out of this.
                            CacheRequest request = new CacheRequest()
                            {
                                RequestId     = changeSetId,
                                RequestType   = CacheRequestType.UploadChanges,
                                Changes       = changeSet.Data,
                                KnowledgeBlob = changeSet.ServerBlob,
                                IsLastBatch   = changeSet.IsLastBatch
                            };

                            // Increment the stats
                            refreshStats.TotalChangeSetsUploaded++;

                            ChangeSetResponse response = (ChangeSetResponse)this._cacheRequestHandler.ProcessCacheRequest(request);

                            // Increment the stats
                            refreshStats.TotalUploads += (uint)request.Changes.Count;
                            response.ConflictsInternal.ForEach((e1) =>
                            {
                                if (e1 is SyncConflict)
                                {
                                    refreshStats.TotalSyncConflicts++;
                                }
                                else
                                {
                                    refreshStats.TotalSyncErrors++;
                                }
                            });

                            // Send the response to the local provider
                            this._localProvider.OnChangeSetUploaded(changeSetId, response);

                            uploadComplete = request.IsLastBatch;
                        }
                    }
                    else if (!downloadComplete)
                    {
                        // Create a SyncRequest for download.
                        CacheRequest request = new CacheRequest()
                        {
                            RequestType   = CacheRequestType.DownloadChanges,
                            KnowledgeBlob = this.LocalProvider.GetServerBlob()
                        };

                        ChangeSet changeSet = (ChangeSet)this._cacheRequestHandler.ProcessCacheRequest(request);

                        // Increment the refresh stats
                        refreshStats.TotalChangeSetsDownloaded++;
                        refreshStats.TotalDownloads += (uint)changeSet.Data.Count;

                        // Call the SaveChangeSet method on local provider.
                        this.LocalProvider.SaveChangeSet(changeSet);

                        downloadComplete = changeSet.IsLastBatch;
                    }
                }

                refreshStats.EndTime = DateTime.Now;
                // Finally return the statistics object
                return(refreshStats);
            }
            finally
            {
                // Unlock the ControllerBehavior object
                this._controllerBehavior.Locked = false;
            }
        }