InstantStreamResumptionModule.java

  1. /**
  2.  *
  3.  * Copyright 2019-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.isr;

  18. import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.AuthenticatedAndResourceBoundStateDescriptor;
  19. import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.ConnectedButUnauthenticatedStateDescriptor;
  20. import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection.SaslAuthenticationStateDescriptor;
  21. import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModule;
  22. import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
  23. import org.jivesoftware.smack.c2s.internal.WalkStateGraphContext;
  24. import org.jivesoftware.smack.fsm.State;
  25. import org.jivesoftware.smack.fsm.StateDescriptor;
  26. import org.jivesoftware.smack.fsm.StateTransitionResult;

  27. public class InstantStreamResumptionModule extends ModularXmppClientToServerConnectionModule<InstantStreamResumptionModuleDescriptor> {

  28.     protected InstantStreamResumptionModule(InstantStreamResumptionModuleDescriptor instantStreamResumptionModuleDescriptor,
  29.                     ModularXmppClientToServerConnectionInternal connectionInternal) {
  30.         super(instantStreamResumptionModuleDescriptor, connectionInternal);
  31.     }

  32.     public static final class InstantStreamResumptionStateDescriptor extends StateDescriptor {
  33.         private InstantStreamResumptionStateDescriptor() {
  34.             super(InstantStreamResumptionState.class, 397, StateDescriptor.Property.notImplemented);

  35.             addSuccessor(AuthenticatedAndResourceBoundStateDescriptor.class);
  36.             addPredeccessor(ConnectedButUnauthenticatedStateDescriptor.class);
  37.             declarePrecedenceOver(SaslAuthenticationStateDescriptor.class);
  38.         }

  39.         @Override
  40.         protected InstantStreamResumptionModule.InstantStreamResumptionState constructState(ModularXmppClientToServerConnectionInternal connectionInternal) {
  41.             // 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.
  42.             InstantStreamResumptionModule isrModule = connectionInternal.connection.getConnectionModuleFor(InstantStreamResumptionModuleDescriptor.class);
  43.             return isrModule.constructInstantStreamResumptionState(this, connectionInternal);
  44.         }
  45.     }

  46.     private boolean useIsr = true;

  47.     private final class InstantStreamResumptionState extends State {
  48.         private InstantStreamResumptionState(InstantStreamResumptionStateDescriptor instantStreamResumptionStateDescriptor,
  49.                         ModularXmppClientToServerConnectionInternal connectionInternal) {
  50.             super(instantStreamResumptionStateDescriptor, connectionInternal);
  51.         }

  52.         @Override
  53.         public StateTransitionResult.TransitionImpossible isTransitionToPossible(WalkStateGraphContext walkStateGraphContext) {
  54.             if (!useIsr) {
  55.                 return new StateTransitionResult.TransitionImpossibleReason("Instant stream resumption not enabled nor implemented");
  56.             }

  57.             return new StateTransitionResult.TransitionImpossibleBecauseNotImplemented(stateDescriptor);
  58.         }

  59.         @Override
  60.         public StateTransitionResult.AttemptResult transitionInto(WalkStateGraphContext walkStateGraphContext) {
  61.             throw new IllegalStateException("Instant stream resumption not implemented");
  62.         }
  63.     }

  64.     public void setInstantStreamResumptionEnabled(boolean useIsr) {
  65.         this.useIsr = useIsr;
  66.     }

  67.     public InstantStreamResumptionState constructInstantStreamResumptionState(
  68.                     InstantStreamResumptionStateDescriptor instantStreamResumptionStateDescriptor,
  69.                     ModularXmppClientToServerConnectionInternal connectionInternal) {
  70.         return new InstantStreamResumptionState(instantStreamResumptionStateDescriptor, connectionInternal);
  71.     }
  72. }