示例#1
0
        public async Task <SyncResult> SynchronizeAsync(SynchronizationMethodEnum synchronizationMethod = SynchronizationMethodEnum.PushThenPull, Dictionary <string, object> customInfo = null)
        {
            SyncResult syncResult = new SyncResult();

            try
            {
                syncResult.Log.Add($"=== Synchronize Started ===");

                if (syncEngine.SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.GlobalTimeStamp)
                {
                    await SynchronizeGlobalTimeStamp(synchronizationMethod, customInfo, syncResult);
                }
                else if (syncEngine.SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.DatabaseTimeStamp)
                {
                    await SynchronizeDatabaseTimeStamp(synchronizationMethod, customInfo, syncResult);
                }
                else
                {
                    throw new NotImplementedException(syncEngine.SyncConfiguration.TimeStampStrategy.ToString());
                }

                syncResult.Log.Add($"=== Synchronize Finished ===");
            }
            catch (Exception e)
            {
                syncResult.ErrorMessage = e.Message;
                syncResult.Log.Add($"=== Error: {e.Message} ===");
            }
            return(syncResult);
        }
示例#2
0
 private async Task SynchronizeDatabaseTimeStamp(SynchronizationMethodEnum synchronizationMethod, Dictionary <string, object> customInfo, SyncResult syncResult)
 {
     if (synchronizationMethod == SynchronizationMethodEnum.PushThenPull)
     {
         await SynchronizeDatabaseTimeStampOneWay(true, customInfo, syncResult);
         await SynchronizeDatabaseTimeStampOneWay(false, customInfo, syncResult);
     }
     else if (synchronizationMethod == SynchronizationMethodEnum.PullThenPush)
     {
         await SynchronizeDatabaseTimeStampOneWay(false, customInfo, syncResult);
         await SynchronizeDatabaseTimeStampOneWay(true, customInfo, syncResult);
     }
     else
     {
         throw new NotImplementedException(synchronizationMethod.ToString());
     }
 }
