001/**
002 *
003 * Copyright 2018-2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.fsm;
018
019import org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex;
020
021public class ConnectionStateEvent {
022
023    private final StateDescriptor currentStateDescriptor;
024    private final StateDescriptor successorStateDescriptor;
025
026    private final long timestamp;
027
028    public ConnectionStateEvent(StateDescriptor currentStateDescriptor) {
029        this(currentStateDescriptor, null);
030    }
031
032    public ConnectionStateEvent(StateDescriptor currentStateDescriptor, StateDescriptor successorStateDescriptor) {
033        this.currentStateDescriptor = currentStateDescriptor;
034        this.successorStateDescriptor = successorStateDescriptor;
035        this.timestamp = System.currentTimeMillis();
036    }
037
038    public StateDescriptor getStateDescriptor() {
039        return currentStateDescriptor;
040    }
041
042    @Override
043    public String toString() {
044        if (successorStateDescriptor == null) {
045            return getClass().getSimpleName() + ": " + currentStateDescriptor.getStateName();
046        } else {
047            return currentStateDescriptor.getStateName() + ' ' + getClass().getSimpleName() + ' '
048                            + successorStateDescriptor.getStateName();
049        }
050    }
051
052    public long getTimestamp() {
053        return timestamp;
054    }
055
056    public static class StateRevertBackwardsWalk extends ConnectionStateEvent {
057        public StateRevertBackwardsWalk(State state) {
058            super(state.getStateDescriptor());
059        }
060    }
061
062    public static class FinalStateReached extends ConnectionStateEvent {
063        public FinalStateReached(State state) {
064            super(state.getStateDescriptor());
065        }
066    }
067
068    public static class TransitionNotPossible extends ConnectionStateEvent {
069        private final StateTransitionResult.TransitionImpossible transitionImpossibleReason;
070
071        public TransitionNotPossible(State currentState, State successorState, StateTransitionResult.TransitionImpossible reason) {
072            super(currentState.getStateDescriptor(), successorState.getStateDescriptor());
073            this.transitionImpossibleReason = reason;
074        }
075
076        @Override
077        public String toString() {
078            return super.toString() + ": " + transitionImpossibleReason;
079        }
080    }
081
082    public static class AboutToTransitionInto extends ConnectionStateEvent {
083        public AboutToTransitionInto(State currentState, State successorState) {
084            super(currentState.getStateDescriptor(), successorState.getStateDescriptor());
085        }
086    }
087
088    public static class TransitionFailed extends ConnectionStateEvent {
089        private final StateTransitionResult.Failure transitionFailedReason;
090
091        public TransitionFailed(State currentState, State failedSuccessorState, StateTransitionResult.Failure transitionFailedReason) {
092            super(currentState.getStateDescriptor(), failedSuccessorState.getStateDescriptor());
093            this.transitionFailedReason = transitionFailedReason;
094        }
095
096        @Override
097        public String toString() {
098            return super.toString() + ": " + transitionFailedReason;
099        }
100    }
101
102    public static class TransitionIgnoredDueCycle extends ConnectionStateEvent {
103        public TransitionIgnoredDueCycle(GraphVertex<State> currentStateVertex, GraphVertex<State> successorStateVertexCausingCycle) {
104            super(currentStateVertex.getElement().getStateDescriptor(), successorStateVertexCausingCycle.getElement().getStateDescriptor());
105        }
106    }
107
108    public static class SuccessfullyTransitionedInto extends ConnectionStateEvent {
109        private final StateTransitionResult.Success transitionSuccessResult;
110
111        public SuccessfullyTransitionedInto(State state, StateTransitionResult.Success transitionSuccessResult) {
112            super(state.getStateDescriptor());
113            this.transitionSuccessResult = transitionSuccessResult;
114        }
115
116        @Override
117        public String toString() {
118            return super.toString() + ": " + transitionSuccessResult;
119        }
120    }
121
122    public abstract static class DetailedTransitionIntoInformation extends ConnectionStateEvent {
123        protected DetailedTransitionIntoInformation(State state) {
124            super(state.getStateDescriptor());
125        }
126    }
127}