001/**
002 *
003 * Copyright 2019-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.sm;
018
019import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.AuthenticatedAndResourceBoundStateDescriptor;
020import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.AuthenticatedButUnboundStateDescriptor;
021import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.ResourceBindingStateDescriptor;
022import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModule;
023import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
024import org.jivesoftware.smack.c2s.internal.WalkStateGraphContext;
025import org.jivesoftware.smack.compression.CompressionModule.CompressionStateDescriptor;
026import org.jivesoftware.smack.fsm.State;
027import org.jivesoftware.smack.fsm.StateDescriptor;
028import org.jivesoftware.smack.fsm.StateTransitionResult;
029
030public class StreamManagementModule extends ModularXmppClientToServerConnectionModule<StreamManagementModuleDescriptor> {
031
032    protected StreamManagementModule(StreamManagementModuleDescriptor moduleDescriptor,
033                    ModularXmppClientToServerConnectionInternal connectionInternal) {
034        super(moduleDescriptor, connectionInternal);
035    }
036
037    private boolean useSm = true;
038
039    private boolean useSmResumption = true;
040
041    public static final class EnableStreamManagementStateDescriptor extends StateDescriptor {
042
043        private EnableStreamManagementStateDescriptor() {
044            super(StreamManagementModule.EnableStreamManagementState.class, 198, StateDescriptor.Property.notImplemented);
045
046            addPredeccessor(ResourceBindingStateDescriptor.class);
047            addSuccessor(AuthenticatedAndResourceBoundStateDescriptor.class);
048            declarePrecedenceOver(AuthenticatedAndResourceBoundStateDescriptor.class);
049        }
050
051        @Override
052        protected StreamManagementModule.EnableStreamManagementState constructState(ModularXmppClientToServerConnectionInternal connectionInternal) {
053            // This is the trick: the module is constructed prior the states, so we get the actual state out of the module by fetching the module from the connection.
054            StreamManagementModule smModule = connectionInternal.connection.getConnectionModuleFor(StreamManagementModuleDescriptor.class);
055            return smModule.constructEnableStreamMangementState(this, connectionInternal);
056        }
057
058    }
059
060    private EnableStreamManagementState constructEnableStreamMangementState(
061                    EnableStreamManagementStateDescriptor enableStreamManagementStateDescriptor,
062                    ModularXmppClientToServerConnectionInternal connectionInternal) {
063        return new EnableStreamManagementState(enableStreamManagementStateDescriptor, connectionInternal);
064    }
065
066    private final class EnableStreamManagementState extends State {
067        private EnableStreamManagementState(EnableStreamManagementStateDescriptor stateDescriptor,
068                        ModularXmppClientToServerConnectionInternal connectionInternal) {
069            super(stateDescriptor, connectionInternal);
070        }
071
072        @Override
073        public StateTransitionResult.TransitionImpossible isTransitionToPossible(WalkStateGraphContext walkStateGraphContext) {
074            if (!useSm) {
075                return new StateTransitionResult.TransitionImpossibleReason("Stream management not enabled");
076            }
077
078            return new StateTransitionResult.TransitionImpossibleBecauseNotImplemented(stateDescriptor);
079        }
080
081        @Override
082        public StateTransitionResult.AttemptResult transitionInto(WalkStateGraphContext walkStateGraphContext) {
083            throw new IllegalStateException("SM not implemented");
084        }
085    }
086
087    public static final class ResumeStreamStateDescriptor extends StateDescriptor {
088        private ResumeStreamStateDescriptor() {
089            super(StreamManagementModule.ResumeStreamState.class, 198, StateDescriptor.Property.notImplemented);
090
091            addPredeccessor(AuthenticatedButUnboundStateDescriptor.class);
092            addSuccessor(AuthenticatedAndResourceBoundStateDescriptor.class);
093            declarePrecedenceOver(ResourceBindingStateDescriptor.class);
094            declareInferiorityTo(CompressionStateDescriptor.class);
095        }
096
097        @Override
098        protected StreamManagementModule.ResumeStreamState constructState(ModularXmppClientToServerConnectionInternal connectionInternal) {
099            StreamManagementModule smModule = connectionInternal.connection.getConnectionModuleFor(StreamManagementModuleDescriptor.class);
100            return smModule.constructResumeStreamState(this, connectionInternal);
101        }
102
103    }
104
105    private ResumeStreamState constructResumeStreamState(
106                    ResumeStreamStateDescriptor resumeStreamStateDescriptor,
107                    ModularXmppClientToServerConnectionInternal connectionInternal) {
108        return new ResumeStreamState(resumeStreamStateDescriptor, connectionInternal);
109    }
110
111    private final class ResumeStreamState extends State {
112        private ResumeStreamState(ResumeStreamStateDescriptor stateDescriptor,
113                        ModularXmppClientToServerConnectionInternal connectionInternal) {
114            super(stateDescriptor, connectionInternal);
115        }
116
117
118        @Override
119        public StateTransitionResult.TransitionImpossible isTransitionToPossible(WalkStateGraphContext walkStateGraphContext) {
120            if (!useSmResumption) {
121                return new StateTransitionResult.TransitionImpossibleReason("Stream resumption not enabled");
122            }
123
124            return new StateTransitionResult.TransitionImpossibleBecauseNotImplemented(stateDescriptor);
125        }
126
127        @Override
128        public StateTransitionResult.AttemptResult transitionInto(WalkStateGraphContext walkStateGraphContext) {
129            throw new IllegalStateException("Stream resumption not implemented");
130        }
131    }
132
133    public void setStreamManagementEnabled(boolean useSm) {
134        this.useSm = useSm;
135    }
136
137    public void setStreamResumptionEnabled(boolean useSmResumption) {
138        this.useSmResumption = useSmResumption;
139    }
140
141}