Example #1
0
 // Reset the state of the objects
 void ResetAsyncWorkerManager()
 {
     lock (this._lockObject)
     {
         this._asyncWorkManager          = null;
         this._refreshRequestWorker      = null;
         this._cacheRequestHandler       = null;
         this._controllerBehavior.Locked = false;
         this._beginSessionComplete      = false;
     }
 }
Example #2
0
        /// <summary>
        /// Called when a new RefreshAsync is invoked by the user
        /// </summary>
        /// <param name="asyncWorker"></param>
        /// <param name="inputParams">An array of input params that can be passed to this method. Null in this case</param>
        void RefreshWorker(AsyncWorkRequest asyncWorker, object[] inputParams)
        {
            this._refreshRequestWorker = asyncWorker;

            refreshStats.StartTime = DateTime.Now;
            try
            {
                // First create the CacheRequestHandler
#if MONOTOUCH
                this._cacheRequestHandler = new NSUrlCacheRequestHandler(_serviceUri, _controllerBehavior, _asyncWorkManager);
#else
                this._cacheRequestHandler = new HttpCacheRequestHandler(_serviceUri, _controllerBehavior, _asyncWorkManager);
#endif

                // Register for the ProcessRequestAsync callback of the CacheRequestHandler
                this._cacheRequestHandler.ProcessCacheRequestCompleted += new EventHandler <ProcessCacheRequestCompletedEventArgs>(ProcessCacheRequestCompleted);

                // Then fire the BeginSession call on the local provider.
                this._localProvider.BeginSession();

                // Set the flag to indicate BeginSession was successful
                this._beginSessionComplete = true;

                // Dont enqueue another request if its been cancelled
                if (this.Cancelled)
                {
                    this._asyncWorkManager.CheckAndSendCancellationNotice();
                    return;
                }

                // Do uploads first
                this.EnqueueUploadRequest();
            }
            catch (Exception e)
            {
                if (ExceptionUtility.IsFatal(e))
                {
                    throw;
                }
                CompleteAsyncWithException(e);
            }
        }
 // Reset the state of the objects
 void ResetAsyncWorkerManager()
 {
     lock (this._lockObject)
     {
         this._asyncWorkManager = null;
         this._refreshRequestWorker = null;
         this._cacheRequestHandler = null;
         this._controllerBehavior.Locked = false;
         this._beginSessionComplete = false;
     }
 }
        /// <summary>
        /// Called when a new RefreshAsync is invoked by the user
        /// </summary>
        /// <param name="asyncWorker"></param>
        /// <param name="inputParams">An array of input params that can be passed to this method. Null in this case</param>
        void RefreshWorker(AsyncWorkRequest asyncWorker, object[] inputParams)
        {
            this._refreshRequestWorker = asyncWorker;

            refreshStats.StartTime = DateTime.Now;
            try
            {
                // First create the CacheRequestHandler
                this._cacheRequestHandler = CacheRequestHandler.CreateRequestHandler(this._serviceUri, this._controllerBehavior, this._asyncWorkManager);

                // Register for the ProcessRequestAsync callback of the CacheRequestHandler
                this._cacheRequestHandler.ProcessCacheRequestCompleted += new EventHandler<ProcessCacheRequestCompletedEventArgs>(ProcessCacheRequestCompleted);

                // Then fire the BeginSession call on the local provider.
                this._localProvider.BeginSession();

                // Set the flag to indicate BeginSession was successful
                this._beginSessionComplete = true;

                // Dont enqueue another request if its been cancelled
                if (this.Cancelled)
                {
                    this._asyncWorkManager.CheckAndSendCancellationNotice();
                    return;
                }

                // Do uploads first
                this.EnqueueUploadRequest();
            }
            catch (Exception e)
            {
                if (ExceptionUtility.IsFatal(e))
                {
                    throw;
                }
                CompleteAsyncWithException(e);
            }
        }
Example #5
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;
            }
        }