Search in sources :

Example 1 with GraphVertex

use of org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex in project Smack by igniterealtime.

the class ModularXmppClientToServerConnection method walkStateGraphInternal.

private void walkStateGraphInternal(WalkStateGraphContext walkStateGraphContext) throws IOException, SmackException, InterruptedException, XMPPException {
    // Save a copy of the current state
    final GraphVertex<State> initialStateVertex = currentStateVertex;
    final State initialState = initialStateVertex.getElement();
    final StateDescriptor initialStateDescriptor = initialState.getStateDescriptor();
    walkStateGraphContext.recordWalkTo(initialState);
    // Check if this is the walk's final state.
    if (walkStateGraphContext.isWalksFinalState(initialStateDescriptor)) {
        // If this is used as final state, then it should be marked as such.
        assert initialStateDescriptor.isFinalState();
        // We reached the final state.
        invokeConnectionStateMachineListener(new ConnectionStateEvent.FinalStateReached(initialState));
        return;
    }
    List<GraphVertex<State>> outgoingStateEdges = initialStateVertex.getOutgoingEdges();
    // See if we need to handle mandatory intermediate states.
    GraphVertex<State> mandatoryIntermediateStateVertex = walkStateGraphContext.maybeReturnMandatoryImmediateState(outgoingStateEdges);
    if (mandatoryIntermediateStateVertex != null) {
        StateTransitionResult reason = attemptEnterState(mandatoryIntermediateStateVertex, walkStateGraphContext);
        if (reason instanceof StateTransitionResult.Success) {
            walkStateGraph(walkStateGraphContext);
            return;
        }
        // We could not enter a mandatory intermediate state. Throw here.
        throw new StateMachineException.SmackMandatoryStateFailedException(mandatoryIntermediateStateVertex.getElement(), reason);
    }
    for (Iterator<GraphVertex<State>> it = outgoingStateEdges.iterator(); it.hasNext(); ) {
        GraphVertex<State> successorStateVertex = it.next();
        State successorState = successorStateVertex.getElement();
        // state.
        if (walkStateGraphContext.wouldCauseCycle(successorStateVertex)) {
            // Ignore this successor.
            invokeConnectionStateMachineListener(new ConnectionStateEvent.TransitionIgnoredDueCycle(initialStateVertex, successorStateVertex));
        } else {
            StateTransitionResult result = attemptEnterState(successorStateVertex, walkStateGraphContext);
            if (result instanceof StateTransitionResult.Success) {
                break;
            }
            // record it.
            if (result != null) {
                walkStateGraphContext.recordFailedState(successorState, result);
            }
        }
        if (!it.hasNext()) {
            throw StateMachineException.SmackStateGraphDeadEndException.from(walkStateGraphContext, initialStateVertex);
        }
    }
    // Walk the state graph by recursion.
    walkStateGraph(walkStateGraphContext);
}
Also used : GraphVertex(org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex) State(org.jivesoftware.smack.fsm.State) NoOpState(org.jivesoftware.smack.fsm.NoOpState) StateDescriptor(org.jivesoftware.smack.fsm.StateDescriptor) StateTransitionResult(org.jivesoftware.smack.fsm.StateTransitionResult) ConnectionStateEvent(org.jivesoftware.smack.fsm.ConnectionStateEvent) LookupConnectionEndpointsSuccess(org.jivesoftware.smack.c2s.XmppClientToServerTransport.LookupConnectionEndpointsSuccess)

Aggregations

LookupConnectionEndpointsSuccess (org.jivesoftware.smack.c2s.XmppClientToServerTransport.LookupConnectionEndpointsSuccess)1 ConnectionStateEvent (org.jivesoftware.smack.fsm.ConnectionStateEvent)1 NoOpState (org.jivesoftware.smack.fsm.NoOpState)1 State (org.jivesoftware.smack.fsm.State)1 StateDescriptor (org.jivesoftware.smack.fsm.StateDescriptor)1 GraphVertex (org.jivesoftware.smack.fsm.StateDescriptorGraph.GraphVertex)1 StateTransitionResult (org.jivesoftware.smack.fsm.StateTransitionResult)1