示例#3
0
        private async Task SynchronizeGlobalTimeStamp(SynchronizationMethodEnum synchronizationMethod, Dictionary <string, object> customInfo, SyncResult syncResult)
        {
            if (synchronizationMethod != SynchronizationMethodEnum.PushThenPull)
            {
                throw new SyncEngineConstraintException($"{nameof(synchronizationMethod)} other than {SynchronizationMethodEnum.PushThenPull.ToString()} is not supported, because {nameof(SyncConfiguration.TimeStampStrategyEnum.GlobalTimeStamp)} is already using Global DateTime (World Clock) as the time stamp, therefore, performing PushThenPull / PullThenPush will not have different effect (due to the same kind of time stamp being compared)");
            }

            syncResult.Log.Add("=== Client Get Changes ===");
            SyncEngine.GetChangesParameter clientGetChangesParameter = new SyncEngine.GetChangesParameter(
                SyncEngine.PayloadAction.Synchronize,
                synchronizationId,
                customInfo);
            clientGetChangesParameter.Log      = syncResult.Log;
            clientGetChangesParameter.LastSync = syncEngine.InvokeGetClientLastSync();
            SyncEngine.GetChangesResult clientGetChangesResult = null;
            try
            {
                syncEngine.GetChanges(clientGetChangesParameter, ref clientGetChangesResult);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (clientGetChangesResult != null)
                {
                    syncResult.ClientLog.SentChanges.AddRange(clientGetChangesResult.LogChanges);
                }
            }

            syncResult.Log.Add("=== Server Apply Changes ===");
            SyncEngine.ApplyChangesParameter serverApplyChangesParameter = new SyncEngine.ApplyChangesParameter(
                SyncEngine.PayloadAction.Synchronize,
                synchronizationId,
                customInfo);
            serverApplyChangesParameter.Changes = clientGetChangesResult.Changes;
            SyncEngine.ApplyChangesResult serverApplyChangesResult = null;
            (string serverApplyChangesErrMsg, JObject jObjectServerApplyChangesResult) = await ExecuteOnServer(serverApplyChangesParameter.GetCompressed(), syncResult);

            if (jObjectServerApplyChangesResult != null)
            {
                serverApplyChangesResult = SyncEngine.ApplyChangesResult.FromPayload(jObjectServerApplyChangesResult);
                syncResult.ServerLog.AppliedChanges.Inserts.AddRange(serverApplyChangesResult.Inserts);
                syncResult.ServerLog.AppliedChanges.Updates.AddRange(serverApplyChangesResult.Updates);
                syncResult.ServerLog.AppliedChanges.Deletes.AddRange(serverApplyChangesResult.Deletes);
                syncResult.ServerLog.AppliedChanges.Conflicts.AddRange(serverApplyChangesResult.Conflicts);
            }
            if (!string.IsNullOrEmpty(serverApplyChangesErrMsg))
            {
                throw new Exception(serverApplyChangesErrMsg);
            }

            syncResult.Log.Add("=== Server Get Changes ===");
            SyncEngine.GetChangesParameter serverGetChangesParameter = new SyncEngine.GetChangesParameter(
                SyncEngine.PayloadAction.SynhronizeReverse,
                synchronizationId,
                customInfo);
            serverGetChangesParameter.LastSync          = clientGetChangesParameter.LastSync;
            serverGetChangesParameter.PayloadAppliedIds = serverApplyChangesResult.PayloadAppliedIds;
            SyncEngine.GetChangesResult serverGetChangesResult = null;
            (string serverGetChangesErrMsg, JObject jObjectServerGetChangesResult) = await ExecuteOnServer(serverGetChangesParameter.GetCompressed(), syncResult);

            if (jObjectServerGetChangesResult != null)
            {
                serverGetChangesResult = SyncEngine.GetChangesResult.FromPayload(jObjectServerGetChangesResult);
                syncResult.ServerLog.SentChanges.AddRange(serverGetChangesResult.LogChanges);
            }
            if (!string.IsNullOrEmpty(serverGetChangesErrMsg))
            {
                throw new Exception(serverGetChangesErrMsg);
            }

            syncResult.Log.Add("=== Client Apply Changes ===");
            SyncEngine.ApplyChangesParameter clientApplyChangesParameter = new SyncEngine.ApplyChangesParameter(
                SyncEngine.PayloadAction.Synchronize,
                synchronizationId,
                customInfo);
            clientApplyChangesParameter.Log     = syncResult.Log;
            clientApplyChangesParameter.Changes = serverGetChangesResult.Changes;
            SyncEngine.ApplyChangesResult clientApplyChangesResult = null;
            try
            {
                syncEngine.ApplyChanges(clientApplyChangesParameter, ref clientApplyChangesResult);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (clientApplyChangesResult != null)
                {
                    syncResult.ClientLog.AppliedChanges.Inserts.AddRange(clientApplyChangesResult.Inserts);
                    syncResult.ClientLog.AppliedChanges.Updates.AddRange(clientApplyChangesResult.Updates);
                    syncResult.ClientLog.AppliedChanges.Deletes.AddRange(clientApplyChangesResult.Deletes);
                    syncResult.ClientLog.AppliedChanges.Conflicts.AddRange(clientApplyChangesResult.Conflicts);
                }
            }

            syncResult.Log.Add($"===LastSync from Client Get Changes Parameter: {clientGetChangesParameter.LastSync} ===");
            syncResult.Log.Add($"===MaxTimeStamp from Client Get Changes Result: {clientGetChangesResult.MaxTimeStamp} ===");
            syncResult.Log.Add($"===MaxTimeStamp from Server Get Changes Result: {serverGetChangesResult.MaxTimeStamp} ===");
            long maxLastSync = clientGetChangesParameter.LastSync;

            if (clientGetChangesResult.MaxTimeStamp > maxLastSync)
            {
                maxLastSync = clientGetChangesResult.MaxTimeStamp;
            }
            if (serverGetChangesResult.MaxTimeStamp > maxLastSync)
            {
                maxLastSync = serverGetChangesResult.MaxTimeStamp;
            }
            syncResult.Log.Add($"=== LastSync Updated To: {maxLastSync} ===");
            syncEngine.SetClientLastSync(maxLastSync);
        }