StreamManagementException.java

  1. /**
  2.  *
  3.  * Copyright © 2014 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.sm;

  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.concurrent.BlockingQueue;

  22. import org.jivesoftware.smack.SmackException;
  23. import org.jivesoftware.smack.packet.Element;
  24. import org.jivesoftware.smack.packet.Stanza;

  25. public abstract class StreamManagementException extends SmackException {

  26.     public StreamManagementException() {
  27.     }

  28.     public StreamManagementException(String message) {
  29.         super(message);
  30.     }

  31.     /**
  32.      *
  33.      */
  34.     private static final long serialVersionUID = 3767590115788821101L;

  35.     public static class StreamManagementNotEnabledException extends StreamManagementException {

  36.         /**
  37.          *
  38.          */
  39.         private static final long serialVersionUID = 2624821584352571307L;

  40.     }

  41.     public static class StreamIdDoesNotMatchException extends StreamManagementException {

  42.         /**
  43.          *
  44.          */
  45.         private static final long serialVersionUID = 1191073341336559621L;

  46.         public StreamIdDoesNotMatchException(String expected, String got) {
  47.             super("Stream IDs do not match. Expected '" + expected + "', but got '" + got + "'");
  48.         }
  49.     }

  50.     public static class StreamManagementCounterError extends StreamManagementException {

  51.         /**
  52.          *
  53.          */
  54.         private static final long serialVersionUID = 1L;

  55.         private final long handledCount;
  56.         private final long previousServerHandledCount;
  57.         private final long ackedStanzaCount;
  58.         private final int outstandingStanzasCount;
  59.         private final List<Stanza> ackedStanzas;

  60.         public StreamManagementCounterError(long handledCount, long previousServerHandlerCount,
  61.                         long ackedStanzaCount,
  62.                         List<Stanza> ackedStanzas) {
  63.             super(
  64.                             "There was an error regarding the Stream Management counters. Server reported "
  65.                                             + handledCount
  66.                                             + " handled stanzas, which means that the "
  67.                                             + ackedStanzaCount
  68.                                             + " recently send stanzas by client are now acked by the server. But Smack had only "
  69.                                             + ackedStanzas.size()
  70.                                             + " to acknowledge. The stanza id of the last acked outstanding stanza is "
  71.                                             + (ackedStanzas.isEmpty() ? "<no acked stanzas>"
  72.                                                             : ackedStanzas.get(ackedStanzas.size() - 1).getStanzaId()));
  73.             this.handledCount = handledCount;
  74.             this.previousServerHandledCount = previousServerHandlerCount;
  75.             this.ackedStanzaCount = ackedStanzaCount;
  76.             this.outstandingStanzasCount = ackedStanzas.size();
  77.             this.ackedStanzas = Collections.unmodifiableList(ackedStanzas);
  78.         }

  79.         public long getHandledCount() {
  80.             return handledCount;
  81.         }

  82.         public long getPreviousServerHandledCount() {
  83.             return previousServerHandledCount;
  84.         }

  85.         public long getAckedStanzaCount() {
  86.             return ackedStanzaCount;
  87.         }

  88.         public int getOutstandingStanzasCount() {
  89.             return outstandingStanzasCount;
  90.         }

  91.         public List<Stanza> getAckedStanzas() {
  92.             return ackedStanzas;
  93.         }
  94.     }

  95.     public static final class UnacknowledgedQueueFullException extends StreamManagementException {

  96.         /**
  97.          *
  98.          */
  99.         private static final long serialVersionUID = 1L;

  100.         private final int overflowElementNum;
  101.         private final int droppedElements;
  102.         private final List<Element> elements;
  103.         private final List<Stanza> unacknowledgesStanzas;

  104.         private UnacknowledgedQueueFullException(String message, int overflowElementNum, int droppedElements, List<Element> elements,
  105.                 List<Stanza> unacknowledgesStanzas) {
  106.             super(message);
  107.             this.overflowElementNum = overflowElementNum;
  108.             this.droppedElements = droppedElements;
  109.             this.elements = elements;
  110.             this.unacknowledgesStanzas = unacknowledgesStanzas;
  111.         }

  112.         public int getOverflowElementNum() {
  113.             return overflowElementNum;
  114.         }

  115.         public int getDroppedElements() {
  116.             return droppedElements;
  117.         }

  118.         public List<Element> getElements() {
  119.             return elements;
  120.         }

  121.         public List<Stanza> getUnacknowledgesStanzas() {
  122.             return unacknowledgesStanzas;
  123.         }

  124.         public static UnacknowledgedQueueFullException newWith(int overflowElementNum, List<Element> elements,
  125.                 BlockingQueue<Stanza> unacknowledgedStanzas) {
  126.             final int unacknowledgesStanzasQueueSize = unacknowledgedStanzas.size();
  127.             List<Stanza> localUnacknowledgesStanzas = new ArrayList<>(unacknowledgesStanzasQueueSize);
  128.             localUnacknowledgesStanzas.addAll(unacknowledgedStanzas);
  129.             int droppedElements = elements.size() - overflowElementNum - 1;

  130.             String message = "The queue size " + unacknowledgesStanzasQueueSize + " is not able to fit another "
  131.                     + droppedElements + " potential stanzas type top-level stream-elements.";
  132.             return new UnacknowledgedQueueFullException(message, overflowElementNum, droppedElements, elements,
  133.                     localUnacknowledgesStanzas);
  134.         }
  135.     }
  136. }