예제 #1
0
        internal RaftEventResult TranslateToState(RaftNodeState newState, RaftMessageBase message = null)
        {
            RaftStateBase <T> newStateObject = null;

            if (newState == RaftNodeState.Candidate)
            {
                newStateObject = new Candicate <T>(this);
            }
            else if (newState == RaftNodeState.Follower)
            {
                newStateObject = new Follower <T>(this);
            }
            else if (newState == RaftNodeState.Leader)
            {
                newStateObject = new Leader <T>(this);
            }
            else
            {
                throw new ArgumentException();
            }

            StateObject = newStateObject;
            RaftEventResult enterStateResult = StateObject.EnterState();

            if (message != null)
            {
                if (enterStateResult.MessageToSend != null)
                {
                    throw new Exception("WTF");
                }
                return(OnMessageReceived(message));
            }
            return(enterStateResult);
        }