Ejemplo n.º 1
0
        protected bool TryApply(MutationPacket mutationPacket, out Mutation mutation, out MutationResult result)
        {
            FragmentKey key = fragmentKeyFactory.FromBytes(mutationPacket.GetKey());

            mutation = mutationFactory.FromBytes(mutationPacket.GetPayload());
            result   = mutation.Mutate(state, key, mutationPacket.GetRequester());
            if (result.IsFailure())
            {
                return(false);
            }
            if (result.IsSuccess())
            {
                bool isUpdate = state.ContainsKey(key);
                state = state.CloneWith(key, result.GetFragment());
                if (isUpdate)
                {
                    SafeInvoker.Invoke(log, OnFragmentUpdatedCallback, result.GetFragment(), state, key, mutation);
                }
                else
                {
                    SafeInvoker.Invoke(log, OnFragmentInsertedCallback, result.GetFragment(), state, key, mutation);
                }
            }
            else if (result.IsDelete())
            {
                if (state.ContainsKey(key))
                {
                    state = state.CloneWithout(key);
                    SafeInvoker.Invoke(log, OnFragmentDeletedCallback, state, key, mutation);
                }
            }
            SafeInvoker.Invoke(log, OnStateChangedCallback, state, key, mutation);
            return(true);
        }
Ejemplo n.º 2
0
 private void HandleMutationBy(MutationPacket mutationPacket, Guid requester)
 {
     if (!TryApply(mutationPacket, out Mutation mutation, out MutationResult result))
     {
         if (requester.Equals(Guid.Empty))
         {
             log.Warning($"Mutation[{mutationPacket.GetId()}] failed. Reason: {result.GetFailureReason()}. Details: {mutation.GetType().Name} on fragment[{fragmentKeyFactory.FromBytes(mutationPacket.GetKey()).AsString()}].\nStackTrace: {Environment.StackTrace}");
             SafeInvoker.Invoke(log, OnMutationFailedCallback, mutationPacket.GetId(), result.GetFailureReason());
         }
         else
         {
             server.Send(requester, new MutationFailedPacket(mutationPacket.GetId(), result.GetFailureReason()));
         }
         return;
     }
     server.Broadcast(mutationPacket);
 }
Ejemplo n.º 3
0
 protected override void Request(MutationPacket mutationPacket, Mutation mutation)
 {
     client.Send(mutationPacket);
     pending.Add(mutationPacket.GetId(), $"{mutation.GetType().Name} on fragment[{fragmentKeyFactory.FromBytes(mutationPacket.GetKey()).AsString()}].\nStackTrace: {Environment.StackTrace}");
 }