Search in sources :

Example 46 with StanzaListener

use of org.jivesoftware.smack.StanzaListener 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

StanzaListener (org.jivesoftware.smack.StanzaListener)46 Stanza (org.jivesoftware.smack.packet.Stanza)24 InputStream (java.io.InputStream)12 Message (org.jivesoftware.smack.packet.Message)12 DataPacketExtension (org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension)12 Test (org.junit.jupiter.api.Test)12 IQ (org.jivesoftware.smack.packet.IQ)10 SmackException (org.jivesoftware.smack.SmackException)9 Data (org.jivesoftware.smackx.bytestreams.ibb.packet.Data)9 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)8 IOException (java.io.IOException)7 XMPPConnection (org.jivesoftware.smack.XMPPConnection)6 XMPPException (org.jivesoftware.smack.XMPPException)6 ArrayList (java.util.ArrayList)5 Random (java.util.Random)5 ActionEvent (java.awt.event.ActionEvent)4 List (java.util.List)4 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)4 SimpleResultSyncPoint (org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)4 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)4