예제 #1
0
        public JObject Process(byte[] syncDataBytes)
        {
            if (syncDataBytes == null)
            {
                throw new NullReferenceException(nameof(syncDataBytes));
            }

            JObject jsonResult = JsonDefaultResponse();

            SyncServerLockObject syncServerLockObject = null;
            bool lockTaken = false;

            List <string> log = new List <string>();

            SyncEngine.PayloadAction payloadAction = SyncEngine.PayloadAction.Synchronize;

            SyncEngine.ApplyChangesResult applyChangesResult = null;
            SyncEngine.GetChangesResult   getChangesResult   = null;

            SyncEngine.GetKnowledgeResult            getKnowledgeResult            = null;
            SyncEngine.ApplyChangesByKnowledgeResult applyChangesByKnowledgeResult = null;
            SyncEngine.GetChangesByKnowledgeResult   getChangesByKnowledgeResult   = null;

            try
            {
                string  json              = SyncEngine.Decompress(syncDataBytes);
                JObject payload           = JsonConvert.DeserializeObject <JObject>(json);
                string  synchronizationId = payload[nameof(SyncEngine.BaseInfo.SynchronizationId)].Value <string>();
                payloadAction = (SyncEngine.PayloadAction)Enum.Parse(typeof(SyncEngine.PayloadAction), payload[nameof(SyncEngine.BaseInfo.PayloadAction)].Value <string>());

                lock (serverLock)
                {
                    if (!serverLockObjects.ContainsKey(synchronizationId))
                    {
                        SyncServerLockObject newLockObject = new SyncServerLockObject(synchronizationId);
                        serverLockObjects.Add(synchronizationId, newLockObject);
                    }
                    syncServerLockObject = serverLockObjects[synchronizationId];
                }

                Monitor.TryEnter(syncServerLockObject, 0, ref lockTaken);
                if (!lockTaken)
                {
                    throw new Exception($"{nameof(SyncServerLockObject.SynchronizationId)}: {syncServerLockObject.SynchronizationId}, Synchronization process is already in progress");
                }

                if (syncEngine.SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.GlobalTimeStamp)
                {
                    if (payloadAction == SyncEngine.PayloadAction.Synchronize)
                    {
                        SyncEngine.ApplyChangesParameter applyChangesParameter = SyncEngine.ApplyChangesParameter.FromPayload(payload);
                        applyChangesParameter.Log = log;
                        syncEngine.ApplyChanges(applyChangesParameter, ref applyChangesResult);
                    }
                    else if (payloadAction == SyncEngine.PayloadAction.SynhronizeReverse)
                    {
                        SyncEngine.GetChangesParameter getChangesParameter = SyncEngine.GetChangesParameter.FromPayload(payload, syncEngine);
                        getChangesParameter.Log = log;
                        syncEngine.GetChanges(getChangesParameter, ref getChangesResult);
                    }
                    else
                    {
                        throw new NotImplementedException(payloadAction.ToString());
                    }
                }
                else if (syncEngine.SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.DatabaseTimeStamp)
                {
                    if (payloadAction == SyncEngine.PayloadAction.Knowledge)
                    {
                        SyncEngine.GetKnowledgeParameter getKnowledgeParameter = SyncEngine.GetKnowledgeParameter.FromPayload(payload);
                        getKnowledgeParameter.Log = log;
                        syncEngine.GetKnowledge(getKnowledgeParameter, ref getKnowledgeResult);
                    }
                    else if (payloadAction == SyncEngine.PayloadAction.Synchronize)
                    {
                        SyncEngine.ApplyChangesByKnowledgeParameter applyChangesByKnowledgeParameter = SyncEngine.ApplyChangesByKnowledgeParameter.FromPayload(payload);
                        applyChangesByKnowledgeParameter.Log = log;
                        syncEngine.ApplyChangesByKnowledge(applyChangesByKnowledgeParameter, ref applyChangesByKnowledgeResult);
                    }
                    else if (payloadAction == SyncEngine.PayloadAction.SynhronizeReverse)
                    {
                        SyncEngine.GetChangesByKnowledgeParameter getChangesByKnowledgeParameter = SyncEngine.GetChangesByKnowledgeParameter.FromPayload(payload);
                        getChangesByKnowledgeParameter.Log = log;
                        syncEngine.GetChangesByKnowledge(getChangesByKnowledgeParameter, ref getChangesByKnowledgeResult);
                    }
                    else
                    {
                        throw new NotImplementedException(payloadAction.ToString());
                    }
                }
                else
                {
                    throw new NotImplementedException(syncEngine.SyncConfiguration.TimeStampStrategy.ToString());
                }
            }
            catch (Exception e)
            {
                jsonResult["isOK"]         = false;
                jsonResult["errorMessage"] = e.Message;
                log.Add(e.Message);
            }
            finally
            {
                if (syncEngine.SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.GlobalTimeStamp)
                {
                    if (payloadAction == SyncEngine.PayloadAction.Synchronize)
                    {
                        if (applyChangesResult != null)
                        {
                            try
                            {
                                jsonResult["payload"] = Convert.ToBase64String(applyChangesResult.GetCompressed());
                            }
                            catch (Exception e)
                            {
                                string errMsg = $"jsonResult payload in applyChangesResult Error: {e.Message}";
                                jsonResult["isOK"]         = false;
                                jsonResult["errorMessage"] = errMsg;
                                log.Add(errMsg);
                            }
                        }
                    }
                    else if (payloadAction == SyncEngine.PayloadAction.SynhronizeReverse)
                    {
                        if (getChangesResult != null)
                        {
                            try
                            {
                                jsonResult["payload"] = Convert.ToBase64String(getChangesResult.GetCompressed());
                            }
                            catch (Exception e)
                            {
                                string errMsg = $"jsonResult payload in getChangesResult Error: {e.Message}";
                                jsonResult["isOK"]         = false;
                                jsonResult["errorMessage"] = errMsg;
                                log.Add(errMsg);
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException(payloadAction.ToString());
                    }
                }
                else if (syncEngine.SyncConfiguration.TimeStampStrategy == SyncConfiguration.TimeStampStrategyEnum.DatabaseTimeStamp)
                {
                    if (payloadAction == SyncEngine.PayloadAction.Knowledge)
                    {
                        if (getKnowledgeResult != null)
                        {
                            try
                            {
                                jsonResult["payload"] = Convert.ToBase64String(getKnowledgeResult.GetCompressed());
                            }
                            catch (Exception e)
                            {
                                string errMsg = $"jsonResult payload in getKnowledgeResult Error: {e.Message}";
                                jsonResult["isOK"]         = false;
                                jsonResult["errorMessage"] = errMsg;
                                log.Add(errMsg);
                            }
                        }
                    }
                    else if (payloadAction == SyncEngine.PayloadAction.Synchronize)
                    {
                        if (applyChangesByKnowledgeResult != null)
                        {
                            try
                            {
                                jsonResult["payload"] = Convert.ToBase64String(applyChangesByKnowledgeResult.GetCompressed());
                            }
                            catch (Exception e)
                            {
                                string errMsg = $"jsonResult payload in applyChangesByKnowledgeResult Error: {e.Message}";
                                jsonResult["isOK"]         = false;
                                jsonResult["errorMessage"] = errMsg;
                                log.Add(errMsg);
                            }
                        }
                    }
                    else if (payloadAction == SyncEngine.PayloadAction.SynhronizeReverse)
                    {
                        if (getChangesByKnowledgeResult != null)
                        {
                            try
                            {
                                jsonResult["payload"] = Convert.ToBase64String(getChangesByKnowledgeResult.GetCompressed());
                            }
                            catch (Exception e)
                            {
                                string errMsg = $"jsonResult payload in getChangesByKnowledgeResult Error: {e.Message}";
                                jsonResult["isOK"]         = false;
                                jsonResult["errorMessage"] = errMsg;
                                log.Add(errMsg);
                            }
                        }
                    }
                    else
                    {
                        throw new NotImplementedException(payloadAction.ToString());
                    }
                }
                else
                {
                    string errMsg = $"Process finally block Not Implemented Error: {syncEngine.SyncConfiguration.TimeStampStrategy.ToString()}";
                    jsonResult["isOK"]         = false;
                    jsonResult["errorMessage"] = errMsg;
                    log.Add(errMsg);
                }

                jsonResult["log"] = JArray.FromObject(log);

                if (lockTaken)
                {
                    Monitor.Exit(syncServerLockObject);
                }
            }

            return(jsonResult);
        }
예제 #2
0
        private async Task SynchronizeDatabaseTimeStampOneWay(bool runOnClient, Dictionary <string, object> customInfo, SyncResult syncResult)
        {
            string localName  = null;
            string remoteName = null;

            SyncEngine.PayloadAction payloadAction = SyncEngine.PayloadAction.Synchronize;
            if (runOnClient)
            {
                localName     = "Client";
                remoteName    = "Server";
                payloadAction = SyncEngine.PayloadAction.Synchronize;
            }
            else
            {
                localName     = "Server";
                remoteName    = "Client";
                payloadAction = SyncEngine.PayloadAction.SynhronizeReverse;
            }

            syncResult.Log.Add($"=== {localName} Get Knowledge (As Local Knowledge) ===");
            SyncEngine.GetKnowledgeParameter localGetKnowledgeParameter = new SyncEngine.GetKnowledgeParameter(
                SyncEngine.PayloadAction.Knowledge,
                synchronizationId,
                customInfo);
            if (runOnClient)
            {
                localGetKnowledgeParameter.Log = syncResult.Log;
            }
            SyncEngine.GetKnowledgeResult localGetKnowledgeResult = null;
            if (runOnClient)
            {
                try
                {
                    syncEngine.GetKnowledge(localGetKnowledgeParameter, ref localGetKnowledgeResult);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                (string localGetKnowledgeErrMsg, JObject jObjectLocalGetKnowledgeResult) = await ExecuteOnServer(localGetKnowledgeParameter.GetCompressed(), syncResult);

                if (jObjectLocalGetKnowledgeResult != null)
                {
                    localGetKnowledgeResult = SyncEngine.GetKnowledgeResult.FromPayload(jObjectLocalGetKnowledgeResult);
                }
                if (!string.IsNullOrEmpty(localGetKnowledgeErrMsg))
                {
                    throw new Exception(localGetKnowledgeErrMsg);
                }
            }

            syncResult.Log.Add($"=== {remoteName} Get Knowledge (As Remote Knowledge) ===");
            SyncEngine.GetKnowledgeParameter remoteGetKnowledgeParameter = new SyncEngine.GetKnowledgeParameter(
                SyncEngine.PayloadAction.Knowledge,
                synchronizationId,
                customInfo);
            if (!runOnClient)
            {
                remoteGetKnowledgeParameter.Log = syncResult.Log;
            }
            SyncEngine.GetKnowledgeResult remoteGetKnowledgeResult = null;
            if (runOnClient)
            {
                (string remoteGetKnowledgeErrMsg, JObject jObjectRemoteGetKnowledgeResult) = await ExecuteOnServer(remoteGetKnowledgeParameter.GetCompressed(), syncResult);

                if (jObjectRemoteGetKnowledgeResult != null)
                {
                    remoteGetKnowledgeResult = SyncEngine.GetKnowledgeResult.FromPayload(jObjectRemoteGetKnowledgeResult);
                }
                if (!string.IsNullOrEmpty(remoteGetKnowledgeErrMsg))
                {
                    throw new Exception(remoteGetKnowledgeErrMsg);
                }
            }
            else
            {
                try
                {
                    syncEngine.GetKnowledge(remoteGetKnowledgeParameter, ref remoteGetKnowledgeResult);
                }
                catch (Exception)
                {
                    throw;
                }
            }

            syncResult.Log.Add($"=== {localName} Get Changes based on {remoteName} Knowledge ===");
            SyncEngine.GetChangesByKnowledgeParameter getChangesByKnowledgeParameter = new SyncEngine.GetChangesByKnowledgeParameter(
                payloadAction,
                synchronizationId,
                customInfo);
            if (runOnClient)
            {
                getChangesByKnowledgeParameter.Log = syncResult.Log;
            }
            getChangesByKnowledgeParameter.LocalKnowledgeInfos  = localGetKnowledgeResult.KnowledgeInfos;
            getChangesByKnowledgeParameter.RemoteKnowledgeInfos = remoteGetKnowledgeResult.KnowledgeInfos;
            SyncEngine.GetChangesByKnowledgeResult getChangesByKnowledgeResult = null;
            if (runOnClient)
            {
                try
                {
                    syncEngine.GetChangesByKnowledge(getChangesByKnowledgeParameter, ref getChangesByKnowledgeResult);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (getChangesByKnowledgeResult != null)
                    {
                        syncResult.ClientLog.SentChanges.AddRange(getChangesByKnowledgeResult.LogChanges);
                    }
                }
            }
            else
            {
                (string getChangesByKnowledgeErrMsg, JObject jObjectGetChangesByKnowledgeResult) = await ExecuteOnServer(getChangesByKnowledgeParameter.GetCompressed(), syncResult);

                if (jObjectGetChangesByKnowledgeResult != null)
                {
                    getChangesByKnowledgeResult = SyncEngine.GetChangesByKnowledgeResult.FromPayload(jObjectGetChangesByKnowledgeResult);
                    syncResult.ServerLog.SentChanges.AddRange(getChangesByKnowledgeResult.LogChanges);
                }
                if (!string.IsNullOrEmpty(getChangesByKnowledgeErrMsg))
                {
                    throw new Exception(getChangesByKnowledgeErrMsg);
                }
            }

            syncResult.Log.Add($"=== {remoteName} Apply Changes ===");
            SyncEngine.ApplyChangesByKnowledgeParameter applyChangesByKnowledgeParameter = new SyncEngine.ApplyChangesByKnowledgeParameter(
                payloadAction,
                synchronizationId,
                customInfo);
            if (!runOnClient)
            {
                applyChangesByKnowledgeParameter.Log = syncResult.Log;
            }
            applyChangesByKnowledgeParameter.Changes = getChangesByKnowledgeResult.Changes;
            applyChangesByKnowledgeParameter.SourceDatabaseInstanceId      = localGetKnowledgeResult.KnowledgeInfos.Where(w => w.IsLocal).First().DatabaseInstanceId;
            applyChangesByKnowledgeParameter.DestinationDatabaseInstanceId = remoteGetKnowledgeResult.KnowledgeInfos.Where(w => w.IsLocal).First().DatabaseInstanceId;
            SyncEngine.ApplyChangesByKnowledgeResult applyChangesByKnowledgeResult = null;
            if (runOnClient)
            {
                (string applyChangesByKnowledgeErrMsg, JObject jObjectApplyChangesByKnowledgeResult) = await ExecuteOnServer(applyChangesByKnowledgeParameter.GetCompressed(), syncResult);

                if (jObjectApplyChangesByKnowledgeResult != null)
                {
                    applyChangesByKnowledgeResult = SyncEngine.ApplyChangesByKnowledgeResult.FromPayload(jObjectApplyChangesByKnowledgeResult);
                    syncResult.ServerLog.AppliedChanges.Inserts.AddRange(applyChangesByKnowledgeResult.Inserts);
                    syncResult.ServerLog.AppliedChanges.Updates.AddRange(applyChangesByKnowledgeResult.Updates);
                    syncResult.ServerLog.AppliedChanges.Deletes.AddRange(applyChangesByKnowledgeResult.Deletes);
                    syncResult.ServerLog.AppliedChanges.Conflicts.AddRange(applyChangesByKnowledgeResult.Conflicts);
                }
                if (!string.IsNullOrEmpty(applyChangesByKnowledgeErrMsg))
                {
                    throw new Exception(applyChangesByKnowledgeErrMsg);
                }
            }
            else
            {
                try
                {
                    syncEngine.ApplyChangesByKnowledge(applyChangesByKnowledgeParameter, ref applyChangesByKnowledgeResult);
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (applyChangesByKnowledgeResult != null)
                    {
                        syncResult.ClientLog.AppliedChanges.Inserts.AddRange(applyChangesByKnowledgeResult.Inserts);
                        syncResult.ClientLog.AppliedChanges.Updates.AddRange(applyChangesByKnowledgeResult.Updates);
                        syncResult.ClientLog.AppliedChanges.Deletes.AddRange(applyChangesByKnowledgeResult.Deletes);
                        syncResult.ClientLog.AppliedChanges.Conflicts.AddRange(applyChangesByKnowledgeResult.Conflicts);
                    }
                }
            }
        }