Search in sources :

Example 1 with StreamManagementCounterError

use of org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError in project xabber-android by redsolution.

the class XMPPTCPConnection method processHandledCount.

private void processHandledCount(long handledCount) throws StreamManagementCounterError {
    long ackedStanzasCount = SMUtils.calculateDelta(handledCount, serverHandledStanzasCount);
    final List<Stanza> ackedStanzas = new ArrayList<Stanza>(ackedStanzasCount <= Integer.MAX_VALUE ? (int) ackedStanzasCount : Integer.MAX_VALUE);
    for (long i = 0; i < ackedStanzasCount; i++) {
        Stanza ackedStanza = unacknowledgedStanzas.poll();
        // unacknowledged stanza queue. There can be no exception.
        if (ackedStanza == null) {
            throw new StreamManagementCounterError(handledCount, serverHandledStanzasCount, ackedStanzasCount, ackedStanzas);
        }
        ackedStanzas.add(ackedStanza);
    }
    boolean atLeastOneStanzaAcknowledgedListener = false;
    if (!stanzaAcknowledgedListeners.isEmpty()) {
        // If stanzaAcknowledgedListeners is not empty, the we have at least one
        atLeastOneStanzaAcknowledgedListener = true;
    } else {
        // Otherwise we look for a matching id in the stanza *id* acknowledged listeners
        for (Stanza ackedStanza : ackedStanzas) {
            String id = ackedStanza.getStanzaId();
            if (id != null && stanzaIdAcknowledgedListeners.containsKey(id)) {
                atLeastOneStanzaAcknowledgedListener = true;
                break;
            }
        }
    }
    // Only spawn a new thread if there is a chance that some listener is invoked
    if (atLeastOneStanzaAcknowledgedListener) {
        asyncGo(new Runnable() {

            @Override
            public void run() {
                for (Stanza ackedStanza : ackedStanzas) {
                    for (StanzaListener listener : stanzaAcknowledgedListeners) {
                        try {
                            listener.processStanza(ackedStanza);
                        } catch (InterruptedException | NotConnectedException e) {
                            LOGGER.log(Level.FINER, "Received exception", e);
                        }
                    }
                    String id = ackedStanza.getStanzaId();
                    if (StringUtils.isNullOrEmpty(id)) {
                        continue;
                    }
                    StanzaListener listener = stanzaIdAcknowledgedListeners.remove(id);
                    if (listener != null) {
                        try {
                            listener.processStanza(ackedStanza);
                        } catch (InterruptedException | NotConnectedException e) {
                            LOGGER.log(Level.FINER, "Received exception", e);
                        }
                    }
                }
            }
        });
    }
    serverHandledStanzasCount = handledCount;
}
Also used : StreamManagementCounterError(org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) StanzaListener(org.jivesoftware.smack.StanzaListener)

Example 2 with StreamManagementCounterError

use of org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError in project Smack by igniterealtime.

the class XMPPTCPConnection method processHandledCount.

private void processHandledCount(long handledCount) throws StreamManagementCounterError {
    long ackedStanzasCount = SMUtils.calculateDelta(handledCount, serverHandledStanzasCount);
    final List<Stanza> ackedStanzas = new ArrayList<>(ackedStanzasCount <= Integer.MAX_VALUE ? (int) ackedStanzasCount : Integer.MAX_VALUE);
    for (long i = 0; i < ackedStanzasCount; i++) {
        Stanza ackedStanza = unacknowledgedStanzas.poll();
        // unacknowledged stanza queue. There can be no exception.
        if (ackedStanza == null) {
            throw new StreamManagementCounterError(handledCount, serverHandledStanzasCount, ackedStanzasCount, ackedStanzas);
        }
        ackedStanzas.add(ackedStanza);
    }
    boolean atLeastOneStanzaAcknowledgedListener = false;
    if (!stanzaAcknowledgedListeners.isEmpty()) {
        // If stanzaAcknowledgedListeners is not empty, the we have at least one
        atLeastOneStanzaAcknowledgedListener = true;
    } else {
        // Otherwise we look for a matching id in the stanza *id* acknowledged listeners
        for (Stanza ackedStanza : ackedStanzas) {
            String id = ackedStanza.getStanzaId();
            if (id != null && stanzaIdAcknowledgedListeners.containsKey(id)) {
                atLeastOneStanzaAcknowledgedListener = true;
                break;
            }
        }
    }
    // Only spawn a new thread if there is a chance that some listener is invoked
    if (atLeastOneStanzaAcknowledgedListener) {
        asyncGo(new Runnable() {

            @Override
            public void run() {
                for (Stanza ackedStanza : ackedStanzas) {
                    for (StanzaListener listener : stanzaAcknowledgedListeners) {
                        try {
                            listener.processStanza(ackedStanza);
                        } catch (InterruptedException | NotConnectedException | NotLoggedInException e) {
                            LOGGER.log(Level.FINER, "Received exception", e);
                        }
                    }
                    String id = ackedStanza.getStanzaId();
                    if (StringUtils.isNullOrEmpty(id)) {
                        continue;
                    }
                    StanzaListener listener = stanzaIdAcknowledgedListeners.remove(id);
                    if (listener != null) {
                        try {
                            listener.processStanza(ackedStanza);
                        } catch (InterruptedException | NotConnectedException | NotLoggedInException e) {
                            LOGGER.log(Level.FINER, "Received exception", e);
                        }
                    }
                }
            }
        });
    }
    serverHandledStanzasCount = handledCount;
}
Also used : StreamManagementCounterError(org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) StanzaListener(org.jivesoftware.smack.StanzaListener)

Aggregations

ArrayList (java.util.ArrayList)2 StanzaListener (org.jivesoftware.smack.StanzaListener)2 Stanza (org.jivesoftware.smack.packet.Stanza)2 StreamManagementCounterError (org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError)2